blob: 6537f04973d1069d627f14e836ee1114daadde53 [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écaille61b851d2022-01-28 21:38:45 +0100782 /* Enqueue this connection asap if we could derive O-RTT secrets as
783 * listener. Note that a listener derives only RX secrets for this
784 * level.
785 */
786 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
787 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100788
789 if (!write_secret)
790 goto tp;
791
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100792 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
793 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100794 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100795 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100796 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100797 }
798
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100799 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100800 tp:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100801 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100802 const unsigned char *buf;
803 size_t buflen;
804
805 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
806 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100807 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100808
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100809 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100810 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100811 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100812
813 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100814 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
815 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
816 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
817
818 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
819 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100820 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100821 goto err;
822 }
823
824 memcpy(rx->secret, read_secret, secret_len);
825 rx->secretlen = secret_len;
826 memcpy(tx->secret, write_secret, secret_len);
827 tx->secretlen = secret_len;
828 /* Initialize all the secret keys lengths */
829 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
830 /* Prepare the next key update */
831 if (!quic_tls_key_update(qc))
832 goto err;
833 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100834 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100835 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100836 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100837
838 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100839 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100840 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100841}
842#else
843/* ->set_read_secret callback to derive the RX secrets at <level> encryption
844 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500845 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100846 */
847int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
848 const SSL_CIPHER *cipher,
849 const uint8_t *secret, size_t secret_len)
850{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100851 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100852 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100853 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100854
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100855 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
856 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
857 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100858 goto out;
859 }
860
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100861 tls_ctx->rx.aead = tls_aead(cipher);
862 tls_ctx->rx.md = tls_md(cipher);
863 tls_ctx->rx.hp = tls_hp(cipher);
864
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100865 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
866 goto err;
867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100868 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 +0100869 tls_ctx->rx.key, tls_ctx->rx.keylen,
870 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100871 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
872 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100873 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100874 goto err;
875 }
876
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100877 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100878 const unsigned char *buf;
879 size_t buflen;
880
881 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
882 if (!buflen)
883 goto err;
884
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100885 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100886 goto err;
887 }
888
889 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100890 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100891 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100892
893 return 1;
894
895 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100896 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897 return 0;
898}
899
900/* ->set_write_secret callback to derive the TX secrets at <level>
901 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500902 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100903 */
904int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
905 const SSL_CIPHER *cipher,
906 const uint8_t *secret, size_t secret_len)
907{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100908 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
909 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 +0100910
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100911 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
912 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
913 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100914 goto out;
915 }
916
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100917 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
918 goto err;
919
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 tls_ctx->tx.aead = tls_aead(cipher);
921 tls_ctx->tx.md = tls_md(cipher);
922 tls_ctx->tx.hp = tls_hp(cipher);
923
924 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 +0100925 tls_ctx->tx.key, tls_ctx->tx.keylen,
926 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100927 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
928 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100929 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930 goto err;
931 }
932
933 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100934 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100935 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100936 return 1;
937
938 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100939 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100940 return 0;
941}
942#endif
943
944/* This function copies the CRYPTO data provided by the TLS stack found at <data>
945 * with <len> as size in CRYPTO buffers dedicated to store the information about
946 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
947 * It fails only if it could not managed to allocate enough CRYPTO buffers to
948 * store all the data.
949 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
950 */
951static int quic_crypto_data_cpy(struct quic_enc_level *qel,
952 const unsigned char *data, size_t len)
953{
954 struct quic_crypto_buf **qcb;
955 /* The remaining byte to store in CRYPTO buffers. */
956 size_t cf_offset, cf_len, *nb_buf;
957 unsigned char *pos;
958
959 nb_buf = &qel->tx.crypto.nb_buf;
960 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
961 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
962 cf_len = len;
963
964 while (len) {
965 size_t to_copy, room;
966
967 pos = (*qcb)->data + (*qcb)->sz;
968 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
969 to_copy = len > room ? room : len;
970 if (to_copy) {
971 memcpy(pos, data, to_copy);
972 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
973 qel->tx.crypto.sz += to_copy;
974 (*qcb)->sz += to_copy;
975 pos += to_copy;
976 len -= to_copy;
977 data += to_copy;
978 }
979 else {
980 struct quic_crypto_buf **tmp;
981
982 tmp = realloc(qel->tx.crypto.bufs,
983 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
984 if (tmp) {
985 qel->tx.crypto.bufs = tmp;
986 qcb = &qel->tx.crypto.bufs[*nb_buf];
987 *qcb = pool_alloc(pool_head_quic_crypto_buf);
988 if (!*qcb)
989 return 0;
990
991 (*qcb)->sz = 0;
992 ++*nb_buf;
993 }
994 else {
995 break;
996 }
997 }
998 }
999
1000 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1001 * have been buffered.
1002 */
1003 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001004 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001005 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001006
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001007 /* There is at most one CRYPTO frame in this packet number
1008 * space. Let's look for it.
1009 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001010 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001011 if (frm->type != QUIC_FT_CRYPTO)
1012 continue;
1013
1014 /* Found */
1015 found = frm;
1016 break;
1017 }
1018
1019 if (found) {
1020 found->crypto.len += cf_len;
1021 }
1022 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001023 frm = pool_alloc(pool_head_quic_frame);
1024 if (!frm)
1025 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001026
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001027 frm->type = QUIC_FT_CRYPTO;
1028 frm->crypto.offset = cf_offset;
1029 frm->crypto.len = cf_len;
1030 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001031 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001032 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001033 }
1034
1035 return len == 0;
1036}
1037
1038
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001039/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1040void quic_set_tls_alert(struct quic_conn *qc, int alert)
1041{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001042 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001043 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001044 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001045}
1046
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001047/* Set the application for <qc> QUIC connection.
1048 * Return 1 if succeeded, 0 if not.
1049 */
1050int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1051{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001052 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1053 qc->app_ops = &h3_ops;
1054 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1055 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001056 else
1057 return 0;
1058
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001059 return 1;
1060}
1061
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001062/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1063 * wants to provide the QUIC layer with CRYPTO data.
1064 * Returns 1 if succeeded, 0 if not.
1065 */
1066int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1067 const uint8_t *data, size_t len)
1068{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001069 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001070 enum quic_tls_enc_level tel;
1071 struct quic_enc_level *qel;
1072
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001073 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1074 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1075 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1076 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001077 goto out;
1078 }
1079
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001080 tel = ssl_to_quic_enc_level(level);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001081 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001082
1083 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001084 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001085 goto err;
1086 }
1087
1088 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001089 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001090 goto err;
1091 }
1092
1093 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001094 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001096 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001097 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001098 return 1;
1099
1100 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001101 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001102 return 0;
1103}
1104
1105int ha_quic_flush_flight(SSL *ssl)
1106{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001107 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001108
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001109 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1110 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111
1112 return 1;
1113}
1114
1115int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1116{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001117 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001118
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001119 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1120 quic_set_tls_alert(qc, alert);
1121 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001122 return 1;
1123}
1124
1125/* QUIC TLS methods */
1126static SSL_QUIC_METHOD ha_quic_method = {
1127#ifdef OPENSSL_IS_BORINGSSL
1128 .set_read_secret = ha_set_rsec,
1129 .set_write_secret = ha_set_wsec,
1130#else
1131 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1132#endif
1133 .add_handshake_data = ha_quic_add_handshake_data,
1134 .flush_flight = ha_quic_flush_flight,
1135 .send_alert = ha_quic_send_alert,
1136};
1137
1138/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1139 * Returns an error count.
1140 */
1141int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1142{
1143 struct proxy *curproxy = bind_conf->frontend;
1144 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1145 int cfgerr = 0;
1146
1147#if 0
1148 /* XXX Did not manage to use this. */
1149 const char *ciphers =
1150 "TLS_AES_128_GCM_SHA256:"
1151 "TLS_AES_256_GCM_SHA384:"
1152 "TLS_CHACHA20_POLY1305_SHA256:"
1153 "TLS_AES_128_CCM_SHA256";
1154#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001155 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001156 long options =
1157 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1158 SSL_OP_SINGLE_ECDH_USE |
1159 SSL_OP_CIPHER_SERVER_PREFERENCE;
1160 SSL_CTX *ctx;
1161
1162 ctx = SSL_CTX_new(TLS_server_method());
1163 bind_conf->initial_ctx = ctx;
1164
1165 SSL_CTX_set_options(ctx, options);
1166#if 0
1167 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1168 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1169 "for bind '%s' at [%s:%d].\n",
1170 curproxy->id, ciphers,
1171 bind_conf->arg, bind_conf->file, bind_conf->line);
1172 cfgerr++;
1173 }
1174#endif
1175
1176 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1177 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1178 "for bind '%s' at [%s:%d].\n",
1179 curproxy->id, groups,
1180 bind_conf->arg, bind_conf->file, bind_conf->line);
1181 cfgerr++;
1182 }
1183
1184 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1185 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1186 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1187 SSL_CTX_set_default_verify_paths(ctx);
1188
1189#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1190#ifdef OPENSSL_IS_BORINGSSL
1191 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1192 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1193#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1194 if (bind_conf->ssl_conf.early_data) {
1195 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001196 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001197 }
1198 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1199 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1200#else
1201 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1202#endif
1203 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1204#endif
1205 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1206
1207 return cfgerr;
1208}
1209
1210/* Decode an expected packet number from <truncated_on> its truncated value,
1211 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1212 * the number of bits used to encode this packet number (its length in bytes * 8).
1213 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1214 */
1215static uint64_t decode_packet_number(uint64_t largest_pn,
1216 uint32_t truncated_pn, unsigned int pn_nbits)
1217{
1218 uint64_t expected_pn = largest_pn + 1;
1219 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1220 uint64_t pn_hwin = pn_win / 2;
1221 uint64_t pn_mask = pn_win - 1;
1222 uint64_t candidate_pn;
1223
1224
1225 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1226 /* Note that <pn_win> > <pn_hwin>. */
1227 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1228 candidate_pn + pn_hwin <= expected_pn)
1229 return candidate_pn + pn_win;
1230
1231 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1232 return candidate_pn - pn_win;
1233
1234 return candidate_pn;
1235}
1236
1237/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1238 * cryptographic context.
1239 * <largest_pn> is the largest received packet number and <pn> the address of
1240 * the packet number field for this packet with <byte0> address of its first byte.
1241 * <end> points to one byte past the end of this packet.
1242 * Returns 1 if succeeded, 0 if not.
1243 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001244static int qc_do_rm_hp(struct quic_conn *qc,
1245 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001246 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001247 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001248{
1249 int ret, outlen, i, pnlen;
1250 uint64_t packet_number;
1251 uint32_t truncated_pn = 0;
1252 unsigned char mask[5] = {0};
1253 unsigned char *sample;
1254 EVP_CIPHER_CTX *cctx;
1255 unsigned char *hp_key;
1256
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 /* Check there is enough data in this packet. */
1258 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001259 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260 return 0;
1261 }
1262
1263 cctx = EVP_CIPHER_CTX_new();
1264 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001265 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001266 return 0;
1267 }
1268
1269 ret = 0;
1270 sample = pn + QUIC_PACKET_PN_MAXLEN;
1271
1272 hp_key = tls_ctx->rx.hp_key;
1273 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1274 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1275 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001276 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001277 goto out;
1278 }
1279
1280 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1281 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1282 for (i = 0; i < pnlen; i++) {
1283 pn[i] ^= mask[i + 1];
1284 truncated_pn = (truncated_pn << 8) | pn[i];
1285 }
1286
1287 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1288 /* Store remaining information for this unprotected header */
1289 pkt->pn = packet_number;
1290 pkt->pnl = pnlen;
1291
1292 ret = 1;
1293
1294 out:
1295 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001296
1297 return ret;
1298}
1299
1300/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1301 * address, with <payload_len> as payload length, <aad> as address of
1302 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1303 * context.
1304 * Returns 1 if succeeded, 0 if not.
1305 */
1306static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1307 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001308 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001309{
1310 unsigned char iv[12];
1311 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001312 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001313 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314
1315 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001316 TRACE_DEVEL("AEAD IV building for 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 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1321 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001322 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001323 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001324 }
1325
1326 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001327
1328 err:
1329 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001330 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001331 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001332}
1333
1334/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1335 * Returns 1 if succeeded, 0 if not.
1336 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001337static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001338{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001339 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001340 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001341 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001342 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001343 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1344 unsigned char *rx_key = tls_ctx->rx.key;
1345
1346 kp_changed = 0;
1347 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1348 /* The two tested bits are not at the same position,
1349 * this is why they are first both inversed.
1350 */
1351 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1352 if (pkt->pn < tls_ctx->rx.pn) {
1353 /* The lowest packet number of a previous key phase
1354 * cannot be null if it really stores previous key phase
1355 * secrets.
1356 */
1357 if (!pkt->qc->ku.prv_rx.pn)
1358 return 0;
1359
1360 rx_iv = pkt->qc->ku.prv_rx.iv;
1361 rx_key = pkt->qc->ku.prv_rx.key;
1362 }
1363 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1364 /* Next key phase */
1365 kp_changed = 1;
1366 rx_iv = pkt->qc->ku.nxt_rx.iv;
1367 rx_key = pkt->qc->ku.nxt_rx.key;
1368 }
1369 }
1370 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001371
1372 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1373 return 0;
1374
1375 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1376 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001377 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001378 if (!ret)
1379 return 0;
1380
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001381 /* Update the keys only if the packet decryption succeeded. */
1382 if (kp_changed) {
1383 quic_tls_rotate_keys(pkt->qc);
1384 /* Toggle the Key Phase bit */
1385 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1386 /* Store the lowest packet number received for the current key phase */
1387 tls_ctx->rx.pn = pkt->pn;
1388 /* Prepare the next key update */
1389 if (!quic_tls_key_update(pkt->qc))
1390 return 0;
1391 }
1392
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001393 /* Update the packet length (required to parse the frames). */
1394 pkt->len = pkt->aad_len + ret;
1395
1396 return 1;
1397}
1398
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001399/* Remove from <qcs> stream the acknowledged frames.
1400 * Never fails.
1401 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001402static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001403{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001404 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001405 struct eb64_node *frm_node;
1406
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001407 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001408 frm_node = eb64_first(&qcs->tx.acked_frms);
1409 while (frm_node) {
1410 struct quic_stream *strm;
1411
1412 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1413 if (strm->offset.key != qcs->tx.ack_offset)
1414 break;
1415
1416 b_del(strm->buf, strm->len);
1417 qcs->tx.ack_offset += strm->len;
1418 frm_node = eb64_next(frm_node);
1419 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001420 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001421 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001422
1423 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001424}
1425
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001426/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001427static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1428 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001429{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001430 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001431
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001432 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001433 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001434 switch (frm->type) {
1435 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1436 {
1437 struct qcs *qcs = frm->stream.qcs;
1438 struct quic_stream *strm = &frm->stream;
1439
1440 if (qcs->tx.ack_offset == strm->offset.key) {
1441 b_del(strm->buf, strm->len);
1442 qcs->tx.ack_offset += strm->len;
1443 LIST_DELETE(&frm->list);
1444 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001445 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001446 }
1447 else {
1448 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1449 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001450 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001451 }
1452 break;
1453 default:
1454 LIST_DELETE(&frm->list);
1455 pool_free(pool_head_quic_frame, frm);
1456 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001457
1458 if (stream_acked) {
1459 struct qcc *qcc = qc->qcc;
1460
1461 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1462 tasklet_wakeup(qcc->subs->tasklet);
1463 qcc->subs->events &= ~SUB_RETRY_SEND;
1464 if (!qcc->subs->events)
1465 qcc->subs = NULL;
1466 }
1467 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001468}
1469
1470/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1471 * deallocating them, and their TX frames.
1472 * Returns the last node reached to be used for the next range.
1473 * May be NULL if <largest> node could not be found.
1474 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001475static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1476 struct eb_root *pkts,
1477 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001478 struct list *newly_acked_pkts,
1479 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001480 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001481{
1482 struct eb64_node *node;
1483 struct quic_tx_packet *pkt;
1484
1485 if (largest_node)
1486 node = largest_node;
1487 else {
1488 node = eb64_lookup(pkts, largest);
1489 while (!node && largest > smallest) {
1490 node = eb64_lookup(pkts, --largest);
1491 }
1492 }
1493
1494 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001495 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001496
1497 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1498 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001499 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001500 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001501 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001502 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001503 node = eb64_prev(node);
1504 eb64_delete(&pkt->pn_node);
1505 }
1506
1507 return node;
1508}
1509
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001510/* Remove all frames from <pkt_frm_list> and reinsert them in the
1511 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001512 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001513static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1514 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001515 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001516{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001517 struct quic_frame *frm, *frmbak;
1518 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001519
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001520 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1521 LIST_DELETE(&frm->list);
1522 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001523 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001524 }
1525
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001526 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001527}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001528
1529/* Free the TX packets of <pkts> list */
1530static inline void free_quic_tx_pkts(struct list *pkts)
1531{
1532 struct quic_tx_packet *pkt, *tmp;
1533
1534 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001535 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001536 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001537 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001538 }
1539}
1540
1541/* Send a packet loss event nofification to the congestion controller
1542 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1543 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1544 * at <now_us> current time.
1545 * Always succeeds.
1546 */
1547static inline void qc_cc_loss_event(struct quic_conn *qc,
1548 unsigned int lost_bytes,
1549 unsigned int newest_time_sent,
1550 unsigned int period,
1551 unsigned int now_us)
1552{
1553 struct quic_cc_event ev = {
1554 .type = QUIC_CC_EVT_LOSS,
1555 .loss.now_ms = now_ms,
1556 .loss.max_ack_delay = qc->max_ack_delay,
1557 .loss.lost_bytes = lost_bytes,
1558 .loss.newest_time_sent = newest_time_sent,
1559 .loss.period = period,
1560 };
1561
1562 quic_cc_event(&qc->path->cc, &ev);
1563}
1564
1565/* Send a packet ack event nofication for each newly acked packet of
1566 * <newly_acked_pkts> list and free them.
1567 * Always succeeds.
1568 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001569static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001570 struct list *newly_acked_pkts)
1571{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001572 struct quic_tx_packet *pkt, *tmp;
1573 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1574
1575 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1576 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001577 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001578 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001579 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 ev.ack.acked = pkt->in_flight_len;
1581 ev.ack.time_sent = pkt->time_sent;
1582 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001583 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001584 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001585 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001586 }
1587
1588}
1589
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001590/* Release all the frames attached to <pktns> packet number space */
1591static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1592{
1593 struct quic_frame *frm, *frmbak;
1594
1595 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1596 LIST_DELETE(&frm->list);
1597 pool_free(pool_head_quic_frame, frm);
1598 }
1599}
1600
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601/* Handle <pkts> list of lost packets detected at <now_us> handling
1602 * their TX frames.
1603 * Send a packet loss event to the congestion controller if
1604 * in flight packet have been lost.
1605 * Also frees the packet in <pkts> list.
1606 * Never fails.
1607 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001608static inline void qc_release_lost_pkts(struct quic_conn *qc,
1609 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001610 struct list *pkts,
1611 uint64_t now_us)
1612{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001613 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001614 uint64_t lost_bytes;
1615
1616 lost_bytes = 0;
1617 oldest_lost = newest_lost = NULL;
1618 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001619 struct list tmp = LIST_HEAD_INIT(tmp);
1620
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001621 lost_bytes += pkt->in_flight_len;
1622 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001623 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001624 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001625 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001626 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001627 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001628 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629 if (!oldest_lost) {
1630 oldest_lost = newest_lost = pkt;
1631 }
1632 else {
1633 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001634 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001635 newest_lost = pkt;
1636 }
1637 }
1638
1639 if (lost_bytes) {
1640 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001641 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001642 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001643 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001644 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001645 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001646 }
1647}
1648
1649/* Look for packet loss from sent packets for <qel> encryption level of a
1650 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1651 * their tree if deemed as lost or set the <loss_time> value the packet number
1652 * space if any not deemed lost.
1653 * Should be called after having received an ACK frame with newly acknowledged
1654 * packets or when the the loss detection timer has expired.
1655 * Always succeeds.
1656 */
1657static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1658 struct quic_conn *qc,
1659 struct list *lost_pkts)
1660{
1661 struct eb_root *pkts;
1662 struct eb64_node *node;
1663 struct quic_loss *ql;
1664 unsigned int loss_delay;
1665
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001666 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001667 pkts = &pktns->tx.pkts;
1668 pktns->tx.loss_time = TICK_ETERNITY;
1669 if (eb_is_empty(pkts))
1670 goto out;
1671
1672 ql = &qc->path->loss;
1673 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1674 loss_delay += loss_delay >> 3;
1675 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1676
1677 node = eb64_first(pkts);
1678 while (node) {
1679 struct quic_tx_packet *pkt;
1680 int64_t largest_acked_pn;
1681 unsigned int loss_time_limit, time_sent;
1682
1683 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001684 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001685 node = eb64_next(node);
1686 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1687 break;
1688
1689 time_sent = pkt->time_sent;
1690 loss_time_limit = tick_add(time_sent, loss_delay);
1691 if (tick_is_le(time_sent, now_ms) ||
1692 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1693 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001694 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001695 }
1696 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001697 if (tick_isset(pktns->tx.loss_time))
1698 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1699 else
1700 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001701 }
1702 }
1703
1704 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001705 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001706}
1707
1708/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1709 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001710 * 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 +01001711 * acked ack-eliciting packet.
1712 * Return 1, if succeeded, 0 if not.
1713 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001714static inline int qc_parse_ack_frm(struct quic_conn *qc,
1715 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001716 struct quic_enc_level *qel,
1717 unsigned int *rtt_sample,
1718 const unsigned char **pos, const unsigned char *end)
1719{
1720 struct quic_ack *ack = &frm->ack;
1721 uint64_t smallest, largest;
1722 struct eb_root *pkts;
1723 struct eb64_node *largest_node;
1724 unsigned int time_sent, pkt_flags;
1725 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1726 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1727
1728 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1729 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001730 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001731 goto err;
1732 }
1733
1734 if (ack->first_ack_range > ack->largest_ack) {
1735 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001736 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001737 goto err;
1738 }
1739
1740 largest = ack->largest_ack;
1741 smallest = largest - ack->first_ack_range;
1742 pkts = &qel->pktns->tx.pkts;
1743 pkt_flags = 0;
1744 largest_node = NULL;
1745 time_sent = 0;
1746
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001747 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 +01001748 largest_node = eb64_lookup(pkts, largest);
1749 if (!largest_node) {
1750 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001751 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001752 }
1753 else {
1754 time_sent = eb64_entry(&largest_node->node,
1755 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001756 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001757 }
1758
1759 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001760 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001761 do {
1762 uint64_t gap, ack_range;
1763
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001764 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1765 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 if (!ack->ack_range_num--)
1767 break;
1768
1769 if (!quic_dec_int(&gap, pos, end))
1770 goto err;
1771
1772 if (smallest < gap + 2) {
1773 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001774 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775 goto err;
1776 }
1777
1778 largest = smallest - gap - 2;
1779 if (!quic_dec_int(&ack_range, pos, end))
1780 goto err;
1781
1782 if (largest < ack_range) {
1783 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001784 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001785 goto err;
1786 }
1787
1788 /* Do not use this node anymore. */
1789 largest_node = NULL;
1790 /* Next range */
1791 smallest = largest - ack_range;
1792
1793 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001794 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001795 } while (1);
1796
1797 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001798 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001799
1800 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1801 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001802 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001803 }
1804
1805 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1806 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001807 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001808 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001809 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001810 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001811 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1812 if (quic_peer_validated_addr(qc))
1813 qc->path->loss.pto_count = 0;
1814 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001815 }
1816
1817
1818 return 1;
1819
1820 err:
1821 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001822 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001823 return 0;
1824}
1825
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001826/* This function gives the detail of the SSL error. It is used only
1827 * if the debug mode and the verbose mode are activated. It dump all
1828 * the SSL error until the stack was empty.
1829 */
1830static forceinline void qc_ssl_dump_errors(struct connection *conn)
1831{
1832 if (unlikely(global.mode & MODE_DEBUG)) {
1833 while (1) {
1834 unsigned long ret;
1835
1836 ret = ERR_get_error();
1837 if (!ret)
1838 return;
1839
1840 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1841 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1842 }
1843 }
1844}
1845
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001846int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1847 const char **str, int *len);
1848
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001849/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1850 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001851 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001852 * Return 1 if succeeded, 0 if not.
1853 */
1854static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001855 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001856 const unsigned char *data, size_t len,
1857 struct quic_rx_packet *pkt,
1858 struct quic_rx_crypto_frm *cf)
1859{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001860 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001861 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001863 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001864 qc = ctx->qc;
1865
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001866 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1869 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001870 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871 goto err;
1872 }
1873
1874 el->rx.crypto.offset += len;
1875 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001876 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001877
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001878 state = HA_ATOMIC_LOAD(&qc->state);
1879 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880 ssl_err = SSL_do_handshake(ctx->ssl);
1881 if (ssl_err != 1) {
1882 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1883 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1884 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001885 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 goto out;
1887 }
1888
1889 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001890 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001891 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001892 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001893 goto err;
1894 }
1895
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001896 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001897 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001898 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001899 /* The connection is ready to be accepted. */
1900 quic_accept_push_qc(qc);
1901 }
1902 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001903 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001904 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001905 } else {
1906 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1907 if (ssl_err != 1) {
1908 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1909 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1910 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001911 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001912 goto out;
1913 }
1914
1915 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001916 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001917 goto err;
1918 }
1919
1920 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001921 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001922 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001923
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001924 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001925 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 return 1;
1927
1928 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001929 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001930 return 0;
1931}
1932
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001933/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1934 * <pkt> RX packet.
1935 * Return it if succeeded, NULL if not.
1936 */
1937static inline
1938struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1939 struct quic_rx_packet *pkt)
1940{
1941 struct quic_rx_strm_frm *frm;
1942
1943 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1944 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001945 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001946 frm->len = stream_frm->len;
1947 frm->data = stream_frm->data;
1948 frm->pkt = pkt;
1949 }
1950
1951 return frm;
1952}
1953
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001954/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001955 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001956 */
1957static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1958{
1959 size_t ret;
1960
1961 ret = 0;
1962 while (strm_frm->len) {
1963 size_t try;
1964
1965 try = b_contig_space(buf);
1966 if (!try)
1967 break;
1968
1969 if (try > strm_frm->len)
1970 try = strm_frm->len;
1971 memcpy(b_tail(buf), strm_frm->data, try);
1972 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001973 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001974 b_add(buf, try);
1975 ret += try;
1976 }
1977
1978 return ret;
1979}
1980
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001981/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1982 * Also update <strm_frm> frame to reflect the data which have been consumed.
1983 */
1984static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1985 struct quic_rx_strm_frm *strm_frm)
1986{
1987 size_t ret;
1988
1989 ret = 0;
1990 while (strm_frm->len) {
1991 size_t try;
1992
1993 try = b_contig_space(buf);
1994 if (!try)
1995 break;
1996
1997 if (try > strm_frm->len)
1998 try = strm_frm->len;
1999 memcpy(b_tail(buf), strm_frm->data, try);
2000 strm_frm->len -= try;
2001 strm_frm->offset_node.key += try;
2002 b_add(buf, try);
2003 ret += try;
2004 }
2005
2006 return ret;
2007}
2008
2009/* Process as much as possible RX STREAM frames received for <qcs> */
2010static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2011{
2012 int total;
2013 struct eb64_node *frm_node;
2014
2015 total = 0;
2016 frm_node = eb64_first(&qcs->rx.frms);
2017 while (frm_node) {
2018 int ret;
2019 struct quic_rx_strm_frm *frm;
2020
2021 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2022 if (frm->offset_node.key != qcs->rx.offset)
2023 break;
2024
2025 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2026 qcs->rx.offset += ret;
2027 total += ret;
2028 if (frm->len) {
2029 /* If there is remaining data in this frame
2030 * this is because the destination buffer is full.
2031 */
2032 break;
2033 }
2034
2035 frm_node = eb64_next(frm_node);
2036 quic_rx_packet_refdec(frm->pkt);
2037 eb64_delete(&frm->offset_node);
2038 pool_free(pool_head_quic_rx_strm_frm, frm);
2039 }
2040
2041 return total;
2042}
2043
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002044/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2045 * streams may be open. The data are copied to the stream RX buffer if possible.
2046 * If not, the STREAM frame is stored to be treated again later.
2047 * We rely on the flow control so that not to store too much STREAM frames.
2048 * Return 1 if succeeded, 0 if not.
2049 */
2050static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2051 struct quic_stream *strm_frm,
2052 struct quic_conn *qc)
2053{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002054 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002055 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002056 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002057 struct quic_rx_strm_frm *frm;
2058
2059 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2060 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002061 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002062 return 0;
2063 }
2064
2065 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002066 if (strm_frm->offset.key < strm->rx.offset) {
2067 size_t diff;
2068
2069 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2070 TRACE_PROTO("Already received STREAM data",
2071 QUIC_EV_CONN_PSTRM, qc);
2072 goto out;
2073 }
2074
2075 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2076 diff = strm->rx.offset - strm_frm->offset.key;
2077 strm_frm->offset.key = strm->rx.offset;
2078 strm_frm->len -= diff;
2079 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002080 }
2081
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002082 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002083 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002084 int ret;
2085
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002086 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002087 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002088 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002089
2090 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002091 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002092 strm->rx.offset += ret;
2093 }
2094
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002095 total += qc_treat_rx_strm_frms(strm);
2096 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 +01002097 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002098 return 0;
2099 }
2100
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002101 if (!strm_frm->len)
2102 goto out;
2103
2104 store_frm:
2105 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2106 if (!frm) {
2107 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002108 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002109 return 0;
2110 }
2111
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002112 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002113 quic_rx_packet_refinc(pkt);
2114
2115 out:
2116 return 1;
2117}
2118
2119/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2120 * streams may be open. The data are copied to the stream RX buffer if possible.
2121 * If not, the STREAM frame is stored to be treated again later.
2122 * We rely on the flow control so that not to store too much STREAM frames.
2123 * Return 1 if succeeded, 0 if not.
2124 */
2125static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2126 struct quic_stream *strm_frm,
2127 struct quic_conn *qc)
2128{
2129 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002130 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002131 struct quic_rx_strm_frm *frm;
2132 size_t strm_frm_len;
2133
2134 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2135 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002136 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002137 return 0;
2138 }
2139
2140 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002141 if (strm_frm->offset.key < strm->rx.offset) {
2142 size_t diff;
2143
2144 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2145 TRACE_PROTO("Already received STREAM data",
2146 QUIC_EV_CONN_PSTRM, qc);
2147 goto out;
2148 }
2149
2150 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2151 diff = strm->rx.offset - strm_frm->offset.key;
2152 strm_frm->offset.key = strm->rx.offset;
2153 strm_frm->len -= diff;
2154 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002155 }
2156
2157 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002158 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002159 int ret;
2160
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002161 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002162 goto store_frm;
2163
2164 /* qc_strm_cpy() will modify the offset, depending on the number
2165 * of bytes copied.
2166 */
2167 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2168 /* Inform the application of the arrival of this new stream */
2169 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002170 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002171 return 0;
2172 }
2173
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002174 if (ret)
2175 qcs_notify_recv(strm);
2176
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002177 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002178 }
2179 /* Take this frame into an account for the stream flow control */
2180 strm->rx.offset += strm_frm_len;
2181 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002182 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002183 */
2184 if (!strm_frm->len)
2185 goto out;
2186
2187 store_frm:
2188 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2189 if (!frm) {
2190 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002191 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002192 return 0;
2193 }
2194
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002195 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002196 quic_rx_packet_refinc(pkt);
2197
2198 out:
2199 return 1;
2200}
2201
2202static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2203 struct quic_stream *strm_frm,
2204 struct quic_conn *qc)
2205{
2206 if (strm_frm->id & QCS_ID_DIR_BIT)
2207 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2208 else
2209 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2210}
2211
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002212/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002213static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2214 struct quic_conn *qc)
2215{
2216 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002217 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002218 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002219
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002220 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002221 pkts = &qel->pktns->tx.pkts;
2222 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002223 /* Skip the empty packet (they have already been retransmitted) */
2224 while (node) {
2225 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2226 if (!LIST_ISEMPTY(&pkt->frms))
2227 break;
2228 node = eb64_next(node);
2229 }
2230
2231 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002232 return;
2233
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002234 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002235}
2236
2237/* Prepare a fast retransmission during handshake after a client
2238 * has resent Initial packets. According to the RFC a server may retransmit
2239 * up to two datagrams of Initial packets if did not receive all Initial packets
2240 * and resend them coalescing with others (Handshake here).
2241 * (Listener only).
2242 */
2243static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2244{
2245 struct list itmp = LIST_HEAD_INIT(itmp);
2246 struct list htmp = LIST_HEAD_INIT(htmp);
2247 struct quic_frame *frm, *frmbak;
2248
2249 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2250 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2251 struct quic_enc_level *qel = iqel;
2252 struct eb_root *pkts;
2253 struct eb64_node *node;
2254 struct quic_tx_packet *pkt;
2255 struct list *tmp = &itmp;
2256
2257 start:
2258 pkt = NULL;
2259 pkts = &qel->pktns->tx.pkts;
2260 node = eb64_first(pkts);
2261 /* Skip the empty packet (they have already been retransmitted) */
2262 while (node) {
2263 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2264 if (!LIST_ISEMPTY(&pkt->frms))
2265 break;
2266 node = eb64_next(node);
2267 }
2268
2269 if (!pkt)
2270 goto end;
2271
2272 qel->pktns->tx.pto_probe += 1;
2273 requeue:
2274 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2275 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2276 LIST_DELETE(&frm->list);
2277 LIST_APPEND(tmp, &frm->list);
2278 }
2279
2280 if (qel == iqel) {
2281 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2282 pkt = pkt->next;
2283 tmp = &htmp;
2284 hqel->pktns->tx.pto_probe += 1;
2285 goto requeue;
2286 }
2287
2288 qel = hqel;
2289 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002290 goto start;
2291 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002292
2293 end:
2294 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2295 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002296}
2297
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002298/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2299 * as I/O handler context and <qel> as encryption level.
2300 * Returns 1 if succeeded, 0 if failed.
2301 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002302static 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 +01002303 struct quic_enc_level *qel)
2304{
2305 struct quic_frame frm;
2306 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002307 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002308 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002309
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002310 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002311 /* Skip the AAD */
2312 pos = pkt->data + pkt->aad_len;
2313 end = pkt->data + pkt->len;
2314
2315 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002316 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317 goto err;
2318
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002319 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002320 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002321 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002322 break;
2323 case QUIC_FT_PING:
2324 break;
2325 case QUIC_FT_ACK:
2326 {
2327 unsigned int rtt_sample;
2328
2329 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002330 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002331 goto err;
2332
2333 if (rtt_sample) {
2334 unsigned int ack_delay;
2335
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002336 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002337 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2338 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2339 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002340 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002341 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002342 break;
2343 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002344 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002345 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002346 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002347 {
2348 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002350 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2351 /* XXX TO DO: <cfdebug> is used only for the traces. */
2352 struct quic_rx_crypto_frm cfdebug = { };
2353
2354 cfdebug.offset_node.key = frm.crypto.offset;
2355 cfdebug.len = frm.crypto.len;
2356 TRACE_PROTO("CRYPTO data discarded",
2357 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2358 break;
2359 }
2360
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002361 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2362 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2363 /* XXX TO DO: <cfdebug> is used only for the traces. */
2364 struct quic_rx_crypto_frm cfdebug = { };
2365
2366 cfdebug.offset_node.key = frm.crypto.offset;
2367 cfdebug.len = frm.crypto.len;
2368 /* Nothing to do */
2369 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002370 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002371 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002372 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2373 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002374 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002375 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002376 else {
2377 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2378 /* XXX TO DO: <cfdebug> is used only for the traces. */
2379 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002380
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002381 cfdebug.offset_node.key = frm.crypto.offset;
2382 cfdebug.len = frm.crypto.len;
2383 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002384 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002385 frm.crypto.len -= diff;
2386 frm.crypto.data += diff;
2387 frm.crypto.offset = qel->rx.crypto.offset;
2388 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002389 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002390
2391 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002393 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002394
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002395 cfdebug.offset_node.key = frm.crypto.offset;
2396 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002397 if (!qc_provide_cdata(qel, ctx,
2398 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002399 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002400 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002401
2402 break;
2403 }
2404
2405 /* frm.crypto.offset > qel->rx.crypto.offset */
2406 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2407 if (!cf) {
2408 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002409 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002410 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002411 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002412
2413 cf->offset_node.key = frm.crypto.offset;
2414 cf->len = frm.crypto.len;
2415 cf->data = frm.crypto.data;
2416 cf->pkt = pkt;
2417 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2418 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002419 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002420 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002421 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002422 {
2423 struct quic_stream *stream = &frm.stream;
2424
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002425 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002426 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2427 goto err;
2428 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2429 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002430
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002431 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002432 goto err;
2433
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002434 break;
2435 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002436 case QUIC_FT_MAX_DATA:
2437 case QUIC_FT_MAX_STREAM_DATA:
2438 case QUIC_FT_MAX_STREAMS_BIDI:
2439 case QUIC_FT_MAX_STREAMS_UNI:
2440 case QUIC_FT_DATA_BLOCKED:
2441 case QUIC_FT_STREAM_DATA_BLOCKED:
2442 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2443 case QUIC_FT_STREAMS_BLOCKED_UNI:
2444 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002445 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002446 case QUIC_FT_RETIRE_CONNECTION_ID:
2447 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002448 break;
2449 case QUIC_FT_CONNECTION_CLOSE:
2450 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002451 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002452 qc->qcc->flags |= QC_CF_CC_RECV;
2453 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002454 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002455 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002456 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002457 goto err;
2458
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002459 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002460 break;
2461 default:
2462 goto err;
2463 }
2464 }
2465
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002466 if (fast_retrans)
2467 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002468
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002469 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2470 * has successfully parse a Handshake packet. The Initial encryption must also
2471 * be discarded.
2472 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002473 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002474 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002475
2476 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002477 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002478 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002479 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002480 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002481 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002482 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002483 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002484 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002485 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002486 }
2487
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002488 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002489 return 1;
2490
2491 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002492 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002493 return 0;
2494}
2495
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002496/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002497 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002498 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2499 * This function must be called only after having built a correct datagram.
2500 * Always succeeds.
2501 */
2502static inline void qc_set_dg(struct cbuf *cbuf,
2503 uint16_t dglen, struct quic_tx_packet *pkt)
2504{
2505 write_u16(cb_wr(cbuf), dglen);
2506 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2507 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2508}
2509
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002510/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002511 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2512 * several packets in the same datagram. A header made of two fields is added
2513 * to each datagram: the datagram length followed by the address of the first
2514 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002515 * Returns 1 if succeeded, or 0 if something wrong happened.
2516 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002517static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2518 enum quic_tls_enc_level tel,
2519 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002520{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002521 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002522 struct cbuf *cbuf;
2523 unsigned char *end_buf, *end, *pos, *spos;
2524 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2525 /* length of datagrams */
2526 uint16_t dglen;
2527 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002528 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002529 /* Each datagram is prepended with its length followed by the
2530 * address of the first packet in the datagram.
2531 */
2532 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002534 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2535
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002536 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002537 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002538 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002539 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002540 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002541 cbuf = qr->cbuf;
2542 spos = pos = cb_wr(cbuf);
2543 /* Leave at least <dglen> bytes at the end of this buffer
2544 * to ensure there is enough room to mark the end of prepared
2545 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002546 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002547 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2548 first_pkt = prv_pkt = NULL;
2549 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002550 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002551 enum quic_pkt_type pkt_type;
2552
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002553 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002554 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002555 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2556 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002557 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002558 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002559 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002560 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002561 * 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 +01002562 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002563 * and if there is no more CRYPTO data available or in flight
2564 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002565 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002566 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002567 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002568 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002569 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002570 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002571 /* Set the current datagram as prepared into <cbuf> if
2572 * the was already a correct packet which was previously written.
2573 */
2574 if (prv_pkt)
2575 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002576 /* Let's select the next encryption level */
2577 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2578 tel = next_tel;
2579 qel = &qc->els[tel];
2580 /* Build a new datagram */
2581 prv_pkt = NULL;
2582 continue;
2583 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002584 break;
2585 }
2586
2587 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002588 if (!prv_pkt) {
2589 /* Leave room for the datagram header */
2590 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002591 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002592 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2593 }
2594 else {
2595 end = pos + qc->path->mtu;
2596 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002597 }
2598
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002599 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002600 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002601 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002602 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002603 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002604 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002605 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002606 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002607 case -2:
2608 goto err;
2609 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002610 /* If there was already a correct packet present, set the
2611 * current datagram as prepared into <cbuf>.
2612 */
2613 if (prv_pkt) {
2614 qc_set_dg(cbuf, dglen, first_pkt);
2615 goto stop_build;
2616 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002617 goto out;
2618 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002619 break;
2620 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002621
Frédéric Lécaille63556772021-12-29 17:18:21 +01002622 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2623 if (!cur_pkt)
2624 goto err;
2625
2626 total += cur_pkt->len;
2627 /* keep trace of the first packet in the datagram */
2628 if (!first_pkt)
2629 first_pkt = cur_pkt;
2630 /* Attach the current one to the previous one */
2631 if (prv_pkt)
2632 prv_pkt->next = cur_pkt;
2633 /* Let's say we have to build a new dgram */
2634 prv_pkt = NULL;
2635 dglen += cur_pkt->len;
2636 /* Client: discard the Initial encryption keys as soon as
2637 * a handshake packet could be built.
2638 */
2639 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2640 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2641 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2642 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2643 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2644 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002645 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002646 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002647 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2648 }
2649 /* If the data for the current encryption level have all been sent,
2650 * select the next level.
2651 */
2652 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002653 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002654 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2655 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2656 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2657 tel = next_tel;
2658 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002659 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002660 /* If there is data for the next level, do not
2661 * consume a datagram.
2662 */
2663 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002664 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002665 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002666 /* If we have to build a new datagram, set the current datagram as
2667 * prepared into <cbuf>.
2668 */
2669 if (!prv_pkt) {
2670 qc_set_dg(cbuf, dglen, first_pkt);
2671 first_pkt = NULL;
2672 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002673 padding = 0;
2674 }
2675 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002676 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002677 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2678 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002679 }
2680 }
2681
2682 stop_build:
2683 /* Reset <wr> writer index if in front of <rd> index */
2684 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2685 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2686
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002687 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002688 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2689 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2690 /* Mark the end of contiguous data for the reader */
2691 write_u16(cb_wr(cbuf), 0);
2692 cb_add(cbuf, sizeof(uint16_t));
2693 }
2694 }
2695
2696 if (rd && rd <= cbuf->wr) {
2697 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002698 /* Let's try to reuse this buffer */
2699 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002700 }
2701 }
2702
2703 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002704 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002705 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002706
2707 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002708 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002709 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002710}
2711
2712/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002713 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002714 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002715int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002716{
2717 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002718 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002719
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002720 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002721 cbuf = qr->cbuf;
2722 while (cb_contig_data(cbuf)) {
2723 unsigned char *pos;
2724 struct buffer tmpbuf = { };
2725 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2726 uint16_t dglen;
2727 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002728 unsigned int time_sent;
2729
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002730 pos = cb_rd(cbuf);
2731 dglen = read_u16(pos);
2732 /* End of prepared datagrams.
2733 * Reset the reader index only if in front of the writer index.
2734 */
2735 if (!dglen) {
2736 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2737
2738 if (wr && wr < cbuf->rd) {
2739 cb_rd_reset(cbuf);
2740 continue;
2741 }
2742 break;
2743 }
2744
2745 pos += sizeof dglen;
2746 first_pkt = read_ptr(pos);
2747 pos += sizeof first_pkt;
2748 tmpbuf.area = (char *)pos;
2749 tmpbuf.size = tmpbuf.data = dglen;
2750
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002751 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002752 for (pkt = first_pkt; pkt; pkt = pkt->next)
2753 quic_tx_packet_refinc(pkt);
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01002754 if (ctx->xprt->snd_buf(NULL, qc->xprt_ctx,
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002755 &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002756 for (pkt = first_pkt; pkt; pkt = pkt->next)
2757 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002758 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002759 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002761 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002762 qc->tx.bytes += tmpbuf.data;
2763 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002764
2765 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2766 pkt->time_sent = time_sent;
2767 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2768 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002769 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002770 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002771 qc->path->in_flight += pkt->in_flight_len;
2772 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2773 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002774 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002775 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002776 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002777 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002778 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002779 }
2780 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002781
2782 return 1;
2783}
2784
2785/* Build all the frames which must be sent just after the handshake have succeeded.
2786 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2787 * a HANDSHAKE_DONE frame.
2788 * Return 1 if succeeded, 0 if not.
2789 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002790static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002791{
2792 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002793 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794 struct quic_frame *frm;
2795
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002796 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002797 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002798 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002800 if (!frm)
2801 return 0;
2802
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002803 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002804 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002805 }
2806
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002807 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002808 struct quic_connection_id *cid;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01002809 struct listener *l = qc->li;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810
2811 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002812 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813 goto err;
2814
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01002815 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002816 if (!cid)
2817 goto err;
2818
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002819 /* insert the allocated CID in the receiver datagram handler tree */
2820 ebmb_insert(&l->rx.dghdlrs[tid]->cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002821
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002822 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002823 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002824 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002825 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002826
2827 return 1;
2828
2829 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002830 return 0;
2831}
2832
2833/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002834void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002835{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002836 struct eb64_node *n;
2837 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002838
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002839 n = eb64_first(&arngs->root);
2840 while (n) {
2841 struct eb64_node *next;
2842
2843 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2844 next = eb64_next(n);
2845 eb64_delete(n);
2846 free(ar);
2847 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002848 }
2849}
2850
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002851/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2852 * descending order.
2853 */
2854static inline size_t sack_gap(struct quic_arng_node *p,
2855 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002857 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858}
2859
2860
2861/* Remove the last elements of <ack_ranges> list of ack range updating its
2862 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002863 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002865static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002867 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002868
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002869 last = eb64_last(&arngs->root);
2870 while (last && arngs->enc_sz > limit) {
2871 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002872
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002873 prev = eb64_prev(last);
2874 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002875 return 0;
2876
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002877 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2878 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2879 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2880 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2881 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2882 --arngs->sz;
2883 eb64_delete(last);
2884 pool_free(pool_head_quic_arng, last);
2885 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886 }
2887
2888 return 1;
2889}
2890
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002891/* Set the encoded size of <arngs> QUIC ack ranges. */
2892static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2893{
2894 struct eb64_node *node, *next;
2895 struct quic_arng_node *ar, *ar_next;
2896
2897 node = eb64_last(&arngs->root);
2898 if (!node)
2899 return;
2900
2901 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2902 arngs->enc_sz = quic_int_getsize(ar->last) +
2903 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2904
2905 while ((next = eb64_prev(node))) {
2906 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2907 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2908 quic_int_getsize(ar_next->last - ar_next->first.key);
2909 node = next;
2910 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2911 }
2912}
2913
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002914/* Insert <ar> ack range into <argns> tree of ack ranges.
2915 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002916 */
2917static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002918struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2919 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002920{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002921 struct quic_arng_node *new_ar;
2922
2923 new_ar = pool_alloc(pool_head_quic_arng);
2924 if (new_ar) {
2925 new_ar->first.key = ar->first;
2926 new_ar->last = ar->last;
2927 eb64_insert(&arngs->root, &new_ar->first);
2928 arngs->sz++;
2929 }
2930
2931 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002932}
2933
2934/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002935 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002936 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002937 *
2938 * Descending order
2939 * ------------->
2940 * range1 range2
2941 * ..........|--------|..............|--------|
2942 * ^ ^ ^ ^
2943 * | | | |
2944 * last1 first1 last2 first2
2945 * ..........+--------+--------------+--------+......
2946 * diff1 gap12 diff2
2947 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002948 * To encode the previous list of ranges we must encode integers as follows in
2949 * descending order:
2950 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002951 * with diff1 = last1 - first1
2952 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002953 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002954 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002955 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002956int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2957 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002958{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002959 struct eb64_node *le;
2960 struct quic_arng_node *new_node;
2961 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002962
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002963 new = NULL;
2964 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002965 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002966 if (!new_node)
2967 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002968
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002969 goto out;
2970 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002971
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002972 le = eb64_lookup_le(&arngs->root, ar->first);
2973 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002974 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002975 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002976 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002977
2978 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002979 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002980 else {
2981 struct quic_arng_node *le_ar =
2982 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002983
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002984 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002985 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002986 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002988 if (le_ar->last + 1 >= ar->first) {
2989 le_ar->last = ar->last;
2990 new = le;
2991 new_node = le_ar;
2992 }
2993 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002994 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002995 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002997
2998 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003000 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003002 /* Verify that the new inserted node does not overlap the nodes
3003 * which follow it.
3004 */
3005 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003006 struct eb64_node *next;
3007 struct quic_arng_node *next_node;
3008
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003009 while ((next = eb64_next(new))) {
3010 next_node =
3011 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003012 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003013 break;
3014
3015 if (next_node->last > new_node->last)
3016 new_node->last = next_node->last;
3017 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003018 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003019 /* Decrement the size of these ranges. */
3020 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022 }
3023
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003024 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003025 quic_arngs_set_enc_sz(arngs);
3026
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027 return 1;
3028}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029/* Remove the header protection of packets at <el> encryption level.
3030 * Always succeeds.
3031 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003032static 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 +01003033{
3034 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003035 struct quic_rx_packet *pqpkt;
3036 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003037 struct quic_enc_level *app_qel;
3038
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003039 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3040 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003041 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003042 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003043 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003045 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 goto out;
3047 }
3048 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003049 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003050 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003051 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003052 pqpkt->data, pqpkt->data + pqpkt->len)) {
3053 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003054 /* XXX TO DO XXX */
3055 }
3056 else {
3057 /* The AAD includes the packet number field */
3058 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3059 /* Store the packet into the tree of packets to decrypt. */
3060 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003061 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003062 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3063 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003064 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003065 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003066 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003067 MT_LIST_DELETE_SAFE(pkttmp1);
3068 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069 }
3070
3071 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003072 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003073}
3074
3075/* Process all the CRYPTO frame at <el> encryption level.
3076 * Return 1 if succeeded, 0 if not.
3077 */
3078static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003079 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080{
3081 struct eb64_node *node;
3082
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003083 node = eb64_first(&el->rx.crypto.frms);
3084 while (node) {
3085 struct quic_rx_crypto_frm *cf;
3086
3087 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3088 if (cf->offset_node.key != el->rx.crypto.offset)
3089 break;
3090
3091 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3092 goto err;
3093
3094 node = eb64_next(node);
3095 quic_rx_packet_refdec(cf->pkt);
3096 eb64_delete(&cf->offset_node);
3097 pool_free(pool_head_quic_rx_crypto_frm, cf);
3098 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003099 return 1;
3100
3101 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003102 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003103 return 0;
3104}
3105
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003106/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003107 * 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 +02003108 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003109 * Return 1 if succeeded, 0 if not.
3110 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003111int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3112 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003113{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003115 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003116 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003117 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003118
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003119 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003120 qel = cur_el;
3121 next_tel:
3122 if (!qel)
3123 goto out;
3124
3125 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3126 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003127 while (node) {
3128 struct quic_rx_packet *pkt;
3129
3130 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003131 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003132 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003133 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003134 /* Drop the packet */
3135 TRACE_PROTO("packet decryption 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écaille2766e782021-08-30 17:16:07 +02003139 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003140 /* Drop the packet */
3141 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003142 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003143 }
3144 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003145 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3146
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003147 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003148 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003149 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003150 if (pkt->pn > largest_pn)
3151 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003152 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003153 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003154 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003155 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003156 }
3157 }
3158 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003159 eb64_delete(&pkt->pn_node);
3160 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003161 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003162 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003163
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003164 /* Update the largest packet number. */
3165 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003166 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3167 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003168 goto err;
3169
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003170 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003171 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003172 qel = next_el;
3173 goto next_tel;
3174 }
3175
3176 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003177 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003178 return 1;
3179
3180 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003181 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003182 return 0;
3183}
3184
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003185/* Check if it's possible to remove header protection for packets related to
3186 * encryption level <qel>. If <qel> is NULL, assume it's false.
3187 *
3188 * Return true if the operation is possible else false.
3189 */
3190static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3191{
3192 enum quic_tls_enc_level tel;
3193
3194 if (!qel)
3195 return 0;
3196
3197 tel = ssl_to_quic_enc_level(qel->level);
3198
3199 /* check if tls secrets are available */
3200 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
3201 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
3202
3203 if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET))
3204 return 0;
3205
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003206 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003207 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
3208 qc->mux_state != QC_MUX_READY)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003209 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003210
3211 return 1;
3212}
3213
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003214/* QUIC connection packet handler task. */
3215struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003216{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003217 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003218 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003219 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003220 enum quic_tls_enc_level tel, next_tel;
3221 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003222 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003223 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003224
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003225 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003226 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003227 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003228 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003229 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003230 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003231 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3232 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3233 /* The I/O handler has been woken up by the dgram listener
3234 * after the anti-amplification was reached.
3235 */
3236 qc_set_timer(qc);
3237 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3238 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3239 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003241 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3242 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3243 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003244 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003245 if (st >= QUIC_HS_ST_COMPLETE &&
3246 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3247 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3248 /* There may be remaining Handshake packets to treat and acknowledge. */
3249 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3250 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3251 }
3252 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003253 goto err;
3254
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003255 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003256 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003257
3258 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003259 /* Treat packets waiting for header packet protection decryption */
3260 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003261 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003262
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003263 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3264 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3265 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003266 goto err;
3267
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003268 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3269 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3270 qel = next_qel;
3271 next_qel = NULL;
3272 goto next_level;
3273 }
3274
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003275 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003276 if (st >= QUIC_HS_ST_COMPLETE) {
3277 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3278 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003279 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003280
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003281 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3282 QUIC_FL_TLS_SECRETS_DCD)) {
3283 /* Discard the Handshake keys. */
3284 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3285 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3286 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3287 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003288 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003289 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003290 }
3291
3292 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3293 /* There may be remaining handshake to build (acks) */
3294 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3295 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003296 }
3297
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003298 if (!qr)
3299 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003300 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3301 * be considered.
3302 */
3303 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003304 goto err;
3305 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003306 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003307 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003308 else if (ret == 0)
3309 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003310
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003311 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003312 goto err;
3313
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003314 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003315 /* Check if there is something to do for the next level.
3316 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003317 if (next_qel && next_qel != qel &&
3318 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003319 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003321 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003322 goto next_level;
3323 }
3324
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003325 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003326 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003327 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003328
3329 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003330 if (qr)
3331 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003332 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003333 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003334}
3335
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003336/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003337static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3338{
3339 int i;
3340
3341 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3342 if (qel->tx.crypto.bufs[i]) {
3343 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3344 qel->tx.crypto.bufs[i] = NULL;
3345 }
3346 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003347 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348}
3349
3350/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003351 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003352 * Returns 1 if succeeded, 0 if not.
3353 */
3354static int quic_conn_enc_level_init(struct quic_conn *qc,
3355 enum quic_tls_enc_level level)
3356{
3357 struct quic_enc_level *qel;
3358
3359 qel = &qc->els[level];
3360 qel->level = quic_to_ssl_enc_level(level);
3361 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3362 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3363 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3364 qel->tls_ctx.rx.flags = 0;
3365 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003366 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003367
3368 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003369 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003370 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003371 qel->rx.crypto.offset = 0;
3372 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003373
3374 /* Allocate only one buffer. */
3375 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3376 if (!qel->tx.crypto.bufs)
3377 goto err;
3378
3379 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3380 if (!qel->tx.crypto.bufs[0])
3381 goto err;
3382
3383 qel->tx.crypto.bufs[0]->sz = 0;
3384 qel->tx.crypto.nb_buf = 1;
3385
3386 qel->tx.crypto.sz = 0;
3387 qel->tx.crypto.offset = 0;
3388
3389 return 1;
3390
3391 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003392 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003393 return 0;
3394}
3395
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003396/* Increment the <qc> refcount.
3397 *
3398 * This operation must be conducted when manipulating the quic_conn outside of
3399 * the connection pinned thread. These threads can only retrieve the connection
3400 * in the CID tree, so this function must be conducted under the CID lock.
3401 */
3402static inline void quic_conn_take(struct quic_conn *qc)
3403{
3404 HA_ATOMIC_INC(&qc->refcount);
3405}
3406
3407/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003408 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003409 */
3410static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003411{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003412 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003413 int i;
3414
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003415 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003416 return;
3417
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003418 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003419 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003420
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003421 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003422 if (conn_ctx) {
3423 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003424 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003425 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003426
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003427 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003428 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003429
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003430 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3431 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003432 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433}
3434
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003435/* Release the quic_conn <qc>. It will decrement its refcount so that the
3436 * connection will be freed once all threads have finished to work with it. The
3437 * connection is removed from the CIDs tree and thus cannot be found by other
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003438 * threads after it. The connection tasklet is killed.
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003439 *
3440 * Do not use <qc> after it as it may be freed. This function must only be
3441 * called by the thread responsible of the quic_conn tasklet.
3442 */
3443static void quic_conn_release(struct quic_conn *qc)
3444{
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003445 struct ssl_sock_ctx *conn_ctx;
3446
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003447 /* remove the connection from receiver cids trees */
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003448 ebmb_delete(&qc->odcid_node);
3449 ebmb_delete(&qc->scid_node);
3450 free_quic_conn_cids(qc);
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003451
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003452 /* Kill the tasklet. Do not use tasklet_free as this is not thread safe
3453 * as other threads may call tasklet_wakeup after this.
3454 */
3455 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3456 if (conn_ctx)
3457 tasklet_kill(conn_ctx->wait_event.tasklet);
3458
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003459 quic_conn_drop(qc);
3460}
3461
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003462void quic_close(struct connection *conn, void *xprt_ctx)
3463{
3464 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003465 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003466
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003467 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003468 /* This task must be deleted by the connection-pinned thread. */
3469 if (qc->timer_task) {
3470 task_destroy(qc->timer_task);
3471 qc->timer_task = NULL;
3472 }
3473
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003474 /* Next application data can be dropped. */
3475 qc->mux_state = QC_MUX_RELEASED;
3476
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003477 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003478
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003479 /* TODO for now release the quic_conn on notification by the upper
3480 * layer. It could be useful to delay it if there is remaining data to
3481 * send or data to be acked.
3482 */
3483 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003484}
3485
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003487static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003488{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003489 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490 struct quic_conn *qc;
3491 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003492 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003493
3494 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003495 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003496 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003497 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003498 task->expire = TICK_ETERNITY;
3499 pktns = quic_loss_pktns(qc);
3500 if (tick_isset(pktns->tx.loss_time)) {
3501 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3502
3503 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3504 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003505 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3506 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003507 goto out;
3508 }
3509
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003510 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003511 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003512 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003513 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3514 pktns->tx.pto_probe = 1;
3515 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3516 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3517 }
3518 else {
3519 pktns->tx.pto_probe = 2;
3520 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003522 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003523 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3524 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3525
3526 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3527 hel->pktns->tx.pto_probe = 1;
3528 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3529 iel->pktns->tx.pto_probe = 1;
3530 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003531
3532 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3533 int j;
3534
3535 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3536 continue;
3537
3538 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3539 qc_prep_fast_retrans(&qc->els[i], qc);
3540 }
3541
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003542 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3543 qc->path->loss.pto_count++;
3544
3545 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003546 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547
3548 return task;
3549}
3550
3551/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3552 * connections used to identify the first Initial packets of client connecting
3553 * to listeners. This parameter must be NULL for QUIC connections attached
3554 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3555 * <scid> is the source connection ID with <scid_len> as length.
3556 * Returns 1 if succeeded, 0 if not.
3557 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003558static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003559 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003560 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561{
3562 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003563 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003564 /* Initial CID. */
3565 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003566 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003567 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003568
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003569 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003570 qc = pool_zalloc(pool_head_quic_conn);
3571 if (!qc) {
3572 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3573 goto err;
3574 }
3575
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003576 HA_ATOMIC_STORE(&qc->refcount, 0);
3577
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003578 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3579 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003580 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003581 goto err;
3582 }
3583
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003584 qc->cids = EB_ROOT;
3585 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003586 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003587 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003588
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003589 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003590 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003591 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003592 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003593 qc->odcid.addrlen = dcid_addr_len;
3594 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003595
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003596 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597 if (scid_len)
3598 memcpy(qc->dcid.data, scid, scid_len);
3599 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003600 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003601 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003602 }
3603 /* QUIC Client (outgoing connection to servers) */
3604 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003605 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003606 if (dcid_len)
3607 memcpy(qc->dcid.data, dcid, dcid_len);
3608 qc->dcid.len = dcid_len;
3609 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003610 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003611
3612 /* Initialize the output buffer */
3613 qc->obuf.pos = qc->obuf.data;
3614
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003615 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003616 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003617 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003618 goto err;
3619 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003620
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003621 /* insert the allocated CID in the receiver datagram handler tree */
3622 if (server)
3623 ebmb_insert(&l->rx.dghdlrs[tid]->cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003624
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625 /* Select our SCID which is the first CID with 0 as sequence number. */
3626 qc->scid = icid->cid;
3627
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003628 /* Packet number spaces initialization. */
3629 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3630 quic_pktns_init(&qc->pktns[i]);
3631 /* QUIC encryption level context initialization. */
3632 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003633 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003634 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003635 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003636 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003637 /* Initialize the packet number space. */
3638 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3639 }
3640
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003641 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003642 qc->tps_tls_ext = qc->version & 0xff000000 ?
3643 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3644 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003645 /* TX part. */
3646 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3648 qc->tx.wbuf = qc->tx.rbuf = 0;
3649 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003650 /* RX part. */
3651 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003652 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003653 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3654 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3655 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003656 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003657 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003658 goto err;
3659 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003661 /* XXX TO DO: Only one path at this time. */
3662 qc->path = &qc->paths[0];
3663 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3664
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003665 /* required to use MTLIST_IN_LIST */
3666 MT_LIST_INIT(&qc->accept_list);
3667
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003668 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003670 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003671
3672 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003673 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003674 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003675 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676}
3677
3678/* Initialize the timer task of <qc> QUIC connection.
3679 * Returns 1 if succeeded, 0 if not.
3680 */
3681static int quic_conn_init_timer(struct quic_conn *qc)
3682{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003683 /* Attach this task to the same thread ID used for the connection */
3684 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003685 if (!qc->timer_task)
3686 return 0;
3687
3688 qc->timer = TICK_ETERNITY;
3689 qc->timer_task->process = process_timer;
3690 qc->timer_task->context = qc->conn->xprt_ctx;
3691
3692 return 1;
3693}
3694
3695/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3696 * past one byte of this buffer.
3697 */
3698static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3699 struct quic_rx_packet *pkt)
3700{
3701 unsigned char dcid_len, scid_len;
3702
3703 /* Version */
3704 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3705 return 0;
3706
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707 /* Destination Connection ID Length */
3708 dcid_len = *(*buf)++;
3709 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3710 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3711 /* XXX MUST BE DROPPED */
3712 return 0;
3713
3714 if (dcid_len) {
3715 /* Check that the length of this received DCID matches the CID lengths
3716 * of our implementation for non Initials packets only.
3717 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003718 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3719 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003720 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003721 return 0;
3722
3723 memcpy(pkt->dcid.data, *buf, dcid_len);
3724 }
3725
3726 pkt->dcid.len = dcid_len;
3727 *buf += dcid_len;
3728
3729 /* Source Connection ID Length */
3730 scid_len = *(*buf)++;
3731 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3732 /* XXX MUST BE DROPPED */
3733 return 0;
3734
3735 if (scid_len)
3736 memcpy(pkt->scid.data, *buf, scid_len);
3737 pkt->scid.len = scid_len;
3738 *buf += scid_len;
3739
3740 return 1;
3741}
3742
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003743/* Insert <pkt> RX packet in its <qel> RX packets tree */
3744static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3745{
3746 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003747 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003748 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3749 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3750 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003751}
3752
3753/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003754 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3755 * byte past the end of the buffer containing this packet and <beg> the address of
3756 * the packet first byte.
3757 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3758 * Returns 1 if succeeded, 0 if not.
3759 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003760static inline int qc_try_rm_hp(struct quic_conn *qc,
3761 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003762 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003764 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003765{
3766 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003767 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003768 struct quic_enc_level *qel;
3769 /* Only for traces. */
3770 struct quic_rx_packet *qpkt_trace;
3771
3772 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003773 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774 /* The packet number is here. This is also the start minus
3775 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3776 * protection.
3777 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003778 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003779
3780 tel = quic_packet_type_enc_level(pkt->type);
3781 qel = &qc->els[tel];
3782
3783 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003784 /* Note that the following function enables us to unprotect the packet
3785 * number and its length subsequently used to decrypt the entire
3786 * packets.
3787 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003788 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3789 qel->pktns->rx.largest_pn, pn, beg, end)) {
3790 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003791 goto err;
3792 }
3793
3794 /* The AAD includes the packet number field found at <pn>. */
3795 pkt->aad_len = pn - beg + pkt->pnl;
3796 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003798 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003799 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3800 /* If the packet number space has been discarded, this packet
3801 * will be not parsed.
3802 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003803 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003804 goto out;
3805 }
3806
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003807 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003808 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003809 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3810 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003811 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003812 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003813 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003814 goto err;
3815 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003817 *el = qel;
3818 /* No reference counter incrementation here!!! */
3819 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3820 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3821 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3822 b_add(&qc->rx.buf, pkt->len);
3823 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003824 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003825 return 1;
3826
3827 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003828 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003829 return 0;
3830}
3831
3832/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3833 * Also set <*long_header> to 1 if this form is long, 0 if not.
3834 */
3835static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3836 unsigned char byte0, int *long_header)
3837{
3838 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3839 pkt->type =
3840 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3841 *long_header = 1;
3842 }
3843 else {
3844 pkt->type = QUIC_PACKET_TYPE_SHORT;
3845 *long_header = 0;
3846 }
3847}
3848
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003849/*
3850 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3851 */
3852static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3853{
3854 int j = 0, version;
3855
3856 do {
3857 version = quic_supported_version[j];
3858 if (version == pkt->version)
3859 return 1;
3860
3861 version = quic_supported_version[++j];
3862 } while(version);
3863
3864 return 0;
3865}
3866
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003867/*
3868 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3869 * address <addr>.
3870 * Implementation of RFC9000 6. Version Negotiation
3871 *
3872 * TODO implement a rate-limiting sending of Version Negotiation packets
3873 *
3874 * Returns 0 on success else non-zero
3875 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003876static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3877 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003878{
3879 char buf[256];
3880 int i = 0, j, version;
3881 const socklen_t addrlen = get_addr_len(addr);
3882
3883 /*
3884 * header form
3885 * long header, fixed bit to 0 for Version Negotiation
3886 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003887 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3888 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003889
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003890 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003891 /* null version for Version Negotiation */
3892 buf[i++] = '\x00';
3893 buf[i++] = '\x00';
3894 buf[i++] = '\x00';
3895 buf[i++] = '\x00';
3896
3897 /* source connection id */
3898 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003899 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003900 i += pkt->scid.len;
3901
3902 /* destination connection id */
3903 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003904 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003905 i += pkt->dcid.len;
3906
3907 /* supported version */
3908 j = 0;
3909 do {
3910 version = htonl(quic_supported_version[j]);
3911 memcpy(&buf[i], &version, sizeof(version));
3912 i += sizeof(version);
3913
3914 version = quic_supported_version[++j];
3915 } while (version);
3916
3917 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3918 return 1;
3919
3920 return 0;
3921}
3922
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003923/* Generate the token to be used in Retry packets. The token is written to
3924 * <buf> which is expected to be <len> bytes.
3925 *
3926 * Various parameters are expected to be encoded in the token. For now, only
3927 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
3928 * as this CID must be repeated by the server in the transport parameters.
3929 *
3930 * TODO add the client address to validate the token origin.
3931 *
3932 * Returns the length of the encoded token or 0 on error.
3933 */
3934static int generate_retry_token(unsigned char *buf, unsigned char len,
3935 struct quic_rx_packet *pkt)
3936{
3937 const size_t token_len = 1 + pkt->dcid.len;
3938 unsigned char i = 0;
3939
3940 if (token_len > len)
3941 return 0;
3942
3943 buf[i++] = pkt->dcid.len;
3944 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
3945 i += pkt->dcid.len;
3946
3947 return i;
3948}
3949
3950/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
3951 * the Initial <pkt> packet.
3952 *
3953 * Returns 0 on success else non-zero.
3954 */
3955static int send_retry(int fd, struct sockaddr_storage *addr,
3956 struct quic_rx_packet *pkt)
3957{
3958 unsigned char buf[128];
3959 int i = 0, token_len;
3960 const socklen_t addrlen = get_addr_len(addr);
3961 struct quic_cid scid;
3962
3963 /* long header + fixed bit + packet type 0x3 */
3964 buf[i++] = 0xf0;
3965 /* version */
3966 buf[i++] = 0x00;
3967 buf[i++] = 0x00;
3968 buf[i++] = 0x00;
3969 buf[i++] = 0x01;
3970
3971 /* Use the SCID from <pkt> for Retry DCID. */
3972 buf[i++] = pkt->scid.len;
3973 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
3974 i += pkt->scid.len;
3975
3976 /* Generate a new CID to be used as SCID for the Retry packet. */
3977 scid.len = QUIC_HAP_CID_LEN;
3978 if (RAND_bytes(scid.data, scid.len) != 1)
3979 return 1;
3980
3981 buf[i++] = scid.len;
3982 memcpy(&buf[i], scid.data, scid.len);
3983 i += scid.len;
3984
3985 /* token */
3986 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
3987 return 1;
3988 i += token_len;
3989
3990 /* token integrity tag */
3991 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
3992 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
3993 pkt->dcid.len, buf, i)) {
3994 return 1;
3995 }
3996
3997 i += QUIC_TLS_TAG_LEN;
3998
3999 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4000 return 1;
4001
4002 return 0;
4003}
4004
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004005/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4006 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4007 * concatenated to the <pkt> DCID field.
4008 *
4009 * Returns the instance or NULL if not found.
4010 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004011static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004012 struct listener *l,
4013 struct sockaddr_storage *saddr)
4014{
4015 struct quic_conn *qc = NULL;
4016 struct ebmb_node *node;
4017 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004018 /* set if the quic_conn is found in the second DCID tree */
4019 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004020
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004021 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4022 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4023 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4024 /* DCIDs of first packets coming from multiple clients may have
4025 * the same values. Let's distinguish them by concatenating the
4026 * socket addresses.
4027 */
4028 quic_cid_saddr_cat(&pkt->dcid, saddr);
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004029 node = ebmb_lookup(&l->rx.dghdlrs[tid]->odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004030 pkt->dcid.len + pkt->dcid.addrlen);
4031 if (node) {
4032 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4033 goto end;
4034 }
4035 }
4036
4037 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4038 * also for INITIAL/0-RTT non-first packets with the final DCID in
4039 * used.
4040 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004041 node = ebmb_lookup(&l->rx.dghdlrs[tid]->cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004042 if (!node)
4043 goto end;
4044
4045 id = ebmb_entry(node, struct quic_connection_id, node);
4046 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004047 found_in_dcid = 1;
4048
4049 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004050 if (qc)
4051 quic_conn_take(qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004052
4053 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4054 * If already done, this is a noop.
4055 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004056 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004057 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004058
4059 return qc;
4060}
4061
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004062/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4063 * will extract the parameters stored in the token : <odcid>.
4064 *
4065 * Returns 0 on success else non-zero.
4066 */
4067static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4068 struct quic_cid *odcid)
4069{
4070 uint64_t odcid_len;
4071
4072 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4073 return 1;
4074
4075 memcpy(odcid->data, token, odcid_len);
4076 odcid->len = odcid_len;
4077
4078 return 0;
4079}
4080
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004081/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4082 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4083 * parameters of this session.
4084 * This is the responsibility of the caller to check the validity of all the
4085 * pointers passed as parameter to this function.
4086 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4087 * CO_ER_SSL_NO_MEM.
4088 */
4089static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4090 unsigned char *params, size_t params_len)
4091{
4092 int retry;
4093
4094 retry = 1;
4095 retry:
4096 *ssl = SSL_new(ssl_ctx);
4097 if (!*ssl) {
4098 if (!retry--)
4099 goto err;
4100
4101 pool_gc(NULL);
4102 goto retry;
4103 }
4104
4105 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4106 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4107 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4108 goto err;
4109
4110 SSL_free(*ssl);
4111 *ssl = NULL;
4112 if (!retry--)
4113 goto err;
4114
4115 pool_gc(NULL);
4116 goto retry;
4117 }
4118
4119 return 0;
4120
4121 err:
4122 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4123 return -1;
4124}
4125
4126/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4127 * used to process <qc> received packets. The allocated context is stored in
4128 * <qc.xprt_ctx>.
4129 *
4130 * Returns 0 on success else non-zero.
4131 */
4132int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4133{
4134 struct bind_conf *bc = qc->li->bind_conf;
4135 struct ssl_sock_ctx *ctx = NULL;
4136
4137 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4138 if (!ctx)
4139 goto err;
4140
4141 ctx->wait_event.tasklet = tasklet_new();
4142 if (!ctx->wait_event.tasklet)
4143 goto err;
4144
4145 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4146 ctx->wait_event.tasklet->context = ctx;
4147 ctx->wait_event.events = 0;
4148 ctx->subs = NULL;
4149 ctx->xprt_ctx = NULL;
4150 ctx->qc = qc;
4151
4152 /* Set tasklet tid based on the SCID selected by us for this
4153 * connection. The upper layer will also be binded on the same thread.
4154 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004155 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004156
4157 if (qc_is_listener(qc)) {
4158 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4159 qc->enc_params, qc->enc_params_len) == -1) {
4160 goto err;
4161 }
4162
4163 /* Enabling 0-RTT */
4164 if (bc->ssl_conf.early_data)
4165 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4166
4167 SSL_set_accept_state(ctx->ssl);
4168 }
4169
4170 ctx->xprt = xprt_get(XPRT_QUIC);
4171
4172 /* Store the allocated context in <qc>. */
4173 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4174
4175 return 0;
4176
4177 err:
4178 if (ctx && ctx->wait_event.tasklet)
4179 tasklet_free(ctx->wait_event.tasklet);
4180 pool_free(pool_head_quic_conn_ctx, ctx);
4181
4182 return 1;
4183}
4184
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004185static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004186 struct quic_rx_packet *pkt, int first_pkt,
4187 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004189 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004190 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004192 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004193 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004194 size_t b_cspace;
4195 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004197 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004198 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004199 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004200 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004201 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4202 /* This ist only to please to traces and distinguish the
4203 * packet with parsed packet number from others.
4204 */
4205 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004206 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004207 goto err;
4208
4209 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004210 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004211 /* XXX TO BE DISCARDED */
4212 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4213 goto err;
4214 }
4215
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004216 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004217 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004218 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004219 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004220 uint64_t len;
4221
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004222 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004223 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4224 goto err;
4225 }
4226
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004227 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4228 * they must have the same DCID.
4229 */
4230 if (!first_pkt &&
4231 (pkt->dcid.len != dgram->dcid_len ||
4232 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4233 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4234 goto err;
4235 }
4236
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004237 /* Retry of Version Negotiation packets are only sent by servers */
4238 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4239 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4240 goto err;
4241 }
4242
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004243 /* RFC9000 6. Version Negotiation */
4244 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004245 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004246 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004247 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4248 goto err;
4249 }
4250
4251 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004252 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004253 }
4254
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004255 /* For Initial packets, and for servers (QUIC clients connections),
4256 * there is no Initial connection IDs storage.
4257 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004258 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004259 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004260
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004261 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4262 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004263 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4264 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004265 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004266
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004267 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4268 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004269 */
4270 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004271
4272 /* TODO Retry should be automatically activated if
4273 * suspect network usage is detected.
4274 */
4275 if (!token_len && l->bind_conf->quic_force_retry) {
4276 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004277 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004278 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4279 goto err;
4280 }
4281
4282 goto err;
4283 }
4284 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004285 pkt->token = buf;
4286 buf += pkt->token_len;
4287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004288 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004289 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004290 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004291 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4292 goto err;
4293 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004294 }
4295
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004296 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4297 end - buf < len) {
4298 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4299 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004300 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004301
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004302 payload = buf;
4303 pkt->len = len + payload - beg;
4304
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004305 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004306 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004307 int ipv4;
4308 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004309 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004310 const unsigned char *salt = initial_salt_v1;
4311 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004312
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004313 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004314 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004315 goto err;
4316 }
4317
Frédéric Lécailledc364042022-01-27 16:51:54 +01004318 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4319 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4320 goto err;
4321 }
4322
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004323 pkt->saddr = dgram->saddr;
4324 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004325 qc = qc_new_conn(pkt->version, ipv4,
4326 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004327 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004328 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004329 goto err;
4330
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004331 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4332
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004333 odcid = &qc->rx.params.original_destination_connection_id;
4334 /* Copy the transport parameters. */
4335 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004336
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004337 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004338 if (pkt->token_len) {
4339 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4340 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4341 goto err;
4342 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004343 /* Copy retry_source_connection_id transport parameter. */
4344 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4345 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004346 }
4347 else {
4348 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4349 odcid->len = pkt->dcid.len;
4350 }
4351
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004352 /* Copy the initial source connection ID. */
4353 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4354 qc->enc_params_len =
4355 quic_transport_params_encode(qc->enc_params,
4356 qc->enc_params + sizeof qc->enc_params,
4357 &qc->rx.params, 1);
4358 if (!qc->enc_params_len)
4359 goto err;
4360
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004361 if (qc_conn_alloc_ssl_ctx(qc))
4362 goto err;
4363
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004364 /* NOTE: the socket address has been concatenated to the destination ID
4365 * chosen by the client for Initial packets.
4366 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004367 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4368 salt = initial_salt_draft_29;
4369 salt_len = sizeof initial_salt_draft_29;
4370 }
4371 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004372 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004373 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004374 goto err;
4375 }
4376
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004377 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004378 n = ebmb_insert(&l->rx.dghdlrs[tid]->odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004379 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004380
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004381 /* If the insertion failed, it means that another
4382 * thread has already allocated a QUIC connection for
4383 * the same CID. Liberate our allocated connection.
4384 */
4385 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004386 qc_to_purge = qc;
4387
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004388 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4389 pkt->qc = qc;
4390 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004391
4392 quic_conn_take(qc);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004393
4394 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004395 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004396 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004397 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004398 else {
4399 quic_conn_drop(qc_to_purge);
4400 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004401 }
4402 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004403 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004404 }
4405 }
4406 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004407 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004408 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4409 goto err;
4410 }
4411
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004412 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004413 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004414
4415 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4416 * they must have the same DCID.
4417 */
4418 if (!first_pkt &&
4419 (pkt->dcid.len != dgram->dcid_len ||
4420 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4421 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4422 goto err;
4423 }
4424
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004425 buf += QUIC_HAP_CID_LEN;
4426
4427 /* A short packet is the last one of a UDP datagram. */
4428 payload = buf;
4429 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004430
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004431 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004432 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004433 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004434 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4435 goto err;
4436 }
4437
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004438 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004439 }
4440
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004441
4442 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4443 * they must have the same DCID.
4444 *
4445 * This check must be done after the final update to pkt.len to
4446 * properly drop the packet on failure.
4447 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004448 if (first_pkt && !quic_peer_validated_addr(qc) &&
4449 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4450 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4451 QUIC_EV_CONN_LPKT, qc);
4452 /* Reset the anti-amplification bit. It will be set again
4453 * when sending the next packet if reached again.
4454 */
4455 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4456 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4457 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004458 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004459
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004460 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004461
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004462 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004463 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004464 goto out;
4465 }
4466
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004467 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004468 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004469 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004470 b_cspace = b_contig_space(&qc->rx.buf);
4471 if (b_cspace < pkt->len) {
4472 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004473 if (b_cspace) {
4474 b_putchr(&qc->rx.buf, 0x00);
4475 b_cspace--;
4476 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004477 b_add(&qc->rx.buf, b_cspace);
4478 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4479 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004480 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004481 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004482 goto err;
4483 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004484 }
4485
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004486 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004487 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004488 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004489 goto err;
4490 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004492 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004493 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004494 if (pkt->aad_len)
4495 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004496 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004497 /* Wake up the connection packet handler task from here only if all
4498 * the contexts have been initialized, especially the mux context
4499 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4500 * will start it if these contexts for the connection are not already
4501 * initialized.
4502 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004503 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004504 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004505 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004506
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004507 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004508
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004509 if (qc)
4510 quic_conn_drop(qc);
4511
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004512 return pkt->len;
4513
4514 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004515 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4516 * This cannot be done by this thread.
4517 */
4518 if (io_cb_wakeup) {
4519 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4520 if (conn_ctx && conn_ctx->wait_event.tasklet)
4521 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4522 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004523 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004524 if (!pkt->len)
4525 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004526 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004527 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004528
4529 if (qc)
4530 quic_conn_drop(qc);
4531
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004532 return -1;
4533}
4534
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004535/* This function builds into <buf> buffer a QUIC long packet header.
4536 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004537 */
4538static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4539 int type, size_t pn_len, struct quic_conn *conn)
4540{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004541 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004542 return 0;
4543
4544 /* #0 byte flags */
4545 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4546 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4547 /* Version */
4548 quic_write_uint32(buf, end, conn->version);
4549 *(*buf)++ = conn->dcid.len;
4550 /* Destination connection ID */
4551 if (conn->dcid.len) {
4552 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4553 *buf += conn->dcid.len;
4554 }
4555 /* Source connection ID */
4556 *(*buf)++ = conn->scid.len;
4557 if (conn->scid.len) {
4558 memcpy(*buf, conn->scid.data, conn->scid.len);
4559 *buf += conn->scid.len;
4560 }
4561
4562 return 1;
4563}
4564
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004565/* This function builds into <buf> buffer a QUIC short packet header.
4566 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004567 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004568static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4569 size_t pn_len, struct quic_conn *conn,
4570 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004571{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004572 if (end - *buf < 1 + conn->dcid.len)
4573 return 0;
4574
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004575 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004576 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4577 ((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 +01004578 /* Destination connection ID */
4579 if (conn->dcid.len) {
4580 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4581 *buf += conn->dcid.len;
4582 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004583
4584 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004585}
4586
4587/* Apply QUIC header protection to the packet with <buf> as first byte address,
4588 * <pn> as address of the Packet number field, <pnlen> being this field length
4589 * with <aead> as AEAD cipher and <key> as secret key.
4590 * Returns 1 if succeeded or 0 if failed.
4591 */
4592static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4593 const EVP_CIPHER *aead, const unsigned char *key)
4594{
4595 int i, ret, outlen;
4596 EVP_CIPHER_CTX *ctx;
4597 /* We need an IV of at least 5 bytes: one byte for bytes #0
4598 * and at most 4 bytes for the packet number
4599 */
4600 unsigned char mask[5] = {0};
4601
4602 ret = 0;
4603 ctx = EVP_CIPHER_CTX_new();
4604 if (!ctx)
4605 return 0;
4606
4607 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4608 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4609 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4610 goto out;
4611
4612 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4613 for (i = 0; i < pnlen; i++)
4614 pn[i] ^= mask[i + 1];
4615
4616 ret = 1;
4617
4618 out:
4619 EVP_CIPHER_CTX_free(ctx);
4620
4621 return ret;
4622}
4623
4624/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4625 * ACK ranges if needed to a value below <limit> in bytes.
4626 * Return 1 if succeeded, 0 if not.
4627 */
4628static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4629{
4630 size_t room, ack_delay_sz;
4631
4632 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4633 /* A frame is made of 1 byte for the frame type. */
4634 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004635 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004636 return 0;
4637
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004638 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004639}
4640
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004641/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4642 * 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 +01004643 * and <*len> the packet Length field initialized with the number of bytes already present
4644 * 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 +02004645 * <headlen> is the number of bytes already present in this packet before building frames.
4646 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004647 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004648 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004649 * Return 1 if succeeded, 0 if not.
4650 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004651static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004652 size_t room, size_t *len, size_t headlen,
4653 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004654 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004655{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004656 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004657 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004658
Frédéric Lécailleea604992020-12-24 13:01:37 +01004659 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004660 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004661 return 0;
4662
Frédéric Lécailleea604992020-12-24 13:01:37 +01004663 /* If we are not probing we must take into an account the congestion
4664 * control window.
4665 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004666 if (!qel->pktns->tx.pto_probe) {
4667 size_t remain = quic_path_prep_data(qc->path);
4668
4669 if (headlen > remain)
4670 return 0;
4671
4672 room = QUIC_MIN(room, remain - headlen);
4673 }
4674
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004675 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004676 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004677 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004678 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004679 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004680
Frédéric Lécailleea604992020-12-24 13:01:37 +01004681 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004682 break;
4683
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004684 switch (cf->type) {
4685 case QUIC_FT_CRYPTO:
4686 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004687 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004688 /* Compute the length of this CRYPTO frame header */
4689 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4690 /* Compute the data length of this CRyPTO frame. */
4691 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4692 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004693 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004694 if (!dlen)
4695 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004696
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004697 /* CRYPTO frame length. */
4698 flen = hlen + quic_int_getsize(dlen) + dlen;
4699 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004700 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004701 /* Add the CRYPTO data length and its encoded length to the packet
4702 * length and the length of this length.
4703 */
4704 *len += flen;
4705 room -= flen;
4706 if (dlen == cf->crypto.len) {
4707 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004708 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004709 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004710 }
4711 else {
4712 struct quic_frame *new_cf;
4713
4714 new_cf = pool_alloc(pool_head_quic_frame);
4715 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004716 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004717 return 0;
4718 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004719
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004720 new_cf->type = QUIC_FT_CRYPTO;
4721 new_cf->crypto.len = dlen;
4722 new_cf->crypto.offset = cf->crypto.offset;
4723 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004724 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004725 /* Consume <dlen> bytes of the current frame. */
4726 cf->crypto.len -= dlen;
4727 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004728 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004729 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004730
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004731 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004732 /* Note that these frames are accepted in short packets only without
4733 * "Length" packet field. Here, <*len> is used only to compute the
4734 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004735 *
4736 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004737 * excepting the variable ones. Note that +1 is for the type of this frame.
4738 */
4739 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004740 ((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 +02004741 /* Compute the data length of this STREAM frame. */
4742 avail_room = room - hlen - *len;
4743 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004744 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004745
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004746 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004747 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004748 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4749 dlen = max_available_room(avail_room, &dlen_sz);
4750 if (dlen > cf->stream.len) {
4751 dlen = cf->stream.len;
4752 }
4753 dlen_sz = quic_int_getsize(dlen);
4754 flen = hlen + dlen_sz + dlen;
4755 }
4756 else {
4757 dlen = QUIC_MIN(avail_room, cf->stream.len);
4758 flen = hlen + dlen;
4759 }
4760 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004761 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004762 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004763 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004764 /* Add the STREAM data length and its encoded length to the packet
4765 * length and the length of this length.
4766 */
4767 *len += flen;
4768 room -= flen;
4769 if (dlen == cf->stream.len) {
4770 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004771 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004772 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004773 }
4774 else {
4775 struct quic_frame *new_cf;
4776
4777 new_cf = pool_zalloc(pool_head_quic_frame);
4778 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004779 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004780 return 0;
4781 }
4782
4783 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004784 new_cf->stream.qcs = cf->stream.qcs;
4785 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004786 new_cf->stream.id = cf->stream.id;
4787 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4788 new_cf->stream.offset = cf->stream.offset;
4789 new_cf->stream.len = dlen;
4790 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4791 /* FIN bit reset */
4792 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4793 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004794 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004795 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4796 /* Consume <dlen> bytes of the current frame. */
4797 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004798 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004799 cf->stream.data += dlen;
4800 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004801 break;
4802
4803 default:
4804 flen = qc_frm_len(cf);
4805 BUG_ON(!flen);
4806 if (flen > room)
4807 continue;
4808
4809 *len += flen;
4810 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004811 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004812 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004813 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004814 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004815 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004816 }
4817
Frédéric Lécailleea604992020-12-24 13:01:37 +01004818 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004819}
4820
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004821/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004822 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4823 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4824 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004825 * 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 +02004826 * 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 +02004827 * having returned from this function.
4828 * 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 +01004829 * number field in this packet. <pn_len> will also have the packet number
4830 * length as value.
4831 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004832 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004833 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004834static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4835 size_t dglen, struct quic_tx_packet *pkt,
4836 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004837 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004838 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004839{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004840 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004841 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4843 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004844 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004845 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004846 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004847 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004848 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004849
Frédéric Lécailleea604992020-12-24 13:01:37 +01004850 /* Length field value with CRYPTO frames if present. */
4851 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004852 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004853 /* When not probing and not acking, and no immediate close is required,
4854 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004855 * the congestion controller window. So, we do not limit the size of this
4856 * packet if we have an ACK frame to send because an ACK frame is not
4857 * ack-eliciting. This size will be limited if we have ack-eliciting
4858 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004859 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004860 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004861 size_t path_room;
4862
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004863 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004864 if (end - beg > path_room)
4865 end = beg + path_room;
4866 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004867
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004868 /* Ensure there is enough room for the TLS encryption tag and a zero token
4869 * length field if any.
4870 */
4871 if (end - pos < QUIC_TLS_TAG_LEN +
4872 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4873 goto no_room;
4874
4875 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004876 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004877 /* packet number length */
4878 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004879 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004880 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004881 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004882 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004883 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004884 goto no_room;
4885
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004886 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4887 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004888 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004889 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004890 /* Build an ACK frame if required. */
4891 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004892 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004893 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004894 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004895 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4896 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4897 * that from here, we do not know if we will have to send a PING frame.
4898 * This will be decided after having computed the ack-eliciting frames
4899 * to be added to this packet.
4900 */
4901 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 +01004902 if (!ack_frm_len)
4903 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004904 }
4905
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004906 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004907 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004908 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004909 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004910 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004911
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004912 /* Initialize the length of the frames built below to <len>.
4913 * If any frame could be successfully built by qc_build_frms(),
4914 * we will have len_frms > len.
4915 */
4916 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004917 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004918 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004919 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004920 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004921 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004922
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004923 /* Length (of the remaining data). Must not fail because, the buffer size
4924 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4925 * for the encryption tag. It must be taken into an account for the length
4926 * of this packet.
4927 */
4928 if (len_frms)
4929 len = len_frms + QUIC_TLS_TAG_LEN;
4930 else
4931 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004932 /* CONNECTION_CLOSE frame */
4933 if (cc) {
4934 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004935
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004936 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004937 len += qc_frm_len(&cc_frm);
4938 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004939 add_ping_frm = 0;
4940 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004941 len_sz = quic_int_getsize(len);
4942 /* Add this packet size to <dglen> */
4943 dglen += head_len + len_sz + len;
4944 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4945 /* This is a maximum padding size */
4946 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4947 /* The length field value is of this packet is <len> + <padding_len>
4948 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004949 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004950 * sizes from <padding_len>.
4951 */
4952 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4953 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004954 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004955 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004956 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004957 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004958 add_ping_frm = 1;
4959 len += 1;
4960 }
4961 /* 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 +01004962 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004963 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4964 }
4965
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004966 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4967 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004968
4969 /* Packet number field address. */
4970 *buf_pn = pos;
4971
4972 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004973 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4974 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004975
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004976 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004977 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004978
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004979 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004980 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004981 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004982
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004983 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004984 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004985 ssize_t room = end - pos;
4986 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004987 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004988 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004989 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004990 }
4991 }
4992
4993 /* Build a PING frame if needed. */
4994 if (add_ping_frm) {
4995 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004996 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004997 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004998 }
4999
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005000 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005001 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005002 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005003
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005004 /* Build a PADDING frame if needed. */
5005 if (padding_len) {
5006 frm.type = QUIC_FT_PADDING;
5007 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005008 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005009 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005010 }
5011
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005012 /* If this packet is ack-eliciting and we are probing let's
5013 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005014 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005015 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5016 qel->pktns->tx.pto_probe)
5017 qel->pktns->tx.pto_probe--;
5018
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005019 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005020 LIST_SPLICE(&pkt->frms, &frm_list);
5021 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005022
5023 return 1;
5024
5025 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005026 /* Replace the pre-built frames which could not be add to this packet */
5027 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005028 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005029 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005030}
5031
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005032static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005033{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005034 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005035 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005036 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005037 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005038 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005039 pkt->next = NULL;
5040 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005041}
5042
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005043/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005044static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5045{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005046 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005047
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005048 if (!pkt)
5049 return;
5050
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005051 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005052 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005053 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005054 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005055 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005056}
5057
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005058/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5059 * type for <qc> QUIC connection from <qel> encryption level.
5060 * Return -2 if the packet could not be allocated or encrypted for any reason,
5061 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005062 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005063static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5064 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005065 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005066 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005067 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005068{
5069 /* The pointer to the packet number field. */
5070 unsigned char *buf_pn;
5071 unsigned char *beg, *end, *payload;
5072 int64_t pn;
5073 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005074 struct quic_tls_ctx *tls_ctx;
5075 struct quic_tx_packet *pkt;
5076
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005077 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005078 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005079 pkt = pool_alloc(pool_head_quic_tx_packet);
5080 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005081 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005082 *err = -2;
5083 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005084 }
5085
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005086 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005087 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005088 pn_len = 0;
5089 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005090
5091 pn = qel->pktns->tx.next_pn + 1;
5092 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 +01005093 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005094 *err = -1;
5095 goto err;
5096 }
5097
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005098 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005099 payload = buf_pn + pn_len;
5100 payload_len = end - payload;
5101 aad_len = payload - beg;
5102
5103 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005104 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005105 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005106 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005107 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108
5109 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005110 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005111 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5112 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005113 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005114 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005115 goto err;
5116 }
5117
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005118 /* Consume a packet number */
5119 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005120 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005121 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5122 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005123 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5124 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005125 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005126 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005127 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005128 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005129 pkt->in_flight_len = pkt->len;
5130 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005131 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005132 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005133 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005135 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136
5137 err:
5138 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005139 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005140 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005141}
5142
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005143/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
5144 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005146static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
5147 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005148{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005149 size_t try, done = 0;
5150
5151 if (!conn_ctrl_ready(conn))
5152 return 0;
5153
5154 if (!fd_recv_ready(conn->handle.fd))
5155 return 0;
5156
5157 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005158
5159 /* read the largest possible block. For this, we perform only one call
5160 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005161 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162 */
5163 while (count > 0) {
5164 try = b_contig_space(buf);
5165 if (!try)
5166 break;
5167
5168 if (try > count)
5169 try = count;
5170
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005171 b_add(buf, try);
5172 done += try;
5173 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005174 }
5175
5176 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5177 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5178
5179 leave:
5180 return done;
5181
5182 read0:
5183 conn_sock_read0(conn);
5184 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5185
5186 /* Now a final check for a possible asynchronous low-level error
5187 * report. This can happen when a connection receives a reset
5188 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
5189 * we might have come from there by just checking POLL_HUP instead
5190 * of recv()'s return value 0, so we have no way to tell there was
5191 * an error without checking.
5192 */
Willy Tarreauf5090652021-04-06 17:23:40 +02005193 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005194 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5195 goto leave;
5196}
5197
5198
5199/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5200 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5201 * other pending data for example, but this flag is ignored at the moment.
5202 * Only one call to send() is performed, unless the buffer wraps, in which case
5203 * a second call may be performed. The connection's flags are updated with
5204 * whatever special event is detected (error, empty). The caller is responsible
5205 * for taking care of those events and avoiding the call if inappropriate. The
5206 * function does not call the connection's polling update function, so the caller
5207 * is responsible for this. It's up to the caller to update the buffer's contents
5208 * based on the return value.
5209 */
5210static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5211{
5212 ssize_t ret;
5213 size_t try, done;
5214 int send_flag;
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005215 struct quic_conn *qc = ((struct ssl_sock_ctx *)xprt_ctx)->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005216
5217 done = 0;
5218 /* send the largest possible block. For this we perform only one call
5219 * to send() unless the buffer wraps and we exactly fill the first hunk,
5220 * in which case we accept to do it once again.
5221 */
5222 while (count) {
5223 try = b_contig_data(buf, done);
5224 if (try > count)
5225 try = count;
5226
5227 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5228 if (try < count || flags & CO_SFL_MSG_MORE)
5229 send_flag |= MSG_MORE;
5230
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005231 ret = sendto(qc->li->rx.fd, b_peek(buf, done), try, send_flag,
5232 (struct sockaddr *)&qc->peer_addr, get_addr_len(&qc->peer_addr));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005233 if (ret > 0) {
5234 count -= ret;
5235 done += ret;
5236
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237 if (ret < try)
5238 break;
5239 }
5240 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005241 ABORT_NOW();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005242 }
5243 else if (errno != EINTR) {
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005244 ABORT_NOW();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005245 }
5246 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005247
5248 if (done > 0) {
5249 /* we count the total bytes sent, and the send rate for 32-byte
5250 * blocks. The reason for the latter is that freq_ctr are
5251 * limited to 4GB and that it's not enough per second.
5252 */
5253 _HA_ATOMIC_ADD(&global.out_bytes, done);
5254 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5255 }
5256 return done;
5257}
5258
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005259/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5260 * event subscriber <es> is not allowed to change from a previous call as long
5261 * as at least one event is still subscribed. The <event_type> must only be a
5262 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5263 */
5264static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5265{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005266 struct qcc *qcc = conn->qc->qcc;
5267
5268 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5269 BUG_ON(qcc->subs && qcc->subs != es);
5270
5271 es->events |= event_type;
5272 qcc->subs = es;
5273
5274 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005275 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005276
5277 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005278 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005279
5280 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005281}
5282
5283/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5284 * The <es> pointer is not allowed to differ from the one passed to the
5285 * subscribe() call. It always returns zero.
5286 */
5287static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5288{
5289 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5290}
5291
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005292/* Store in <xprt_ctx> the context attached to <conn>.
5293 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005294 */
5295static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5296{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005297 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005298
5299 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5300
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005301 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005302 if (*xprt_ctx)
5303 goto out;
5304
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005305 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005306
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005307 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005308 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005309
5310 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005311}
5312
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005313/* Start the QUIC transport layer */
5314static int qc_xprt_start(struct connection *conn, void *ctx)
5315{
5316 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005317 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005318
5319 qc = conn->qc;
5320 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005321 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005322 return 0;
5323 }
5324
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005325 quic_mux_transport_params_update(qc->qcc);
5326 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5327 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5328 return 0;
5329 }
5330
5331 /* mux-quic can now be considered ready. */
5332 qc->mux_state = QC_MUX_READY;
5333
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005334 tasklet_wakeup(qctx->wait_event.tasklet);
5335 return 1;
5336}
5337
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005338/* transport-layer operations for QUIC connections. */
5339static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005340 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005341 .snd_buf = quic_conn_from_buf,
5342 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005343 .subscribe = quic_conn_subscribe,
5344 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005345 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005346 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005347 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5348 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005349 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005350 .name = "QUIC",
5351};
5352
5353__attribute__((constructor))
5354static void __quic_conn_init(void)
5355{
5356 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5357 xprt_register(XPRT_QUIC, &ssl_quic);
5358}
5359
5360__attribute__((destructor))
5361static void __quic_conn_deinit(void)
5362{
5363 BIO_meth_free(ha_quic_meth);
5364}
5365
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005366/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5367 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005368 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005369 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005370struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005371{
5372 unsigned char *pos;
5373 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005374 struct quic_dghdlr *dghdlr = ctx;
5375 struct quic_dgram *dgram;
5376 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005377
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005378 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
5379 if (!dgram)
5380 goto err;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005381
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005382 pos = dgram->buf;
5383 end = pos + dgram->len;
5384 do {
5385 int ret;
5386 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005387
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005388 pkt = pool_zalloc(pool_head_quic_rx_packet);
5389 if (!pkt)
5390 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005391
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005392 quic_rx_packet_refinc(pkt);
5393 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5394 first_pkt = 0;
5395 pos += pkt->len;
5396 quic_rx_packet_refdec(pkt);
5397 if (ret == -1)
5398 /* If the packet length could not be found, we cannot continue. */
5399 break;
5400 } while (pos < end);
5401
5402 /* Mark this datagram as consumed */
5403 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005404
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005405 /* Increasing the received bytes counter by the UDP datagram length
5406 * if this datagram could be associated to a connection.
5407 */
5408 if (dgram->qc)
5409 dgram->qc->rx.bytes += dgram->len;
5410 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005411
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005412 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005413
5414 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005415 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005416}
5417
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005418/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5419 * Returns 1 if succeeded, 0 if not.
5420 */
5421static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5422 unsigned char **dcid, size_t *dcid_len)
5423{
5424 int long_header;
5425 size_t minlen, skip;
5426
5427 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5428 goto err;
5429
5430 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5431 minlen = long_header ?
5432 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5433 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
5434 if (end - buf <= minlen || !(*buf & QUIC_PACKET_FIXED_BIT))
5435 goto err;
5436
5437 buf += skip;
5438 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5439 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5440 goto err;
5441
5442 *dcid = buf;
5443
5444 return 1;
5445
5446 err:
5447 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5448 return 0;
5449}
5450
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005451/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5452 * correct datagram handler.
5453 * Return 1 if a correct datagram could be found, 0 if not.
5454 */
5455int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5456 struct sockaddr_storage *saddr,
5457 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005458{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005459 struct quic_dgram *dgram;
5460 unsigned char *dcid;
5461 size_t dcid_len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005462 int tid;
5463 struct listener *l = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005464
5465 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005466 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005467
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005468 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005469 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005470 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005471
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005472 tid = quic_get_cid_tid(dcid);
5473 /* All the members must be initialized! */
5474 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005475 dgram->buf = buf;
5476 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005477 dgram->dcid = dcid;
5478 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005479 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005480 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005481 LIST_APPEND(dgrams, &dgram->list);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005482 MT_LIST_APPEND(&l->rx.dghdlrs[tid]->dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005483
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005484 tasklet_wakeup(l->rx.dghdlrs[tid]->task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005485
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005486 return 1;
5487
5488 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005489 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005490}
5491
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005492/* Function to automatically activate QUIC traces on stdout.
5493 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5494 * Main use for now is in the docker image for QUIC interop testing.
5495 */
5496static void quic_init_stdout_traces(void)
5497{
5498#ifdef ENABLE_QUIC_STDOUT_TRACES
5499 trace_quic.sink = sink_find("stdout");
5500 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005501 trace_quic.state = TRACE_STATE_RUNNING;
5502#endif
5503}
5504INITCALL0(STG_INIT, quic_init_stdout_traces);
5505
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005506/*
5507 * Local variables:
5508 * c-indent-level: 8
5509 * c-basic-offset: 8
5510 * End:
5511 */