blob: 07dcbd72e485517d806802a9d949d26c45d43ff9 [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écailleba85acd2022-01-11 14:43:50 +0100119 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
120 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100121 { /* end */ }
122};
123
124static const struct name_desc quic_trace_lockon_args[4] = {
125 /* arg1 */ { /* already used by the connection */ },
126 /* arg2 */ { .name="quic", .desc="QUIC transport" },
127 /* arg3 */ { },
128 /* arg4 */ { }
129};
130
131static const struct name_desc quic_trace_decoding[] = {
132#define QUIC_VERB_CLEAN 1
133 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
134 { /* end */ }
135};
136
137
138struct trace_source trace_quic = {
139 .name = IST("quic"),
140 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100141 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142 .default_cb = quic_trace,
143 .known_events = quic_trace_events,
144 .lockon_args = quic_trace_lockon_args,
145 .decoding = quic_trace_decoding,
146 .report_events = ~0, /* report everything by default */
147};
148
149#define TRACE_SOURCE &trace_quic
150INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
151
152static BIO_METHOD *ha_quic_meth;
153
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200154DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200155DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100156DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100157DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200158 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_POOL(pool_head_quic_connection_id,
161 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_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 +0100163DECLARE_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 +0100164DECLARE_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 +0100165DECLARE_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 +0100166DECLARE_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 +0200167DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100168DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100169
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200170static 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 +0200171 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100172 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +0100173 int padding, int ack, int nb_pto_dgrams, int cc, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100174
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100175/* Only for debug purpose */
176struct enc_debug_info {
177 unsigned char *payload;
178 size_t payload_len;
179 unsigned char *aad;
180 size_t aad_len;
181 uint64_t pn;
182};
183
184/* Initializes a enc_debug_info struct (only for debug purpose) */
185static inline void enc_debug_info_init(struct enc_debug_info *edi,
186 unsigned char *payload, size_t payload_len,
187 unsigned char *aad, size_t aad_len, uint64_t pn)
188{
189 edi->payload = payload;
190 edi->payload_len = payload_len;
191 edi->aad = aad;
192 edi->aad_len = aad_len;
193 edi->pn = pn;
194}
195
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100196/* Trace callback for QUIC.
197 * These traces always expect that arg1, if non-null, is of type connection.
198 */
199static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
200 const struct ist where, const struct ist func,
201 const void *a1, const void *a2, const void *a3, const void *a4)
202{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100203 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100204
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100205 if (qc) {
206 const struct quic_tls_secrets *secs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100208 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209 if ((mask & QUIC_EV_CONN_INIT) && qc) {
210 chunk_appendf(&trace_buf, "\n odcid");
211 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100212 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100213 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100214 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 }
217
218 if (mask & QUIC_EV_CONN_ADDDATA) {
219 const enum ssl_encryption_level_t *level = a2;
220 const size_t *len = a3;
221
222 if (level) {
223 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
224
225 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
226 }
227 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100228 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100229 }
230 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
231 /* Initial read & write secrets. */
232 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
233 const unsigned char *rx_sec = a2;
234 const unsigned char *tx_sec = a3;
235
236 secs = &qc->els[level].tls_ctx.rx;
237 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
238 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
239 if (rx_sec)
240 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
241 quic_tls_keys_hexdump(&trace_buf, secs);
242 }
243 secs = &qc->els[level].tls_ctx.tx;
244 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
245 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
246 if (tx_sec)
247 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
248 quic_tls_keys_hexdump(&trace_buf, secs);
249 }
250 }
251 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
252 const enum ssl_encryption_level_t *level = a2;
253 const unsigned char *secret = a3;
254 const size_t *secret_len = a4;
255
256 if (level) {
257 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
258
259 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
260 if (secret && secret_len)
261 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
262 secs = &qc->els[lvl].tls_ctx.rx;
263 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
264 quic_tls_keys_hexdump(&trace_buf, secs);
265 }
266 }
267
268 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
269 const enum ssl_encryption_level_t *level = a2;
270 const unsigned char *secret = a3;
271 const size_t *secret_len = a4;
272
273 if (level) {
274 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
275
276 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
277 if (secret && secret_len)
278 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
279 secs = &qc->els[lvl].tls_ctx.tx;
280 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
281 quic_tls_keys_hexdump(&trace_buf, secs);
282 }
283
284 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100286 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287 const struct quic_tx_packet *pkt = a2;
288 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100289 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290
291 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100292 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100293 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100294 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100295 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100296 (unsigned long long)qc->path->cwnd,
297 (unsigned long long)qc->path->prep_in_flight,
298 (unsigned long long)qc->path->in_flight,
299 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100300 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100301 }
302 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200303 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100304 if (pkt->pn_node.key != (uint64_t)-1)
305 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100306 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100307 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100308 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
309 }
310
311 if (room) {
312 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
313 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
314 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100315 }
316 }
317
318 if (mask & QUIC_EV_CONN_HDSHK) {
319 const enum quic_handshake_state *state = a2;
320 const int *err = a3;
321
322 if (state)
323 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
324 if (err)
325 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
326 }
327
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100328 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100329 const struct quic_rx_packet *pkt = a2;
330 const unsigned long *pktlen = a3;
331 const SSL *ssl = a4;
332
333 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100334 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100335 pkt, quic_packet_type_enc_level_char(pkt->type));
336 if (pkt->pnl)
337 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
338 (unsigned long long)pkt->pn);
339 if (pkt->token_len)
340 chunk_appendf(&trace_buf, " toklen=%llu",
341 (unsigned long long)pkt->token_len);
342 if (pkt->aad_len)
343 chunk_appendf(&trace_buf, " aadlen=%llu",
344 (unsigned long long)pkt->aad_len);
345 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
346 pkt->flags, (unsigned long long)pkt->len);
347 }
348 if (pktlen)
349 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
350 if (ssl) {
351 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
352 chunk_appendf(&trace_buf, " el=%c",
353 quic_enc_level_char(ssl_to_quic_enc_level(level)));
354 }
355 }
356
357 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
358 const struct quic_rx_packet *pkt = a2;
359 const struct quic_rx_crypto_frm *cf = a3;
360 const SSL *ssl = a4;
361
362 if (pkt)
363 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
364 quic_packet_type_enc_level_char(pkt->type),
365 (unsigned long long)pkt->pn);
366 if (cf)
367 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
368 (unsigned long long)cf->offset_node.key,
369 (unsigned long long)cf->len);
370 if (ssl) {
371 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200372 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100373 quic_enc_level_char(ssl_to_quic_enc_level(level)));
374 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100375
376 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100377 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100378 }
379
380 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
381 const struct quic_frame *frm = a2;
382
383 if (frm)
384 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
385 }
386
387 if (mask & QUIC_EV_CONN_PHPKTS) {
388 const struct quic_enc_level *qel = a2;
389
390 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100391 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100392 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100393 " 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 +0100394 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200395 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100396 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100397 (unsigned long long)qc->path->cwnd,
398 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100399 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100400 (unsigned long long)pktns->tx.in_flight,
401 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100402 }
403 }
404
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100405 if (mask & QUIC_EV_CONN_ENCPKT) {
406 const struct enc_debug_info *edi = a2;
407
408 if (edi)
409 chunk_appendf(&trace_buf,
410 " payload=@%p payload_len=%llu"
411 " aad=@%p aad_len=%llu pn=%llu",
412 edi->payload, (unsigned long long)edi->payload_len,
413 edi->aad, (unsigned long long)edi->aad_len,
414 (unsigned long long)edi->pn);
415 }
416
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100417 if (mask & QUIC_EV_CONN_RMHP) {
418 const struct quic_rx_packet *pkt = a2;
419
420 if (pkt) {
421 const int *ret = a3;
422
423 chunk_appendf(&trace_buf, " pkt@%p", pkt);
424 if (ret && *ret)
425 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
426 pkt->pnl, (unsigned long long)pkt->pn);
427 }
428 }
429
430 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200431 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100432 const unsigned long *val1 = a3;
433 const unsigned long *val2 = a4;
434
435 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100436 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100437 if (val1)
438 chunk_appendf(&trace_buf, " %lu", *val1);
439 if (val2)
440 chunk_appendf(&trace_buf, "..%lu", *val2);
441 }
442
443 if (mask & QUIC_EV_CONN_RTTUPDT) {
444 const unsigned int *rtt_sample = a2;
445 const unsigned int *ack_delay = a3;
446 const struct quic_loss *ql = a4;
447
448 if (rtt_sample)
449 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
450 if (ack_delay)
451 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
452 if (ql)
453 chunk_appendf(&trace_buf,
454 " srtt=%ums rttvar=%ums min_rtt=%ums",
455 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
456 }
457 if (mask & QUIC_EV_CONN_CC) {
458 const struct quic_cc_event *ev = a2;
459 const struct quic_cc *cc = a3;
460
461 if (a2)
462 quic_cc_event_trace(&trace_buf, ev);
463 if (a3)
464 quic_cc_state_trace(&trace_buf, cc);
465 }
466
467 if (mask & QUIC_EV_CONN_PKTLOSS) {
468 const struct quic_pktns *pktns = a2;
469 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100470
471 if (pktns) {
472 chunk_appendf(&trace_buf, " pktns=%s",
473 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
474 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
475 if (pktns->tx.loss_time)
476 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100477 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100478 }
479 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
480 struct quic_tx_packet *pkt;
481
482 chunk_appendf(&trace_buf, " lost_pkts:");
483 list_for_each_entry(pkt, lost_pkts, list)
484 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
485 }
486 }
487
488 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100489 const struct quic_pktns *pktns = a2;
490 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100491 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100492
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100493 if (ifae_pkts)
494 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
495 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100496 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100497 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100498 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100499 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
500 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100501 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
502 if (pktns->tx.in_flight)
503 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100504 if (pktns->tx.loss_time)
505 chunk_appendf(&trace_buf, " loss_time=%dms",
506 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
507 }
508 if (mask & QUIC_EV_CONN_SPTO) {
509 if (pktns->tx.time_of_last_eliciting)
510 chunk_appendf(&trace_buf, " tole=%dms",
511 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
512 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100513 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100514 }
515 }
516
517 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
518 chunk_appendf(&trace_buf,
519 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
520 }
521 }
522
523 if (mask & QUIC_EV_CONN_SPPKTS) {
524 const struct quic_tx_packet *pkt = a2;
525
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100526 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
527 (unsigned long long)qc->path->cwnd,
528 (unsigned long long)qc->path->prep_in_flight,
529 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100531 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 (unsigned long)pkt->pn_node.key,
533 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
534 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100535 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100536 }
537 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100538
539 if (mask & QUIC_EV_CONN_SSLALERT) {
540 const uint8_t *alert = a2;
541 const enum ssl_encryption_level_t *level = a3;
542
543 if (alert)
544 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
545 if (level)
546 chunk_appendf(&trace_buf, " el=%c",
547 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
548 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100549
550 if (mask & QUIC_EV_CONN_BCFRMS) {
551 const size_t *sz1 = a2;
552 const size_t *sz2 = a3;
553 const size_t *sz3 = a4;
554
555 if (sz1)
556 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
557 if (sz2)
558 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
559 if (sz3)
560 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
561 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100562
563 if (mask & QUIC_EV_CONN_PSTRM) {
564 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100565
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100566 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100567 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100568 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100569 }
570 if (mask & QUIC_EV_CONN_LPKT) {
571 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200572 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100573
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100574 if (pkt) {
575 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
576 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
577 if (pkt->pn_node.key != (uint64_t)-1)
578 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
579 }
580
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200581 if (len)
582 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100583 }
584
585}
586
587/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100588static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100589{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200590 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100591
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100592 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593 return 1;
594
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200595 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
596 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
597 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
598 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200599 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100600 return 1;
601
602 return 0;
603}
604
605/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
606 * both loss detection and PTO and schedule the task assiated to this timer if needed.
607 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100608static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100609{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100610 struct quic_pktns *pktns;
611 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200612 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100614 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100615 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100616
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100617 pktns = quic_loss_pktns(qc);
618 if (tick_isset(pktns->tx.loss_time)) {
619 qc->timer = pktns->tx.loss_time;
620 goto out;
621 }
622
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100623 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100624 * cancelled for a server which reached the anti-amplification limit.
625 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100626 if (!quic_peer_validated_addr(qc) &&
627 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100628 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100629 qc->timer = TICK_ETERNITY;
630 goto out;
631 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100633 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100634 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100635 /* Timer cancellation. */
636 qc->timer = TICK_ETERNITY;
637 goto out;
638 }
639
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200640 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
641 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100642 if (tick_isset(pto))
643 qc->timer = pto;
644 out:
Amaury Denoyelle0a29e132021-12-23 15:06:56 +0100645 if (qc->timer_task && qc->timer != TICK_ETERNITY)
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200646 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100647 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100648}
649
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100650/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
651 * connection.
652 * Return 1 if succeeded, 0 if not.
653 */
654static int quic_tls_key_update(struct quic_conn *qc)
655{
656 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
657 struct quic_tls_secrets *rx, *tx;
658 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
659 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
660
661 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
662 rx = &tls_ctx->rx;
663 tx = &tls_ctx->tx;
664 nxt_rx = &qc->ku.nxt_rx;
665 nxt_tx = &qc->ku.nxt_tx;
666
667 /* Prepare new RX secrets */
668 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
669 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100670 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100671 return 0;
672 }
673
674 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
675 nxt_rx->key, nxt_rx->keylen,
676 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
677 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100678 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100679 return 0;
680 }
681
682 /* Prepare new TX secrets */
683 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
684 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100685 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100686 return 0;
687 }
688
689 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
690 nxt_tx->key, nxt_tx->keylen,
691 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
692 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100693 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100694 return 0;
695 }
696
697 return 1;
698}
699
700/* Rotate the Key Update information for <qc> QUIC connection.
701 * Must be used after having updated them.
702 * Always succeeds.
703 */
704static void quic_tls_rotate_keys(struct quic_conn *qc)
705{
706 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
707 unsigned char *curr_secret, *curr_iv, *curr_key;
708
709 /* Rotate the RX secrets */
710 curr_secret = tls_ctx->rx.secret;
711 curr_iv = tls_ctx->rx.iv;
712 curr_key = tls_ctx->rx.key;
713
714 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
715 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
716 tls_ctx->rx.key = qc->ku.nxt_rx.key;
717
718 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
719 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
720 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
721
722 qc->ku.prv_rx.secret = curr_secret;
723 qc->ku.prv_rx.iv = curr_iv;
724 qc->ku.prv_rx.key = curr_key;
725 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
726
727 /* Update the TX secrets */
728 curr_secret = tls_ctx->tx.secret;
729 curr_iv = tls_ctx->tx.iv;
730 curr_key = tls_ctx->tx.key;
731
732 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
733 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
734 tls_ctx->tx.key = qc->ku.nxt_tx.key;
735
736 qc->ku.nxt_tx.secret = curr_secret;
737 qc->ku.nxt_tx.iv = curr_iv;
738 qc->ku.nxt_tx.key = curr_key;
739}
740
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100741#ifndef OPENSSL_IS_BORINGSSL
742int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
743 const uint8_t *read_secret,
744 const uint8_t *write_secret, size_t secret_len)
745{
746 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
747 struct quic_tls_ctx *tls_ctx =
748 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
749 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100750 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100751
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100752 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100753 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100754 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100755 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100756 goto out;
757 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100758
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100759 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100760 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100761 goto err;
762 }
763
764 rx = &tls_ctx->rx;
765 tx = &tls_ctx->tx;
766
767 rx->aead = tx->aead = tls_aead(cipher);
768 rx->md = tx->md = tls_md(cipher);
769 rx->hp = tx->hp = tls_hp(cipher);
770
771 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
772 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100773 read_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100774 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100775 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100776 }
777
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100778 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100779
780 if (!write_secret)
781 goto tp;
782
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100783 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
784 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100785 write_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100786 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100787 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100788 }
789
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100790 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100791 tp:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100792 if (objt_server(conn->target) && level == ssl_encryption_application) {
793 const unsigned char *buf;
794 size_t buflen;
795
796 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
797 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100798 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100799
800 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100801 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100802 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100803
804 if (level == ssl_encryption_application) {
805 struct quic_conn *qc = conn->qc;
806 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
807 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
808 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
809
810 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
811 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100812 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100813 goto err;
814 }
815
816 memcpy(rx->secret, read_secret, secret_len);
817 rx->secretlen = secret_len;
818 memcpy(tx->secret, write_secret, secret_len);
819 tx->secretlen = secret_len;
820 /* Initialize all the secret keys lengths */
821 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
822 /* Prepare the next key update */
823 if (!quic_tls_key_update(qc))
824 goto err;
825 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100826 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100827 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100828 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100829
830 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100831 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100832 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100833}
834#else
835/* ->set_read_secret callback to derive the RX secrets at <level> encryption
836 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500837 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100838 */
839int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
840 const SSL_CIPHER *cipher,
841 const uint8_t *secret, size_t secret_len)
842{
843 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
844 struct quic_tls_ctx *tls_ctx =
845 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
846
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100847 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100848 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100849 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100850 goto out;
851 }
852
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100853 tls_ctx->rx.aead = tls_aead(cipher);
854 tls_ctx->rx.md = tls_md(cipher);
855 tls_ctx->rx.hp = tls_hp(cipher);
856
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100857 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
858 goto err;
859
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100860 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 +0100861 tls_ctx->rx.key, tls_ctx->rx.keylen,
862 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100863 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
864 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100865 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100866 goto err;
867 }
868
869 if (objt_server(conn->target) && level == ssl_encryption_application) {
870 const unsigned char *buf;
871 size_t buflen;
872
873 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
874 if (!buflen)
875 goto err;
876
877 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
878 goto err;
879 }
880
881 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100882 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100883 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100884
885 return 1;
886
887 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100888 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100889 return 0;
890}
891
892/* ->set_write_secret callback to derive the TX secrets at <level>
893 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500894 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100895 */
896int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
897 const SSL_CIPHER *cipher,
898 const uint8_t *secret, size_t secret_len)
899{
900 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
901 struct quic_tls_ctx *tls_ctx =
902 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
903
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100904 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100905 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100906 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100907 goto out;
908 }
909
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100910 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
911 goto err;
912
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100913 tls_ctx->tx.aead = tls_aead(cipher);
914 tls_ctx->tx.md = tls_md(cipher);
915 tls_ctx->tx.hp = tls_hp(cipher);
916
917 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 +0100918 tls_ctx->tx.key, tls_ctx->tx.keylen,
919 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
921 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100922 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100923 goto err;
924 }
925
926 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100927 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100928 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100929 return 1;
930
931 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100932 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100933 return 0;
934}
935#endif
936
937/* This function copies the CRYPTO data provided by the TLS stack found at <data>
938 * with <len> as size in CRYPTO buffers dedicated to store the information about
939 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
940 * It fails only if it could not managed to allocate enough CRYPTO buffers to
941 * store all the data.
942 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
943 */
944static int quic_crypto_data_cpy(struct quic_enc_level *qel,
945 const unsigned char *data, size_t len)
946{
947 struct quic_crypto_buf **qcb;
948 /* The remaining byte to store in CRYPTO buffers. */
949 size_t cf_offset, cf_len, *nb_buf;
950 unsigned char *pos;
951
952 nb_buf = &qel->tx.crypto.nb_buf;
953 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
954 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
955 cf_len = len;
956
957 while (len) {
958 size_t to_copy, room;
959
960 pos = (*qcb)->data + (*qcb)->sz;
961 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
962 to_copy = len > room ? room : len;
963 if (to_copy) {
964 memcpy(pos, data, to_copy);
965 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
966 qel->tx.crypto.sz += to_copy;
967 (*qcb)->sz += to_copy;
968 pos += to_copy;
969 len -= to_copy;
970 data += to_copy;
971 }
972 else {
973 struct quic_crypto_buf **tmp;
974
975 tmp = realloc(qel->tx.crypto.bufs,
976 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
977 if (tmp) {
978 qel->tx.crypto.bufs = tmp;
979 qcb = &qel->tx.crypto.bufs[*nb_buf];
980 *qcb = pool_alloc(pool_head_quic_crypto_buf);
981 if (!*qcb)
982 return 0;
983
984 (*qcb)->sz = 0;
985 ++*nb_buf;
986 }
987 else {
988 break;
989 }
990 }
991 }
992
993 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
994 * have been buffered.
995 */
996 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200997 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100998 struct quic_frame *found = NULL;
999 struct mt_list *tmp1, tmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001000
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001001 /* There is at most one CRYPTO frame in this packet number
1002 * space. Let's look for it.
1003 */
1004 mt_list_for_each_entry_safe(frm, &qel->pktns->tx.frms,
1005 mt_list, tmp1, tmp2) {
1006 if (frm->type != QUIC_FT_CRYPTO)
1007 continue;
1008
1009 /* Found */
1010 found = frm;
1011 break;
1012 }
1013
1014 if (found) {
1015 found->crypto.len += cf_len;
1016 }
1017 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001018 frm = pool_alloc(pool_head_quic_frame);
1019 if (!frm)
1020 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001021
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001022 frm->type = QUIC_FT_CRYPTO;
1023 frm->crypto.offset = cf_offset;
1024 frm->crypto.len = cf_len;
1025 frm->crypto.qel = qel;
1026 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
1027 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001028 }
1029
1030 return len == 0;
1031}
1032
1033
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001034/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1035void quic_set_tls_alert(struct quic_conn *qc, int alert)
1036{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001037 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001038 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001039 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001040}
1041
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001042/* Set the application for <qc> QUIC connection.
1043 * Return 1 if succeeded, 0 if not.
1044 */
1045int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1046{
1047 const struct qcc_app_ops *app_ops;
1048
1049 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1050 app_ops = qc->qcc->app_ops = &h3_ops;
1051 }
1052 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1053 app_ops = qc->qcc->app_ops = &hq_interop_ops;
1054 }
1055 else
1056 return 0;
1057
1058 if (app_ops->init && !app_ops->init(qc->qcc))
1059 return 0;
1060
1061 if (app_ops->finalize)
1062 app_ops->finalize(qc->qcc->ctx);
1063
1064 return 1;
1065}
1066
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001067/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1068 * wants to provide the QUIC layer with CRYPTO data.
1069 * Returns 1 if succeeded, 0 if not.
1070 */
1071int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1072 const uint8_t *data, size_t len)
1073{
1074 struct connection *conn;
1075 enum quic_tls_enc_level tel;
1076 struct quic_enc_level *qel;
1077
1078 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001079 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001080 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001081 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001082 goto out;
1083 }
1084
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001085 tel = ssl_to_quic_enc_level(level);
1086 qel = &conn->qc->els[tel];
1087
1088 if (tel == -1) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001089 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001090 goto err;
1091 }
1092
1093 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001094 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095 goto err;
1096 }
1097
1098 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001099 conn->qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001100
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001101 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001102 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001103 return 1;
1104
1105 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001106 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107 return 0;
1108}
1109
1110int ha_quic_flush_flight(SSL *ssl)
1111{
1112 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1113
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001114 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc);
1115 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116
1117 return 1;
1118}
1119
1120int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1121{
1122 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1123
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001124 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001125 quic_set_tls_alert(conn->qc, alert);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001126 HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001127 return 1;
1128}
1129
1130/* QUIC TLS methods */
1131static SSL_QUIC_METHOD ha_quic_method = {
1132#ifdef OPENSSL_IS_BORINGSSL
1133 .set_read_secret = ha_set_rsec,
1134 .set_write_secret = ha_set_wsec,
1135#else
1136 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1137#endif
1138 .add_handshake_data = ha_quic_add_handshake_data,
1139 .flush_flight = ha_quic_flush_flight,
1140 .send_alert = ha_quic_send_alert,
1141};
1142
1143/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1144 * Returns an error count.
1145 */
1146int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1147{
1148 struct proxy *curproxy = bind_conf->frontend;
1149 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1150 int cfgerr = 0;
1151
1152#if 0
1153 /* XXX Did not manage to use this. */
1154 const char *ciphers =
1155 "TLS_AES_128_GCM_SHA256:"
1156 "TLS_AES_256_GCM_SHA384:"
1157 "TLS_CHACHA20_POLY1305_SHA256:"
1158 "TLS_AES_128_CCM_SHA256";
1159#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001160 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001161 long options =
1162 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1163 SSL_OP_SINGLE_ECDH_USE |
1164 SSL_OP_CIPHER_SERVER_PREFERENCE;
1165 SSL_CTX *ctx;
1166
1167 ctx = SSL_CTX_new(TLS_server_method());
1168 bind_conf->initial_ctx = ctx;
1169
1170 SSL_CTX_set_options(ctx, options);
1171#if 0
1172 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1173 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1174 "for bind '%s' at [%s:%d].\n",
1175 curproxy->id, ciphers,
1176 bind_conf->arg, bind_conf->file, bind_conf->line);
1177 cfgerr++;
1178 }
1179#endif
1180
1181 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1182 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1183 "for bind '%s' at [%s:%d].\n",
1184 curproxy->id, groups,
1185 bind_conf->arg, bind_conf->file, bind_conf->line);
1186 cfgerr++;
1187 }
1188
1189 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1190 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1191 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1192 SSL_CTX_set_default_verify_paths(ctx);
1193
1194#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1195#ifdef OPENSSL_IS_BORINGSSL
1196 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1197 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1198#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1199 if (bind_conf->ssl_conf.early_data) {
1200 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001201 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202 }
1203 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1204 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1205#else
1206 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1207#endif
1208 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1209#endif
1210 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1211
1212 return cfgerr;
1213}
1214
1215/* Decode an expected packet number from <truncated_on> its truncated value,
1216 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1217 * the number of bits used to encode this packet number (its length in bytes * 8).
1218 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1219 */
1220static uint64_t decode_packet_number(uint64_t largest_pn,
1221 uint32_t truncated_pn, unsigned int pn_nbits)
1222{
1223 uint64_t expected_pn = largest_pn + 1;
1224 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1225 uint64_t pn_hwin = pn_win / 2;
1226 uint64_t pn_mask = pn_win - 1;
1227 uint64_t candidate_pn;
1228
1229
1230 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1231 /* Note that <pn_win> > <pn_hwin>. */
1232 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1233 candidate_pn + pn_hwin <= expected_pn)
1234 return candidate_pn + pn_win;
1235
1236 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1237 return candidate_pn - pn_win;
1238
1239 return candidate_pn;
1240}
1241
1242/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1243 * cryptographic context.
1244 * <largest_pn> is the largest received packet number and <pn> the address of
1245 * the packet number field for this packet with <byte0> address of its first byte.
1246 * <end> points to one byte past the end of this packet.
1247 * Returns 1 if succeeded, 0 if not.
1248 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001249static int qc_do_rm_hp(struct quic_conn *qc,
1250 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001252 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253{
1254 int ret, outlen, i, pnlen;
1255 uint64_t packet_number;
1256 uint32_t truncated_pn = 0;
1257 unsigned char mask[5] = {0};
1258 unsigned char *sample;
1259 EVP_CIPHER_CTX *cctx;
1260 unsigned char *hp_key;
1261
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001262 /* Check there is enough data in this packet. */
1263 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001264 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 return 0;
1266 }
1267
1268 cctx = EVP_CIPHER_CTX_new();
1269 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001270 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271 return 0;
1272 }
1273
1274 ret = 0;
1275 sample = pn + QUIC_PACKET_PN_MAXLEN;
1276
1277 hp_key = tls_ctx->rx.hp_key;
1278 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1279 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1280 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001281 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001282 goto out;
1283 }
1284
1285 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1286 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1287 for (i = 0; i < pnlen; i++) {
1288 pn[i] ^= mask[i + 1];
1289 truncated_pn = (truncated_pn << 8) | pn[i];
1290 }
1291
1292 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1293 /* Store remaining information for this unprotected header */
1294 pkt->pn = packet_number;
1295 pkt->pnl = pnlen;
1296
1297 ret = 1;
1298
1299 out:
1300 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001301
1302 return ret;
1303}
1304
1305/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1306 * address, with <payload_len> as payload length, <aad> as address of
1307 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1308 * context.
1309 * Returns 1 if succeeded, 0 if not.
1310 */
1311static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1312 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001313 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314{
1315 unsigned char iv[12];
1316 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001317 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001318 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001319
1320 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001321 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001322 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323 }
1324
1325 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1326 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001327 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001328 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329 }
1330
1331 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001332
1333 err:
1334 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001335 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001336 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001337}
1338
1339/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1340 * Returns 1 if succeeded, 0 if not.
1341 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001342static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001343{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001344 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001346 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001347 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001348 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1349 unsigned char *rx_key = tls_ctx->rx.key;
1350
1351 kp_changed = 0;
1352 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1353 /* The two tested bits are not at the same position,
1354 * this is why they are first both inversed.
1355 */
1356 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1357 if (pkt->pn < tls_ctx->rx.pn) {
1358 /* The lowest packet number of a previous key phase
1359 * cannot be null if it really stores previous key phase
1360 * secrets.
1361 */
1362 if (!pkt->qc->ku.prv_rx.pn)
1363 return 0;
1364
1365 rx_iv = pkt->qc->ku.prv_rx.iv;
1366 rx_key = pkt->qc->ku.prv_rx.key;
1367 }
1368 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1369 /* Next key phase */
1370 kp_changed = 1;
1371 rx_iv = pkt->qc->ku.nxt_rx.iv;
1372 rx_key = pkt->qc->ku.nxt_rx.key;
1373 }
1374 }
1375 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001376
1377 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1378 return 0;
1379
1380 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1381 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001382 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001383 if (!ret)
1384 return 0;
1385
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001386 /* Update the keys only if the packet decryption succeeded. */
1387 if (kp_changed) {
1388 quic_tls_rotate_keys(pkt->qc);
1389 /* Toggle the Key Phase bit */
1390 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1391 /* Store the lowest packet number received for the current key phase */
1392 tls_ctx->rx.pn = pkt->pn;
1393 /* Prepare the next key update */
1394 if (!quic_tls_key_update(pkt->qc))
1395 return 0;
1396 }
1397
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001398 /* Update the packet length (required to parse the frames). */
1399 pkt->len = pkt->aad_len + ret;
1400
1401 return 1;
1402}
1403
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001404/* Remove from <qcs> stream the acknowledged frames.
1405 * Never fails.
1406 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001407static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001408{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001409 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001410 struct eb64_node *frm_node;
1411
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001412 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001413 frm_node = eb64_first(&qcs->tx.acked_frms);
1414 while (frm_node) {
1415 struct quic_stream *strm;
1416
1417 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1418 if (strm->offset.key != qcs->tx.ack_offset)
1419 break;
1420
1421 b_del(strm->buf, strm->len);
1422 qcs->tx.ack_offset += strm->len;
1423 frm_node = eb64_next(frm_node);
1424 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001425 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001426 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001427
1428 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001429}
1430
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001431/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001432static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1433 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001434{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001435 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001436
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001437 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001438 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001439 switch (frm->type) {
1440 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1441 {
1442 struct qcs *qcs = frm->stream.qcs;
1443 struct quic_stream *strm = &frm->stream;
1444
1445 if (qcs->tx.ack_offset == strm->offset.key) {
1446 b_del(strm->buf, strm->len);
1447 qcs->tx.ack_offset += strm->len;
1448 LIST_DELETE(&frm->list);
1449 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001450 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001451 }
1452 else {
1453 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1454 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001455 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001456 }
1457 break;
1458 default:
1459 LIST_DELETE(&frm->list);
1460 pool_free(pool_head_quic_frame, frm);
1461 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001462
1463 if (stream_acked) {
1464 struct qcc *qcc = qc->qcc;
1465
1466 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1467 tasklet_wakeup(qcc->subs->tasklet);
1468 qcc->subs->events &= ~SUB_RETRY_SEND;
1469 if (!qcc->subs->events)
1470 qcc->subs = NULL;
1471 }
1472 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001473}
1474
1475/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1476 * deallocating them, and their TX frames.
1477 * Returns the last node reached to be used for the next range.
1478 * May be NULL if <largest> node could not be found.
1479 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001480static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1481 struct eb_root *pkts,
1482 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001483 struct list *newly_acked_pkts,
1484 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001485 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001486{
1487 struct eb64_node *node;
1488 struct quic_tx_packet *pkt;
1489
1490 if (largest_node)
1491 node = largest_node;
1492 else {
1493 node = eb64_lookup(pkts, largest);
1494 while (!node && largest > smallest) {
1495 node = eb64_lookup(pkts, --largest);
1496 }
1497 }
1498
1499 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001500 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001501
1502 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1503 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001504 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001505 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001506 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001507 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001508 node = eb64_prev(node);
1509 eb64_delete(&pkt->pn_node);
1510 }
1511
1512 return node;
1513}
1514
1515/* Treat <frm> frame whose packet it is attached to has just been detected as non
1516 * acknowledged.
1517 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001518static inline void qc_treat_nacked_tx_frm(struct quic_conn *qc,
1519 struct quic_frame *frm,
1520 struct quic_pktns *pktns)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001521{
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001522 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001523 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001524 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001525}
1526
1527
1528/* Free the TX packets of <pkts> list */
1529static inline void free_quic_tx_pkts(struct list *pkts)
1530{
1531 struct quic_tx_packet *pkt, *tmp;
1532
1533 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001534 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001535 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001536 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001537 }
1538}
1539
1540/* Send a packet loss event nofification to the congestion controller
1541 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1542 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1543 * at <now_us> current time.
1544 * Always succeeds.
1545 */
1546static inline void qc_cc_loss_event(struct quic_conn *qc,
1547 unsigned int lost_bytes,
1548 unsigned int newest_time_sent,
1549 unsigned int period,
1550 unsigned int now_us)
1551{
1552 struct quic_cc_event ev = {
1553 .type = QUIC_CC_EVT_LOSS,
1554 .loss.now_ms = now_ms,
1555 .loss.max_ack_delay = qc->max_ack_delay,
1556 .loss.lost_bytes = lost_bytes,
1557 .loss.newest_time_sent = newest_time_sent,
1558 .loss.period = period,
1559 };
1560
1561 quic_cc_event(&qc->path->cc, &ev);
1562}
1563
1564/* Send a packet ack event nofication for each newly acked packet of
1565 * <newly_acked_pkts> list and free them.
1566 * Always succeeds.
1567 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001568static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 struct list *newly_acked_pkts)
1570{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001571 struct quic_tx_packet *pkt, *tmp;
1572 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1573
1574 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1575 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001576 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001577 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001578 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001579 ev.ack.acked = pkt->in_flight_len;
1580 ev.ack.time_sent = pkt->time_sent;
1581 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001582 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001583 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001584 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585 }
1586
1587}
1588
1589/* Handle <pkts> list of lost packets detected at <now_us> handling
1590 * their TX frames.
1591 * Send a packet loss event to the congestion controller if
1592 * in flight packet have been lost.
1593 * Also frees the packet in <pkts> list.
1594 * Never fails.
1595 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001596static inline void qc_release_lost_pkts(struct quic_conn *qc,
1597 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001598 struct list *pkts,
1599 uint64_t now_us)
1600{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001602 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001603 uint64_t lost_bytes;
1604
1605 lost_bytes = 0;
1606 oldest_lost = newest_lost = NULL;
1607 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1608 lost_bytes += pkt->in_flight_len;
1609 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001610 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001611 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001612 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001613 /* Treat the frames of this lost packet. */
1614 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001615 qc_treat_nacked_tx_frm(qc, frm, pktns);
Willy Tarreau2b718102021-04-21 07:32:39 +02001616 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001617 if (!oldest_lost) {
1618 oldest_lost = newest_lost = pkt;
1619 }
1620 else {
1621 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001622 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001623 newest_lost = pkt;
1624 }
1625 }
1626
1627 if (lost_bytes) {
1628 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001629 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001630 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001631 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001632 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001633 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 }
1635}
1636
1637/* Look for packet loss from sent packets for <qel> encryption level of a
1638 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1639 * their tree if deemed as lost or set the <loss_time> value the packet number
1640 * space if any not deemed lost.
1641 * Should be called after having received an ACK frame with newly acknowledged
1642 * packets or when the the loss detection timer has expired.
1643 * Always succeeds.
1644 */
1645static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1646 struct quic_conn *qc,
1647 struct list *lost_pkts)
1648{
1649 struct eb_root *pkts;
1650 struct eb64_node *node;
1651 struct quic_loss *ql;
1652 unsigned int loss_delay;
1653
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001654 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001655 pkts = &pktns->tx.pkts;
1656 pktns->tx.loss_time = TICK_ETERNITY;
1657 if (eb_is_empty(pkts))
1658 goto out;
1659
1660 ql = &qc->path->loss;
1661 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1662 loss_delay += loss_delay >> 3;
1663 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1664
1665 node = eb64_first(pkts);
1666 while (node) {
1667 struct quic_tx_packet *pkt;
1668 int64_t largest_acked_pn;
1669 unsigned int loss_time_limit, time_sent;
1670
1671 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001672 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001673 node = eb64_next(node);
1674 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1675 break;
1676
1677 time_sent = pkt->time_sent;
1678 loss_time_limit = tick_add(time_sent, loss_delay);
1679 if (tick_is_le(time_sent, now_ms) ||
1680 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1681 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001682 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001683 }
1684 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001685 if (tick_isset(pktns->tx.loss_time))
1686 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1687 else
1688 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001689 }
1690 }
1691
1692 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001693 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001694}
1695
1696/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1697 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001698 * 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 +01001699 * acked ack-eliciting packet.
1700 * Return 1, if succeeded, 0 if not.
1701 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001702static inline int qc_parse_ack_frm(struct quic_conn *qc,
1703 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001704 struct quic_enc_level *qel,
1705 unsigned int *rtt_sample,
1706 const unsigned char **pos, const unsigned char *end)
1707{
1708 struct quic_ack *ack = &frm->ack;
1709 uint64_t smallest, largest;
1710 struct eb_root *pkts;
1711 struct eb64_node *largest_node;
1712 unsigned int time_sent, pkt_flags;
1713 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1714 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1715
1716 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1717 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001718 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001719 goto err;
1720 }
1721
1722 if (ack->first_ack_range > ack->largest_ack) {
1723 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001724 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001725 goto err;
1726 }
1727
1728 largest = ack->largest_ack;
1729 smallest = largest - ack->first_ack_range;
1730 pkts = &qel->pktns->tx.pkts;
1731 pkt_flags = 0;
1732 largest_node = NULL;
1733 time_sent = 0;
1734
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001735 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 +01001736 largest_node = eb64_lookup(pkts, largest);
1737 if (!largest_node) {
1738 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001739 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001740 }
1741 else {
1742 time_sent = eb64_entry(&largest_node->node,
1743 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001744 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001745 }
1746
1747 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001748 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001749 do {
1750 uint64_t gap, ack_range;
1751
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001752 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1753 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001754 if (!ack->ack_range_num--)
1755 break;
1756
1757 if (!quic_dec_int(&gap, pos, end))
1758 goto err;
1759
1760 if (smallest < gap + 2) {
1761 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001762 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001763 goto err;
1764 }
1765
1766 largest = smallest - gap - 2;
1767 if (!quic_dec_int(&ack_range, pos, end))
1768 goto err;
1769
1770 if (largest < ack_range) {
1771 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001772 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001773 goto err;
1774 }
1775
1776 /* Do not use this node anymore. */
1777 largest_node = NULL;
1778 /* Next range */
1779 smallest = largest - ack_range;
1780
1781 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001782 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001783 } while (1);
1784
1785 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001786 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001787
1788 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1789 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001790 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001791 }
1792
1793 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1794 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001795 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001796 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001797 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001798 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001799 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1800 if (quic_peer_validated_addr(qc))
1801 qc->path->loss.pto_count = 0;
1802 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001803 }
1804
1805
1806 return 1;
1807
1808 err:
1809 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001810 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001811 return 0;
1812}
1813
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001814/* This function gives the detail of the SSL error. It is used only
1815 * if the debug mode and the verbose mode are activated. It dump all
1816 * the SSL error until the stack was empty.
1817 */
1818static forceinline void qc_ssl_dump_errors(struct connection *conn)
1819{
1820 if (unlikely(global.mode & MODE_DEBUG)) {
1821 while (1) {
1822 unsigned long ret;
1823
1824 ret = ERR_get_error();
1825 if (!ret)
1826 return;
1827
1828 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1829 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1830 }
1831 }
1832}
1833
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001834int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1835 const char **str, int *len);
1836
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001837/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1838 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001839 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001840 * Return 1 if succeeded, 0 if not.
1841 */
1842static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001843 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001844 const unsigned char *data, size_t len,
1845 struct quic_rx_packet *pkt,
1846 struct quic_rx_crypto_frm *cf)
1847{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001848 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001849 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001850
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001852 qc = ctx->qc;
1853
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001854 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1855
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001856 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1857 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001858 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001859 goto err;
1860 }
1861
1862 el->rx.crypto.offset += len;
1863 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001864 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001866 state = HA_ATOMIC_LOAD(&qc->state);
1867 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 ssl_err = SSL_do_handshake(ctx->ssl);
1869 if (ssl_err != 1) {
1870 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1871 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1872 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001873 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874 goto out;
1875 }
1876
1877 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001878 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001879 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001880 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001881 goto err;
1882 }
1883
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001884 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01001885 if (qc_is_listener(ctx->qc))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001886 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001888 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001889 } else {
1890 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1891 if (ssl_err != 1) {
1892 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1893 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1894 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001895 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001896 goto out;
1897 }
1898
1899 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001900 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001901 goto err;
1902 }
1903
1904 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001905 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001906 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001907
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001909 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001910 return 1;
1911
1912 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001913 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001914 return 0;
1915}
1916
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001917/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1918 * <pkt> RX packet.
1919 * Return it if succeeded, NULL if not.
1920 */
1921static inline
1922struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1923 struct quic_rx_packet *pkt)
1924{
1925 struct quic_rx_strm_frm *frm;
1926
1927 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1928 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001929 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001930 frm->len = stream_frm->len;
1931 frm->data = stream_frm->data;
1932 frm->pkt = pkt;
1933 }
1934
1935 return frm;
1936}
1937
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001938/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001939 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001940 */
1941static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1942{
1943 size_t ret;
1944
1945 ret = 0;
1946 while (strm_frm->len) {
1947 size_t try;
1948
1949 try = b_contig_space(buf);
1950 if (!try)
1951 break;
1952
1953 if (try > strm_frm->len)
1954 try = strm_frm->len;
1955 memcpy(b_tail(buf), strm_frm->data, try);
1956 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001957 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001958 b_add(buf, try);
1959 ret += try;
1960 }
1961
1962 return ret;
1963}
1964
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001965/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1966 * Also update <strm_frm> frame to reflect the data which have been consumed.
1967 */
1968static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1969 struct quic_rx_strm_frm *strm_frm)
1970{
1971 size_t ret;
1972
1973 ret = 0;
1974 while (strm_frm->len) {
1975 size_t try;
1976
1977 try = b_contig_space(buf);
1978 if (!try)
1979 break;
1980
1981 if (try > strm_frm->len)
1982 try = strm_frm->len;
1983 memcpy(b_tail(buf), strm_frm->data, try);
1984 strm_frm->len -= try;
1985 strm_frm->offset_node.key += try;
1986 b_add(buf, try);
1987 ret += try;
1988 }
1989
1990 return ret;
1991}
1992
1993/* Process as much as possible RX STREAM frames received for <qcs> */
1994static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
1995{
1996 int total;
1997 struct eb64_node *frm_node;
1998
1999 total = 0;
2000 frm_node = eb64_first(&qcs->rx.frms);
2001 while (frm_node) {
2002 int ret;
2003 struct quic_rx_strm_frm *frm;
2004
2005 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2006 if (frm->offset_node.key != qcs->rx.offset)
2007 break;
2008
2009 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2010 qcs->rx.offset += ret;
2011 total += ret;
2012 if (frm->len) {
2013 /* If there is remaining data in this frame
2014 * this is because the destination buffer is full.
2015 */
2016 break;
2017 }
2018
2019 frm_node = eb64_next(frm_node);
2020 quic_rx_packet_refdec(frm->pkt);
2021 eb64_delete(&frm->offset_node);
2022 pool_free(pool_head_quic_rx_strm_frm, frm);
2023 }
2024
2025 return total;
2026}
2027
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002028/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2029 * streams may be open. The data are copied to the stream RX buffer if possible.
2030 * If not, the STREAM frame is stored to be treated again later.
2031 * We rely on the flow control so that not to store too much STREAM frames.
2032 * Return 1 if succeeded, 0 if not.
2033 */
2034static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2035 struct quic_stream *strm_frm,
2036 struct quic_conn *qc)
2037{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002038 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002039 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002040 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002041 struct quic_rx_strm_frm *frm;
2042
2043 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2044 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002045 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002046 return 0;
2047 }
2048
2049 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002050 if (strm_frm->offset.key < strm->rx.offset) {
2051 size_t diff;
2052
2053 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2054 TRACE_PROTO("Already received STREAM data",
2055 QUIC_EV_CONN_PSTRM, qc);
2056 goto out;
2057 }
2058
2059 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2060 diff = strm->rx.offset - strm_frm->offset.key;
2061 strm_frm->offset.key = strm->rx.offset;
2062 strm_frm->len -= diff;
2063 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002064 }
2065
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002066 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002067 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002068 int ret;
2069
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002070 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002071 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002072 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002073
2074 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002075 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002076 strm->rx.offset += ret;
2077 }
2078
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002079 total += qc_treat_rx_strm_frms(strm);
2080 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 +01002081 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002082 return 0;
2083 }
2084
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002085 if (!strm_frm->len)
2086 goto out;
2087
2088 store_frm:
2089 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2090 if (!frm) {
2091 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002092 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002093 return 0;
2094 }
2095
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002096 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002097 quic_rx_packet_refinc(pkt);
2098
2099 out:
2100 return 1;
2101}
2102
2103/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2104 * streams may be open. The data are copied to the stream RX buffer if possible.
2105 * If not, the STREAM frame is stored to be treated again later.
2106 * We rely on the flow control so that not to store too much STREAM frames.
2107 * Return 1 if succeeded, 0 if not.
2108 */
2109static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2110 struct quic_stream *strm_frm,
2111 struct quic_conn *qc)
2112{
2113 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002114 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002115 struct quic_rx_strm_frm *frm;
2116 size_t strm_frm_len;
2117
2118 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2119 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002120 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002121 return 0;
2122 }
2123
2124 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002125 if (strm_frm->offset.key < strm->rx.offset) {
2126 size_t diff;
2127
2128 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2129 TRACE_PROTO("Already received STREAM data",
2130 QUIC_EV_CONN_PSTRM, qc);
2131 goto out;
2132 }
2133
2134 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2135 diff = strm->rx.offset - strm_frm->offset.key;
2136 strm_frm->offset.key = strm->rx.offset;
2137 strm_frm->len -= diff;
2138 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139 }
2140
2141 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002142 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002143 int ret;
2144
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002145 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002146 goto store_frm;
2147
2148 /* qc_strm_cpy() will modify the offset, depending on the number
2149 * of bytes copied.
2150 */
2151 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2152 /* Inform the application of the arrival of this new stream */
2153 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002154 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002155 return 0;
2156 }
2157
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002158 if (ret)
2159 qcs_notify_recv(strm);
2160
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002161 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002162 }
2163 /* Take this frame into an account for the stream flow control */
2164 strm->rx.offset += strm_frm_len;
2165 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002166 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002167 */
2168 if (!strm_frm->len)
2169 goto out;
2170
2171 store_frm:
2172 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2173 if (!frm) {
2174 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002175 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002176 return 0;
2177 }
2178
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002179 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002180 quic_rx_packet_refinc(pkt);
2181
2182 out:
2183 return 1;
2184}
2185
2186static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2187 struct quic_stream *strm_frm,
2188 struct quic_conn *qc)
2189{
2190 if (strm_frm->id & QCS_ID_DIR_BIT)
2191 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2192 else
2193 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2194}
2195
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002196/* Prepare a fast retransmission from <qel> encryption level
2197 * which must be Initial encryption level.
2198 */
2199static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2200 struct quic_conn *qc)
2201{
2202 struct eb_root *pkts = &qel->pktns->tx.pkts;
2203 struct eb64_node *node = eb64_first(pkts);
2204 struct quic_tx_packet *pkt;
2205 struct quic_frame *frm, *frmbak;
2206
2207 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002208 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002209 pkts = &qel->pktns->tx.pkts;
2210 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002211 /* Skip the empty packet (they have already been retransmitted) */
2212 while (node) {
2213 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2214 if (!LIST_ISEMPTY(&pkt->frms))
2215 break;
2216 node = eb64_next(node);
2217 }
2218
2219 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002220 return;
2221
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002222 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
2223 qc_treat_nacked_tx_frm(qc, frm, qel->pktns);
2224 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2225 qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight) {
2226 qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.pto_probe = 1;
2227 qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2228 goto start;
2229 }
2230}
2231
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002232/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2233 * as I/O handler context and <qel> as encryption level.
2234 * Returns 1 if succeeded, 0 if failed.
2235 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002236static 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 +01002237 struct quic_enc_level *qel)
2238{
2239 struct quic_frame frm;
2240 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002241 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002242 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002243
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002244 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002245 /* Skip the AAD */
2246 pos = pkt->data + pkt->aad_len;
2247 end = pkt->data + pkt->len;
2248
2249 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002250 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002251 goto err;
2252
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002253 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002254 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002255 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002256 break;
2257 case QUIC_FT_PING:
2258 break;
2259 case QUIC_FT_ACK:
2260 {
2261 unsigned int rtt_sample;
2262
2263 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002264 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002265 goto err;
2266
2267 if (rtt_sample) {
2268 unsigned int ack_delay;
2269
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002270 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002271 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2272 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2273 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002274 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002275 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002276 break;
2277 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002278 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002279 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002280 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002281 {
2282 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002283
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002284 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2285 /* XXX TO DO: <cfdebug> is used only for the traces. */
2286 struct quic_rx_crypto_frm cfdebug = { };
2287
2288 cfdebug.offset_node.key = frm.crypto.offset;
2289 cfdebug.len = frm.crypto.len;
2290 TRACE_PROTO("CRYPTO data discarded",
2291 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2292 break;
2293 }
2294
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002295 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2296 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2297 /* XXX TO DO: <cfdebug> is used only for the traces. */
2298 struct quic_rx_crypto_frm cfdebug = { };
2299
2300 cfdebug.offset_node.key = frm.crypto.offset;
2301 cfdebug.len = frm.crypto.len;
2302 /* Nothing to do */
2303 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002304 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002305 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002306 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2307 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002308 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002309 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002310 else {
2311 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2312 /* XXX TO DO: <cfdebug> is used only for the traces. */
2313 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002314
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002315 cfdebug.offset_node.key = frm.crypto.offset;
2316 cfdebug.len = frm.crypto.len;
2317 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002318 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002319 frm.crypto.len -= diff;
2320 frm.crypto.data += diff;
2321 frm.crypto.offset = qel->rx.crypto.offset;
2322 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002324
2325 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002326 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002327 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002328
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002329 cfdebug.offset_node.key = frm.crypto.offset;
2330 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002331 if (!qc_provide_cdata(qel, ctx,
2332 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002333 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002334 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002335
2336 break;
2337 }
2338
2339 /* frm.crypto.offset > qel->rx.crypto.offset */
2340 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2341 if (!cf) {
2342 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002343 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002344 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002345 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002346
2347 cf->offset_node.key = frm.crypto.offset;
2348 cf->len = frm.crypto.len;
2349 cf->data = frm.crypto.data;
2350 cf->pkt = pkt;
2351 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2352 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002353 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002354 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002355 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002356 {
2357 struct quic_stream *stream = &frm.stream;
2358
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002359 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002360 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2361 goto err;
2362 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2363 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002364
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002365 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002366 goto err;
2367
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002368 break;
2369 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002370 case QUIC_FT_MAX_DATA:
2371 case QUIC_FT_MAX_STREAM_DATA:
2372 case QUIC_FT_MAX_STREAMS_BIDI:
2373 case QUIC_FT_MAX_STREAMS_UNI:
2374 case QUIC_FT_DATA_BLOCKED:
2375 case QUIC_FT_STREAM_DATA_BLOCKED:
2376 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2377 case QUIC_FT_STREAMS_BLOCKED_UNI:
2378 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002379 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002380 break;
2381 case QUIC_FT_CONNECTION_CLOSE:
2382 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002383 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002384 qc->qcc->flags |= QC_CF_CC_RECV;
2385 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002386 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002387 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002388 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002389 goto err;
2390
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002391 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392 break;
2393 default:
2394 goto err;
2395 }
2396 }
2397
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002398 if (fast_retrans) {
2399 qc_prep_fast_retrans(qel, qc);
2400 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight)
2401 qc_prep_fast_retrans(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE], qc);
2402 }
2403
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2405 * has successfully parse a Handshake packet. The Initial encryption must also
2406 * be discarded.
2407 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002408 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002409 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002410
2411 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002412 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002413 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002414 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002415 qc_set_timer(ctx->qc);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002416 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002417 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002418 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002419 }
2420
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002421 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002422 return 1;
2423
2424 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002425 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002426 return 0;
2427}
2428
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002429/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002430 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002431 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2432 * This function must be called only after having built a correct datagram.
2433 * Always succeeds.
2434 */
2435static inline void qc_set_dg(struct cbuf *cbuf,
2436 uint16_t dglen, struct quic_tx_packet *pkt)
2437{
2438 write_u16(cb_wr(cbuf), dglen);
2439 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2440 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2441}
2442
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002443/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002444 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2445 * several packets in the same datagram. A header made of two fields is added
2446 * to each datagram: the datagram length followed by the address of the first
2447 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002448 * Returns 1 if succeeded, or 0 if something wrong happened.
2449 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002450static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2451 enum quic_tls_enc_level tel,
2452 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002453{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002454 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002455 struct cbuf *cbuf;
2456 unsigned char *end_buf, *end, *pos, *spos;
2457 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2458 /* length of datagrams */
2459 uint16_t dglen;
2460 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002461 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002462 /* Each datagram is prepended with its length followed by the
2463 * address of the first packet in the datagram.
2464 */
2465 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002466
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002467 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2468
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002469 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002470 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002471 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002472 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002473 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002474 cbuf = qr->cbuf;
2475 spos = pos = cb_wr(cbuf);
2476 /* Leave at least <dglen> bytes at the end of this buffer
2477 * to ensure there is enough room to mark the end of prepared
2478 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002479 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002480 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2481 first_pkt = prv_pkt = NULL;
2482 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002483 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002484 enum quic_pkt_type pkt_type;
2485
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002486 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002487 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002488 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2489 if (!cc) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002490 if (!prv_pkt)
2491 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002492 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002493 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002494 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002495 * 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 +01002496 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002497 * and if there is no more CRYPTO data available or in flight
2498 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002499 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002500 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002501 (!cc && !ack && !probe &&
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002502 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2503 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002504 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002505 /* Set the current datagram as prepared into <cbuf> if
2506 * the was already a correct packet which was previously written.
2507 */
2508 if (prv_pkt)
2509 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002510 break;
2511 }
2512
2513 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002514 if (!prv_pkt) {
2515 /* Leave room for the datagram header */
2516 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002517 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002518 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2519 }
2520 else {
2521 end = pos + qc->path->mtu;
2522 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002523 }
2524
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002525 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002526 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002527 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002528 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002529 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002530 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002531 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002532 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533 case -2:
2534 goto err;
2535 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002536 /* If there was already a correct packet present, set the
2537 * current datagram as prepared into <cbuf>.
2538 */
2539 if (prv_pkt) {
2540 qc_set_dg(cbuf, dglen, first_pkt);
2541 goto stop_build;
2542 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002543 goto out;
2544 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002545 break;
2546 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002547
Frédéric Lécaille63556772021-12-29 17:18:21 +01002548 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2549 if (!cur_pkt)
2550 goto err;
2551
2552 total += cur_pkt->len;
2553 /* keep trace of the first packet in the datagram */
2554 if (!first_pkt)
2555 first_pkt = cur_pkt;
2556 /* Attach the current one to the previous one */
2557 if (prv_pkt)
2558 prv_pkt->next = cur_pkt;
2559 /* Let's say we have to build a new dgram */
2560 prv_pkt = NULL;
2561 dglen += cur_pkt->len;
2562 /* Client: discard the Initial encryption keys as soon as
2563 * a handshake packet could be built.
2564 */
2565 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2566 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2567 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2568 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2569 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2570 qc_set_timer(qc);
2571 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2572 }
2573 /* If the data for the current encryption level have all been sent,
2574 * select the next level.
2575 */
2576 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
2577 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms))) {
2578 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2579 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2580 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2581 tel = next_tel;
2582 qel = &qc->els[tel];
2583 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
2584 /* If there is data for the next level, do not
2585 * consume a datagram.
2586 */
2587 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002588 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002589 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002590 /* If we have to build a new datagram, set the current datagram as
2591 * prepared into <cbuf>.
2592 */
2593 if (!prv_pkt) {
2594 qc_set_dg(cbuf, dglen, first_pkt);
2595 first_pkt = NULL;
2596 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002597 padding = 0;
2598 }
2599 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002600 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002601 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2602 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002603 }
2604 }
2605
2606 stop_build:
2607 /* Reset <wr> writer index if in front of <rd> index */
2608 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2609 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2610
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002611 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002612 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2613 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2614 /* Mark the end of contiguous data for the reader */
2615 write_u16(cb_wr(cbuf), 0);
2616 cb_add(cbuf, sizeof(uint16_t));
2617 }
2618 }
2619
2620 if (rd && rd <= cbuf->wr) {
2621 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002622 /* Let's try to reuse this buffer */
2623 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002624 }
2625 }
2626
2627 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002628 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002629 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002630
2631 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002632 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002633 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002634}
2635
2636/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002637 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002639int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640{
2641 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002642 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002643
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002644 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002645 cbuf = qr->cbuf;
2646 while (cb_contig_data(cbuf)) {
2647 unsigned char *pos;
2648 struct buffer tmpbuf = { };
2649 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2650 uint16_t dglen;
2651 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 unsigned int time_sent;
2653
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002654 pos = cb_rd(cbuf);
2655 dglen = read_u16(pos);
2656 /* End of prepared datagrams.
2657 * Reset the reader index only if in front of the writer index.
2658 */
2659 if (!dglen) {
2660 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2661
2662 if (wr && wr < cbuf->rd) {
2663 cb_rd_reset(cbuf);
2664 continue;
2665 }
2666 break;
2667 }
2668
2669 pos += sizeof dglen;
2670 first_pkt = read_ptr(pos);
2671 pos += sizeof first_pkt;
2672 tmpbuf.area = (char *)pos;
2673 tmpbuf.size = tmpbuf.data = dglen;
2674
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002675 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002676 for (pkt = first_pkt; pkt; pkt = pkt->next)
2677 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002678 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002679 &tmpbuf, tmpbuf.data, 0) <= 0) {
2680 for (pkt = first_pkt; pkt; pkt = pkt->next)
2681 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002682 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002683 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002684
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002685 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002686 qc->tx.bytes += tmpbuf.data;
2687 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002688
2689 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2690 pkt->time_sent = time_sent;
2691 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2692 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002693 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002694 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002695 qc->path->in_flight += pkt->in_flight_len;
2696 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2697 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002698 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002699 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002700 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002701 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002702 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002703 }
2704 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002705
2706 return 1;
2707}
2708
2709/* Build all the frames which must be sent just after the handshake have succeeded.
2710 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2711 * a HANDSHAKE_DONE frame.
2712 * Return 1 if succeeded, 0 if not.
2713 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002714static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002715{
2716 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002717 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002718 struct quic_frame *frm;
2719
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002720 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002721 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002722 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002723 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002724 if (!frm)
2725 return 0;
2726
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002727 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002728 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002729 }
2730
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002731 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002732 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002733 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002734
2735 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002736 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002737 goto err;
2738
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002739 cid = new_quic_cid(&qc->cids, qc, i);
2740 if (!cid)
2741 goto err;
2742
2743 /* insert the allocated CID in the receiver tree */
2744 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2745 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2746 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2747
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002749 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002750 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002751 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752
2753 return 1;
2754
2755 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756 return 0;
2757}
2758
2759/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002760void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002762 struct eb64_node *n;
2763 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002764
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002765 n = eb64_first(&arngs->root);
2766 while (n) {
2767 struct eb64_node *next;
2768
2769 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2770 next = eb64_next(n);
2771 eb64_delete(n);
2772 free(ar);
2773 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002774 }
2775}
2776
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002777/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2778 * descending order.
2779 */
2780static inline size_t sack_gap(struct quic_arng_node *p,
2781 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002782{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002783 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002784}
2785
2786
2787/* Remove the last elements of <ack_ranges> list of ack range updating its
2788 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002789 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002790 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002791static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002793 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002795 last = eb64_last(&arngs->root);
2796 while (last && arngs->enc_sz > limit) {
2797 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002799 prev = eb64_prev(last);
2800 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002801 return 0;
2802
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002803 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2804 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2805 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2806 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2807 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2808 --arngs->sz;
2809 eb64_delete(last);
2810 pool_free(pool_head_quic_arng, last);
2811 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002812 }
2813
2814 return 1;
2815}
2816
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002817/* Set the encoded size of <arngs> QUIC ack ranges. */
2818static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2819{
2820 struct eb64_node *node, *next;
2821 struct quic_arng_node *ar, *ar_next;
2822
2823 node = eb64_last(&arngs->root);
2824 if (!node)
2825 return;
2826
2827 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2828 arngs->enc_sz = quic_int_getsize(ar->last) +
2829 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2830
2831 while ((next = eb64_prev(node))) {
2832 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2833 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2834 quic_int_getsize(ar_next->last - ar_next->first.key);
2835 node = next;
2836 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2837 }
2838}
2839
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002840/* Insert <ar> ack range into <argns> tree of ack ranges.
2841 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002842 */
2843static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002844struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2845 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002846{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002847 struct quic_arng_node *new_ar;
2848
2849 new_ar = pool_alloc(pool_head_quic_arng);
2850 if (new_ar) {
2851 new_ar->first.key = ar->first;
2852 new_ar->last = ar->last;
2853 eb64_insert(&arngs->root, &new_ar->first);
2854 arngs->sz++;
2855 }
2856
2857 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002858}
2859
2860/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002862 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002863 *
2864 * Descending order
2865 * ------------->
2866 * range1 range2
2867 * ..........|--------|..............|--------|
2868 * ^ ^ ^ ^
2869 * | | | |
2870 * last1 first1 last2 first2
2871 * ..........+--------+--------------+--------+......
2872 * diff1 gap12 diff2
2873 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002874 * To encode the previous list of ranges we must encode integers as follows in
2875 * descending order:
2876 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877 * with diff1 = last1 - first1
2878 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002879 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002880 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002881 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002882int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2883 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002884{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002885 struct eb64_node *le;
2886 struct quic_arng_node *new_node;
2887 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002889 new = NULL;
2890 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002891 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002892 if (!new_node)
2893 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002895 goto out;
2896 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002897
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002898 le = eb64_lookup_le(&arngs->root, ar->first);
2899 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002900 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002901 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002902 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002903
2904 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002905 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002906 else {
2907 struct quic_arng_node *le_ar =
2908 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002909
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002910 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002911 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002912 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002913
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002914 if (le_ar->last + 1 >= ar->first) {
2915 le_ar->last = ar->last;
2916 new = le;
2917 new_node = le_ar;
2918 }
2919 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002920 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002921 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002923
2924 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002925 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002926 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002927
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002928 /* Verify that the new inserted node does not overlap the nodes
2929 * which follow it.
2930 */
2931 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002932 struct eb64_node *next;
2933 struct quic_arng_node *next_node;
2934
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002935 while ((next = eb64_next(new))) {
2936 next_node =
2937 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002938 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002939 break;
2940
2941 if (next_node->last > new_node->last)
2942 new_node->last = next_node->last;
2943 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002944 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002945 /* Decrement the size of these ranges. */
2946 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002947 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002948 }
2949
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002950 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002951 quic_arngs_set_enc_sz(arngs);
2952
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002953 return 1;
2954}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002955/* Remove the header protection of packets at <el> encryption level.
2956 * Always succeeds.
2957 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002958static 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 +01002959{
2960 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002961 struct quic_rx_packet *pqpkt;
2962 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002963 struct quic_enc_level *app_qel;
2964
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002965 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
2966 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002967 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002968 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002969 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002970 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002971 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002972 goto out;
2973 }
2974 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002975 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002976 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002977 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002978 pqpkt->data, pqpkt->data + pqpkt->len)) {
2979 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 /* XXX TO DO XXX */
2981 }
2982 else {
2983 /* The AAD includes the packet number field */
2984 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2985 /* Store the packet into the tree of packets to decrypt. */
2986 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002987 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002988 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2989 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002990 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002991 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002993 MT_LIST_DELETE_SAFE(pkttmp1);
2994 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995 }
2996
2997 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002998 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999}
3000
3001/* Process all the CRYPTO frame at <el> encryption level.
3002 * Return 1 if succeeded, 0 if not.
3003 */
3004static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003005 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003006{
3007 struct eb64_node *node;
3008
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003009 node = eb64_first(&el->rx.crypto.frms);
3010 while (node) {
3011 struct quic_rx_crypto_frm *cf;
3012
3013 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3014 if (cf->offset_node.key != el->rx.crypto.offset)
3015 break;
3016
3017 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3018 goto err;
3019
3020 node = eb64_next(node);
3021 quic_rx_packet_refdec(cf->pkt);
3022 eb64_delete(&cf->offset_node);
3023 pool_free(pool_head_quic_rx_crypto_frm, cf);
3024 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025 return 1;
3026
3027 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003028 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029 return 0;
3030}
3031
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003032/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003033 * 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 +02003034 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003035 * Return 1 if succeeded, 0 if not.
3036 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003037int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3038 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003041 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003042 struct quic_conn *qc = ctx->conn->qc;
3043 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003045 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003046 qel = cur_el;
3047 next_tel:
3048 if (!qel)
3049 goto out;
3050
3051 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3052 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003053 while (node) {
3054 struct quic_rx_packet *pkt;
3055
3056 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003057 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003058 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003059 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 /* Drop the packet */
3061 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003062 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003063 }
3064 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003065 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003066 /* Drop the packet */
3067 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003068 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069 }
3070 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003071 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3072
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003073 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003074 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003075 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003076 if (pkt->pn > largest_pn)
3077 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003078 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003079 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003081 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003082 }
3083 }
3084 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003085 eb64_delete(&pkt->pn_node);
3086 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003087 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003088 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003089
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003090 /* Update the largest packet number. */
3091 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003092 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3093 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003094 goto err;
3095
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003096 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003097 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003098 qel = next_el;
3099 goto next_tel;
3100 }
3101
3102 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003103 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003104 return 1;
3105
3106 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003107 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108 return 0;
3109}
3110
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003111/* QUIC connection packet handler task. */
3112struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003113{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003114 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003115 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003116 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117 enum quic_tls_enc_level tel, next_tel;
3118 struct quic_enc_level *qel, *next_qel;
3119 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003120 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003121 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003122
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003123 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003124 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003125 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003126 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003127 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003128 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003129 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3130 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3131 /* The I/O handler has been woken up by the dgram listener
3132 * after the anti-amplification was reached.
3133 */
3134 qc_set_timer(qc);
3135 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3136 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3137 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003138 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003139 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3140 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3141 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003142 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003143 if (st >= QUIC_HS_ST_COMPLETE &&
3144 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3145 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3146 /* There may be remaining Handshake packets to treat and acknowledge. */
3147 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3148 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3149 }
3150 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003151 goto err;
3152
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003153 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003154 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155
3156 next_level:
3157 tls_ctx = &qel->tls_ctx;
3158
3159 /* If the header protection key for this level has been derived,
3160 * remove the packet header protections.
3161 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003162 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003163 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003164 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003165
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003166 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3167 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3168 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003169 goto err;
3170
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003171 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3172 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3173 qel = next_qel;
3174 next_qel = NULL;
3175 goto next_level;
3176 }
3177
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003178 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003179 if (st >= QUIC_HS_ST_COMPLETE) {
3180 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3181 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003182 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003183
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003184 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3185 QUIC_FL_TLS_SECRETS_DCD)) {
3186 /* Discard the Handshake keys. */
3187 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3188 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3189 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3190 qc_set_timer(qc);
3191 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3192 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3193 }
3194
3195 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3196 /* There may be remaining handshake to build (acks) */
3197 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3198 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003199 }
3200
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003201 if (!qr)
3202 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003203 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
3204 goto err;
3205 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003206 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003207 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003208 else if (ret == 0)
3209 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003211 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003212 goto err;
3213
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003214 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215 /* Check if there is something to do for the next level.
3216 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003217 if (next_qel && next_qel != qel &&
3218 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003219 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003220 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003221 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003222 goto next_level;
3223 }
3224
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003225 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003226 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003227 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003228
3229 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003230 if (qr)
3231 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003232 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003233 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003234}
3235
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003236/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003237static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3238{
3239 int i;
3240
3241 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3242 if (qel->tx.crypto.bufs[i]) {
3243 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3244 qel->tx.crypto.bufs[i] = NULL;
3245 }
3246 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003247 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003248}
3249
3250/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003251 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252 * Returns 1 if succeeded, 0 if not.
3253 */
3254static int quic_conn_enc_level_init(struct quic_conn *qc,
3255 enum quic_tls_enc_level level)
3256{
3257 struct quic_enc_level *qel;
3258
3259 qel = &qc->els[level];
3260 qel->level = quic_to_ssl_enc_level(level);
3261 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3262 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3263 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3264 qel->tls_ctx.rx.flags = 0;
3265 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003266 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003267
3268 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003269 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003270 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003271 qel->rx.crypto.offset = 0;
3272 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003273
3274 /* Allocate only one buffer. */
3275 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3276 if (!qel->tx.crypto.bufs)
3277 goto err;
3278
3279 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3280 if (!qel->tx.crypto.bufs[0])
3281 goto err;
3282
3283 qel->tx.crypto.bufs[0]->sz = 0;
3284 qel->tx.crypto.nb_buf = 1;
3285
3286 qel->tx.crypto.sz = 0;
3287 qel->tx.crypto.offset = 0;
3288
3289 return 1;
3290
3291 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003292 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003293 return 0;
3294}
3295
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003296/* Increment the <qc> refcount.
3297 *
3298 * This operation must be conducted when manipulating the quic_conn outside of
3299 * the connection pinned thread. These threads can only retrieve the connection
3300 * in the CID tree, so this function must be conducted under the CID lock.
3301 */
3302static inline void quic_conn_take(struct quic_conn *qc)
3303{
3304 HA_ATOMIC_INC(&qc->refcount);
3305}
3306
3307/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003308 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003309 */
3310static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003311{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003312 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003313 int i;
3314
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003315 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003316 return;
3317
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003318 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3319 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) {
3320 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3321 return;
3322 }
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003323
3324 /* remove the connection from receiver cids trees */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003325 ebmb_delete(&qc->odcid_node);
3326 ebmb_delete(&qc->scid_node);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003327 free_quic_conn_cids(qc);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003328 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003329
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003330 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003331 if (conn_ctx)
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003332 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003333
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003334 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003335 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003336
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003337 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3338 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003339 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003340}
3341
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003342void quic_close(struct connection *conn, void *xprt_ctx)
3343{
3344 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3345 struct quic_conn *qc = conn_ctx->conn->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003346
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003347 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003348 /* This task must be deleted by the connection-pinned thread. */
3349 if (qc->timer_task) {
3350 task_destroy(qc->timer_task);
3351 qc->timer_task = NULL;
3352 }
3353
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003354 if (conn_ctx->wait_event.tasklet) {
3355 tasklet_free(conn_ctx->wait_event.tasklet);
3356 conn_ctx->wait_event.tasklet = NULL;
3357 }
3358
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003359 quic_conn_drop(qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003360 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003361}
3362
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003363/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003364static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003366 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003367 struct quic_conn *qc;
3368 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003369 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003370
3371 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003372 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003373 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003374 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375 task->expire = TICK_ETERNITY;
3376 pktns = quic_loss_pktns(qc);
3377 if (tick_isset(pktns->tx.loss_time)) {
3378 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3379
3380 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3381 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003382 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3383 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003384 goto out;
3385 }
3386
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003387 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003388 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003389 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003390 if (qc_is_listener(qc) &&
Frédéric Lécailledb6a4722021-12-30 23:16:51 +01003391 pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE] &&
3392 qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight)
3393 qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.pto_probe = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003394 pktns->tx.pto_probe = 1;
3395 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003396 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003397 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3398 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3399
3400 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3401 hel->pktns->tx.pto_probe = 1;
3402 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3403 iel->pktns->tx.pto_probe = 1;
3404 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003405
3406 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3407 int j;
3408
3409 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3410 continue;
3411
3412 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3413 qc_prep_fast_retrans(&qc->els[i], qc);
3414 }
3415
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003416 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3417 qc->path->loss.pto_count++;
3418
3419 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003420 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421
3422 return task;
3423}
3424
3425/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3426 * connections used to identify the first Initial packets of client connecting
3427 * to listeners. This parameter must be NULL for QUIC connections attached
3428 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3429 * <scid> is the source connection ID with <scid_len> as length.
3430 * Returns 1 if succeeded, 0 if not.
3431 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003432static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003433 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003434 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003435{
3436 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003437 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003438 /* Initial CID. */
3439 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003440 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003441 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003442
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003443 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003444 qc = pool_zalloc(pool_head_quic_conn);
3445 if (!qc) {
3446 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3447 goto err;
3448 }
3449
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003450 HA_ATOMIC_STORE(&qc->refcount, 0);
3451
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003452 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3453 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003454 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003455 goto err;
3456 }
3457
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003458 qc->cids = EB_ROOT;
3459 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003460 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003461 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003462
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003463 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003464 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003465 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003466 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003467 qc->odcid.addrlen = dcid_addr_len;
3468 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003469
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003470 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003471 if (scid_len)
3472 memcpy(qc->dcid.data, scid, scid_len);
3473 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003474 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003475 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476 }
3477 /* QUIC Client (outgoing connection to servers) */
3478 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003479 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003480 if (dcid_len)
3481 memcpy(qc->dcid.data, dcid, dcid_len);
3482 qc->dcid.len = dcid_len;
3483 }
3484
3485 /* Initialize the output buffer */
3486 qc->obuf.pos = qc->obuf.data;
3487
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003488 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003489 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003490 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003491 goto err;
3492 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003493
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003494 /* insert the allocated CID in the receiver tree */
3495 if (server) {
3496 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3497 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3498 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3499 }
3500
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003501 /* Select our SCID which is the first CID with 0 as sequence number. */
3502 qc->scid = icid->cid;
3503
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003504 /* Packet number spaces initialization. */
3505 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3506 quic_pktns_init(&qc->pktns[i]);
3507 /* QUIC encryption level context initialization. */
3508 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003509 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003510 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003511 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003512 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003513 /* Initialize the packet number space. */
3514 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3515 }
3516
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003517 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003518 qc->tps_tls_ext = qc->version & 0xff000000 ?
3519 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3520 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 /* TX part. */
3522 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003523 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3524 qc->tx.wbuf = qc->tx.rbuf = 0;
3525 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003526 /* RX part. */
3527 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003528 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003529 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3530 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3531 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003532 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003533 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003534 goto err;
3535 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003536
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003537 /* XXX TO DO: Only one path at this time. */
3538 qc->path = &qc->paths[0];
3539 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3540
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003541 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003542
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003543 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003544
3545 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003546 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003547 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003548 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003549}
3550
3551/* Initialize the timer task of <qc> QUIC connection.
3552 * Returns 1 if succeeded, 0 if not.
3553 */
3554static int quic_conn_init_timer(struct quic_conn *qc)
3555{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003556 /* Attach this task to the same thread ID used for the connection */
3557 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 if (!qc->timer_task)
3559 return 0;
3560
3561 qc->timer = TICK_ETERNITY;
3562 qc->timer_task->process = process_timer;
3563 qc->timer_task->context = qc->conn->xprt_ctx;
3564
3565 return 1;
3566}
3567
3568/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3569 * past one byte of this buffer.
3570 */
3571static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3572 struct quic_rx_packet *pkt)
3573{
3574 unsigned char dcid_len, scid_len;
3575
3576 /* Version */
3577 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3578 return 0;
3579
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003580 /* Destination Connection ID Length */
3581 dcid_len = *(*buf)++;
3582 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3583 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3584 /* XXX MUST BE DROPPED */
3585 return 0;
3586
3587 if (dcid_len) {
3588 /* Check that the length of this received DCID matches the CID lengths
3589 * of our implementation for non Initials packets only.
3590 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003591 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3592 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003593 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594 return 0;
3595
3596 memcpy(pkt->dcid.data, *buf, dcid_len);
3597 }
3598
3599 pkt->dcid.len = dcid_len;
3600 *buf += dcid_len;
3601
3602 /* Source Connection ID Length */
3603 scid_len = *(*buf)++;
3604 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3605 /* XXX MUST BE DROPPED */
3606 return 0;
3607
3608 if (scid_len)
3609 memcpy(pkt->scid.data, *buf, scid_len);
3610 pkt->scid.len = scid_len;
3611 *buf += scid_len;
3612
3613 return 1;
3614}
3615
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003616/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3617 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3618 * encryption level matching with the packet type. <*qel> may be null if not found.
3619 * Note that <ctx> may be null (for Initial packets).
3620 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003621static 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 +02003622 struct quic_enc_level **qel)
3623{
3624 enum quic_tls_enc_level tel;
3625
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003626 tel = quic_packet_type_enc_level(pkt->type);
3627 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3628 *qel = NULL;
3629 return 0;
3630 }
3631
3632 *qel = &qc->els[tel];
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003633 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003634 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003635
3636 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003637 (tel != QUIC_TLS_ENC_LEVEL_APP ||
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003638 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003639 return 1;
3640
3641 return 0;
3642}
3643
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003644/* Insert <pkt> RX packet in its <qel> RX packets tree */
3645static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3646{
3647 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003648 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003649 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3650 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3651 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003652}
3653
3654/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003655 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3656 * byte past the end of the buffer containing this packet and <beg> the address of
3657 * the packet first byte.
3658 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3659 * Returns 1 if succeeded, 0 if not.
3660 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003661static inline int qc_try_rm_hp(struct quic_conn *qc,
3662 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003663 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003664 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003665 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666{
3667 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003668 struct quic_enc_level *qel;
3669 /* Only for traces. */
3670 struct quic_rx_packet *qpkt_trace;
3671
3672 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003673 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003674 /* The packet number is here. This is also the start minus
3675 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3676 * protection.
3677 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003678 pn = buf;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003679 if (qc_pkt_may_rm_hp(qc, pkt, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680 /* Note that the following function enables us to unprotect the packet
3681 * number and its length subsequently used to decrypt the entire
3682 * packets.
3683 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003684 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3685 qel->pktns->rx.largest_pn, pn, beg, end)) {
3686 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003687 goto err;
3688 }
3689
3690 /* The AAD includes the packet number field found at <pn>. */
3691 pkt->aad_len = pn - beg + pkt->pnl;
3692 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003694 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003695 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3696 /* If the packet number space has been discarded, this packet
3697 * will be not parsed.
3698 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003699 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003700 goto out;
3701 }
3702
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003703 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003704 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003705 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3706 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003708 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003709 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003710 goto err;
3711 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003713 *el = qel;
3714 /* No reference counter incrementation here!!! */
3715 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3716 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3717 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3718 b_add(&qc->rx.buf, pkt->len);
3719 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003720 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003721 return 1;
3722
3723 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003724 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725 return 0;
3726}
3727
3728/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3729 * Also set <*long_header> to 1 if this form is long, 0 if not.
3730 */
3731static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3732 unsigned char byte0, int *long_header)
3733{
3734 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3735 pkt->type =
3736 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3737 *long_header = 1;
3738 }
3739 else {
3740 pkt->type = QUIC_PACKET_TYPE_SHORT;
3741 *long_header = 0;
3742 }
3743}
3744
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003745/*
3746 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3747 */
3748static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3749{
3750 int j = 0, version;
3751
3752 do {
3753 version = quic_supported_version[j];
3754 if (version == pkt->version)
3755 return 1;
3756
3757 version = quic_supported_version[++j];
3758 } while(version);
3759
3760 return 0;
3761}
3762
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003763__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003764static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3765 struct quic_rx_packet *pkt,
3766 struct quic_dgram_ctx *dgram_ctx,
3767 struct sockaddr_storage *saddr)
3768{
3769 unsigned char *beg;
3770 uint64_t len;
3771 struct quic_conn *qc;
3772 struct eb_root *cids;
3773 struct ebmb_node *node;
3774 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003775 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003776 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003777 size_t b_cspace;
3778 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003779
3780 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003781 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3783 if (end <= *buf)
3784 goto err;
3785
3786 /* Fixed bit */
3787 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3788 /* XXX TO BE DISCARDED */
3789 goto err;
3790
3791 srv_conn = dgram_ctx->owner;
3792 beg = *buf;
3793 /* Header form */
3794 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3795 if (long_header) {
3796 size_t cid_lookup_len;
3797
3798 if (!quic_packet_read_long_header(buf, end, pkt))
3799 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003800
3801 /* unsupported QUIC version */
3802 if (!qc_pkt_is_supported_version(pkt)) {
3803 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3804 goto err;
3805 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003806
3807 /* For Initial packets, and for servers (QUIC clients connections),
3808 * there is no Initial connection IDs storage.
3809 */
3810 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3811 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3812 cid_lookup_len = pkt->dcid.len;
3813 }
3814 else {
3815 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003816 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003817 }
3818
3819 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3820 if (!node)
3821 goto err;
3822
3823 qc = ebmb_entry(node, struct quic_conn, scid_node);
3824
3825 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3826 qc->dcid.len = pkt->scid.len;
3827 if (pkt->scid.len)
3828 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3829 }
3830
3831 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3832 uint64_t token_len;
3833
3834 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3835 goto err;
3836
3837 /* XXX TO DO XXX 0 value means "the token is not present".
3838 * A server which sends an Initial packet must not set the token.
3839 * So, a client which receives an Initial packet with a token
3840 * MUST discard the packet or generate a connection error with
3841 * PROTOCOL_VIOLATION as type.
3842 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3843 */
3844 pkt->token_len = token_len;
3845 }
3846 }
3847 else {
3848 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003849 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003850 goto err;
3851
3852 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003853 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003854 if (!node)
3855 goto err;
3856
3857 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003858 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003859 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003860
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003861 /* Only packets packets with long headers and not RETRY or VERSION as type
3862 * have a length field.
3863 */
3864 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3865 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3866 goto err;
3867
3868 pkt->len = len;
3869 }
3870 else if (!long_header) {
3871 /* A short packet is the last one of an UDP datagram. */
3872 pkt->len = end - *buf;
3873 }
3874
3875 conn_ctx = qc->conn->xprt_ctx;
3876
3877 /* Increase the total length of this packet by the header length. */
3878 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003879
3880 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3881 * they must have the same DCID.
3882 *
3883 * This check must be done after the final update to pkt.len to
3884 * properly drop the packet on failure.
3885 */
3886 if (!dgram_ctx->dcid.len) {
3887 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3888 }
3889 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003890 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003891 goto err;
3892 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003893 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003894
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003895 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3896 b_cspace = b_contig_space(&qc->rx.buf);
3897 if (b_cspace < pkt->len) {
3898 /* Let us consume the remaining contiguous space. */
3899 b_add(&qc->rx.buf, b_cspace);
3900 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3901 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003902 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003903 goto err;
3904 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003905 }
3906
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003907 if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003908 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003909 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003910 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003911 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003912
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003913 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3914 if (pkt->aad_len)
3915 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003916 /* Wake the tasklet of the QUIC connection packet handler. */
3917 if (conn_ctx)
3918 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3919
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003920 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003921
3922 return pkt->len;
3923
3924 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003925 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003926 return -1;
3927}
3928
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003929/*
3930 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3931 * address <addr>.
3932 * Implementation of RFC9000 6. Version Negotiation
3933 *
3934 * TODO implement a rate-limiting sending of Version Negotiation packets
3935 *
3936 * Returns 0 on success else non-zero
3937 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003938static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3939 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003940{
3941 char buf[256];
3942 int i = 0, j, version;
3943 const socklen_t addrlen = get_addr_len(addr);
3944
3945 /*
3946 * header form
3947 * long header, fixed bit to 0 for Version Negotiation
3948 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003949 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3950 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003951
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003952 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003953 /* null version for Version Negotiation */
3954 buf[i++] = '\x00';
3955 buf[i++] = '\x00';
3956 buf[i++] = '\x00';
3957 buf[i++] = '\x00';
3958
3959 /* source connection id */
3960 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003961 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003962 i += pkt->scid.len;
3963
3964 /* destination connection id */
3965 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003966 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003967 i += pkt->dcid.len;
3968
3969 /* supported version */
3970 j = 0;
3971 do {
3972 version = htonl(quic_supported_version[j]);
3973 memcpy(&buf[i], &version, sizeof(version));
3974 i += sizeof(version);
3975
3976 version = quic_supported_version[++j];
3977 } while (version);
3978
3979 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3980 return 1;
3981
3982 return 0;
3983}
3984
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003985/* Generate the token to be used in Retry packets. The token is written to
3986 * <buf> which is expected to be <len> bytes.
3987 *
3988 * Various parameters are expected to be encoded in the token. For now, only
3989 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
3990 * as this CID must be repeated by the server in the transport parameters.
3991 *
3992 * TODO add the client address to validate the token origin.
3993 *
3994 * Returns the length of the encoded token or 0 on error.
3995 */
3996static int generate_retry_token(unsigned char *buf, unsigned char len,
3997 struct quic_rx_packet *pkt)
3998{
3999 const size_t token_len = 1 + pkt->dcid.len;
4000 unsigned char i = 0;
4001
4002 if (token_len > len)
4003 return 0;
4004
4005 buf[i++] = pkt->dcid.len;
4006 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4007 i += pkt->dcid.len;
4008
4009 return i;
4010}
4011
4012/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4013 * the Initial <pkt> packet.
4014 *
4015 * Returns 0 on success else non-zero.
4016 */
4017static int send_retry(int fd, struct sockaddr_storage *addr,
4018 struct quic_rx_packet *pkt)
4019{
4020 unsigned char buf[128];
4021 int i = 0, token_len;
4022 const socklen_t addrlen = get_addr_len(addr);
4023 struct quic_cid scid;
4024
4025 /* long header + fixed bit + packet type 0x3 */
4026 buf[i++] = 0xf0;
4027 /* version */
4028 buf[i++] = 0x00;
4029 buf[i++] = 0x00;
4030 buf[i++] = 0x00;
4031 buf[i++] = 0x01;
4032
4033 /* Use the SCID from <pkt> for Retry DCID. */
4034 buf[i++] = pkt->scid.len;
4035 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4036 i += pkt->scid.len;
4037
4038 /* Generate a new CID to be used as SCID for the Retry packet. */
4039 scid.len = QUIC_HAP_CID_LEN;
4040 if (RAND_bytes(scid.data, scid.len) != 1)
4041 return 1;
4042
4043 buf[i++] = scid.len;
4044 memcpy(&buf[i], scid.data, scid.len);
4045 i += scid.len;
4046
4047 /* token */
4048 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4049 return 1;
4050 i += token_len;
4051
4052 /* token integrity tag */
4053 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4054 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4055 pkt->dcid.len, buf, i)) {
4056 return 1;
4057 }
4058
4059 i += QUIC_TLS_TAG_LEN;
4060
4061 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4062 return 1;
4063
4064 return 0;
4065}
4066
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004067/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4068 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4069 * concatenated to the <pkt> DCID field.
4070 *
4071 * Returns the instance or NULL if not found.
4072 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004073static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004074 struct listener *l,
4075 struct sockaddr_storage *saddr)
4076{
4077 struct quic_conn *qc = NULL;
4078 struct ebmb_node *node;
4079 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004080 /* set if the quic_conn is found in the second DCID tree */
4081 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004082
4083 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
4084
4085 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4086 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4087 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4088 /* DCIDs of first packets coming from multiple clients may have
4089 * the same values. Let's distinguish them by concatenating the
4090 * socket addresses.
4091 */
4092 quic_cid_saddr_cat(&pkt->dcid, saddr);
4093 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
4094 pkt->dcid.len + pkt->dcid.addrlen);
4095 if (node) {
4096 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4097 goto end;
4098 }
4099 }
4100
4101 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4102 * also for INITIAL/0-RTT non-first packets with the final DCID in
4103 * used.
4104 */
4105 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
4106 if (!node)
4107 goto end;
4108
4109 id = ebmb_entry(node, struct quic_connection_id, node);
4110 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004111 found_in_dcid = 1;
4112
4113 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004114 if (qc)
4115 quic_conn_take(qc);
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004116 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004117
4118 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4119 * If already done, this is a noop.
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004120 *
4121 * node.leaf_p is first checked to avoid unnecessary locking.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004122 */
Amaury Denoyellec6fab982021-12-23 16:27:56 +01004123 if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) {
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004124 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
4125 ebmb_delete(&qc->odcid_node);
4126 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4127 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004128
4129 return qc;
4130}
4131
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004132/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4133 * will extract the parameters stored in the token : <odcid>.
4134 *
4135 * Returns 0 on success else non-zero.
4136 */
4137static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4138 struct quic_cid *odcid)
4139{
4140 uint64_t odcid_len;
4141
4142 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4143 return 1;
4144
4145 memcpy(odcid->data, token, odcid_len);
4146 odcid->len = odcid_len;
4147
4148 return 0;
4149}
4150
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004151static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004152 struct quic_rx_packet *pkt,
4153 struct quic_dgram_ctx *dgram_ctx,
4154 struct sockaddr_storage *saddr)
4155{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004156 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004157 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004158 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004159 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004160 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004161 size_t b_cspace;
4162 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004163
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004164 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004165 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004166 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004167 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004168 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4169 /* This ist only to please to traces and distinguish the
4170 * packet with parsed packet number from others.
4171 */
4172 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004173 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004174 goto err;
4175
4176 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004177 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004178 /* XXX TO BE DISCARDED */
4179 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4180 goto err;
4181 }
4182
4183 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004184 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004185 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004186 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004187 uint64_t len;
4188
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004189 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004190 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4191 goto err;
4192 }
4193
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004194 /* Retry of Version Negotiation packets are only sent by servers */
4195 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4196 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4197 goto err;
4198 }
4199
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004200 /* RFC9000 6. Version Negotiation */
4201 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004202 /* unsupported version, send Negotiation packet */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004203 if (send_version_negotiation(l->rx.fd, saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004204 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4205 goto err;
4206 }
4207
4208 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004209 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004210 }
4211
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 /* For Initial packets, and for servers (QUIC clients connections),
4213 * there is no Initial connection IDs storage.
4214 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004215 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004216 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004217
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004218 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4219 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004220 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4221 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004222 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004223
4224 /* XXX TO DO XXX 0 value means "the token is not present".
4225 * A server which sends an Initial packet must not set the token.
4226 * So, a client which receives an Initial packet with a token
4227 * MUST discard the packet or generate a connection error with
4228 * PROTOCOL_VIOLATION as type.
4229 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4230 */
4231 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004232
4233 /* TODO Retry should be automatically activated if
4234 * suspect network usage is detected.
4235 */
4236 if (!token_len && l->bind_conf->quic_force_retry) {
4237 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
4238 if (send_retry(l->rx.fd, saddr, pkt)) {
4239 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4240 goto err;
4241 }
4242
4243 goto err;
4244 }
4245 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004246 pkt->token = buf;
4247 buf += pkt->token_len;
4248 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004249 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004250 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004251 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4253 goto err;
4254 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004255 }
4256
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004257 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4258 end - buf < len) {
4259 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4260 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004261 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004262
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004263 payload = buf;
4264 pkt->len = len + payload - beg;
4265
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004266 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004267 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004268 int ipv4;
4269 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004270 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004271 const unsigned char *salt = initial_salt_v1;
4272 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004273
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004274 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004275 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004276 goto err;
4277 }
4278
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004279 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004280 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004281 qc = qc_new_conn(pkt->version, ipv4,
4282 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004283 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004284 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004285 goto err;
4286
4287 odcid = &qc->rx.params.original_destination_connection_id;
4288 /* Copy the transport parameters. */
4289 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004290
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004291 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004292 if (pkt->token_len) {
4293 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4294 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4295 goto err;
4296 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004297 /* Copy retry_source_connection_id transport parameter. */
4298 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4299 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004300 }
4301 else {
4302 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4303 odcid->len = pkt->dcid.len;
4304 }
4305
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004306 /* Copy the initial source connection ID. */
4307 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4308 qc->enc_params_len =
4309 quic_transport_params_encode(qc->enc_params,
4310 qc->enc_params + sizeof qc->enc_params,
4311 &qc->rx.params, 1);
4312 if (!qc->enc_params_len)
4313 goto err;
4314
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004315 /* NOTE: the socket address has been concatenated to the destination ID
4316 * chosen by the client for Initial packets.
4317 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004318 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4319 salt = initial_salt_draft_29;
4320 salt_len = sizeof initial_salt_draft_29;
4321 }
4322 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004323 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004324 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004325 goto err;
4326 }
4327
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004328 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004329 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004330 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4331 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004332
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004333 /* If the insertion failed, it means that another
4334 * thread has already allocated a QUIC connection for
4335 * the same CID. Liberate our allocated connection.
4336 */
4337 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004338 qc_to_purge = qc;
4339
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004340 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4341 pkt->qc = qc;
4342 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004343
4344 quic_conn_take(qc);
4345 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4346
4347 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004348 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004349 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004350 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4351 /* Try to accept a new connection. */
4352 listener_accept(l);
4353 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004354 else {
4355 quic_conn_drop(qc_to_purge);
4356 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004357 }
4358 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004359 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004360 }
4361 }
4362 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004363 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004364 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4365 goto err;
4366 }
4367
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004368 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004369 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004370 buf += QUIC_HAP_CID_LEN;
4371
4372 /* A short packet is the last one of a UDP datagram. */
4373 payload = buf;
4374 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004375
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004376 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004377 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004378 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004379 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4380 goto err;
4381 }
4382
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004383 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004384 }
4385
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004386
4387 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4388 * they must have the same DCID.
4389 *
4390 * This check must be done after the final update to pkt.len to
4391 * properly drop the packet on failure.
4392 */
4393 if (!dgram_ctx->dcid.len) {
4394 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004395 if (!quic_peer_validated_addr(qc) &&
4396 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4397 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4398 QUIC_EV_CONN_LPKT, qc);
4399 /* Reset the anti-amplification bit. It will be set again
4400 * when sending the next packet if reached again.
4401 */
4402 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4403 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4404 io_cb_wakeup = 1;
4405 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004406 }
4407 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004408 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004409 goto err;
4410 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004411 dgram_ctx->qc = qc;
4412
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004413
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004414 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004415 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004416 goto out;
4417 }
4418
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004419 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004420 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004421 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004422 b_cspace = b_contig_space(&qc->rx.buf);
4423 if (b_cspace < pkt->len) {
4424 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004425 if (b_cspace) {
4426 b_putchr(&qc->rx.buf, 0x00);
4427 b_cspace--;
4428 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004429 b_add(&qc->rx.buf, b_cspace);
4430 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4431 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004432 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004433 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004434 goto err;
4435 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004436 }
4437
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004438 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004439 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004440 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004441 goto err;
4442 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004443
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004444 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004445 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004446 if (pkt->aad_len)
4447 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004448 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004449 /* Wake up the connection packet handler task from here only if all
4450 * the contexts have been initialized, especially the mux context
4451 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4452 * will start it if these contexts for the connection are not already
4453 * initialized.
4454 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004455 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4456 if (conn_ctx && conn_ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004458
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004459 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004460
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004461 if (qc)
4462 quic_conn_drop(qc);
4463
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004464 return pkt->len;
4465
4466 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004467 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4468 * This cannot be done by this thread.
4469 */
4470 if (io_cb_wakeup) {
4471 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4472 if (conn_ctx && conn_ctx->wait_event.tasklet)
4473 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4474 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004475 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004476 if (!pkt->len)
4477 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004478 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004479 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004480
4481 if (qc)
4482 quic_conn_drop(qc);
4483
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004484 return -1;
4485}
4486
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004487/* This function builds into <buf> buffer a QUIC long packet header.
4488 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004489 */
4490static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4491 int type, size_t pn_len, struct quic_conn *conn)
4492{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004493 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004494 return 0;
4495
4496 /* #0 byte flags */
4497 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4498 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4499 /* Version */
4500 quic_write_uint32(buf, end, conn->version);
4501 *(*buf)++ = conn->dcid.len;
4502 /* Destination connection ID */
4503 if (conn->dcid.len) {
4504 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4505 *buf += conn->dcid.len;
4506 }
4507 /* Source connection ID */
4508 *(*buf)++ = conn->scid.len;
4509 if (conn->scid.len) {
4510 memcpy(*buf, conn->scid.data, conn->scid.len);
4511 *buf += conn->scid.len;
4512 }
4513
4514 return 1;
4515}
4516
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004517/* This function builds into <buf> buffer a QUIC short packet header.
4518 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004519 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004520static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4521 size_t pn_len, struct quic_conn *conn,
4522 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004524 if (end - *buf < 1 + conn->dcid.len)
4525 return 0;
4526
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004527 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004528 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4529 ((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 +01004530 /* Destination connection ID */
4531 if (conn->dcid.len) {
4532 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4533 *buf += conn->dcid.len;
4534 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004535
4536 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004537}
4538
4539/* Apply QUIC header protection to the packet with <buf> as first byte address,
4540 * <pn> as address of the Packet number field, <pnlen> being this field length
4541 * with <aead> as AEAD cipher and <key> as secret key.
4542 * Returns 1 if succeeded or 0 if failed.
4543 */
4544static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4545 const EVP_CIPHER *aead, const unsigned char *key)
4546{
4547 int i, ret, outlen;
4548 EVP_CIPHER_CTX *ctx;
4549 /* We need an IV of at least 5 bytes: one byte for bytes #0
4550 * and at most 4 bytes for the packet number
4551 */
4552 unsigned char mask[5] = {0};
4553
4554 ret = 0;
4555 ctx = EVP_CIPHER_CTX_new();
4556 if (!ctx)
4557 return 0;
4558
4559 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4560 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4561 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4562 goto out;
4563
4564 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4565 for (i = 0; i < pnlen; i++)
4566 pn[i] ^= mask[i + 1];
4567
4568 ret = 1;
4569
4570 out:
4571 EVP_CIPHER_CTX_free(ctx);
4572
4573 return ret;
4574}
4575
4576/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4577 * ACK ranges if needed to a value below <limit> in bytes.
4578 * Return 1 if succeeded, 0 if not.
4579 */
4580static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4581{
4582 size_t room, ack_delay_sz;
4583
4584 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4585 /* A frame is made of 1 byte for the frame type. */
4586 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004587 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004588 return 0;
4589
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004590 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004591}
4592
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004593/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4594 * 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 +01004595 * and <*len> the packet Length field initialized with the number of bytes already present
4596 * 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 +02004597 * <headlen> is the number of bytes already present in this packet before building frames.
4598 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004599 * Update consequently <*len> to reflect the size of these frames built
4600 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004601 * Return 1 if succeeded, 0 if not.
4602 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004603static inline int qc_build_frms(struct quic_tx_packet *pkt,
4604 size_t room, size_t *len, size_t headlen,
4605 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004606 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004607{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004608 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004609 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004610 struct mt_list *tmp1, tmp2;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004611 size_t remain = quic_path_prep_data(qc->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004612
Frédéric Lécailleea604992020-12-24 13:01:37 +01004613 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004614 if (*len > room || headlen > remain)
4615 return 0;
4616
Frédéric Lécailleea604992020-12-24 13:01:37 +01004617 /* If we are not probing we must take into an account the congestion
4618 * control window.
4619 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004620 if (!qel->pktns->tx.pto_probe)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004621 room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004622 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004623 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004624 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 +01004625 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004626 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004627
Frédéric Lécailleea604992020-12-24 13:01:37 +01004628 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004629 break;
4630
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004631 switch (cf->type) {
4632 case QUIC_FT_CRYPTO:
4633 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004634 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004635 /* Compute the length of this CRYPTO frame header */
4636 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4637 /* Compute the data length of this CRyPTO frame. */
4638 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4639 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004640 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004641 if (!dlen)
4642 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004643
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004644 /* CRYPTO frame length. */
4645 flen = hlen + quic_int_getsize(dlen) + dlen;
4646 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004647 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004648 /* Add the CRYPTO data length and its encoded length to the packet
4649 * length and the length of this length.
4650 */
4651 *len += flen;
4652 room -= flen;
4653 if (dlen == cf->crypto.len) {
4654 /* <cf> CRYPTO data have been consumed. */
4655 MT_LIST_DELETE_SAFE(tmp1);
4656 LIST_APPEND(&pkt->frms, &cf->list);
4657 }
4658 else {
4659 struct quic_frame *new_cf;
4660
4661 new_cf = pool_alloc(pool_head_quic_frame);
4662 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004663 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004664 return 0;
4665 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004666
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004667 new_cf->type = QUIC_FT_CRYPTO;
4668 new_cf->crypto.len = dlen;
4669 new_cf->crypto.offset = cf->crypto.offset;
4670 new_cf->crypto.qel = qel;
4671 LIST_APPEND(&pkt->frms, &new_cf->list);
4672 /* Consume <dlen> bytes of the current frame. */
4673 cf->crypto.len -= dlen;
4674 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004675 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004676 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004677
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004678 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004679 /* Note that these frames are accepted in short packets only without
4680 * "Length" packet field. Here, <*len> is used only to compute the
4681 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004682 *
4683 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004684 * excepting the variable ones. Note that +1 is for the type of this frame.
4685 */
4686 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004687 ((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 +02004688 /* Compute the data length of this STREAM frame. */
4689 avail_room = room - hlen - *len;
4690 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004691 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004692
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004693 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004694 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004695 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4696 dlen = max_available_room(avail_room, &dlen_sz);
4697 if (dlen > cf->stream.len) {
4698 dlen = cf->stream.len;
4699 }
4700 dlen_sz = quic_int_getsize(dlen);
4701 flen = hlen + dlen_sz + dlen;
4702 }
4703 else {
4704 dlen = QUIC_MIN(avail_room, cf->stream.len);
4705 flen = hlen + dlen;
4706 }
4707 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004708 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004709 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004710 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004711 /* Add the STREAM data length and its encoded length to the packet
4712 * length and the length of this length.
4713 */
4714 *len += flen;
4715 room -= flen;
4716 if (dlen == cf->stream.len) {
4717 /* <cf> STREAM data have been consumed. */
4718 MT_LIST_DELETE_SAFE(tmp1);
4719 LIST_APPEND(&pkt->frms, &cf->list);
4720 }
4721 else {
4722 struct quic_frame *new_cf;
4723
4724 new_cf = pool_zalloc(pool_head_quic_frame);
4725 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004726 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004727 return 0;
4728 }
4729
4730 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004731 new_cf->stream.qcs = cf->stream.qcs;
4732 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004733 new_cf->stream.id = cf->stream.id;
4734 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4735 new_cf->stream.offset = cf->stream.offset;
4736 new_cf->stream.len = dlen;
4737 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4738 /* FIN bit reset */
4739 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4740 new_cf->stream.data = cf->stream.data;
4741 LIST_APPEND(&pkt->frms, &new_cf->list);
4742 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4743 /* Consume <dlen> bytes of the current frame. */
4744 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004745 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004746 cf->stream.data += dlen;
4747 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004748 break;
4749
4750 default:
4751 flen = qc_frm_len(cf);
4752 BUG_ON(!flen);
4753 if (flen > room)
4754 continue;
4755
4756 *len += flen;
4757 room -= flen;
4758 MT_LIST_DELETE_SAFE(tmp1);
4759 LIST_APPEND(&pkt->frms, &cf->list);
4760 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004761 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004762 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004763 }
4764
Frédéric Lécailleea604992020-12-24 13:01:37 +01004765 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004766}
4767
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004768/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004769 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4770 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4771 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004772 * 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 +02004773 * 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 +02004774 * having returned from this function.
4775 * 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 +01004776 * number field in this packet. <pn_len> will also have the packet number
4777 * length as value.
4778 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004779 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004780 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004781static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4782 size_t dglen, struct quic_tx_packet *pkt,
4783 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4784 int ack, int padding, int cc, int nb_pto_dgrams,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004785 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004786{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004787 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004788 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004789 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4790 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004791 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004792 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004793 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004794 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004795
Frédéric Lécailleea604992020-12-24 13:01:37 +01004796 /* Length field value with CRYPTO frames if present. */
4797 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004798 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004799 /* When not probing and not acking, and no immediate close is required,
4800 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004801 * the congestion controller window. So, we do not limit the size of this
4802 * packet if we have an ACK frame to send because an ACK frame is not
4803 * ack-eliciting. This size will be limited if we have ack-eliciting
4804 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004805 */
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004806 if (!nb_pto_dgrams && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004807 size_t path_room;
4808
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004809 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004810 if (end - beg > path_room)
4811 end = beg + path_room;
4812 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004813
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004814 /* Ensure there is enough room for the TLS encryption tag and a zero token
4815 * length field if any.
4816 */
4817 if (end - pos < QUIC_TLS_TAG_LEN +
4818 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4819 goto no_room;
4820
4821 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004822 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004823 /* packet number length */
4824 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004825 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004826 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004827 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004828 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004829 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004830 goto no_room;
4831
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004832 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4833 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004834 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004835 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004836 /* Build an ACK frame if required. */
4837 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004838 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004839 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004840 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004841 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4842 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4843 * that from here, we do not know if we will have to send a PING frame.
4844 * This will be decided after having computed the ack-eliciting frames
4845 * to be added to this packet.
4846 */
4847 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 +01004848 if (!ack_frm_len)
4849 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004850 }
4851
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004852 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004853 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004854 len_frms = 0;
4855 if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004856 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004857
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004858 /* Initialize the length of the frames built below to <len>.
4859 * If any frame could be successfully built by qc_build_frms(),
4860 * we will have len_frms > len.
4861 */
4862 len_frms = len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004863 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004864 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004865 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004866 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004867 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004868
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004869 /* Length (of the remaining data). Must not fail because, the buffer size
4870 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4871 * for the encryption tag. It must be taken into an account for the length
4872 * of this packet.
4873 */
4874 if (len_frms)
4875 len = len_frms + QUIC_TLS_TAG_LEN;
4876 else
4877 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004878 /* CONNECTION_CLOSE frame */
4879 if (cc) {
4880 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004881
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004882 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004883 len += qc_frm_len(&cc_frm);
4884 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004885 add_ping_frm = 0;
4886 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004887 len_sz = quic_int_getsize(len);
4888 /* Add this packet size to <dglen> */
4889 dglen += head_len + len_sz + len;
4890 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4891 /* This is a maximum padding size */
4892 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4893 /* The length field value is of this packet is <len> + <padding_len>
4894 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004895 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004896 * sizes from <padding_len>.
4897 */
4898 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4899 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004900 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004901 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004902 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004903 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004904 add_ping_frm = 1;
4905 len += 1;
4906 }
4907 /* 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 +01004908 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004909 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4910 }
4911
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004912 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4913 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004914
4915 /* Packet number field address. */
4916 *buf_pn = pos;
4917
4918 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004919 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4920 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004921
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004922 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004923 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004924
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004925 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004926 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004927 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004928
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004929 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004930 list_for_each_entry(cf, &pkt->frms, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004931 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004932 ssize_t room = end - pos;
4933 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004934 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004935 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004936 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004937 }
4938 }
4939
4940 /* Build a PING frame if needed. */
4941 if (add_ping_frm) {
4942 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004943 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004944 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004945 }
4946
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004947 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004948 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004949 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004950
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004951 /* Build a PADDING frame if needed. */
4952 if (padding_len) {
4953 frm.type = QUIC_FT_PADDING;
4954 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004955 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004956 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004957 }
4958
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004959 /* If this packet is ack-eliciting and we are probing let's
4960 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004961 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004962 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
4963 qel->pktns->tx.pto_probe)
4964 qel->pktns->tx.pto_probe--;
4965
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004966 pkt->len = pos - beg;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004967
4968 return 1;
4969
4970 no_room:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004971 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004972 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004973}
4974
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004975static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004976{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004977 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004978 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004979 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004980 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004981 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004982 pkt->next = NULL;
4983 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004984}
4985
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004986/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004987static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4988{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004989 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004990
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004991 if (!pkt)
4992 return;
4993
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004994 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004995 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004996 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004998 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004999}
5000
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005001/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5002 * type for <qc> QUIC connection from <qel> encryption level.
5003 * Return -2 if the packet could not be allocated or encrypted for any reason,
5004 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005005 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005006static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5007 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005008 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005009 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005010 int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005011{
5012 /* The pointer to the packet number field. */
5013 unsigned char *buf_pn;
5014 unsigned char *beg, *end, *payload;
5015 int64_t pn;
5016 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005017 struct quic_tls_ctx *tls_ctx;
5018 struct quic_tx_packet *pkt;
5019
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005020 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005021 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005022 pkt = pool_alloc(pool_head_quic_tx_packet);
5023 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005024 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005025 *err = -2;
5026 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005027 }
5028
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005029 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005030 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005031 pn_len = 0;
5032 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005033
5034 pn = qel->pktns->tx.next_pn + 1;
5035 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
5036 ack, padding, cc, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005037 *err = -1;
5038 goto err;
5039 }
5040
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005041 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005042 payload = buf_pn + pn_len;
5043 payload_len = end - payload;
5044 aad_len = payload - beg;
5045
5046 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005047 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005048 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005049 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005050 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005051
5052 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005053 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005054 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5055 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005056 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005057 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005058 goto err;
5059 }
5060
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005061 /* Consume a packet number */
5062 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005063 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005064 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5065 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005066 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5067 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005068 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005069 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005070 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005071 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005072 pkt->in_flight_len = pkt->len;
5073 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005074 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005075 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005076 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005077
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005078 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005079
5080 err:
5081 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005082 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005083 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005084}
5085
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005086/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
5087 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005088 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005089static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
5090 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005091{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005092 size_t try, done = 0;
5093
5094 if (!conn_ctrl_ready(conn))
5095 return 0;
5096
5097 if (!fd_recv_ready(conn->handle.fd))
5098 return 0;
5099
5100 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101
5102 /* read the largest possible block. For this, we perform only one call
5103 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005104 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005105 */
5106 while (count > 0) {
5107 try = b_contig_space(buf);
5108 if (!try)
5109 break;
5110
5111 if (try > count)
5112 try = count;
5113
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005114 b_add(buf, try);
5115 done += try;
5116 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005117 }
5118
5119 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5120 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5121
5122 leave:
5123 return done;
5124
5125 read0:
5126 conn_sock_read0(conn);
5127 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5128
5129 /* Now a final check for a possible asynchronous low-level error
5130 * report. This can happen when a connection receives a reset
5131 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
5132 * we might have come from there by just checking POLL_HUP instead
5133 * of recv()'s return value 0, so we have no way to tell there was
5134 * an error without checking.
5135 */
Willy Tarreauf5090652021-04-06 17:23:40 +02005136 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005137 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5138 goto leave;
5139}
5140
5141
5142/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5143 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5144 * other pending data for example, but this flag is ignored at the moment.
5145 * Only one call to send() is performed, unless the buffer wraps, in which case
5146 * a second call may be performed. The connection's flags are updated with
5147 * whatever special event is detected (error, empty). The caller is responsible
5148 * for taking care of those events and avoiding the call if inappropriate. The
5149 * function does not call the connection's polling update function, so the caller
5150 * is responsible for this. It's up to the caller to update the buffer's contents
5151 * based on the return value.
5152 */
5153static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5154{
5155 ssize_t ret;
5156 size_t try, done;
5157 int send_flag;
5158
5159 if (!conn_ctrl_ready(conn))
5160 return 0;
5161
5162 if (!fd_send_ready(conn->handle.fd))
5163 return 0;
5164
5165 done = 0;
5166 /* send the largest possible block. For this we perform only one call
5167 * to send() unless the buffer wraps and we exactly fill the first hunk,
5168 * in which case we accept to do it once again.
5169 */
5170 while (count) {
5171 try = b_contig_data(buf, done);
5172 if (try > count)
5173 try = count;
5174
5175 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5176 if (try < count || flags & CO_SFL_MSG_MORE)
5177 send_flag |= MSG_MORE;
5178
5179 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
5180 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
5181 if (ret > 0) {
5182 count -= ret;
5183 done += ret;
5184
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005185 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005186 conn->flags |= CO_FL_WAIT_L4L6;
5187 /* if the system buffer is full, don't insist */
5188 if (ret < try)
5189 break;
5190 }
5191 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
5192 /* nothing written, we need to poll for write first */
5193 fd_cant_send(conn->handle.fd);
5194 break;
5195 }
5196 else if (errno != EINTR) {
5197 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5198 break;
5199 }
5200 }
5201 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5202 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5203
5204 if (done > 0) {
5205 /* we count the total bytes sent, and the send rate for 32-byte
5206 * blocks. The reason for the latter is that freq_ctr are
5207 * limited to 4GB and that it's not enough per second.
5208 */
5209 _HA_ATOMIC_ADD(&global.out_bytes, done);
5210 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5211 }
5212 return done;
5213}
5214
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005215/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5216 * event subscriber <es> is not allowed to change from a previous call as long
5217 * as at least one event is still subscribed. The <event_type> must only be a
5218 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5219 */
5220static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5221{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005222 struct qcc *qcc = conn->qc->qcc;
5223
5224 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5225 BUG_ON(qcc->subs && qcc->subs != es);
5226
5227 es->events |= event_type;
5228 qcc->subs = es;
5229
5230 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005231 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005232
5233 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005234 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005235
5236 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005237}
5238
5239/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5240 * The <es> pointer is not allowed to differ from the one passed to the
5241 * subscribe() call. It always returns zero.
5242 */
5243static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5244{
5245 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5246}
5247
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005248/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5249 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5250 * parameters of this session.
5251 * This is the responsibility of the caller to check the validity of all the
5252 * pointers passed as parameter to this function.
5253 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5254 * CO_ER_SSL_NO_MEM.
5255 */
5256static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5257 unsigned char *params, size_t params_len)
5258{
5259 int retry;
5260
5261 retry = 1;
5262 retry:
5263 *ssl = SSL_new(ssl_ctx);
5264 if (!*ssl) {
5265 if (!retry--)
5266 goto err;
5267
5268 pool_gc(NULL);
5269 goto retry;
5270 }
5271
5272 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5273 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
5274 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5275 goto err;
5276
5277 SSL_free(*ssl);
5278 *ssl = NULL;
5279 if (!retry--)
5280 goto err;
5281
5282 pool_gc(NULL);
5283 goto retry;
5284 }
5285
5286 return 0;
5287
5288 err:
5289 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5290 return -1;
5291}
5292
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005293/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5294 * connection with <xprt_ctx> as address of the xprt context.
5295 * Returns 1 if succeeded, 0 if not.
5296 */
5297static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5298{
Amaury Denoyelle9979d0d2021-12-23 16:32:24 +01005299 struct ssl_sock_ctx *ctx = NULL;
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005300 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005301
5302 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5303
5304 if (*xprt_ctx)
5305 goto out;
5306
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005307 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005308 if (!ctx) {
5309 conn->err_code = CO_ER_SYS_MEMLIM;
5310 goto err;
5311 }
5312
5313 ctx->wait_event.tasklet = tasklet_new();
5314 if (!ctx->wait_event.tasklet) {
5315 conn->err_code = CO_ER_SYS_MEMLIM;
5316 goto err;
5317 }
5318
5319 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5320 ctx->wait_event.tasklet->context = ctx;
5321 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005322 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005323 ctx->subs = NULL;
5324 ctx->xprt_ctx = NULL;
5325
5326 ctx->xprt = xprt_get(XPRT_QUIC);
5327 if (objt_server(conn->target)) {
5328 /* Server */
5329 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005330 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005331 int ssl_err, ipv4;
5332
5333 ssl_err = SSL_ERROR_NONE;
5334 if (RAND_bytes(dcid, sizeof dcid) != 1)
5335 goto err;
5336
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005337 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005338 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005339 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005340 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005341 goto err;
5342
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005343 ctx->qc = qc;
5344
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005345 /* Insert our SCID, the connection ID for the QUIC client. */
5346 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5347
5348 conn->qc = qc;
5349 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005350 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5351 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005352 goto err;
5353
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005354 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005355 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005356 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5357 qc->enc_params_len =
5358 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5359 &qc->rx.params, 0);
5360 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005361 goto err;
5362
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005363 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5364 qc->enc_params, qc->enc_params_len) == -1)
5365 goto err;
5366
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005367 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005368 SSL_set_connect_state(ctx->ssl);
5369 ssl_err = SSL_do_handshake(ctx->ssl);
5370 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005371 int st;
5372
5373 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005374 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5375 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005376 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005377 }
5378 else {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005379 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005380 goto err;
5381 }
5382 }
5383 }
5384 else if (objt_listener(conn->target)) {
5385 /* Listener */
5386 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
5387
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005388 qc = ctx->conn->qc;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005389 ctx->qc = qc;
5390
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005391 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005392 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5393 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005394 goto err;
5395
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005396 /* Enabling 0-RTT */
5397 if (bc->ssl_conf.early_data)
5398 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5399
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005400 SSL_set_accept_state(ctx->ssl);
5401 }
5402
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005403 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005404
5405 /* Leave init state and start handshake */
5406 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005407
5408 out:
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005409 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005410 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005411
5412 return 0;
5413
5414 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005415 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005416 tasklet_free(ctx->wait_event.tasklet);
5417 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005418 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005419 return -1;
5420}
5421
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005422/* Start the QUIC transport layer */
5423static int qc_xprt_start(struct connection *conn, void *ctx)
5424{
5425 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005426 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005427
5428 qc = conn->qc;
5429 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005430 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005431 return 0;
5432 }
5433
5434 tasklet_wakeup(qctx->wait_event.tasklet);
5435 return 1;
5436}
5437
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005438/* transport-layer operations for QUIC connections. */
5439static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005440 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005441 .snd_buf = quic_conn_from_buf,
5442 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005443 .subscribe = quic_conn_subscribe,
5444 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005445 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005446 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005447 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5448 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005449 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005450 .name = "QUIC",
5451};
5452
5453__attribute__((constructor))
5454static void __quic_conn_init(void)
5455{
5456 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5457 xprt_register(XPRT_QUIC, &ssl_quic);
5458}
5459
5460__attribute__((destructor))
5461static void __quic_conn_deinit(void)
5462{
5463 BIO_meth_free(ha_quic_meth);
5464}
5465
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005466/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5467 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005468 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005469 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005470static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005471 struct sockaddr_storage *saddr, qpkt_read_func *func)
5472{
5473 unsigned char *pos;
5474 const unsigned char *end;
5475 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005476 .qc = NULL,
5477 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005478 .owner = owner,
5479 };
5480
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005481 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005482 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005483 do {
5484 int ret;
5485 struct quic_rx_packet *pkt;
5486
Willy Tarreaue4498932021-03-22 21:13:05 +01005487 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005488 if (!pkt)
5489 goto err;
5490
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005491 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005492 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5493 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005494 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005495 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005496 /* If the packet length could not be found, we cannot continue. */
5497 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005498 } while (pos < end);
5499
5500 /* Increasing the received bytes counter by the UDP datagram length
5501 * if this datagram could be associated to a connection.
5502 */
5503 if (dgram_ctx.qc)
5504 dgram_ctx.qc->rx.bytes += len;
5505
Amaury Denoyellece340fe2022-01-11 14:20:46 +01005506 return pos - (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005507
5508 err:
5509 return -1;
5510}
5511
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005512ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005513 struct sockaddr_storage *saddr)
5514{
5515 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5516}
5517
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005518/* Function to automatically activate QUIC traces on stdout.
5519 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5520 * Main use for now is in the docker image for QUIC interop testing.
5521 */
5522static void quic_init_stdout_traces(void)
5523{
5524#ifdef ENABLE_QUIC_STDOUT_TRACES
5525 trace_quic.sink = sink_find("stdout");
5526 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005527 trace_quic.state = TRACE_STATE_RUNNING;
5528#endif
5529}
5530INITCALL0(STG_INIT, quic_init_stdout_traces);
5531
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005532/*
5533 * Local variables:
5534 * c-indent-level: 8
5535 * c-basic-offset: 8
5536 * End:
5537 */