blob: f1adc19a8e48bcbecc742f315c4b465d9b8dcaba [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>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020047#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010048#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010049#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/ssl_sock.h>
51#include <haproxy/stream_interface.h>
52#include <haproxy/task.h>
53#include <haproxy/trace.h>
54#include <haproxy/xprt_quic.h>
55
Amaury Denoyellea22d8602021-11-10 15:17:56 +010056/* list of supported QUIC versions by this implementation */
57static int quic_supported_version[] = {
58 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010059 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010060
61 /* placeholder, do not add entry after this */
62 0x0
63};
64
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020065/* This is the values of some QUIC transport parameters when absent.
66 * Should be used to initialize any transport parameters (local or remote)
67 * before updating them with customized values.
68 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010069struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020070 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020071 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
72 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020073 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074};
75
76/* trace source and events */
77static void quic_trace(enum trace_level level, uint64_t mask, \
78 const struct trace_source *src,
79 const struct ist where, const struct ist func,
80 const void *a1, const void *a2, const void *a3, const void *a4);
81
82static const struct trace_event quic_trace_events[] = {
83 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
84 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
85 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
86 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
87 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
88 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
89 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050090 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010091 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
92 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
93 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
94 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
95 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
96 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
97 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
98 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
99 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
100 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
101 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
102 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
103 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
104 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
105 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
106 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
107 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
108 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
109 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100110 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
111 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
112 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
113 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
114 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
115 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100116 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200117 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
118 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100119 { /* end */ }
120};
121
122static const struct name_desc quic_trace_lockon_args[4] = {
123 /* arg1 */ { /* already used by the connection */ },
124 /* arg2 */ { .name="quic", .desc="QUIC transport" },
125 /* arg3 */ { },
126 /* arg4 */ { }
127};
128
129static const struct name_desc quic_trace_decoding[] = {
130#define QUIC_VERB_CLEAN 1
131 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
132 { /* end */ }
133};
134
135
136struct trace_source trace_quic = {
137 .name = IST("quic"),
138 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100139 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100140 .default_cb = quic_trace,
141 .known_events = quic_trace_events,
142 .lockon_args = quic_trace_lockon_args,
143 .decoding = quic_trace_decoding,
144 .report_events = ~0, /* report everything by default */
145};
146
147#define TRACE_SOURCE &trace_quic
148INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
149
150static BIO_METHOD *ha_quic_meth;
151
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200152DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200153DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100154DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200156 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100157DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_POOL(pool_head_quic_connection_id,
159 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_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 +0100161DECLARE_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 +0100162DECLARE_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 +0100163DECLARE_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 +0100164DECLARE_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 +0200165DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100166DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100167
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200168static 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 +0200169 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100170 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +0100171 int padding, int ack, int nb_pto_dgrams, int cc, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100172
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100173/* Only for debug purpose */
174struct enc_debug_info {
175 unsigned char *payload;
176 size_t payload_len;
177 unsigned char *aad;
178 size_t aad_len;
179 uint64_t pn;
180};
181
182/* Initializes a enc_debug_info struct (only for debug purpose) */
183static inline void enc_debug_info_init(struct enc_debug_info *edi,
184 unsigned char *payload, size_t payload_len,
185 unsigned char *aad, size_t aad_len, uint64_t pn)
186{
187 edi->payload = payload;
188 edi->payload_len = payload_len;
189 edi->aad = aad;
190 edi->aad_len = aad_len;
191 edi->pn = pn;
192}
193
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100194/* Trace callback for QUIC.
195 * These traces always expect that arg1, if non-null, is of type connection.
196 */
197static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
198 const struct ist where, const struct ist func,
199 const void *a1, const void *a2, const void *a3, const void *a4)
200{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100201 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100202
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100203 if (qc) {
204 const struct quic_tls_secrets *secs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100205
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100206 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207 if ((mask & QUIC_EV_CONN_INIT) && qc) {
208 chunk_appendf(&trace_buf, "\n odcid");
209 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100210 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100212 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100213 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 }
215
216 if (mask & QUIC_EV_CONN_ADDDATA) {
217 const enum ssl_encryption_level_t *level = a2;
218 const size_t *len = a3;
219
220 if (level) {
221 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
222
223 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
224 }
225 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100226 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100227 }
228 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
229 /* Initial read & write secrets. */
230 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
231 const unsigned char *rx_sec = a2;
232 const unsigned char *tx_sec = a3;
233
234 secs = &qc->els[level].tls_ctx.rx;
235 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
236 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
237 if (rx_sec)
238 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
239 quic_tls_keys_hexdump(&trace_buf, secs);
240 }
241 secs = &qc->els[level].tls_ctx.tx;
242 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
243 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
244 if (tx_sec)
245 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
246 quic_tls_keys_hexdump(&trace_buf, secs);
247 }
248 }
249 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
250 const enum ssl_encryption_level_t *level = a2;
251 const unsigned char *secret = a3;
252 const size_t *secret_len = a4;
253
254 if (level) {
255 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
256
257 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
258 if (secret && secret_len)
259 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
260 secs = &qc->els[lvl].tls_ctx.rx;
261 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
262 quic_tls_keys_hexdump(&trace_buf, secs);
263 }
264 }
265
266 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
267 const enum ssl_encryption_level_t *level = a2;
268 const unsigned char *secret = a3;
269 const size_t *secret_len = a4;
270
271 if (level) {
272 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
273
274 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
275 if (secret && secret_len)
276 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
277 secs = &qc->els[lvl].tls_ctx.tx;
278 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
279 quic_tls_keys_hexdump(&trace_buf, secs);
280 }
281
282 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100284 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285 const struct quic_tx_packet *pkt = a2;
286 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100287 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288
289 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100290 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100292 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100293 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100294 (unsigned long long)qc->path->cwnd,
295 (unsigned long long)qc->path->prep_in_flight,
296 (unsigned long long)qc->path->in_flight,
297 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100298 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100299 }
300 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200301 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100302 if (pkt->pn_node.key != (uint64_t)-1)
303 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100305 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100306 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
307 }
308
309 if (room) {
310 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
311 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
312 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100313 }
314 }
315
316 if (mask & QUIC_EV_CONN_HDSHK) {
317 const enum quic_handshake_state *state = a2;
318 const int *err = a3;
319
320 if (state)
321 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
322 if (err)
323 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
324 }
325
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100326 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100327 const struct quic_rx_packet *pkt = a2;
328 const unsigned long *pktlen = a3;
329 const SSL *ssl = a4;
330
331 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100332 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100333 pkt, quic_packet_type_enc_level_char(pkt->type));
334 if (pkt->pnl)
335 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
336 (unsigned long long)pkt->pn);
337 if (pkt->token_len)
338 chunk_appendf(&trace_buf, " toklen=%llu",
339 (unsigned long long)pkt->token_len);
340 if (pkt->aad_len)
341 chunk_appendf(&trace_buf, " aadlen=%llu",
342 (unsigned long long)pkt->aad_len);
343 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
344 pkt->flags, (unsigned long long)pkt->len);
345 }
346 if (pktlen)
347 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
348 if (ssl) {
349 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
350 chunk_appendf(&trace_buf, " el=%c",
351 quic_enc_level_char(ssl_to_quic_enc_level(level)));
352 }
353 }
354
355 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
356 const struct quic_rx_packet *pkt = a2;
357 const struct quic_rx_crypto_frm *cf = a3;
358 const SSL *ssl = a4;
359
360 if (pkt)
361 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
362 quic_packet_type_enc_level_char(pkt->type),
363 (unsigned long long)pkt->pn);
364 if (cf)
365 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
366 (unsigned long long)cf->offset_node.key,
367 (unsigned long long)cf->len);
368 if (ssl) {
369 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200370 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100371 quic_enc_level_char(ssl_to_quic_enc_level(level)));
372 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100373
374 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100375 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100376 }
377
378 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
379 const struct quic_frame *frm = a2;
380
381 if (frm)
382 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
383 }
384
385 if (mask & QUIC_EV_CONN_PHPKTS) {
386 const struct quic_enc_level *qel = a2;
387
388 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100389 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100390 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100391 " 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 +0100392 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200393 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100394 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100395 (unsigned long long)qc->path->cwnd,
396 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100398 (unsigned long long)pktns->tx.in_flight,
399 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100400 }
401 }
402
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100403 if (mask & QUIC_EV_CONN_ENCPKT) {
404 const struct enc_debug_info *edi = a2;
405
406 if (edi)
407 chunk_appendf(&trace_buf,
408 " payload=@%p payload_len=%llu"
409 " aad=@%p aad_len=%llu pn=%llu",
410 edi->payload, (unsigned long long)edi->payload_len,
411 edi->aad, (unsigned long long)edi->aad_len,
412 (unsigned long long)edi->pn);
413 }
414
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100415 if (mask & QUIC_EV_CONN_RMHP) {
416 const struct quic_rx_packet *pkt = a2;
417
418 if (pkt) {
419 const int *ret = a3;
420
421 chunk_appendf(&trace_buf, " pkt@%p", pkt);
422 if (ret && *ret)
423 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
424 pkt->pnl, (unsigned long long)pkt->pn);
425 }
426 }
427
428 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200429 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100430 const unsigned long *val1 = a3;
431 const unsigned long *val2 = a4;
432
433 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100434 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100435 if (val1)
436 chunk_appendf(&trace_buf, " %lu", *val1);
437 if (val2)
438 chunk_appendf(&trace_buf, "..%lu", *val2);
439 }
440
441 if (mask & QUIC_EV_CONN_RTTUPDT) {
442 const unsigned int *rtt_sample = a2;
443 const unsigned int *ack_delay = a3;
444 const struct quic_loss *ql = a4;
445
446 if (rtt_sample)
447 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
448 if (ack_delay)
449 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
450 if (ql)
451 chunk_appendf(&trace_buf,
452 " srtt=%ums rttvar=%ums min_rtt=%ums",
453 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
454 }
455 if (mask & QUIC_EV_CONN_CC) {
456 const struct quic_cc_event *ev = a2;
457 const struct quic_cc *cc = a3;
458
459 if (a2)
460 quic_cc_event_trace(&trace_buf, ev);
461 if (a3)
462 quic_cc_state_trace(&trace_buf, cc);
463 }
464
465 if (mask & QUIC_EV_CONN_PKTLOSS) {
466 const struct quic_pktns *pktns = a2;
467 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100468
469 if (pktns) {
470 chunk_appendf(&trace_buf, " pktns=%s",
471 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
472 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
473 if (pktns->tx.loss_time)
474 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100475 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100476 }
477 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
478 struct quic_tx_packet *pkt;
479
480 chunk_appendf(&trace_buf, " lost_pkts:");
481 list_for_each_entry(pkt, lost_pkts, list)
482 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
483 }
484 }
485
486 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100487 const struct quic_pktns *pktns = a2;
488 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100489 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100490
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100491 if (ifae_pkts)
492 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
493 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100495 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100496 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100497 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
498 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100499 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
500 if (pktns->tx.in_flight)
501 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100502 if (pktns->tx.loss_time)
503 chunk_appendf(&trace_buf, " loss_time=%dms",
504 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
505 }
506 if (mask & QUIC_EV_CONN_SPTO) {
507 if (pktns->tx.time_of_last_eliciting)
508 chunk_appendf(&trace_buf, " tole=%dms",
509 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
510 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100511 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512 }
513 }
514
515 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
516 chunk_appendf(&trace_buf,
517 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
518 }
519 }
520
521 if (mask & QUIC_EV_CONN_SPPKTS) {
522 const struct quic_tx_packet *pkt = a2;
523
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100524 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
525 (unsigned long long)qc->path->cwnd,
526 (unsigned long long)qc->path->prep_in_flight,
527 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100529 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530 (unsigned long)pkt->pn_node.key,
531 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
532 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100533 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 }
535 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100536
537 if (mask & QUIC_EV_CONN_SSLALERT) {
538 const uint8_t *alert = a2;
539 const enum ssl_encryption_level_t *level = a3;
540
541 if (alert)
542 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
543 if (level)
544 chunk_appendf(&trace_buf, " el=%c",
545 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
546 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100547
548 if (mask & QUIC_EV_CONN_BCFRMS) {
549 const size_t *sz1 = a2;
550 const size_t *sz2 = a3;
551 const size_t *sz3 = a4;
552
553 if (sz1)
554 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
555 if (sz2)
556 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
557 if (sz3)
558 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
559 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100560
561 if (mask & QUIC_EV_CONN_PSTRM) {
562 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100563
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100564 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100565 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100566 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100567 }
568 if (mask & QUIC_EV_CONN_LPKT) {
569 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200570 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100571
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100572 if (pkt) {
573 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
574 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
575 if (pkt->pn_node.key != (uint64_t)-1)
576 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
577 }
578
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200579 if (len)
580 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 }
582
583}
584
585/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100586static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200588 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100589
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100590 if (objt_server(qc->conn->target))
591 return 1;
592
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200593 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
594 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
595 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
596 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200597 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 return 1;
599
600 return 0;
601}
602
603/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
604 * both loss detection and PTO and schedule the task assiated to this timer if needed.
605 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100606static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100607{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100608 struct quic_pktns *pktns;
609 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200610 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100612 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100613 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100614
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615 pktns = quic_loss_pktns(qc);
616 if (tick_isset(pktns->tx.loss_time)) {
617 qc->timer = pktns->tx.loss_time;
618 goto out;
619 }
620
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100621 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622 * cancelled for a server which reached the anti-amplification limit.
623 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100624 if (!quic_peer_validated_addr(qc) &&
625 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100626 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100627 qc->timer = TICK_ETERNITY;
628 goto out;
629 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100630
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100631 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100632 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100633 /* Timer cancellation. */
634 qc->timer = TICK_ETERNITY;
635 goto out;
636 }
637
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200638 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
639 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100640 if (tick_isset(pto))
641 qc->timer = pto;
642 out:
Amaury Denoyelle0a29e132021-12-23 15:06:56 +0100643 if (qc->timer_task && qc->timer != TICK_ETERNITY)
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200644 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100645 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646}
647
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100648/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
649 * connection.
650 * Return 1 if succeeded, 0 if not.
651 */
652static int quic_tls_key_update(struct quic_conn *qc)
653{
654 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
655 struct quic_tls_secrets *rx, *tx;
656 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
657 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
658
659 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
660 rx = &tls_ctx->rx;
661 tx = &tls_ctx->tx;
662 nxt_rx = &qc->ku.nxt_rx;
663 nxt_tx = &qc->ku.nxt_tx;
664
665 /* Prepare new RX secrets */
666 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
667 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100668 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100669 return 0;
670 }
671
672 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
673 nxt_rx->key, nxt_rx->keylen,
674 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
675 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100676 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100677 return 0;
678 }
679
680 /* Prepare new TX secrets */
681 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
682 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100683 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100684 return 0;
685 }
686
687 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
688 nxt_tx->key, nxt_tx->keylen,
689 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
690 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100691 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100692 return 0;
693 }
694
695 return 1;
696}
697
698/* Rotate the Key Update information for <qc> QUIC connection.
699 * Must be used after having updated them.
700 * Always succeeds.
701 */
702static void quic_tls_rotate_keys(struct quic_conn *qc)
703{
704 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
705 unsigned char *curr_secret, *curr_iv, *curr_key;
706
707 /* Rotate the RX secrets */
708 curr_secret = tls_ctx->rx.secret;
709 curr_iv = tls_ctx->rx.iv;
710 curr_key = tls_ctx->rx.key;
711
712 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
713 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
714 tls_ctx->rx.key = qc->ku.nxt_rx.key;
715
716 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
717 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
718 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
719
720 qc->ku.prv_rx.secret = curr_secret;
721 qc->ku.prv_rx.iv = curr_iv;
722 qc->ku.prv_rx.key = curr_key;
723 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
724
725 /* Update the TX secrets */
726 curr_secret = tls_ctx->tx.secret;
727 curr_iv = tls_ctx->tx.iv;
728 curr_key = tls_ctx->tx.key;
729
730 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
731 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
732 tls_ctx->tx.key = qc->ku.nxt_tx.key;
733
734 qc->ku.nxt_tx.secret = curr_secret;
735 qc->ku.nxt_tx.iv = curr_iv;
736 qc->ku.nxt_tx.key = curr_key;
737}
738
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100739#ifndef OPENSSL_IS_BORINGSSL
740int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
741 const uint8_t *read_secret,
742 const uint8_t *write_secret, size_t secret_len)
743{
744 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
745 struct quic_tls_ctx *tls_ctx =
746 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
747 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100748 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100749
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100750 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100751 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100752 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100753 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100754 goto out;
755 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100756
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100757 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100758 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100759 goto err;
760 }
761
762 rx = &tls_ctx->rx;
763 tx = &tls_ctx->tx;
764
765 rx->aead = tx->aead = tls_aead(cipher);
766 rx->md = tx->md = tls_md(cipher);
767 rx->hp = tx->hp = tls_hp(cipher);
768
769 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
770 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100771 read_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100772 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100773 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100774 }
775
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100776 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100777
778 if (!write_secret)
779 goto tp;
780
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100781 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
782 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100783 write_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100784 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100785 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100786 }
787
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100788 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100789 tp:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100790 if (objt_server(conn->target) && level == ssl_encryption_application) {
791 const unsigned char *buf;
792 size_t buflen;
793
794 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
795 if (!buflen)
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 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100799 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100800 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100801
802 if (level == ssl_encryption_application) {
803 struct quic_conn *qc = conn->qc;
804 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
805 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
806 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
807
808 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
809 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100810 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100811 goto err;
812 }
813
814 memcpy(rx->secret, read_secret, secret_len);
815 rx->secretlen = secret_len;
816 memcpy(tx->secret, write_secret, secret_len);
817 tx->secretlen = secret_len;
818 /* Initialize all the secret keys lengths */
819 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
820 /* Prepare the next key update */
821 if (!quic_tls_key_update(qc))
822 goto err;
823 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100824 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100825 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100826 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100827
828 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100829 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100830 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100831}
832#else
833/* ->set_read_secret callback to derive the RX secrets at <level> encryption
834 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500835 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100836 */
837int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
838 const SSL_CIPHER *cipher,
839 const uint8_t *secret, size_t secret_len)
840{
841 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
842 struct quic_tls_ctx *tls_ctx =
843 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
844
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100845 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100846 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100847 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100848 goto out;
849 }
850
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100851 tls_ctx->rx.aead = tls_aead(cipher);
852 tls_ctx->rx.md = tls_md(cipher);
853 tls_ctx->rx.hp = tls_hp(cipher);
854
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100855 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
856 goto err;
857
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100858 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 +0100859 tls_ctx->rx.key, tls_ctx->rx.keylen,
860 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100861 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
862 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100863 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100864 goto err;
865 }
866
867 if (objt_server(conn->target) && level == ssl_encryption_application) {
868 const unsigned char *buf;
869 size_t buflen;
870
871 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
872 if (!buflen)
873 goto err;
874
875 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
876 goto err;
877 }
878
879 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100880 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100881 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100882
883 return 1;
884
885 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100886 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100887 return 0;
888}
889
890/* ->set_write_secret callback to derive the TX secrets at <level>
891 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500892 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100893 */
894int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
895 const SSL_CIPHER *cipher,
896 const uint8_t *secret, size_t secret_len)
897{
898 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
899 struct quic_tls_ctx *tls_ctx =
900 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
901
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100902 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100903 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100904 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100905 goto out;
906 }
907
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100908 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
909 goto err;
910
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100911 tls_ctx->tx.aead = tls_aead(cipher);
912 tls_ctx->tx.md = tls_md(cipher);
913 tls_ctx->tx.hp = tls_hp(cipher);
914
915 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 +0100916 tls_ctx->tx.key, tls_ctx->tx.keylen,
917 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100918 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
919 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100920 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100921 goto err;
922 }
923
924 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100925 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100926 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100927 return 1;
928
929 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100930 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100931 return 0;
932}
933#endif
934
935/* This function copies the CRYPTO data provided by the TLS stack found at <data>
936 * with <len> as size in CRYPTO buffers dedicated to store the information about
937 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
938 * It fails only if it could not managed to allocate enough CRYPTO buffers to
939 * store all the data.
940 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
941 */
942static int quic_crypto_data_cpy(struct quic_enc_level *qel,
943 const unsigned char *data, size_t len)
944{
945 struct quic_crypto_buf **qcb;
946 /* The remaining byte to store in CRYPTO buffers. */
947 size_t cf_offset, cf_len, *nb_buf;
948 unsigned char *pos;
949
950 nb_buf = &qel->tx.crypto.nb_buf;
951 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
952 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
953 cf_len = len;
954
955 while (len) {
956 size_t to_copy, room;
957
958 pos = (*qcb)->data + (*qcb)->sz;
959 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
960 to_copy = len > room ? room : len;
961 if (to_copy) {
962 memcpy(pos, data, to_copy);
963 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
964 qel->tx.crypto.sz += to_copy;
965 (*qcb)->sz += to_copy;
966 pos += to_copy;
967 len -= to_copy;
968 data += to_copy;
969 }
970 else {
971 struct quic_crypto_buf **tmp;
972
973 tmp = realloc(qel->tx.crypto.bufs,
974 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
975 if (tmp) {
976 qel->tx.crypto.bufs = tmp;
977 qcb = &qel->tx.crypto.bufs[*nb_buf];
978 *qcb = pool_alloc(pool_head_quic_crypto_buf);
979 if (!*qcb)
980 return 0;
981
982 (*qcb)->sz = 0;
983 ++*nb_buf;
984 }
985 else {
986 break;
987 }
988 }
989 }
990
991 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
992 * have been buffered.
993 */
994 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200995 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100996
Frédéric Lécailled4ecf942022-01-04 23:15:40 +0100997 if (MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
998 frm = pool_alloc(pool_head_quic_frame);
999 if (!frm)
1000 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001001
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001002 frm->type = QUIC_FT_CRYPTO;
1003 frm->crypto.offset = cf_offset;
1004 frm->crypto.len = cf_len;
1005 frm->crypto.qel = qel;
1006 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
1007 }
1008 else {
1009 frm = MT_LIST_NEXT(&qel->pktns->tx.frms, struct quic_frame *, mt_list);
1010 frm->crypto.len += cf_len;
1011 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001012 }
1013
1014 return len == 0;
1015}
1016
1017
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001018/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1019void quic_set_tls_alert(struct quic_conn *qc, int alert)
1020{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001021 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001022 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001023 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001024}
1025
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001026/* Set the application for <qc> QUIC connection.
1027 * Return 1 if succeeded, 0 if not.
1028 */
1029int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1030{
1031 const struct qcc_app_ops *app_ops;
1032
1033 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1034 app_ops = qc->qcc->app_ops = &h3_ops;
1035 }
1036 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1037 app_ops = qc->qcc->app_ops = &hq_interop_ops;
1038 }
1039 else
1040 return 0;
1041
1042 if (app_ops->init && !app_ops->init(qc->qcc))
1043 return 0;
1044
1045 if (app_ops->finalize)
1046 app_ops->finalize(qc->qcc->ctx);
1047
1048 return 1;
1049}
1050
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001051/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1052 * wants to provide the QUIC layer with CRYPTO data.
1053 * Returns 1 if succeeded, 0 if not.
1054 */
1055int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1056 const uint8_t *data, size_t len)
1057{
1058 struct connection *conn;
1059 enum quic_tls_enc_level tel;
1060 struct quic_enc_level *qel;
1061
1062 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001063 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001064 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001065 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001066 goto out;
1067 }
1068
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001069 tel = ssl_to_quic_enc_level(level);
1070 qel = &conn->qc->els[tel];
1071
1072 if (tel == -1) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001073 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001074 goto err;
1075 }
1076
1077 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001078 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001079 goto err;
1080 }
1081
1082 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001083 conn->qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001084
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001085 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001086 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001087 return 1;
1088
1089 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001090 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001091 return 0;
1092}
1093
1094int ha_quic_flush_flight(SSL *ssl)
1095{
1096 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1097
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001098 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc);
1099 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001100
1101 return 1;
1102}
1103
1104int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1105{
1106 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1107
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001108 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001109 quic_set_tls_alert(conn->qc, alert);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001110 HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111 return 1;
1112}
1113
1114/* QUIC TLS methods */
1115static SSL_QUIC_METHOD ha_quic_method = {
1116#ifdef OPENSSL_IS_BORINGSSL
1117 .set_read_secret = ha_set_rsec,
1118 .set_write_secret = ha_set_wsec,
1119#else
1120 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1121#endif
1122 .add_handshake_data = ha_quic_add_handshake_data,
1123 .flush_flight = ha_quic_flush_flight,
1124 .send_alert = ha_quic_send_alert,
1125};
1126
1127/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1128 * Returns an error count.
1129 */
1130int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1131{
1132 struct proxy *curproxy = bind_conf->frontend;
1133 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1134 int cfgerr = 0;
1135
1136#if 0
1137 /* XXX Did not manage to use this. */
1138 const char *ciphers =
1139 "TLS_AES_128_GCM_SHA256:"
1140 "TLS_AES_256_GCM_SHA384:"
1141 "TLS_CHACHA20_POLY1305_SHA256:"
1142 "TLS_AES_128_CCM_SHA256";
1143#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001144 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001145 long options =
1146 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1147 SSL_OP_SINGLE_ECDH_USE |
1148 SSL_OP_CIPHER_SERVER_PREFERENCE;
1149 SSL_CTX *ctx;
1150
1151 ctx = SSL_CTX_new(TLS_server_method());
1152 bind_conf->initial_ctx = ctx;
1153
1154 SSL_CTX_set_options(ctx, options);
1155#if 0
1156 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1157 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1158 "for bind '%s' at [%s:%d].\n",
1159 curproxy->id, ciphers,
1160 bind_conf->arg, bind_conf->file, bind_conf->line);
1161 cfgerr++;
1162 }
1163#endif
1164
1165 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1166 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1167 "for bind '%s' at [%s:%d].\n",
1168 curproxy->id, groups,
1169 bind_conf->arg, bind_conf->file, bind_conf->line);
1170 cfgerr++;
1171 }
1172
1173 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1174 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1175 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1176 SSL_CTX_set_default_verify_paths(ctx);
1177
1178#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1179#ifdef OPENSSL_IS_BORINGSSL
1180 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1181 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1182#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1183 if (bind_conf->ssl_conf.early_data) {
1184 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001185 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001186 }
1187 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1188 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1189#else
1190 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1191#endif
1192 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1193#endif
1194 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1195
1196 return cfgerr;
1197}
1198
1199/* Decode an expected packet number from <truncated_on> its truncated value,
1200 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1201 * the number of bits used to encode this packet number (its length in bytes * 8).
1202 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1203 */
1204static uint64_t decode_packet_number(uint64_t largest_pn,
1205 uint32_t truncated_pn, unsigned int pn_nbits)
1206{
1207 uint64_t expected_pn = largest_pn + 1;
1208 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1209 uint64_t pn_hwin = pn_win / 2;
1210 uint64_t pn_mask = pn_win - 1;
1211 uint64_t candidate_pn;
1212
1213
1214 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1215 /* Note that <pn_win> > <pn_hwin>. */
1216 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1217 candidate_pn + pn_hwin <= expected_pn)
1218 return candidate_pn + pn_win;
1219
1220 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1221 return candidate_pn - pn_win;
1222
1223 return candidate_pn;
1224}
1225
1226/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1227 * cryptographic context.
1228 * <largest_pn> is the largest received packet number and <pn> the address of
1229 * the packet number field for this packet with <byte0> address of its first byte.
1230 * <end> points to one byte past the end of this packet.
1231 * Returns 1 if succeeded, 0 if not.
1232 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001233static int qc_do_rm_hp(struct quic_conn *qc,
1234 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001235 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001236 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001237{
1238 int ret, outlen, i, pnlen;
1239 uint64_t packet_number;
1240 uint32_t truncated_pn = 0;
1241 unsigned char mask[5] = {0};
1242 unsigned char *sample;
1243 EVP_CIPHER_CTX *cctx;
1244 unsigned char *hp_key;
1245
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001246 /* Check there is enough data in this packet. */
1247 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001248 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001249 return 0;
1250 }
1251
1252 cctx = EVP_CIPHER_CTX_new();
1253 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001254 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001255 return 0;
1256 }
1257
1258 ret = 0;
1259 sample = pn + QUIC_PACKET_PN_MAXLEN;
1260
1261 hp_key = tls_ctx->rx.hp_key;
1262 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1263 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1264 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001265 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001266 goto out;
1267 }
1268
1269 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1270 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1271 for (i = 0; i < pnlen; i++) {
1272 pn[i] ^= mask[i + 1];
1273 truncated_pn = (truncated_pn << 8) | pn[i];
1274 }
1275
1276 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1277 /* Store remaining information for this unprotected header */
1278 pkt->pn = packet_number;
1279 pkt->pnl = pnlen;
1280
1281 ret = 1;
1282
1283 out:
1284 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001285
1286 return ret;
1287}
1288
1289/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1290 * address, with <payload_len> as payload length, <aad> as address of
1291 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1292 * context.
1293 * Returns 1 if succeeded, 0 if not.
1294 */
1295static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1296 unsigned char *aad, size_t aad_len, uint64_t pn,
1297 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1298{
1299 unsigned char iv[12];
1300 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001301 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001302 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001303
1304 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001305 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn->qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001306 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 }
1308
1309 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1310 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001311 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn->qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001312 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313 }
1314
1315 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001316
1317 err:
1318 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001319 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn->qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001320 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001321}
1322
1323/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1324 * Returns 1 if succeeded, 0 if not.
1325 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001326static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001327{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001328 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001330 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001331 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001332 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1333 unsigned char *rx_key = tls_ctx->rx.key;
1334
1335 kp_changed = 0;
1336 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1337 /* The two tested bits are not at the same position,
1338 * this is why they are first both inversed.
1339 */
1340 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1341 if (pkt->pn < tls_ctx->rx.pn) {
1342 /* The lowest packet number of a previous key phase
1343 * cannot be null if it really stores previous key phase
1344 * secrets.
1345 */
1346 if (!pkt->qc->ku.prv_rx.pn)
1347 return 0;
1348
1349 rx_iv = pkt->qc->ku.prv_rx.iv;
1350 rx_key = pkt->qc->ku.prv_rx.key;
1351 }
1352 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1353 /* Next key phase */
1354 kp_changed = 1;
1355 rx_iv = pkt->qc->ku.nxt_rx.iv;
1356 rx_key = pkt->qc->ku.nxt_rx.key;
1357 }
1358 }
1359 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001360
1361 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1362 return 0;
1363
1364 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1365 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001366 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001367 if (!ret)
1368 return 0;
1369
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001370 /* Update the keys only if the packet decryption succeeded. */
1371 if (kp_changed) {
1372 quic_tls_rotate_keys(pkt->qc);
1373 /* Toggle the Key Phase bit */
1374 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1375 /* Store the lowest packet number received for the current key phase */
1376 tls_ctx->rx.pn = pkt->pn;
1377 /* Prepare the next key update */
1378 if (!quic_tls_key_update(pkt->qc))
1379 return 0;
1380 }
1381
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001382 /* Update the packet length (required to parse the frames). */
1383 pkt->len = pkt->aad_len + ret;
1384
1385 return 1;
1386}
1387
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001388/* Remove from <qcs> stream the acknowledged frames.
1389 * Never fails.
1390 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001391static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001392{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001393 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001394 struct eb64_node *frm_node;
1395
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001396 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001397 frm_node = eb64_first(&qcs->tx.acked_frms);
1398 while (frm_node) {
1399 struct quic_stream *strm;
1400
1401 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1402 if (strm->offset.key != qcs->tx.ack_offset)
1403 break;
1404
1405 b_del(strm->buf, strm->len);
1406 qcs->tx.ack_offset += strm->len;
1407 frm_node = eb64_next(frm_node);
1408 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001409 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001410 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001411
1412 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001413}
1414
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001415/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001416static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1417 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001418{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001419 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001420
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001421 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001422 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001423 switch (frm->type) {
1424 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1425 {
1426 struct qcs *qcs = frm->stream.qcs;
1427 struct quic_stream *strm = &frm->stream;
1428
1429 if (qcs->tx.ack_offset == strm->offset.key) {
1430 b_del(strm->buf, strm->len);
1431 qcs->tx.ack_offset += strm->len;
1432 LIST_DELETE(&frm->list);
1433 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001434 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001435 }
1436 else {
1437 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1438 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001439 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001440 }
1441 break;
1442 default:
1443 LIST_DELETE(&frm->list);
1444 pool_free(pool_head_quic_frame, frm);
1445 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001446
1447 if (stream_acked) {
1448 struct qcc *qcc = qc->qcc;
1449
1450 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1451 tasklet_wakeup(qcc->subs->tasklet);
1452 qcc->subs->events &= ~SUB_RETRY_SEND;
1453 if (!qcc->subs->events)
1454 qcc->subs = NULL;
1455 }
1456 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001457}
1458
1459/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1460 * deallocating them, and their TX frames.
1461 * Returns the last node reached to be used for the next range.
1462 * May be NULL if <largest> node could not be found.
1463 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001464static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1465 struct eb_root *pkts,
1466 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001467 struct list *newly_acked_pkts,
1468 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001469 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001470{
1471 struct eb64_node *node;
1472 struct quic_tx_packet *pkt;
1473
1474 if (largest_node)
1475 node = largest_node;
1476 else {
1477 node = eb64_lookup(pkts, largest);
1478 while (!node && largest > smallest) {
1479 node = eb64_lookup(pkts, --largest);
1480 }
1481 }
1482
1483 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001484 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001485
1486 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1487 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001488 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001489 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001490 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001491 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001492 node = eb64_prev(node);
1493 eb64_delete(&pkt->pn_node);
1494 }
1495
1496 return node;
1497}
1498
1499/* Treat <frm> frame whose packet it is attached to has just been detected as non
1500 * acknowledged.
1501 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001502static inline void qc_treat_nacked_tx_frm(struct quic_conn *qc,
1503 struct quic_frame *frm,
1504 struct quic_pktns *pktns)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001505{
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001506 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001507 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001508 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001509}
1510
1511
1512/* Free the TX packets of <pkts> list */
1513static inline void free_quic_tx_pkts(struct list *pkts)
1514{
1515 struct quic_tx_packet *pkt, *tmp;
1516
1517 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001518 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001519 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001520 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001521 }
1522}
1523
1524/* Send a packet loss event nofification to the congestion controller
1525 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1526 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1527 * at <now_us> current time.
1528 * Always succeeds.
1529 */
1530static inline void qc_cc_loss_event(struct quic_conn *qc,
1531 unsigned int lost_bytes,
1532 unsigned int newest_time_sent,
1533 unsigned int period,
1534 unsigned int now_us)
1535{
1536 struct quic_cc_event ev = {
1537 .type = QUIC_CC_EVT_LOSS,
1538 .loss.now_ms = now_ms,
1539 .loss.max_ack_delay = qc->max_ack_delay,
1540 .loss.lost_bytes = lost_bytes,
1541 .loss.newest_time_sent = newest_time_sent,
1542 .loss.period = period,
1543 };
1544
1545 quic_cc_event(&qc->path->cc, &ev);
1546}
1547
1548/* Send a packet ack event nofication for each newly acked packet of
1549 * <newly_acked_pkts> list and free them.
1550 * Always succeeds.
1551 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001552static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001553 struct list *newly_acked_pkts)
1554{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001555 struct quic_tx_packet *pkt, *tmp;
1556 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1557
1558 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1559 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001560 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001561 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001562 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001563 ev.ack.acked = pkt->in_flight_len;
1564 ev.ack.time_sent = pkt->time_sent;
1565 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001566 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001567 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001568 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 }
1570
1571}
1572
1573/* Handle <pkts> list of lost packets detected at <now_us> handling
1574 * their TX frames.
1575 * Send a packet loss event to the congestion controller if
1576 * in flight packet have been lost.
1577 * Also frees the packet in <pkts> list.
1578 * Never fails.
1579 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001580static inline void qc_release_lost_pkts(struct quic_conn *qc,
1581 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001582 struct list *pkts,
1583 uint64_t now_us)
1584{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001586 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001587 uint64_t lost_bytes;
1588
1589 lost_bytes = 0;
1590 oldest_lost = newest_lost = NULL;
1591 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1592 lost_bytes += pkt->in_flight_len;
1593 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001594 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001595 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001596 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001597 /* Treat the frames of this lost packet. */
1598 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001599 qc_treat_nacked_tx_frm(qc, frm, pktns);
Willy Tarreau2b718102021-04-21 07:32:39 +02001600 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601 if (!oldest_lost) {
1602 oldest_lost = newest_lost = pkt;
1603 }
1604 else {
1605 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001606 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001607 newest_lost = pkt;
1608 }
1609 }
1610
1611 if (lost_bytes) {
1612 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001613 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001614 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001615 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001616 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001617 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001618 }
1619}
1620
1621/* Look for packet loss from sent packets for <qel> encryption level of a
1622 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1623 * their tree if deemed as lost or set the <loss_time> value the packet number
1624 * space if any not deemed lost.
1625 * Should be called after having received an ACK frame with newly acknowledged
1626 * packets or when the the loss detection timer has expired.
1627 * Always succeeds.
1628 */
1629static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1630 struct quic_conn *qc,
1631 struct list *lost_pkts)
1632{
1633 struct eb_root *pkts;
1634 struct eb64_node *node;
1635 struct quic_loss *ql;
1636 unsigned int loss_delay;
1637
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001638 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001639 pkts = &pktns->tx.pkts;
1640 pktns->tx.loss_time = TICK_ETERNITY;
1641 if (eb_is_empty(pkts))
1642 goto out;
1643
1644 ql = &qc->path->loss;
1645 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1646 loss_delay += loss_delay >> 3;
1647 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1648
1649 node = eb64_first(pkts);
1650 while (node) {
1651 struct quic_tx_packet *pkt;
1652 int64_t largest_acked_pn;
1653 unsigned int loss_time_limit, time_sent;
1654
1655 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001656 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001657 node = eb64_next(node);
1658 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1659 break;
1660
1661 time_sent = pkt->time_sent;
1662 loss_time_limit = tick_add(time_sent, loss_delay);
1663 if (tick_is_le(time_sent, now_ms) ||
1664 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1665 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001666 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001667 }
1668 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001669 if (tick_isset(pktns->tx.loss_time))
1670 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1671 else
1672 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001673 }
1674 }
1675
1676 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001677 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001678}
1679
1680/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1681 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001682 * 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 +01001683 * acked ack-eliciting packet.
1684 * Return 1, if succeeded, 0 if not.
1685 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001686static inline int qc_parse_ack_frm(struct quic_conn *qc,
1687 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001688 struct quic_enc_level *qel,
1689 unsigned int *rtt_sample,
1690 const unsigned char **pos, const unsigned char *end)
1691{
1692 struct quic_ack *ack = &frm->ack;
1693 uint64_t smallest, largest;
1694 struct eb_root *pkts;
1695 struct eb64_node *largest_node;
1696 unsigned int time_sent, pkt_flags;
1697 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1698 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1699
1700 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1701 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001702 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001703 goto err;
1704 }
1705
1706 if (ack->first_ack_range > ack->largest_ack) {
1707 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001708 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001709 goto err;
1710 }
1711
1712 largest = ack->largest_ack;
1713 smallest = largest - ack->first_ack_range;
1714 pkts = &qel->pktns->tx.pkts;
1715 pkt_flags = 0;
1716 largest_node = NULL;
1717 time_sent = 0;
1718
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001719 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 +01001720 largest_node = eb64_lookup(pkts, largest);
1721 if (!largest_node) {
1722 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001723 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001724 }
1725 else {
1726 time_sent = eb64_entry(&largest_node->node,
1727 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001728 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001729 }
1730
1731 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001732 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001733 do {
1734 uint64_t gap, ack_range;
1735
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001736 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1737 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001738 if (!ack->ack_range_num--)
1739 break;
1740
1741 if (!quic_dec_int(&gap, pos, end))
1742 goto err;
1743
1744 if (smallest < gap + 2) {
1745 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001746 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001747 goto err;
1748 }
1749
1750 largest = smallest - gap - 2;
1751 if (!quic_dec_int(&ack_range, pos, end))
1752 goto err;
1753
1754 if (largest < ack_range) {
1755 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001756 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001757 goto err;
1758 }
1759
1760 /* Do not use this node anymore. */
1761 largest_node = NULL;
1762 /* Next range */
1763 smallest = largest - ack_range;
1764
1765 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001766 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001767 } while (1);
1768
1769 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001770 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001771
1772 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1773 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001774 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775 }
1776
1777 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1778 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001779 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001781 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001782 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001783 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1784 if (quic_peer_validated_addr(qc))
1785 qc->path->loss.pto_count = 0;
1786 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001787 }
1788
1789
1790 return 1;
1791
1792 err:
1793 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001794 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001795 return 0;
1796}
1797
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001798/* This function gives the detail of the SSL error. It is used only
1799 * if the debug mode and the verbose mode are activated. It dump all
1800 * the SSL error until the stack was empty.
1801 */
1802static forceinline void qc_ssl_dump_errors(struct connection *conn)
1803{
1804 if (unlikely(global.mode & MODE_DEBUG)) {
1805 while (1) {
1806 unsigned long ret;
1807
1808 ret = ERR_get_error();
1809 if (!ret)
1810 return;
1811
1812 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1813 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1814 }
1815 }
1816}
1817
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001818int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1819 const char **str, int *len);
1820
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001821/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1822 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001823 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001824 * Return 1 if succeeded, 0 if not.
1825 */
1826static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001827 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828 const unsigned char *data, size_t len,
1829 struct quic_rx_packet *pkt,
1830 struct quic_rx_crypto_frm *cf)
1831{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001832 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001833 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001834
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001835 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001836 qc = ctx->qc;
1837
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001838 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1839
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001840 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1841 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001842 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001843 goto err;
1844 }
1845
1846 el->rx.crypto.offset += len;
1847 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001848 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001849
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001850 state = HA_ATOMIC_LOAD(&qc->state);
1851 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001852 ssl_err = SSL_do_handshake(ctx->ssl);
1853 if (ssl_err != 1) {
1854 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1855 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1856 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001857 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001858 goto out;
1859 }
1860
1861 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001862 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001863 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001864 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865 goto err;
1866 }
1867
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001868 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001869 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001870 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001872 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001873 } else {
1874 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1875 if (ssl_err != 1) {
1876 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1877 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1878 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001879 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880 goto out;
1881 }
1882
1883 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001884 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001885 goto err;
1886 }
1887
1888 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001889 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001890 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001891
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001892 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001893 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001894 return 1;
1895
1896 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001897 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 return 0;
1899}
1900
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001901/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1902 * <pkt> RX packet.
1903 * Return it if succeeded, NULL if not.
1904 */
1905static inline
1906struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1907 struct quic_rx_packet *pkt)
1908{
1909 struct quic_rx_strm_frm *frm;
1910
1911 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1912 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001913 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001914 frm->len = stream_frm->len;
1915 frm->data = stream_frm->data;
1916 frm->pkt = pkt;
1917 }
1918
1919 return frm;
1920}
1921
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001922/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001923 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001924 */
1925static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1926{
1927 size_t ret;
1928
1929 ret = 0;
1930 while (strm_frm->len) {
1931 size_t try;
1932
1933 try = b_contig_space(buf);
1934 if (!try)
1935 break;
1936
1937 if (try > strm_frm->len)
1938 try = strm_frm->len;
1939 memcpy(b_tail(buf), strm_frm->data, try);
1940 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001941 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001942 b_add(buf, try);
1943 ret += try;
1944 }
1945
1946 return ret;
1947}
1948
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001949/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1950 * Also update <strm_frm> frame to reflect the data which have been consumed.
1951 */
1952static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1953 struct quic_rx_strm_frm *strm_frm)
1954{
1955 size_t ret;
1956
1957 ret = 0;
1958 while (strm_frm->len) {
1959 size_t try;
1960
1961 try = b_contig_space(buf);
1962 if (!try)
1963 break;
1964
1965 if (try > strm_frm->len)
1966 try = strm_frm->len;
1967 memcpy(b_tail(buf), strm_frm->data, try);
1968 strm_frm->len -= try;
1969 strm_frm->offset_node.key += try;
1970 b_add(buf, try);
1971 ret += try;
1972 }
1973
1974 return ret;
1975}
1976
1977/* Process as much as possible RX STREAM frames received for <qcs> */
1978static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
1979{
1980 int total;
1981 struct eb64_node *frm_node;
1982
1983 total = 0;
1984 frm_node = eb64_first(&qcs->rx.frms);
1985 while (frm_node) {
1986 int ret;
1987 struct quic_rx_strm_frm *frm;
1988
1989 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
1990 if (frm->offset_node.key != qcs->rx.offset)
1991 break;
1992
1993 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
1994 qcs->rx.offset += ret;
1995 total += ret;
1996 if (frm->len) {
1997 /* If there is remaining data in this frame
1998 * this is because the destination buffer is full.
1999 */
2000 break;
2001 }
2002
2003 frm_node = eb64_next(frm_node);
2004 quic_rx_packet_refdec(frm->pkt);
2005 eb64_delete(&frm->offset_node);
2006 pool_free(pool_head_quic_rx_strm_frm, frm);
2007 }
2008
2009 return total;
2010}
2011
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002012/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2013 * streams may be open. The data are copied to the stream RX buffer if possible.
2014 * If not, the STREAM frame is stored to be treated again later.
2015 * We rely on the flow control so that not to store too much STREAM frames.
2016 * Return 1 if succeeded, 0 if not.
2017 */
2018static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2019 struct quic_stream *strm_frm,
2020 struct quic_conn *qc)
2021{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002022 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002023 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002024 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002025 struct quic_rx_strm_frm *frm;
2026
2027 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2028 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002029 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002030 return 0;
2031 }
2032
2033 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002034 if (strm_frm->offset.key < strm->rx.offset) {
2035 size_t diff;
2036
2037 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2038 TRACE_PROTO("Already received STREAM data",
2039 QUIC_EV_CONN_PSTRM, qc);
2040 goto out;
2041 }
2042
2043 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2044 diff = strm->rx.offset - strm_frm->offset.key;
2045 strm_frm->offset.key = strm->rx.offset;
2046 strm_frm->len -= diff;
2047 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002048 }
2049
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002050 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002051 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002052 int ret;
2053
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002054 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002055 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002056 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002057
2058 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002059 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002060 strm->rx.offset += ret;
2061 }
2062
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002063 total += qc_treat_rx_strm_frms(strm);
2064 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 +01002065 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002066 return 0;
2067 }
2068
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002069 if (!strm_frm->len)
2070 goto out;
2071
2072 store_frm:
2073 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2074 if (!frm) {
2075 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002076 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002077 return 0;
2078 }
2079
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002080 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002081 quic_rx_packet_refinc(pkt);
2082
2083 out:
2084 return 1;
2085}
2086
2087/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2088 * streams may be open. The data are copied to the stream RX buffer if possible.
2089 * If not, the STREAM frame is stored to be treated again later.
2090 * We rely on the flow control so that not to store too much STREAM frames.
2091 * Return 1 if succeeded, 0 if not.
2092 */
2093static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2094 struct quic_stream *strm_frm,
2095 struct quic_conn *qc)
2096{
2097 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002098 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002099 struct quic_rx_strm_frm *frm;
2100 size_t strm_frm_len;
2101
2102 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2103 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002104 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002105 return 0;
2106 }
2107
2108 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002109 if (strm_frm->offset.key < strm->rx.offset) {
2110 size_t diff;
2111
2112 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2113 TRACE_PROTO("Already received STREAM data",
2114 QUIC_EV_CONN_PSTRM, qc);
2115 goto out;
2116 }
2117
2118 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2119 diff = strm->rx.offset - strm_frm->offset.key;
2120 strm_frm->offset.key = strm->rx.offset;
2121 strm_frm->len -= diff;
2122 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002123 }
2124
2125 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002126 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002127 int ret;
2128
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002129 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002130 goto store_frm;
2131
2132 /* qc_strm_cpy() will modify the offset, depending on the number
2133 * of bytes copied.
2134 */
2135 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2136 /* Inform the application of the arrival of this new stream */
2137 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002138 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139 return 0;
2140 }
2141
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002142 if (ret)
2143 qcs_notify_recv(strm);
2144
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002145 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002146 }
2147 /* Take this frame into an account for the stream flow control */
2148 strm->rx.offset += strm_frm_len;
2149 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002150 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002151 */
2152 if (!strm_frm->len)
2153 goto out;
2154
2155 store_frm:
2156 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2157 if (!frm) {
2158 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002159 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002160 return 0;
2161 }
2162
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002163 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002164 quic_rx_packet_refinc(pkt);
2165
2166 out:
2167 return 1;
2168}
2169
2170static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2171 struct quic_stream *strm_frm,
2172 struct quic_conn *qc)
2173{
2174 if (strm_frm->id & QCS_ID_DIR_BIT)
2175 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2176 else
2177 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2178}
2179
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002180/* Prepare a fast retransmission from <qel> encryption level
2181 * which must be Initial encryption level.
2182 */
2183static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2184 struct quic_conn *qc)
2185{
2186 struct eb_root *pkts = &qel->pktns->tx.pkts;
2187 struct eb64_node *node = eb64_first(pkts);
2188 struct quic_tx_packet *pkt;
2189 struct quic_frame *frm, *frmbak;
2190
2191 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002192 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002193 pkts = &qel->pktns->tx.pkts;
2194 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002195 /* Skip the empty packet (they have already been retransmitted) */
2196 while (node) {
2197 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2198 if (!LIST_ISEMPTY(&pkt->frms))
2199 break;
2200 node = eb64_next(node);
2201 }
2202
2203 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002204 return;
2205
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002206 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
2207 qc_treat_nacked_tx_frm(qc, frm, qel->pktns);
2208 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2209 qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight) {
2210 qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.pto_probe = 1;
2211 qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2212 goto start;
2213 }
2214}
2215
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002216/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2217 * as I/O handler context and <qel> as encryption level.
2218 * Returns 1 if succeeded, 0 if failed.
2219 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002220static 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 +01002221 struct quic_enc_level *qel)
2222{
2223 struct quic_frame frm;
2224 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002225 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002226 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002227
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002228 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002229 /* Skip the AAD */
2230 pos = pkt->data + pkt->aad_len;
2231 end = pkt->data + pkt->len;
2232
2233 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002234 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002235 goto err;
2236
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002237 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002238 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002239 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002240 break;
2241 case QUIC_FT_PING:
2242 break;
2243 case QUIC_FT_ACK:
2244 {
2245 unsigned int rtt_sample;
2246
2247 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002248 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002249 goto err;
2250
2251 if (rtt_sample) {
2252 unsigned int ack_delay;
2253
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002254 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002255 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2256 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2257 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002258 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002259 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002260 break;
2261 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002262 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002263 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002264 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002265 {
2266 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002267
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002268 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2269 /* XXX TO DO: <cfdebug> is used only for the traces. */
2270 struct quic_rx_crypto_frm cfdebug = { };
2271
2272 cfdebug.offset_node.key = frm.crypto.offset;
2273 cfdebug.len = frm.crypto.len;
2274 TRACE_PROTO("CRYPTO data discarded",
2275 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2276 break;
2277 }
2278
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002279 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2280 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2281 /* XXX TO DO: <cfdebug> is used only for the traces. */
2282 struct quic_rx_crypto_frm cfdebug = { };
2283
2284 cfdebug.offset_node.key = frm.crypto.offset;
2285 cfdebug.len = frm.crypto.len;
2286 /* Nothing to do */
2287 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002288 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002289 if (objt_listener(ctx->conn->target) &&
2290 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2291 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002292 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002293 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002294 else {
2295 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2296 /* XXX TO DO: <cfdebug> is used only for the traces. */
2297 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002298
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002299 cfdebug.offset_node.key = frm.crypto.offset;
2300 cfdebug.len = frm.crypto.len;
2301 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002302 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002303 frm.crypto.len -= diff;
2304 frm.crypto.data += diff;
2305 frm.crypto.offset = qel->rx.crypto.offset;
2306 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002307 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002308
2309 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002310 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002311 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002313 cfdebug.offset_node.key = frm.crypto.offset;
2314 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002315 if (!qc_provide_cdata(qel, ctx,
2316 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002317 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002319
2320 break;
2321 }
2322
2323 /* frm.crypto.offset > qel->rx.crypto.offset */
2324 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2325 if (!cf) {
2326 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002327 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002328 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002329 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002330
2331 cf->offset_node.key = frm.crypto.offset;
2332 cf->len = frm.crypto.len;
2333 cf->data = frm.crypto.data;
2334 cf->pkt = pkt;
2335 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2336 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002338 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002339 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002340 {
2341 struct quic_stream *stream = &frm.stream;
2342
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002343 if (objt_listener(ctx->conn->target)) {
2344 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2345 goto err;
2346 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2347 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002348
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002349 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002350 goto err;
2351
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002352 break;
2353 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002354 case QUIC_FT_MAX_DATA:
2355 case QUIC_FT_MAX_STREAM_DATA:
2356 case QUIC_FT_MAX_STREAMS_BIDI:
2357 case QUIC_FT_MAX_STREAMS_UNI:
2358 case QUIC_FT_DATA_BLOCKED:
2359 case QUIC_FT_STREAM_DATA_BLOCKED:
2360 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2361 case QUIC_FT_STREAMS_BLOCKED_UNI:
2362 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002363 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002364 break;
2365 case QUIC_FT_CONNECTION_CLOSE:
2366 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002367 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002368 qc->qcc->flags |= QC_CF_CC_RECV;
2369 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002370 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002371 case QUIC_FT_HANDSHAKE_DONE:
2372 if (objt_listener(ctx->conn->target))
2373 goto err;
2374
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002375 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376 break;
2377 default:
2378 goto err;
2379 }
2380 }
2381
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002382 if (fast_retrans) {
2383 qc_prep_fast_retrans(qel, qc);
2384 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight)
2385 qc_prep_fast_retrans(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE], qc);
2386 }
2387
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002388 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2389 * has successfully parse a Handshake packet. The Initial encryption must also
2390 * be discarded.
2391 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002392 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002393 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002394
2395 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002396 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002397 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002398 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002399 qc_set_timer(ctx->qc);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002400 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002401 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002402 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002403 }
2404
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002405 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406 return 1;
2407
2408 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002409 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002410 return 0;
2411}
2412
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002413/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002414 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002415 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2416 * This function must be called only after having built a correct datagram.
2417 * Always succeeds.
2418 */
2419static inline void qc_set_dg(struct cbuf *cbuf,
2420 uint16_t dglen, struct quic_tx_packet *pkt)
2421{
2422 write_u16(cb_wr(cbuf), dglen);
2423 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2424 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2425}
2426
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002427/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002428 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2429 * several packets in the same datagram. A header made of two fields is added
2430 * to each datagram: the datagram length followed by the address of the first
2431 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002432 * Returns 1 if succeeded, or 0 if something wrong happened.
2433 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002434static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2435 enum quic_tls_enc_level tel,
2436 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002437{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002438 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002439 struct cbuf *cbuf;
2440 unsigned char *end_buf, *end, *pos, *spos;
2441 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2442 /* length of datagrams */
2443 uint16_t dglen;
2444 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002445 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002446 /* Each datagram is prepended with its length followed by the
2447 * address of the first packet in the datagram.
2448 */
2449 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002451 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2452
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002453 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002454 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002455 total = 0;
2456 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002457 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002458 cbuf = qr->cbuf;
2459 spos = pos = cb_wr(cbuf);
2460 /* Leave at least <dglen> bytes at the end of this buffer
2461 * to ensure there is enough room to mark the end of prepared
2462 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002464 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2465 first_pkt = prv_pkt = NULL;
2466 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002467 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468 enum quic_pkt_type pkt_type;
2469
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002470 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002471 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002472 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2473 if (!cc) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002474 if (!prv_pkt)
2475 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002476 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002477 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002478 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002479 * 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 +01002480 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002481 * and if there is no more CRYPTO data available or in flight
2482 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002483 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002484 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002485 (!cc && !ack && !probe &&
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002486 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2487 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002488 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002489 /* Set the current datagram as prepared into <cbuf> if
2490 * the was already a correct packet which was previously written.
2491 */
2492 if (prv_pkt)
2493 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494 break;
2495 }
2496
2497 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002498 if (!prv_pkt) {
2499 /* Leave room for the datagram header */
2500 pos += dg_headlen;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002501 if (!quic_peer_validated_addr(qc) && objt_listener(qc->conn->target)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002502 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2503 }
2504 else {
2505 end = pos + qc->path->mtu;
2506 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002507 }
2508
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002509 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002510 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002511 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002512 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002513 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002514 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002515 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002516 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002517 case -2:
2518 goto err;
2519 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002520 /* If there was already a correct packet present, set the
2521 * current datagram as prepared into <cbuf>.
2522 */
2523 if (prv_pkt) {
2524 qc_set_dg(cbuf, dglen, first_pkt);
2525 goto stop_build;
2526 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002527 goto out;
2528 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002529 break;
2530 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002531
Frédéric Lécaille63556772021-12-29 17:18:21 +01002532 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2533 if (!cur_pkt)
2534 goto err;
2535
2536 total += cur_pkt->len;
2537 /* keep trace of the first packet in the datagram */
2538 if (!first_pkt)
2539 first_pkt = cur_pkt;
2540 /* Attach the current one to the previous one */
2541 if (prv_pkt)
2542 prv_pkt->next = cur_pkt;
2543 /* Let's say we have to build a new dgram */
2544 prv_pkt = NULL;
2545 dglen += cur_pkt->len;
2546 /* Client: discard the Initial encryption keys as soon as
2547 * a handshake packet could be built.
2548 */
2549 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2550 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2551 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2552 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2553 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2554 qc_set_timer(qc);
2555 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2556 }
2557 /* If the data for the current encryption level have all been sent,
2558 * select the next level.
2559 */
2560 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
2561 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms))) {
2562 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2563 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2564 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2565 tel = next_tel;
2566 qel = &qc->els[tel];
2567 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
2568 /* If there is data for the next level, do not
2569 * consume a datagram.
2570 */
2571 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002572 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002573 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002574 /* If we have to build a new datagram, set the current datagram as
2575 * prepared into <cbuf>.
2576 */
2577 if (!prv_pkt) {
2578 qc_set_dg(cbuf, dglen, first_pkt);
2579 first_pkt = NULL;
2580 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002581 padding = 0;
2582 }
2583 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
2584 (objt_server(qc->conn->target) ||
2585 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2586 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002587 }
2588 }
2589
2590 stop_build:
2591 /* Reset <wr> writer index if in front of <rd> index */
2592 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2593 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2594
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002595 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002596 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2597 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2598 /* Mark the end of contiguous data for the reader */
2599 write_u16(cb_wr(cbuf), 0);
2600 cb_add(cbuf, sizeof(uint16_t));
2601 }
2602 }
2603
2604 if (rd && rd <= cbuf->wr) {
2605 cb_wr_reset(cbuf);
2606 if (pos == spos) {
2607 /* Reuse the same buffer if nothing was built. */
2608 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002609 }
2610 }
2611 }
2612
2613 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002614 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002615 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002616
2617 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002618 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002619 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002620}
2621
2622/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002623 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002624 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002625int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002626{
2627 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002628 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002629
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002630 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002631 cbuf = qr->cbuf;
2632 while (cb_contig_data(cbuf)) {
2633 unsigned char *pos;
2634 struct buffer tmpbuf = { };
2635 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2636 uint16_t dglen;
2637 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638 unsigned int time_sent;
2639
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002640 pos = cb_rd(cbuf);
2641 dglen = read_u16(pos);
2642 /* End of prepared datagrams.
2643 * Reset the reader index only if in front of the writer index.
2644 */
2645 if (!dglen) {
2646 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2647
2648 if (wr && wr < cbuf->rd) {
2649 cb_rd_reset(cbuf);
2650 continue;
2651 }
2652 break;
2653 }
2654
2655 pos += sizeof dglen;
2656 first_pkt = read_ptr(pos);
2657 pos += sizeof first_pkt;
2658 tmpbuf.area = (char *)pos;
2659 tmpbuf.size = tmpbuf.data = dglen;
2660
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002661 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002662 for (pkt = first_pkt; pkt; pkt = pkt->next)
2663 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002664 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002665 &tmpbuf, tmpbuf.data, 0) <= 0) {
2666 for (pkt = first_pkt; pkt; pkt = pkt->next)
2667 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002669 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002670
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002671 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002672 qc->tx.bytes += tmpbuf.data;
2673 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002674
2675 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2676 pkt->time_sent = time_sent;
2677 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2678 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002679 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002680 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002681 qc->path->in_flight += pkt->in_flight_len;
2682 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2683 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002684 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002685 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002686 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002687 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002688 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002689 }
2690 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002691
2692 return 1;
2693}
2694
2695/* Build all the frames which must be sent just after the handshake have succeeded.
2696 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2697 * a HANDSHAKE_DONE frame.
2698 * Return 1 if succeeded, 0 if not.
2699 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002700static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002701{
2702 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002703 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002704 struct quic_frame *frm;
2705
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002706 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002707 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002708 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002709 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002710 if (!frm)
2711 return 0;
2712
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002714 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002715 }
2716
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002717 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002718 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002719 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002720
2721 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002722 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002723 goto err;
2724
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002725 cid = new_quic_cid(&qc->cids, qc, i);
2726 if (!cid)
2727 goto err;
2728
2729 /* insert the allocated CID in the receiver tree */
2730 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2731 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2732 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2733
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002734 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002735 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002736 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002737 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002738
2739 return 1;
2740
2741 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002742 return 0;
2743}
2744
2745/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002746void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002747{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002748 struct eb64_node *n;
2749 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002750
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002751 n = eb64_first(&arngs->root);
2752 while (n) {
2753 struct eb64_node *next;
2754
2755 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2756 next = eb64_next(n);
2757 eb64_delete(n);
2758 free(ar);
2759 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 }
2761}
2762
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002763/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2764 * descending order.
2765 */
2766static inline size_t sack_gap(struct quic_arng_node *p,
2767 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002768{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002769 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002770}
2771
2772
2773/* Remove the last elements of <ack_ranges> list of ack range updating its
2774 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002775 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002776 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002777static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002778{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002779 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002781 last = eb64_last(&arngs->root);
2782 while (last && arngs->enc_sz > limit) {
2783 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002784
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002785 prev = eb64_prev(last);
2786 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002787 return 0;
2788
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002789 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2790 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2791 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2792 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2793 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2794 --arngs->sz;
2795 eb64_delete(last);
2796 pool_free(pool_head_quic_arng, last);
2797 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 }
2799
2800 return 1;
2801}
2802
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002803/* Set the encoded size of <arngs> QUIC ack ranges. */
2804static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2805{
2806 struct eb64_node *node, *next;
2807 struct quic_arng_node *ar, *ar_next;
2808
2809 node = eb64_last(&arngs->root);
2810 if (!node)
2811 return;
2812
2813 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2814 arngs->enc_sz = quic_int_getsize(ar->last) +
2815 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2816
2817 while ((next = eb64_prev(node))) {
2818 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2819 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2820 quic_int_getsize(ar_next->last - ar_next->first.key);
2821 node = next;
2822 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2823 }
2824}
2825
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002826/* Insert <ar> ack range into <argns> tree of ack ranges.
2827 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002828 */
2829static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002830struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2831 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002832{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002833 struct quic_arng_node *new_ar;
2834
2835 new_ar = pool_alloc(pool_head_quic_arng);
2836 if (new_ar) {
2837 new_ar->first.key = ar->first;
2838 new_ar->last = ar->last;
2839 eb64_insert(&arngs->root, &new_ar->first);
2840 arngs->sz++;
2841 }
2842
2843 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002844}
2845
2846/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002847 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002848 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002849 *
2850 * Descending order
2851 * ------------->
2852 * range1 range2
2853 * ..........|--------|..............|--------|
2854 * ^ ^ ^ ^
2855 * | | | |
2856 * last1 first1 last2 first2
2857 * ..........+--------+--------------+--------+......
2858 * diff1 gap12 diff2
2859 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002860 * To encode the previous list of ranges we must encode integers as follows in
2861 * descending order:
2862 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002863 * with diff1 = last1 - first1
2864 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002865 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002867 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002868int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2869 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002871 struct eb64_node *le;
2872 struct quic_arng_node *new_node;
2873 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002874
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002875 new = NULL;
2876 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002877 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002878 if (!new_node)
2879 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002880
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002881 goto out;
2882 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002884 le = eb64_lookup_le(&arngs->root, ar->first);
2885 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002886 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002887 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002889
2890 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002891 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002892 else {
2893 struct quic_arng_node *le_ar =
2894 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002896 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002897 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002898 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002900 if (le_ar->last + 1 >= ar->first) {
2901 le_ar->last = ar->last;
2902 new = le;
2903 new_node = le_ar;
2904 }
2905 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002906 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002907 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002908 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002909
2910 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002911 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002912 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002913
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002914 /* Verify that the new inserted node does not overlap the nodes
2915 * which follow it.
2916 */
2917 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002918 struct eb64_node *next;
2919 struct quic_arng_node *next_node;
2920
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002921 while ((next = eb64_next(new))) {
2922 next_node =
2923 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002924 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002925 break;
2926
2927 if (next_node->last > new_node->last)
2928 new_node->last = next_node->last;
2929 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002930 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002931 /* Decrement the size of these ranges. */
2932 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002933 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002934 }
2935
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002936 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002937 quic_arngs_set_enc_sz(arngs);
2938
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002939 return 1;
2940}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002941/* Remove the header protection of packets at <el> encryption level.
2942 * Always succeeds.
2943 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002944static 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 +01002945{
2946 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002947 struct quic_rx_packet *pqpkt;
2948 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002949 struct quic_enc_level *app_qel;
2950
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002951 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
2952 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002953 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002954 if (el == app_qel && objt_listener(qc->conn->target) &&
2955 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002956 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002957 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002958 goto out;
2959 }
2960 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002961 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002962 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002963 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002964 pqpkt->data, pqpkt->data + pqpkt->len)) {
2965 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002966 /* XXX TO DO XXX */
2967 }
2968 else {
2969 /* The AAD includes the packet number field */
2970 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2971 /* Store the packet into the tree of packets to decrypt. */
2972 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002973 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002974 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2975 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002976 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002977 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002978 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002979 MT_LIST_DELETE_SAFE(pkttmp1);
2980 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002981 }
2982
2983 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002984 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002985}
2986
2987/* Process all the CRYPTO frame at <el> encryption level.
2988 * Return 1 if succeeded, 0 if not.
2989 */
2990static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002991 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992{
2993 struct eb64_node *node;
2994
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995 node = eb64_first(&el->rx.crypto.frms);
2996 while (node) {
2997 struct quic_rx_crypto_frm *cf;
2998
2999 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3000 if (cf->offset_node.key != el->rx.crypto.offset)
3001 break;
3002
3003 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3004 goto err;
3005
3006 node = eb64_next(node);
3007 quic_rx_packet_refdec(cf->pkt);
3008 eb64_delete(&cf->offset_node);
3009 pool_free(pool_head_quic_rx_crypto_frm, cf);
3010 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003011 return 1;
3012
3013 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003014 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 return 0;
3016}
3017
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003018/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003019 * 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 +02003020 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 * Return 1 if succeeded, 0 if not.
3022 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003023int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3024 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003027 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003028 struct quic_conn *qc = ctx->conn->qc;
3029 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003031 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003032 qel = cur_el;
3033 next_tel:
3034 if (!qel)
3035 goto out;
3036
3037 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3038 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039 while (node) {
3040 struct quic_rx_packet *pkt;
3041
3042 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003043 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003044 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003045 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 /* Drop the packet */
3047 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003048 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003049 }
3050 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003051 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003052 /* Drop the packet */
3053 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003054 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055 }
3056 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003057 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3058
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003059 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003060 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003061 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003062 if (pkt->pn > largest_pn)
3063 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003064 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003065 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003066 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003067 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003068 }
3069 }
3070 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003071 eb64_delete(&pkt->pn_node);
3072 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003073 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003074 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003075
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003076 /* Update the largest packet number. */
3077 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003078 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3079 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080 goto err;
3081
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003082 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003083 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003084 qel = next_el;
3085 goto next_tel;
3086 }
3087
3088 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003089 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003090 return 1;
3091
3092 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003093 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003094 return 0;
3095}
3096
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003097/* QUIC connection packet handler task. */
3098struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003099{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003100 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003101 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003102 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003103 enum quic_tls_enc_level tel, next_tel;
3104 struct quic_enc_level *qel, *next_qel;
3105 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003106 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003107 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003109 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003110 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003111 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003112 st = HA_ATOMIC_LOAD(&qc->state);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003113 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003114 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3115 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3116 /* The I/O handler has been woken up by the dgram listener
3117 * after the anti-amplification was reached.
3118 */
3119 qc_set_timer(qc);
3120 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3121 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3122 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003123 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003124 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3125 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3126 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003127 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003128 if (st >= QUIC_HS_ST_COMPLETE &&
3129 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3130 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3131 /* There may be remaining Handshake packets to treat and acknowledge. */
3132 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3133 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3134 }
3135 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003136 goto err;
3137
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003138 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003139 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003140
3141 next_level:
3142 tls_ctx = &qel->tls_ctx;
3143
3144 /* If the header protection key for this level has been derived,
3145 * remove the packet header protections.
3146 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003147 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003148 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003149 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003150
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003151 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3152 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3153 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003154 goto err;
3155
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003156 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3157 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3158 qel = next_qel;
3159 next_qel = NULL;
3160 goto next_level;
3161 }
3162
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003163 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003164 if (st >= QUIC_HS_ST_COMPLETE) {
3165 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3166 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003167 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003168
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003169 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3170 QUIC_FL_TLS_SECRETS_DCD)) {
3171 /* Discard the Handshake keys. */
3172 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3173 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3174 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3175 qc_set_timer(qc);
3176 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3177 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3178 }
3179
3180 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3181 /* There may be remaining handshake to build (acks) */
3182 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3183 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003184 }
3185
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003186 if (!qr)
3187 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003188 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
3189 goto err;
3190 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003191 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003192 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003193 else if (ret == 0)
3194 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003195
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003196 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003197 goto err;
3198
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003199 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003200 /* Check if there is something to do for the next level.
3201 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003202 if (next_qel && next_qel != qel &&
3203 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003204 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003205 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003206 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003207 goto next_level;
3208 }
3209
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003210 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003211 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003212 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213
3214 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003215 if (qr)
3216 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003217 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003218 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003219}
3220
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003221/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003222static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3223{
3224 int i;
3225
3226 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3227 if (qel->tx.crypto.bufs[i]) {
3228 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3229 qel->tx.crypto.bufs[i] = NULL;
3230 }
3231 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003232 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233}
3234
3235/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003236 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003237 * Returns 1 if succeeded, 0 if not.
3238 */
3239static int quic_conn_enc_level_init(struct quic_conn *qc,
3240 enum quic_tls_enc_level level)
3241{
3242 struct quic_enc_level *qel;
3243
3244 qel = &qc->els[level];
3245 qel->level = quic_to_ssl_enc_level(level);
3246 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3247 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3248 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3249 qel->tls_ctx.rx.flags = 0;
3250 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003251 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252
3253 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003254 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003255 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003256 qel->rx.crypto.offset = 0;
3257 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003258
3259 /* Allocate only one buffer. */
3260 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3261 if (!qel->tx.crypto.bufs)
3262 goto err;
3263
3264 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3265 if (!qel->tx.crypto.bufs[0])
3266 goto err;
3267
3268 qel->tx.crypto.bufs[0]->sz = 0;
3269 qel->tx.crypto.nb_buf = 1;
3270
3271 qel->tx.crypto.sz = 0;
3272 qel->tx.crypto.offset = 0;
3273
3274 return 1;
3275
3276 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003277 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003278 return 0;
3279}
3280
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003281/* Increment the <qc> refcount.
3282 *
3283 * This operation must be conducted when manipulating the quic_conn outside of
3284 * the connection pinned thread. These threads can only retrieve the connection
3285 * in the CID tree, so this function must be conducted under the CID lock.
3286 */
3287static inline void quic_conn_take(struct quic_conn *qc)
3288{
3289 HA_ATOMIC_INC(&qc->refcount);
3290}
3291
3292/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003293 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003294 */
3295static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003296{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003297 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003298 int i;
3299
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003300 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003301 return;
3302
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003303 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3304 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) {
3305 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3306 return;
3307 }
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003308
3309 /* remove the connection from receiver cids trees */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003310 ebmb_delete(&qc->odcid_node);
3311 ebmb_delete(&qc->scid_node);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003312 free_quic_conn_cids(qc);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003313 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003314
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003315 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3316 if (conn_ctx) {
3317 tasklet_free(conn_ctx->wait_event.tasklet);
3318 conn_ctx->wait_event.tasklet = NULL;
3319
3320 pool_free(pool_head_quic_conn_ctx, conn_ctx);
3321 }
3322
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003323 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003324 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003325
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003326 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3327 pool_free(pool_head_quic_conn, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003328}
3329
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003330void quic_close(struct connection *conn, void *xprt_ctx)
3331{
3332 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3333 struct quic_conn *qc = conn_ctx->conn->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003334
3335 /* This task must be deleted by the connection-pinned thread. */
3336 if (qc->timer_task) {
3337 task_destroy(qc->timer_task);
3338 qc->timer_task = NULL;
3339 }
3340
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003341 quic_conn_drop(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003342}
3343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003344/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003345static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003346{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003347 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348 struct quic_conn *qc;
3349 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003350 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003351
3352 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003353 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003354 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003355 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003356 task->expire = TICK_ETERNITY;
3357 pktns = quic_loss_pktns(qc);
3358 if (tick_isset(pktns->tx.loss_time)) {
3359 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3360
3361 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3362 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003363 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3364 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365 goto out;
3366 }
3367
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003368 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003369 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003370 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledb6a4722021-12-30 23:16:51 +01003371 if (objt_listener(qc->conn->target) &&
3372 pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE] &&
3373 qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight)
3374 qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.pto_probe = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375 pktns->tx.pto_probe = 1;
3376 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003377 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003378 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3379 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3380
3381 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3382 hel->pktns->tx.pto_probe = 1;
3383 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3384 iel->pktns->tx.pto_probe = 1;
3385 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003386
3387 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3388 int j;
3389
3390 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3391 continue;
3392
3393 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3394 qc_prep_fast_retrans(&qc->els[i], qc);
3395 }
3396
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003397 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3398 qc->path->loss.pto_count++;
3399
3400 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003401 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402
3403 return task;
3404}
3405
3406/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3407 * connections used to identify the first Initial packets of client connecting
3408 * to listeners. This parameter must be NULL for QUIC connections attached
3409 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3410 * <scid> is the source connection ID with <scid_len> as length.
3411 * Returns 1 if succeeded, 0 if not.
3412 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003413static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003414 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003415 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003416{
3417 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003418 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003419 /* Initial CID. */
3420 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003421 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003422 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003424 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003425 qc = pool_zalloc(pool_head_quic_conn);
3426 if (!qc) {
3427 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3428 goto err;
3429 }
3430
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003431 HA_ATOMIC_STORE(&qc->refcount, 0);
3432
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003433 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3434 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003435 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003436 goto err;
3437 }
3438
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003439 qc->cids = EB_ROOT;
3440 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003441 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003442 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003443
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003444 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003445 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003446 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003447 qc->odcid.addrlen = dcid_addr_len;
3448 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003450 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003451 if (scid_len)
3452 memcpy(qc->dcid.data, scid, scid_len);
3453 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003454 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003455 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003456 }
3457 /* QUIC Client (outgoing connection to servers) */
3458 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003459 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003460 if (dcid_len)
3461 memcpy(qc->dcid.data, dcid, dcid_len);
3462 qc->dcid.len = dcid_len;
3463 }
3464
3465 /* Initialize the output buffer */
3466 qc->obuf.pos = qc->obuf.data;
3467
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003468 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003469 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003470 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003471 goto err;
3472 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003473
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003474 /* insert the allocated CID in the receiver tree */
3475 if (server) {
3476 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3477 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3478 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3479 }
3480
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003481 /* Select our SCID which is the first CID with 0 as sequence number. */
3482 qc->scid = icid->cid;
3483
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003484 /* Packet number spaces initialization. */
3485 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3486 quic_pktns_init(&qc->pktns[i]);
3487 /* QUIC encryption level context initialization. */
3488 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003489 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003490 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003491 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003492 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003493 /* Initialize the packet number space. */
3494 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3495 }
3496
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003497 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003498 qc->tps_tls_ext = qc->version & 0xff000000 ?
3499 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3500 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003501 /* TX part. */
3502 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003503 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3504 qc->tx.wbuf = qc->tx.rbuf = 0;
3505 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003506 /* RX part. */
3507 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003508 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003509 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3510 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3511 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003512 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003513 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003514 goto err;
3515 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003517 /* XXX TO DO: Only one path at this time. */
3518 qc->path = &qc->paths[0];
3519 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3520
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003521 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003522
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003523 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524
3525 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003526 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003527 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003528 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003529}
3530
3531/* Initialize the timer task of <qc> QUIC connection.
3532 * Returns 1 if succeeded, 0 if not.
3533 */
3534static int quic_conn_init_timer(struct quic_conn *qc)
3535{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003536 /* Attach this task to the same thread ID used for the connection */
3537 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003538 if (!qc->timer_task)
3539 return 0;
3540
3541 qc->timer = TICK_ETERNITY;
3542 qc->timer_task->process = process_timer;
3543 qc->timer_task->context = qc->conn->xprt_ctx;
3544
3545 return 1;
3546}
3547
3548/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3549 * past one byte of this buffer.
3550 */
3551static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3552 struct quic_rx_packet *pkt)
3553{
3554 unsigned char dcid_len, scid_len;
3555
3556 /* Version */
3557 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3558 return 0;
3559
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560 /* Destination Connection ID Length */
3561 dcid_len = *(*buf)++;
3562 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3563 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3564 /* XXX MUST BE DROPPED */
3565 return 0;
3566
3567 if (dcid_len) {
3568 /* Check that the length of this received DCID matches the CID lengths
3569 * of our implementation for non Initials packets only.
3570 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003571 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3572 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003573 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003574 return 0;
3575
3576 memcpy(pkt->dcid.data, *buf, dcid_len);
3577 }
3578
3579 pkt->dcid.len = dcid_len;
3580 *buf += dcid_len;
3581
3582 /* Source Connection ID Length */
3583 scid_len = *(*buf)++;
3584 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3585 /* XXX MUST BE DROPPED */
3586 return 0;
3587
3588 if (scid_len)
3589 memcpy(pkt->scid.data, *buf, scid_len);
3590 pkt->scid.len = scid_len;
3591 *buf += scid_len;
3592
3593 return 1;
3594}
3595
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003596/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3597 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3598 * encryption level matching with the packet type. <*qel> may be null if not found.
3599 * Note that <ctx> may be null (for Initial packets).
3600 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003601static int qc_pkt_may_rm_hp(struct quic_conn *qc, struct quic_rx_packet *pkt,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003602 struct quic_enc_level **qel)
3603{
3604 enum quic_tls_enc_level tel;
3605
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003606 tel = quic_packet_type_enc_level(pkt->type);
3607 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3608 *qel = NULL;
3609 return 0;
3610 }
3611
3612 *qel = &qc->els[tel];
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003613 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003614 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003615
3616 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003617 (tel != QUIC_TLS_ENC_LEVEL_APP ||
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003618 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003619 return 1;
3620
3621 return 0;
3622}
3623
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003624/* Insert <pkt> RX packet in its <qel> RX packets tree */
3625static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3626{
3627 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003628 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003629 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3630 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3631 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003632}
3633
3634/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003635 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3636 * byte past the end of the buffer containing this packet and <beg> the address of
3637 * the packet first byte.
3638 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3639 * Returns 1 if succeeded, 0 if not.
3640 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003641static inline int qc_try_rm_hp(struct quic_conn *qc,
3642 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003643 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003644 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003645 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003646{
3647 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003648 struct quic_enc_level *qel;
3649 /* Only for traces. */
3650 struct quic_rx_packet *qpkt_trace;
3651
3652 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003653 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003654 /* The packet number is here. This is also the start minus
3655 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3656 * protection.
3657 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003658 pn = buf;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003659 if (qc_pkt_may_rm_hp(qc, pkt, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660 /* Note that the following function enables us to unprotect the packet
3661 * number and its length subsequently used to decrypt the entire
3662 * packets.
3663 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003664 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3665 qel->pktns->rx.largest_pn, pn, beg, end)) {
3666 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003667 goto err;
3668 }
3669
3670 /* The AAD includes the packet number field found at <pn>. */
3671 pkt->aad_len = pn - beg + pkt->pnl;
3672 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003673 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003674 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003675 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3676 /* If the packet number space has been discarded, this packet
3677 * will be not parsed.
3678 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003679 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003680 goto out;
3681 }
3682
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003683 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003685 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3686 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003687 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003688 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003689 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003690 goto err;
3691 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003692
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003693 *el = qel;
3694 /* No reference counter incrementation here!!! */
3695 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3696 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3697 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3698 b_add(&qc->rx.buf, pkt->len);
3699 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003700 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003701 return 1;
3702
3703 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003704 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003705 return 0;
3706}
3707
3708/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3709 * Also set <*long_header> to 1 if this form is long, 0 if not.
3710 */
3711static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3712 unsigned char byte0, int *long_header)
3713{
3714 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3715 pkt->type =
3716 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3717 *long_header = 1;
3718 }
3719 else {
3720 pkt->type = QUIC_PACKET_TYPE_SHORT;
3721 *long_header = 0;
3722 }
3723}
3724
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003725/*
3726 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3727 */
3728static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3729{
3730 int j = 0, version;
3731
3732 do {
3733 version = quic_supported_version[j];
3734 if (version == pkt->version)
3735 return 1;
3736
3737 version = quic_supported_version[++j];
3738 } while(version);
3739
3740 return 0;
3741}
3742
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003743__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003744static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3745 struct quic_rx_packet *pkt,
3746 struct quic_dgram_ctx *dgram_ctx,
3747 struct sockaddr_storage *saddr)
3748{
3749 unsigned char *beg;
3750 uint64_t len;
3751 struct quic_conn *qc;
3752 struct eb_root *cids;
3753 struct ebmb_node *node;
3754 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003755 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003756 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003757 size_t b_cspace;
3758 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003759
3760 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003761 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003762 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3763 if (end <= *buf)
3764 goto err;
3765
3766 /* Fixed bit */
3767 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3768 /* XXX TO BE DISCARDED */
3769 goto err;
3770
3771 srv_conn = dgram_ctx->owner;
3772 beg = *buf;
3773 /* Header form */
3774 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3775 if (long_header) {
3776 size_t cid_lookup_len;
3777
3778 if (!quic_packet_read_long_header(buf, end, pkt))
3779 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003780
3781 /* unsupported QUIC version */
3782 if (!qc_pkt_is_supported_version(pkt)) {
3783 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3784 goto err;
3785 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003786
3787 /* For Initial packets, and for servers (QUIC clients connections),
3788 * there is no Initial connection IDs storage.
3789 */
3790 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3791 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3792 cid_lookup_len = pkt->dcid.len;
3793 }
3794 else {
3795 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003796 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797 }
3798
3799 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3800 if (!node)
3801 goto err;
3802
3803 qc = ebmb_entry(node, struct quic_conn, scid_node);
3804
3805 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3806 qc->dcid.len = pkt->scid.len;
3807 if (pkt->scid.len)
3808 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3809 }
3810
3811 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3812 uint64_t token_len;
3813
3814 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3815 goto err;
3816
3817 /* XXX TO DO XXX 0 value means "the token is not present".
3818 * A server which sends an Initial packet must not set the token.
3819 * So, a client which receives an Initial packet with a token
3820 * MUST discard the packet or generate a connection error with
3821 * PROTOCOL_VIOLATION as type.
3822 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3823 */
3824 pkt->token_len = token_len;
3825 }
3826 }
3827 else {
3828 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003829 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003830 goto err;
3831
3832 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003833 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003834 if (!node)
3835 goto err;
3836
3837 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003838 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003839 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003840
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003841 /* Only packets packets with long headers and not RETRY or VERSION as type
3842 * have a length field.
3843 */
3844 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3845 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3846 goto err;
3847
3848 pkt->len = len;
3849 }
3850 else if (!long_header) {
3851 /* A short packet is the last one of an UDP datagram. */
3852 pkt->len = end - *buf;
3853 }
3854
3855 conn_ctx = qc->conn->xprt_ctx;
3856
3857 /* Increase the total length of this packet by the header length. */
3858 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003859
3860 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3861 * they must have the same DCID.
3862 *
3863 * This check must be done after the final update to pkt.len to
3864 * properly drop the packet on failure.
3865 */
3866 if (!dgram_ctx->dcid.len) {
3867 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3868 }
3869 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003870 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003871 goto err;
3872 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003873 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003874
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003875 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3876 b_cspace = b_contig_space(&qc->rx.buf);
3877 if (b_cspace < pkt->len) {
3878 /* Let us consume the remaining contiguous space. */
3879 b_add(&qc->rx.buf, b_cspace);
3880 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3881 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003882 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003883 goto err;
3884 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003885 }
3886
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003887 if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003888 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003889 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003890 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003891 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003892
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003893 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3894 if (pkt->aad_len)
3895 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003896 /* Wake the tasklet of the QUIC connection packet handler. */
3897 if (conn_ctx)
3898 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3899
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003900 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003901
3902 return pkt->len;
3903
3904 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003905 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003906 return -1;
3907}
3908
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003909/*
3910 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3911 * address <addr>.
3912 * Implementation of RFC9000 6. Version Negotiation
3913 *
3914 * TODO implement a rate-limiting sending of Version Negotiation packets
3915 *
3916 * Returns 0 on success else non-zero
3917 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003918static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3919 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003920{
3921 char buf[256];
3922 int i = 0, j, version;
3923 const socklen_t addrlen = get_addr_len(addr);
3924
3925 /*
3926 * header form
3927 * long header, fixed bit to 0 for Version Negotiation
3928 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003929 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3930 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003931
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003932 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003933 /* null version for Version Negotiation */
3934 buf[i++] = '\x00';
3935 buf[i++] = '\x00';
3936 buf[i++] = '\x00';
3937 buf[i++] = '\x00';
3938
3939 /* source connection id */
3940 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003941 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003942 i += pkt->scid.len;
3943
3944 /* destination connection id */
3945 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003946 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003947 i += pkt->dcid.len;
3948
3949 /* supported version */
3950 j = 0;
3951 do {
3952 version = htonl(quic_supported_version[j]);
3953 memcpy(&buf[i], &version, sizeof(version));
3954 i += sizeof(version);
3955
3956 version = quic_supported_version[++j];
3957 } while (version);
3958
3959 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3960 return 1;
3961
3962 return 0;
3963}
3964
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003965/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
3966 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
3967 * concatenated to the <pkt> DCID field.
3968 *
3969 * Returns the instance or NULL if not found.
3970 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003971static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003972 struct listener *l,
3973 struct sockaddr_storage *saddr)
3974{
3975 struct quic_conn *qc = NULL;
3976 struct ebmb_node *node;
3977 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01003978 /* set if the quic_conn is found in the second DCID tree */
3979 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003980
3981 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
3982
3983 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
3984 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
3985 pkt->type == QUIC_PACKET_TYPE_0RTT) {
3986 /* DCIDs of first packets coming from multiple clients may have
3987 * the same values. Let's distinguish them by concatenating the
3988 * socket addresses.
3989 */
3990 quic_cid_saddr_cat(&pkt->dcid, saddr);
3991 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
3992 pkt->dcid.len + pkt->dcid.addrlen);
3993 if (node) {
3994 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3995 goto end;
3996 }
3997 }
3998
3999 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4000 * also for INITIAL/0-RTT non-first packets with the final DCID in
4001 * used.
4002 */
4003 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
4004 if (!node)
4005 goto end;
4006
4007 id = ebmb_entry(node, struct quic_connection_id, node);
4008 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004009 found_in_dcid = 1;
4010
4011 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004012 if (qc)
4013 quic_conn_take(qc);
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004014 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004015
4016 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4017 * If already done, this is a noop.
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004018 *
4019 * node.leaf_p is first checked to avoid unnecessary locking.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004020 */
Amaury Denoyellec6fab982021-12-23 16:27:56 +01004021 if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) {
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004022 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
4023 ebmb_delete(&qc->odcid_node);
4024 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4025 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004026
4027 return qc;
4028}
4029
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004030static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004031 struct quic_rx_packet *pkt,
4032 struct quic_dgram_ctx *dgram_ctx,
4033 struct sockaddr_storage *saddr)
4034{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004035 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004036 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004037 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004038 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004039 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004040 size_t b_cspace;
4041 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004042
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004043 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004044 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004045 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004046 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004047 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4048 /* This ist only to please to traces and distinguish the
4049 * packet with parsed packet number from others.
4050 */
4051 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004052 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004053 goto err;
4054
4055 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004056 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004057 /* XXX TO BE DISCARDED */
4058 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4059 goto err;
4060 }
4061
4062 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004063 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004064 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004065 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004066 uint64_t len;
4067
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004068 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004069 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4070 goto err;
4071 }
4072
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004073 /* Retry of Version Negotiation packets are only sent by servers */
4074 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4075 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4076 goto err;
4077 }
4078
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004079 /* RFC9000 6. Version Negotiation */
4080 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004081 /* unsupported version, send Negotiation packet */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004082 if (send_version_negotiation(l->rx.fd, saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004083 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4084 goto err;
4085 }
4086
4087 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004088 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004089 }
4090
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004091 /* For Initial packets, and for servers (QUIC clients connections),
4092 * there is no Initial connection IDs storage.
4093 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004094 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004095 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004096
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004097 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4098 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004099 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4100 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004101 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004102
4103 /* XXX TO DO XXX 0 value means "the token is not present".
4104 * A server which sends an Initial packet must not set the token.
4105 * So, a client which receives an Initial packet with a token
4106 * MUST discard the packet or generate a connection error with
4107 * PROTOCOL_VIOLATION as type.
4108 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4109 */
4110 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004111 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004112 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004113 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004114 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4115 goto err;
4116 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004117 }
4118
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004119 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4120 end - buf < len) {
4121 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4122 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004123 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004124
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004125 payload = buf;
4126 pkt->len = len + payload - beg;
4127
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004128 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004129 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004130 int ipv4;
4131 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004132 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004133 const unsigned char *salt = initial_salt_v1;
4134 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004135
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004136 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004137 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004138 goto err;
4139 }
4140
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004141 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004142 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004143 qc = qc_new_conn(pkt->version, ipv4,
4144 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004145 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004146 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004147 goto err;
4148
4149 odcid = &qc->rx.params.original_destination_connection_id;
4150 /* Copy the transport parameters. */
4151 qc->rx.params = l->bind_conf->quic_params;
4152 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004153 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4154 odcid->len = pkt->dcid.len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004155 /* Copy the initial source connection ID. */
4156 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4157 qc->enc_params_len =
4158 quic_transport_params_encode(qc->enc_params,
4159 qc->enc_params + sizeof qc->enc_params,
4160 &qc->rx.params, 1);
4161 if (!qc->enc_params_len)
4162 goto err;
4163
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004164 /* NOTE: the socket address has been concatenated to the destination ID
4165 * chosen by the client for Initial packets.
4166 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004167 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4168 salt = initial_salt_draft_29;
4169 salt_len = sizeof initial_salt_draft_29;
4170 }
4171 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004172 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004173 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004174 goto err;
4175 }
4176
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004177 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004178 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004179 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4180 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004181
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004182 /* If the insertion failed, it means that another
4183 * thread has already allocated a QUIC connection for
4184 * the same CID. Liberate our allocated connection.
4185 */
4186 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004187 qc_to_purge = qc;
4188
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004189 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4190 pkt->qc = qc;
4191 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004192
4193 quic_conn_take(qc);
4194 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4195
4196 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004197 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004198 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004199 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4200 /* Try to accept a new connection. */
4201 listener_accept(l);
4202 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004203 else {
4204 quic_conn_drop(qc_to_purge);
4205 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004206 }
4207 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004208 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004209 }
4210 }
4211 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004212 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004213 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4214 goto err;
4215 }
4216
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004217 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004218 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004219 buf += QUIC_HAP_CID_LEN;
4220
4221 /* A short packet is the last one of a UDP datagram. */
4222 payload = buf;
4223 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004224
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004225 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004226 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004227 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004228 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4229 goto err;
4230 }
4231
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004232 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004233 }
4234
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004235
4236 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4237 * they must have the same DCID.
4238 *
4239 * This check must be done after the final update to pkt.len to
4240 * properly drop the packet on failure.
4241 */
4242 if (!dgram_ctx->dcid.len) {
4243 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004244 if (!quic_peer_validated_addr(qc) &&
4245 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4246 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4247 QUIC_EV_CONN_LPKT, qc);
4248 /* Reset the anti-amplification bit. It will be set again
4249 * when sending the next packet if reached again.
4250 */
4251 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4252 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4253 io_cb_wakeup = 1;
4254 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004255 }
4256 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004257 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004258 goto err;
4259 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004260 dgram_ctx->qc = qc;
4261
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004262
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004263 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004264 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004265 goto out;
4266 }
4267
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004268 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004269 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004270 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004271 b_cspace = b_contig_space(&qc->rx.buf);
4272 if (b_cspace < pkt->len) {
4273 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004274 if (b_cspace) {
4275 b_putchr(&qc->rx.buf, 0x00);
4276 b_cspace--;
4277 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004278 b_add(&qc->rx.buf, b_cspace);
4279 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4280 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004281 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004282 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004283 goto err;
4284 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004285 }
4286
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004287 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004288 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004289 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004290 goto err;
4291 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004292
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004293 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004294 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004295 if (pkt->aad_len)
4296 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004297 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004298 /* Wake up the connection packet handler task from here only if all
4299 * the contexts have been initialized, especially the mux context
4300 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4301 * will start it if these contexts for the connection are not already
4302 * initialized.
4303 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004304 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4305 if (conn_ctx && conn_ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004306 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004307
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004308 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004309
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004310 if (qc)
4311 quic_conn_drop(qc);
4312
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004313 return pkt->len;
4314
4315 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004316 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4317 * This cannot be done by this thread.
4318 */
4319 if (io_cb_wakeup) {
4320 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4321 if (conn_ctx && conn_ctx->wait_event.tasklet)
4322 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4323 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004324 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004325 if (!pkt->len)
4326 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004327 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004328 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004329
4330 if (qc)
4331 quic_conn_drop(qc);
4332
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004333 return -1;
4334}
4335
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004336/* This function builds into <buf> buffer a QUIC long packet header.
4337 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004338 */
4339static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4340 int type, size_t pn_len, struct quic_conn *conn)
4341{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004342 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004343 return 0;
4344
4345 /* #0 byte flags */
4346 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4347 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4348 /* Version */
4349 quic_write_uint32(buf, end, conn->version);
4350 *(*buf)++ = conn->dcid.len;
4351 /* Destination connection ID */
4352 if (conn->dcid.len) {
4353 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4354 *buf += conn->dcid.len;
4355 }
4356 /* Source connection ID */
4357 *(*buf)++ = conn->scid.len;
4358 if (conn->scid.len) {
4359 memcpy(*buf, conn->scid.data, conn->scid.len);
4360 *buf += conn->scid.len;
4361 }
4362
4363 return 1;
4364}
4365
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004366/* This function builds into <buf> buffer a QUIC short packet header.
4367 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004368 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004369static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4370 size_t pn_len, struct quic_conn *conn,
4371 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004372{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004373 if (end - *buf < 1 + conn->dcid.len)
4374 return 0;
4375
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004376 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004377 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4378 ((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 +01004379 /* Destination connection ID */
4380 if (conn->dcid.len) {
4381 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4382 *buf += conn->dcid.len;
4383 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004384
4385 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004386}
4387
4388/* Apply QUIC header protection to the packet with <buf> as first byte address,
4389 * <pn> as address of the Packet number field, <pnlen> being this field length
4390 * with <aead> as AEAD cipher and <key> as secret key.
4391 * Returns 1 if succeeded or 0 if failed.
4392 */
4393static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4394 const EVP_CIPHER *aead, const unsigned char *key)
4395{
4396 int i, ret, outlen;
4397 EVP_CIPHER_CTX *ctx;
4398 /* We need an IV of at least 5 bytes: one byte for bytes #0
4399 * and at most 4 bytes for the packet number
4400 */
4401 unsigned char mask[5] = {0};
4402
4403 ret = 0;
4404 ctx = EVP_CIPHER_CTX_new();
4405 if (!ctx)
4406 return 0;
4407
4408 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4409 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4410 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4411 goto out;
4412
4413 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4414 for (i = 0; i < pnlen; i++)
4415 pn[i] ^= mask[i + 1];
4416
4417 ret = 1;
4418
4419 out:
4420 EVP_CIPHER_CTX_free(ctx);
4421
4422 return ret;
4423}
4424
4425/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4426 * ACK ranges if needed to a value below <limit> in bytes.
4427 * Return 1 if succeeded, 0 if not.
4428 */
4429static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4430{
4431 size_t room, ack_delay_sz;
4432
4433 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4434 /* A frame is made of 1 byte for the frame type. */
4435 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004436 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004437 return 0;
4438
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004439 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004440}
4441
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004442/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4443 * 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 +01004444 * and <*len> the packet Length field initialized with the number of bytes already present
4445 * 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 +02004446 * <headlen> is the number of bytes already present in this packet before building frames.
4447 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004448 * Update consequently <*len> to reflect the size of these frames built
4449 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004450 * Return 1 if succeeded, 0 if not.
4451 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004452static inline int qc_build_frms(struct quic_tx_packet *pkt,
4453 size_t room, size_t *len, size_t headlen,
4454 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004455 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004456{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004457 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004458 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004459 struct mt_list *tmp1, tmp2;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004460 size_t remain = quic_path_prep_data(qc->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004461
Frédéric Lécailleea604992020-12-24 13:01:37 +01004462 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004463 if (*len > room || headlen > remain)
4464 return 0;
4465
Frédéric Lécailleea604992020-12-24 13:01:37 +01004466 /* If we are not probing we must take into an account the congestion
4467 * control window.
4468 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004469 if (!qel->pktns->tx.pto_probe)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004470 room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004471 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004472 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004473 mt_list_for_each_entry_safe(cf, &qel->pktns->tx.frms, mt_list, tmp1, tmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004474 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004475 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004476
Frédéric Lécailleea604992020-12-24 13:01:37 +01004477 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004478 break;
4479
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004480 switch (cf->type) {
4481 case QUIC_FT_CRYPTO:
4482 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004483 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004484 /* Compute the length of this CRYPTO frame header */
4485 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4486 /* Compute the data length of this CRyPTO frame. */
4487 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4488 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004489 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004490 if (!dlen)
4491 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004492
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004493 /* CRYPTO frame length. */
4494 flen = hlen + quic_int_getsize(dlen) + dlen;
4495 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004496 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004497 /* Add the CRYPTO data length and its encoded length to the packet
4498 * length and the length of this length.
4499 */
4500 *len += flen;
4501 room -= flen;
4502 if (dlen == cf->crypto.len) {
4503 /* <cf> CRYPTO data have been consumed. */
4504 MT_LIST_DELETE_SAFE(tmp1);
4505 LIST_APPEND(&pkt->frms, &cf->list);
4506 }
4507 else {
4508 struct quic_frame *new_cf;
4509
4510 new_cf = pool_alloc(pool_head_quic_frame);
4511 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004512 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004513 return 0;
4514 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004515
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004516 new_cf->type = QUIC_FT_CRYPTO;
4517 new_cf->crypto.len = dlen;
4518 new_cf->crypto.offset = cf->crypto.offset;
4519 new_cf->crypto.qel = qel;
4520 LIST_APPEND(&pkt->frms, &new_cf->list);
4521 /* Consume <dlen> bytes of the current frame. */
4522 cf->crypto.len -= dlen;
4523 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004524 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004525 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004526
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004527 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004528 /* Note that these frames are accepted in short packets only without
4529 * "Length" packet field. Here, <*len> is used only to compute the
4530 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004531 *
4532 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004533 * excepting the variable ones. Note that +1 is for the type of this frame.
4534 */
4535 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004536 ((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 +02004537 /* Compute the data length of this STREAM frame. */
4538 avail_room = room - hlen - *len;
4539 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004540 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004541
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004542 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004543 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004544 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4545 dlen = max_available_room(avail_room, &dlen_sz);
4546 if (dlen > cf->stream.len) {
4547 dlen = cf->stream.len;
4548 }
4549 dlen_sz = quic_int_getsize(dlen);
4550 flen = hlen + dlen_sz + dlen;
4551 }
4552 else {
4553 dlen = QUIC_MIN(avail_room, cf->stream.len);
4554 flen = hlen + dlen;
4555 }
4556 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004557 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004558 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004559 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004560 /* Add the STREAM data length and its encoded length to the packet
4561 * length and the length of this length.
4562 */
4563 *len += flen;
4564 room -= flen;
4565 if (dlen == cf->stream.len) {
4566 /* <cf> STREAM data have been consumed. */
4567 MT_LIST_DELETE_SAFE(tmp1);
4568 LIST_APPEND(&pkt->frms, &cf->list);
4569 }
4570 else {
4571 struct quic_frame *new_cf;
4572
4573 new_cf = pool_zalloc(pool_head_quic_frame);
4574 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004575 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004576 return 0;
4577 }
4578
4579 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004580 new_cf->stream.qcs = cf->stream.qcs;
4581 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004582 new_cf->stream.id = cf->stream.id;
4583 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4584 new_cf->stream.offset = cf->stream.offset;
4585 new_cf->stream.len = dlen;
4586 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4587 /* FIN bit reset */
4588 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4589 new_cf->stream.data = cf->stream.data;
4590 LIST_APPEND(&pkt->frms, &new_cf->list);
4591 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4592 /* Consume <dlen> bytes of the current frame. */
4593 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004594 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004595 cf->stream.data += dlen;
4596 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004597 break;
4598
4599 default:
4600 flen = qc_frm_len(cf);
4601 BUG_ON(!flen);
4602 if (flen > room)
4603 continue;
4604
4605 *len += flen;
4606 room -= flen;
4607 MT_LIST_DELETE_SAFE(tmp1);
4608 LIST_APPEND(&pkt->frms, &cf->list);
4609 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004610 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004611 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004612 }
4613
Frédéric Lécailleea604992020-12-24 13:01:37 +01004614 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004615}
4616
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004617/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004618 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4619 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4620 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004621 * 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 +02004622 * 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 +02004623 * having returned from this function.
4624 * 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 +01004625 * number field in this packet. <pn_len> will also have the packet number
4626 * length as value.
4627 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004628 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004629 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004630static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4631 size_t dglen, struct quic_tx_packet *pkt,
4632 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4633 int ack, int padding, int cc, int nb_pto_dgrams,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004634 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004635{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004636 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004637 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004638 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4639 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004640 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004641 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004642 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004643 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004644
Frédéric Lécailleea604992020-12-24 13:01:37 +01004645 /* Length field value with CRYPTO frames if present. */
4646 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004647 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004648 /* When not probing and not acking, and no immediate close is required,
4649 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004650 * the congestion controller window. So, we do not limit the size of this
4651 * packet if we have an ACK frame to send because an ACK frame is not
4652 * ack-eliciting. This size will be limited if we have ack-eliciting
4653 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004654 */
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004655 if (!nb_pto_dgrams && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004656 size_t path_room;
4657
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004658 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004659 if (end - beg > path_room)
4660 end = beg + path_room;
4661 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004662
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004663 /* Ensure there is enough room for the TLS encryption tag and a zero token
4664 * length field if any.
4665 */
4666 if (end - pos < QUIC_TLS_TAG_LEN +
4667 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4668 goto no_room;
4669
4670 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004671 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004672 /* packet number length */
4673 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004674 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004675 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004676 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004677 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004678 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004679 goto no_room;
4680
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004681 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4682 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004683 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004684 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004685 /* Build an ACK frame if required. */
4686 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004687 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004688 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004689 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004690 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4691 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4692 * that from here, we do not know if we will have to send a PING frame.
4693 * This will be decided after having computed the ack-eliciting frames
4694 * to be added to this packet.
4695 */
4696 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 +01004697 if (!ack_frm_len)
4698 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004699 }
4700
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004701 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004702 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004703 len_frms = 0;
4704 if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004705 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004706
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004707 /* Initialize the length of the frames built below to <len>.
4708 * If any frame could be successfully built by qc_build_frms(),
4709 * we will have len_frms > len.
4710 */
4711 len_frms = len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004712 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004713 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004714 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004715 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004716 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004717
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004718 /* Length (of the remaining data). Must not fail because, the buffer size
4719 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4720 * for the encryption tag. It must be taken into an account for the length
4721 * of this packet.
4722 */
4723 if (len_frms)
4724 len = len_frms + QUIC_TLS_TAG_LEN;
4725 else
4726 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004727 /* CONNECTION_CLOSE frame */
4728 if (cc) {
4729 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004730
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004731 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004732 len += qc_frm_len(&cc_frm);
4733 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004734 add_ping_frm = 0;
4735 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004736 len_sz = quic_int_getsize(len);
4737 /* Add this packet size to <dglen> */
4738 dglen += head_len + len_sz + len;
4739 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4740 /* This is a maximum padding size */
4741 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4742 /* The length field value is of this packet is <len> + <padding_len>
4743 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004744 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004745 * sizes from <padding_len>.
4746 */
4747 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4748 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004749 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004750 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004751 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004752 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004753 add_ping_frm = 1;
4754 len += 1;
4755 }
4756 /* 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 +01004757 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004758 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4759 }
4760
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004761 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4762 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004763
4764 /* Packet number field address. */
4765 *buf_pn = pos;
4766
4767 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004768 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4769 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004770
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004771 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004772 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004773
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004774 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004775 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004776 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004777
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004778 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004779 list_for_each_entry(cf, &pkt->frms, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004780 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004781 ssize_t room = end - pos;
4782 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004783 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004784 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004785 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004786 }
4787 }
4788
4789 /* Build a PING frame if needed. */
4790 if (add_ping_frm) {
4791 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004792 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004793 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004794 }
4795
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004796 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004797 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004798 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004799
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800 /* Build a PADDING frame if needed. */
4801 if (padding_len) {
4802 frm.type = QUIC_FT_PADDING;
4803 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004804 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004805 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004806 }
4807
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004808 /* If this packet is ack-eliciting and we are probing let's
4809 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004810 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004811 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
4812 qel->pktns->tx.pto_probe)
4813 qel->pktns->tx.pto_probe--;
4814
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004815 pkt->len = pos - beg;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004816
4817 return 1;
4818
4819 no_room:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004820 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004821 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004822}
4823
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004824static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004825{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004826 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004827 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004828 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004829 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004830 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004831 pkt->next = NULL;
4832 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004833}
4834
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004835/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004836static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4837{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004838 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004839
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004840 if (!pkt)
4841 return;
4842
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004843 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004844 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004845 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004846 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004847 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004848}
4849
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004850/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4851 * type for <qc> QUIC connection from <qel> encryption level.
4852 * Return -2 if the packet could not be allocated or encrypted for any reason,
4853 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004854 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004855static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4856 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004857 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004858 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004859 int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004860{
4861 /* The pointer to the packet number field. */
4862 unsigned char *buf_pn;
4863 unsigned char *beg, *end, *payload;
4864 int64_t pn;
4865 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004866 struct quic_tls_ctx *tls_ctx;
4867 struct quic_tx_packet *pkt;
4868
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004869 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004870 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004871 pkt = pool_alloc(pool_head_quic_tx_packet);
4872 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004873 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004874 *err = -2;
4875 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004876 }
4877
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004878 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004879 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004880 pn_len = 0;
4881 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004882
4883 pn = qel->pktns->tx.next_pn + 1;
4884 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
4885 ack, padding, cc, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004886 *err = -1;
4887 goto err;
4888 }
4889
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004890 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004891 payload = buf_pn + pn_len;
4892 payload_len = end - payload;
4893 aad_len = payload - beg;
4894
4895 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004896 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4897 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004898 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004899 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004900
4901 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004902 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004903 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4904 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004905 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004906 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004907 goto err;
4908 }
4909
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004910 /* Consume a packet number */
4911 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01004912 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01004913 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
4914 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004915 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4916 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004917 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004918 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004919 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004920 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004921 pkt->in_flight_len = pkt->len;
4922 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004923 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004924 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004925 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004926
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004927 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004928
4929 err:
4930 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004931 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004932 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004933}
4934
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004935/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4936 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004937 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004938static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4939 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004940{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004941 size_t try, done = 0;
4942
4943 if (!conn_ctrl_ready(conn))
4944 return 0;
4945
4946 if (!fd_recv_ready(conn->handle.fd))
4947 return 0;
4948
4949 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004950
4951 /* read the largest possible block. For this, we perform only one call
4952 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004953 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004954 */
4955 while (count > 0) {
4956 try = b_contig_space(buf);
4957 if (!try)
4958 break;
4959
4960 if (try > count)
4961 try = count;
4962
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004963 b_add(buf, try);
4964 done += try;
4965 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004966 }
4967
4968 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4969 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4970
4971 leave:
4972 return done;
4973
4974 read0:
4975 conn_sock_read0(conn);
4976 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4977
4978 /* Now a final check for a possible asynchronous low-level error
4979 * report. This can happen when a connection receives a reset
4980 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4981 * we might have come from there by just checking POLL_HUP instead
4982 * of recv()'s return value 0, so we have no way to tell there was
4983 * an error without checking.
4984 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004985 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004986 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4987 goto leave;
4988}
4989
4990
4991/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4992 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4993 * other pending data for example, but this flag is ignored at the moment.
4994 * Only one call to send() is performed, unless the buffer wraps, in which case
4995 * a second call may be performed. The connection's flags are updated with
4996 * whatever special event is detected (error, empty). The caller is responsible
4997 * for taking care of those events and avoiding the call if inappropriate. The
4998 * function does not call the connection's polling update function, so the caller
4999 * is responsible for this. It's up to the caller to update the buffer's contents
5000 * based on the return value.
5001 */
5002static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5003{
5004 ssize_t ret;
5005 size_t try, done;
5006 int send_flag;
5007
5008 if (!conn_ctrl_ready(conn))
5009 return 0;
5010
5011 if (!fd_send_ready(conn->handle.fd))
5012 return 0;
5013
5014 done = 0;
5015 /* send the largest possible block. For this we perform only one call
5016 * to send() unless the buffer wraps and we exactly fill the first hunk,
5017 * in which case we accept to do it once again.
5018 */
5019 while (count) {
5020 try = b_contig_data(buf, done);
5021 if (try > count)
5022 try = count;
5023
5024 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5025 if (try < count || flags & CO_SFL_MSG_MORE)
5026 send_flag |= MSG_MORE;
5027
5028 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
5029 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
5030 if (ret > 0) {
5031 count -= ret;
5032 done += ret;
5033
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005034 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035 conn->flags |= CO_FL_WAIT_L4L6;
5036 /* if the system buffer is full, don't insist */
5037 if (ret < try)
5038 break;
5039 }
5040 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
5041 /* nothing written, we need to poll for write first */
5042 fd_cant_send(conn->handle.fd);
5043 break;
5044 }
5045 else if (errno != EINTR) {
5046 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5047 break;
5048 }
5049 }
5050 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5051 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5052
5053 if (done > 0) {
5054 /* we count the total bytes sent, and the send rate for 32-byte
5055 * blocks. The reason for the latter is that freq_ctr are
5056 * limited to 4GB and that it's not enough per second.
5057 */
5058 _HA_ATOMIC_ADD(&global.out_bytes, done);
5059 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5060 }
5061 return done;
5062}
5063
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005064/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5065 * event subscriber <es> is not allowed to change from a previous call as long
5066 * as at least one event is still subscribed. The <event_type> must only be a
5067 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5068 */
5069static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5070{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005071 struct qcc *qcc = conn->qc->qcc;
5072
5073 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5074 BUG_ON(qcc->subs && qcc->subs != es);
5075
5076 es->events |= event_type;
5077 qcc->subs = es;
5078
5079 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005080 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005081
5082 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005083 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005084
5085 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005086}
5087
5088/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5089 * The <es> pointer is not allowed to differ from the one passed to the
5090 * subscribe() call. It always returns zero.
5091 */
5092static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5093{
5094 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5095}
5096
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005097/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5098 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5099 * parameters of this session.
5100 * This is the responsibility of the caller to check the validity of all the
5101 * pointers passed as parameter to this function.
5102 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5103 * CO_ER_SSL_NO_MEM.
5104 */
5105static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5106 unsigned char *params, size_t params_len)
5107{
5108 int retry;
5109
5110 retry = 1;
5111 retry:
5112 *ssl = SSL_new(ssl_ctx);
5113 if (!*ssl) {
5114 if (!retry--)
5115 goto err;
5116
5117 pool_gc(NULL);
5118 goto retry;
5119 }
5120
5121 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5122 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
5123 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5124 goto err;
5125
5126 SSL_free(*ssl);
5127 *ssl = NULL;
5128 if (!retry--)
5129 goto err;
5130
5131 pool_gc(NULL);
5132 goto retry;
5133 }
5134
5135 return 0;
5136
5137 err:
5138 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5139 return -1;
5140}
5141
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005142/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5143 * connection with <xprt_ctx> as address of the xprt context.
5144 * Returns 1 if succeeded, 0 if not.
5145 */
5146static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5147{
Amaury Denoyelle9979d0d2021-12-23 16:32:24 +01005148 struct ssl_sock_ctx *ctx = NULL;
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005149 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005150
5151 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5152
5153 if (*xprt_ctx)
5154 goto out;
5155
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005156 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005157 if (!ctx) {
5158 conn->err_code = CO_ER_SYS_MEMLIM;
5159 goto err;
5160 }
5161
5162 ctx->wait_event.tasklet = tasklet_new();
5163 if (!ctx->wait_event.tasklet) {
5164 conn->err_code = CO_ER_SYS_MEMLIM;
5165 goto err;
5166 }
5167
5168 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5169 ctx->wait_event.tasklet->context = ctx;
5170 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005171 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005172 ctx->subs = NULL;
5173 ctx->xprt_ctx = NULL;
5174
5175 ctx->xprt = xprt_get(XPRT_QUIC);
5176 if (objt_server(conn->target)) {
5177 /* Server */
5178 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005179 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005180 int ssl_err, ipv4;
5181
5182 ssl_err = SSL_ERROR_NONE;
5183 if (RAND_bytes(dcid, sizeof dcid) != 1)
5184 goto err;
5185
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005186 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005187 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005188 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005189 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005190 goto err;
5191
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005192 ctx->qc = qc;
5193
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005194 /* Insert our SCID, the connection ID for the QUIC client. */
5195 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5196
5197 conn->qc = qc;
5198 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005199 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5200 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005201 goto err;
5202
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005203 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005204 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005205 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5206 qc->enc_params_len =
5207 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5208 &qc->rx.params, 0);
5209 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005210 goto err;
5211
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005212 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5213 qc->enc_params, qc->enc_params_len) == -1)
5214 goto err;
5215
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005216 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005217 SSL_set_connect_state(ctx->ssl);
5218 ssl_err = SSL_do_handshake(ctx->ssl);
5219 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005220 int st;
5221
5222 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005223 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5224 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005225 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005226 }
5227 else {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005228 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005229 goto err;
5230 }
5231 }
5232 }
5233 else if (objt_listener(conn->target)) {
5234 /* Listener */
5235 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
5236
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005237 qc = ctx->conn->qc;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005238 ctx->qc = qc;
5239
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005240 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005241 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5242 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005243 goto err;
5244
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005245 /* Enabling 0-RTT */
5246 if (bc->ssl_conf.early_data)
5247 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5248
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005249 SSL_set_accept_state(ctx->ssl);
5250 }
5251
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005252 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005253
5254 /* Leave init state and start handshake */
5255 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005256
5257 out:
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005258 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005259 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005260
5261 return 0;
5262
5263 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005264 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005265 tasklet_free(ctx->wait_event.tasklet);
5266 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005267 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005268 return -1;
5269}
5270
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005271/* Start the QUIC transport layer */
5272static int qc_xprt_start(struct connection *conn, void *ctx)
5273{
5274 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005275 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005276
5277 qc = conn->qc;
5278 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005279 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005280 return 0;
5281 }
5282
5283 tasklet_wakeup(qctx->wait_event.tasklet);
5284 return 1;
5285}
5286
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005287/* transport-layer operations for QUIC connections. */
5288static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005289 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005290 .snd_buf = quic_conn_from_buf,
5291 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005292 .subscribe = quic_conn_subscribe,
5293 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005294 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005295 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005296 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5297 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005298 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005299 .name = "QUIC",
5300};
5301
5302__attribute__((constructor))
5303static void __quic_conn_init(void)
5304{
5305 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5306 xprt_register(XPRT_QUIC, &ssl_quic);
5307}
5308
5309__attribute__((destructor))
5310static void __quic_conn_deinit(void)
5311{
5312 BIO_meth_free(ha_quic_meth);
5313}
5314
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005315/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5316 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005317 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005318 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005319static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005320 struct sockaddr_storage *saddr, qpkt_read_func *func)
5321{
5322 unsigned char *pos;
5323 const unsigned char *end;
5324 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005325 .qc = NULL,
5326 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005327 .owner = owner,
5328 };
5329
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005330 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005331 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005332 do {
5333 int ret;
5334 struct quic_rx_packet *pkt;
5335
Willy Tarreaue4498932021-03-22 21:13:05 +01005336 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005337 if (!pkt)
5338 goto err;
5339
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005340 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005341 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5342 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005343 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005344 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005345 /* If the packet length could not be found, we cannot continue. */
5346 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005347 } while (pos < end);
5348
5349 /* Increasing the received bytes counter by the UDP datagram length
5350 * if this datagram could be associated to a connection.
5351 */
5352 if (dgram_ctx.qc)
5353 dgram_ctx.qc->rx.bytes += len;
5354
5355 return pos - (unsigned char *)buf;
5356
5357 err:
5358 return -1;
5359}
5360
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005361ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005362 struct sockaddr_storage *saddr)
5363{
5364 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5365}
5366
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005367/* Function to automatically activate QUIC traces on stdout.
5368 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5369 * Main use for now is in the docker image for QUIC interop testing.
5370 */
5371static void quic_init_stdout_traces(void)
5372{
5373#ifdef ENABLE_QUIC_STDOUT_TRACES
5374 trace_quic.sink = sink_find("stdout");
5375 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005376 trace_quic.state = TRACE_STATE_RUNNING;
5377#endif
5378}
5379INITCALL0(STG_INIT, quic_init_stdout_traces);
5380
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005381/*
5382 * Local variables:
5383 * c-indent-level: 8
5384 * c-basic-offset: 8
5385 * End:
5386 */