blob: 4d0f75246a524a5ece8e2f8819658ba57358395e [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écaillebec186d2022-01-12 15:32:55 +01003203 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3204 * be considered.
3205 */
3206 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003207 goto err;
3208 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003209 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003211 else if (ret == 0)
3212 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003214 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215 goto err;
3216
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003217 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003218 /* Check if there is something to do for the next level.
3219 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003220 if (next_qel && next_qel != qel &&
3221 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003222 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003223 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003224 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225 goto next_level;
3226 }
3227
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003228 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003229 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003230 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003231
3232 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003233 if (qr)
3234 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003235 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003236 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003237}
3238
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003239/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3241{
3242 int i;
3243
3244 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3245 if (qel->tx.crypto.bufs[i]) {
3246 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3247 qel->tx.crypto.bufs[i] = NULL;
3248 }
3249 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003250 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003251}
3252
3253/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003254 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003255 * Returns 1 if succeeded, 0 if not.
3256 */
3257static int quic_conn_enc_level_init(struct quic_conn *qc,
3258 enum quic_tls_enc_level level)
3259{
3260 struct quic_enc_level *qel;
3261
3262 qel = &qc->els[level];
3263 qel->level = quic_to_ssl_enc_level(level);
3264 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3265 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3266 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3267 qel->tls_ctx.rx.flags = 0;
3268 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003269 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003270
3271 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003272 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003273 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003274 qel->rx.crypto.offset = 0;
3275 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276
3277 /* Allocate only one buffer. */
3278 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3279 if (!qel->tx.crypto.bufs)
3280 goto err;
3281
3282 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3283 if (!qel->tx.crypto.bufs[0])
3284 goto err;
3285
3286 qel->tx.crypto.bufs[0]->sz = 0;
3287 qel->tx.crypto.nb_buf = 1;
3288
3289 qel->tx.crypto.sz = 0;
3290 qel->tx.crypto.offset = 0;
3291
3292 return 1;
3293
3294 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003295 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003296 return 0;
3297}
3298
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003299/* Increment the <qc> refcount.
3300 *
3301 * This operation must be conducted when manipulating the quic_conn outside of
3302 * the connection pinned thread. These threads can only retrieve the connection
3303 * in the CID tree, so this function must be conducted under the CID lock.
3304 */
3305static inline void quic_conn_take(struct quic_conn *qc)
3306{
3307 HA_ATOMIC_INC(&qc->refcount);
3308}
3309
3310/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003311 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003312 */
3313static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003314{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003315 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003316 int i;
3317
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003318 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003319 return;
3320
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003321 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3322 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) {
3323 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3324 return;
3325 }
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003326
3327 /* remove the connection from receiver cids trees */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003328 ebmb_delete(&qc->odcid_node);
3329 ebmb_delete(&qc->scid_node);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003330 free_quic_conn_cids(qc);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003331 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003332
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003333 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003334 if (conn_ctx)
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003335 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003336
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003337 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003338 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003339
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003340 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3341 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003342 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343}
3344
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003345void quic_close(struct connection *conn, void *xprt_ctx)
3346{
3347 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3348 struct quic_conn *qc = conn_ctx->conn->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003349
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003350 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003351 /* This task must be deleted by the connection-pinned thread. */
3352 if (qc->timer_task) {
3353 task_destroy(qc->timer_task);
3354 qc->timer_task = NULL;
3355 }
3356
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003357 if (conn_ctx->wait_event.tasklet) {
3358 tasklet_free(conn_ctx->wait_event.tasklet);
3359 conn_ctx->wait_event.tasklet = NULL;
3360 }
3361
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003362 quic_conn_drop(qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003363 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003364}
3365
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003366/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003367static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003369 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003370 struct quic_conn *qc;
3371 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003372 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003373
3374 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003375 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003376 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003377 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003378 task->expire = TICK_ETERNITY;
3379 pktns = quic_loss_pktns(qc);
3380 if (tick_isset(pktns->tx.loss_time)) {
3381 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3382
3383 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3384 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003385 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3386 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003387 goto out;
3388 }
3389
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003390 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003391 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003392 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003393 if (qc_is_listener(qc) &&
Frédéric Lécailledb6a4722021-12-30 23:16:51 +01003394 pktns == &qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE] &&
3395 qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.in_flight)
3396 qc->pktns[QUIC_TLS_PKTNS_INITIAL].tx.pto_probe = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003397 pktns->tx.pto_probe = 1;
3398 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003399 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003400 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3401 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3402
3403 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3404 hel->pktns->tx.pto_probe = 1;
3405 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3406 iel->pktns->tx.pto_probe = 1;
3407 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003408
3409 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3410 int j;
3411
3412 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3413 continue;
3414
3415 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3416 qc_prep_fast_retrans(&qc->els[i], qc);
3417 }
3418
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003419 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3420 qc->path->loss.pto_count++;
3421
3422 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003423 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003424
3425 return task;
3426}
3427
3428/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3429 * connections used to identify the first Initial packets of client connecting
3430 * to listeners. This parameter must be NULL for QUIC connections attached
3431 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3432 * <scid> is the source connection ID with <scid_len> as length.
3433 * Returns 1 if succeeded, 0 if not.
3434 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003435static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003436 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003437 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003438{
3439 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003440 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003441 /* Initial CID. */
3442 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003443 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003444 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003445
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003446 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003447 qc = pool_zalloc(pool_head_quic_conn);
3448 if (!qc) {
3449 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3450 goto err;
3451 }
3452
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003453 HA_ATOMIC_STORE(&qc->refcount, 0);
3454
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003455 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3456 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003457 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003458 goto err;
3459 }
3460
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461 qc->cids = EB_ROOT;
3462 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003463 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003464 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003465
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003466 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003467 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003468 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003469 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003470 qc->odcid.addrlen = dcid_addr_len;
3471 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003472
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003473 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003474 if (scid_len)
3475 memcpy(qc->dcid.data, scid, scid_len);
3476 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003477 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003478 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003479 }
3480 /* QUIC Client (outgoing connection to servers) */
3481 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003482 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483 if (dcid_len)
3484 memcpy(qc->dcid.data, dcid, dcid_len);
3485 qc->dcid.len = dcid_len;
3486 }
3487
3488 /* Initialize the output buffer */
3489 qc->obuf.pos = qc->obuf.data;
3490
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003491 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003492 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003493 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003494 goto err;
3495 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003496
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003497 /* insert the allocated CID in the receiver tree */
3498 if (server) {
3499 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3500 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3501 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3502 }
3503
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003504 /* Select our SCID which is the first CID with 0 as sequence number. */
3505 qc->scid = icid->cid;
3506
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003507 /* Packet number spaces initialization. */
3508 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3509 quic_pktns_init(&qc->pktns[i]);
3510 /* QUIC encryption level context initialization. */
3511 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003512 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003513 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003515 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516 /* Initialize the packet number space. */
3517 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3518 }
3519
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003520 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003521 qc->tps_tls_ext = qc->version & 0xff000000 ?
3522 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3523 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 /* TX part. */
3525 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003526 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3527 qc->tx.wbuf = qc->tx.rbuf = 0;
3528 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003529 /* RX part. */
3530 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003531 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003532 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3533 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3534 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003535 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003536 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003537 goto err;
3538 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003539
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003540 /* XXX TO DO: Only one path at this time. */
3541 qc->path = &qc->paths[0];
3542 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3543
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003544 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003545
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003546 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547
3548 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003549 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003550 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003551 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003552}
3553
3554/* Initialize the timer task of <qc> QUIC connection.
3555 * Returns 1 if succeeded, 0 if not.
3556 */
3557static int quic_conn_init_timer(struct quic_conn *qc)
3558{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003559 /* Attach this task to the same thread ID used for the connection */
3560 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561 if (!qc->timer_task)
3562 return 0;
3563
3564 qc->timer = TICK_ETERNITY;
3565 qc->timer_task->process = process_timer;
3566 qc->timer_task->context = qc->conn->xprt_ctx;
3567
3568 return 1;
3569}
3570
3571/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3572 * past one byte of this buffer.
3573 */
3574static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3575 struct quic_rx_packet *pkt)
3576{
3577 unsigned char dcid_len, scid_len;
3578
3579 /* Version */
3580 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3581 return 0;
3582
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003583 /* Destination Connection ID Length */
3584 dcid_len = *(*buf)++;
3585 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3586 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3587 /* XXX MUST BE DROPPED */
3588 return 0;
3589
3590 if (dcid_len) {
3591 /* Check that the length of this received DCID matches the CID lengths
3592 * of our implementation for non Initials packets only.
3593 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003594 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3595 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003596 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597 return 0;
3598
3599 memcpy(pkt->dcid.data, *buf, dcid_len);
3600 }
3601
3602 pkt->dcid.len = dcid_len;
3603 *buf += dcid_len;
3604
3605 /* Source Connection ID Length */
3606 scid_len = *(*buf)++;
3607 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3608 /* XXX MUST BE DROPPED */
3609 return 0;
3610
3611 if (scid_len)
3612 memcpy(pkt->scid.data, *buf, scid_len);
3613 pkt->scid.len = scid_len;
3614 *buf += scid_len;
3615
3616 return 1;
3617}
3618
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003619/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3620 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3621 * encryption level matching with the packet type. <*qel> may be null if not found.
3622 * Note that <ctx> may be null (for Initial packets).
3623 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003624static 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 +02003625 struct quic_enc_level **qel)
3626{
3627 enum quic_tls_enc_level tel;
3628
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003629 tel = quic_packet_type_enc_level(pkt->type);
3630 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3631 *qel = NULL;
3632 return 0;
3633 }
3634
3635 *qel = &qc->els[tel];
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003636 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003637 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003638
3639 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003640 (tel != QUIC_TLS_ENC_LEVEL_APP ||
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003641 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003642 return 1;
3643
3644 return 0;
3645}
3646
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003647/* Insert <pkt> RX packet in its <qel> RX packets tree */
3648static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3649{
3650 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003651 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003652 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3653 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3654 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003655}
3656
3657/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003658 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3659 * byte past the end of the buffer containing this packet and <beg> the address of
3660 * the packet first byte.
3661 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3662 * Returns 1 if succeeded, 0 if not.
3663 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003664static inline int qc_try_rm_hp(struct quic_conn *qc,
3665 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003666 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003667 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003668 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669{
3670 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003671 struct quic_enc_level *qel;
3672 /* Only for traces. */
3673 struct quic_rx_packet *qpkt_trace;
3674
3675 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003676 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677 /* The packet number is here. This is also the start minus
3678 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3679 * protection.
3680 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003681 pn = buf;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003682 if (qc_pkt_may_rm_hp(qc, pkt, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003683 /* Note that the following function enables us to unprotect the packet
3684 * number and its length subsequently used to decrypt the entire
3685 * packets.
3686 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003687 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3688 qel->pktns->rx.largest_pn, pn, beg, end)) {
3689 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003690 goto err;
3691 }
3692
3693 /* The AAD includes the packet number field found at <pn>. */
3694 pkt->aad_len = pn - beg + pkt->pnl;
3695 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003696 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003697 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003698 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3699 /* If the packet number space has been discarded, this packet
3700 * will be not parsed.
3701 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003702 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003703 goto out;
3704 }
3705
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003706 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003708 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3709 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003710 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003711 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003712 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003713 goto err;
3714 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003715
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003716 *el = qel;
3717 /* No reference counter incrementation here!!! */
3718 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3719 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3720 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3721 b_add(&qc->rx.buf, pkt->len);
3722 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003723 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724 return 1;
3725
3726 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003727 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003728 return 0;
3729}
3730
3731/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3732 * Also set <*long_header> to 1 if this form is long, 0 if not.
3733 */
3734static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3735 unsigned char byte0, int *long_header)
3736{
3737 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3738 pkt->type =
3739 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3740 *long_header = 1;
3741 }
3742 else {
3743 pkt->type = QUIC_PACKET_TYPE_SHORT;
3744 *long_header = 0;
3745 }
3746}
3747
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003748/*
3749 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3750 */
3751static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3752{
3753 int j = 0, version;
3754
3755 do {
3756 version = quic_supported_version[j];
3757 if (version == pkt->version)
3758 return 1;
3759
3760 version = quic_supported_version[++j];
3761 } while(version);
3762
3763 return 0;
3764}
3765
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003766__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3768 struct quic_rx_packet *pkt,
3769 struct quic_dgram_ctx *dgram_ctx,
3770 struct sockaddr_storage *saddr)
3771{
3772 unsigned char *beg;
3773 uint64_t len;
3774 struct quic_conn *qc;
3775 struct eb_root *cids;
3776 struct ebmb_node *node;
3777 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003778 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003779 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003780 size_t b_cspace;
3781 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782
3783 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003784 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3786 if (end <= *buf)
3787 goto err;
3788
3789 /* Fixed bit */
3790 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3791 /* XXX TO BE DISCARDED */
3792 goto err;
3793
3794 srv_conn = dgram_ctx->owner;
3795 beg = *buf;
3796 /* Header form */
3797 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3798 if (long_header) {
3799 size_t cid_lookup_len;
3800
3801 if (!quic_packet_read_long_header(buf, end, pkt))
3802 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003803
3804 /* unsupported QUIC version */
3805 if (!qc_pkt_is_supported_version(pkt)) {
3806 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3807 goto err;
3808 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003809
3810 /* For Initial packets, and for servers (QUIC clients connections),
3811 * there is no Initial connection IDs storage.
3812 */
3813 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3814 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3815 cid_lookup_len = pkt->dcid.len;
3816 }
3817 else {
3818 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003819 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003820 }
3821
3822 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3823 if (!node)
3824 goto err;
3825
3826 qc = ebmb_entry(node, struct quic_conn, scid_node);
3827
3828 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3829 qc->dcid.len = pkt->scid.len;
3830 if (pkt->scid.len)
3831 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3832 }
3833
3834 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3835 uint64_t token_len;
3836
3837 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3838 goto err;
3839
3840 /* XXX TO DO XXX 0 value means "the token is not present".
3841 * A server which sends an Initial packet must not set the token.
3842 * So, a client which receives an Initial packet with a token
3843 * MUST discard the packet or generate a connection error with
3844 * PROTOCOL_VIOLATION as type.
3845 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3846 */
3847 pkt->token_len = token_len;
3848 }
3849 }
3850 else {
3851 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003852 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003853 goto err;
3854
3855 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003856 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003857 if (!node)
3858 goto err;
3859
3860 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003861 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003862 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003863
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003864 /* Only packets packets with long headers and not RETRY or VERSION as type
3865 * have a length field.
3866 */
3867 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3868 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3869 goto err;
3870
3871 pkt->len = len;
3872 }
3873 else if (!long_header) {
3874 /* A short packet is the last one of an UDP datagram. */
3875 pkt->len = end - *buf;
3876 }
3877
3878 conn_ctx = qc->conn->xprt_ctx;
3879
3880 /* Increase the total length of this packet by the header length. */
3881 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003882
3883 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3884 * they must have the same DCID.
3885 *
3886 * This check must be done after the final update to pkt.len to
3887 * properly drop the packet on failure.
3888 */
3889 if (!dgram_ctx->dcid.len) {
3890 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3891 }
3892 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003893 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003894 goto err;
3895 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003896 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003897
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003898 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3899 b_cspace = b_contig_space(&qc->rx.buf);
3900 if (b_cspace < pkt->len) {
3901 /* Let us consume the remaining contiguous space. */
3902 b_add(&qc->rx.buf, b_cspace);
3903 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3904 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003905 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003906 goto err;
3907 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003908 }
3909
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003910 if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003911 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003912 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003913 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003914 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003915
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003916 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3917 if (pkt->aad_len)
3918 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003919 /* Wake the tasklet of the QUIC connection packet handler. */
3920 if (conn_ctx)
3921 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3922
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003923 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003924
3925 return pkt->len;
3926
3927 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003928 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003929 return -1;
3930}
3931
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003932/*
3933 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3934 * address <addr>.
3935 * Implementation of RFC9000 6. Version Negotiation
3936 *
3937 * TODO implement a rate-limiting sending of Version Negotiation packets
3938 *
3939 * Returns 0 on success else non-zero
3940 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003941static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3942 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003943{
3944 char buf[256];
3945 int i = 0, j, version;
3946 const socklen_t addrlen = get_addr_len(addr);
3947
3948 /*
3949 * header form
3950 * long header, fixed bit to 0 for Version Negotiation
3951 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003952 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3953 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003954
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003955 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003956 /* null version for Version Negotiation */
3957 buf[i++] = '\x00';
3958 buf[i++] = '\x00';
3959 buf[i++] = '\x00';
3960 buf[i++] = '\x00';
3961
3962 /* source connection id */
3963 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003964 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003965 i += pkt->scid.len;
3966
3967 /* destination connection id */
3968 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003969 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003970 i += pkt->dcid.len;
3971
3972 /* supported version */
3973 j = 0;
3974 do {
3975 version = htonl(quic_supported_version[j]);
3976 memcpy(&buf[i], &version, sizeof(version));
3977 i += sizeof(version);
3978
3979 version = quic_supported_version[++j];
3980 } while (version);
3981
3982 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3983 return 1;
3984
3985 return 0;
3986}
3987
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003988/* Generate the token to be used in Retry packets. The token is written to
3989 * <buf> which is expected to be <len> bytes.
3990 *
3991 * Various parameters are expected to be encoded in the token. For now, only
3992 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
3993 * as this CID must be repeated by the server in the transport parameters.
3994 *
3995 * TODO add the client address to validate the token origin.
3996 *
3997 * Returns the length of the encoded token or 0 on error.
3998 */
3999static int generate_retry_token(unsigned char *buf, unsigned char len,
4000 struct quic_rx_packet *pkt)
4001{
4002 const size_t token_len = 1 + pkt->dcid.len;
4003 unsigned char i = 0;
4004
4005 if (token_len > len)
4006 return 0;
4007
4008 buf[i++] = pkt->dcid.len;
4009 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4010 i += pkt->dcid.len;
4011
4012 return i;
4013}
4014
4015/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4016 * the Initial <pkt> packet.
4017 *
4018 * Returns 0 on success else non-zero.
4019 */
4020static int send_retry(int fd, struct sockaddr_storage *addr,
4021 struct quic_rx_packet *pkt)
4022{
4023 unsigned char buf[128];
4024 int i = 0, token_len;
4025 const socklen_t addrlen = get_addr_len(addr);
4026 struct quic_cid scid;
4027
4028 /* long header + fixed bit + packet type 0x3 */
4029 buf[i++] = 0xf0;
4030 /* version */
4031 buf[i++] = 0x00;
4032 buf[i++] = 0x00;
4033 buf[i++] = 0x00;
4034 buf[i++] = 0x01;
4035
4036 /* Use the SCID from <pkt> for Retry DCID. */
4037 buf[i++] = pkt->scid.len;
4038 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4039 i += pkt->scid.len;
4040
4041 /* Generate a new CID to be used as SCID for the Retry packet. */
4042 scid.len = QUIC_HAP_CID_LEN;
4043 if (RAND_bytes(scid.data, scid.len) != 1)
4044 return 1;
4045
4046 buf[i++] = scid.len;
4047 memcpy(&buf[i], scid.data, scid.len);
4048 i += scid.len;
4049
4050 /* token */
4051 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4052 return 1;
4053 i += token_len;
4054
4055 /* token integrity tag */
4056 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4057 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4058 pkt->dcid.len, buf, i)) {
4059 return 1;
4060 }
4061
4062 i += QUIC_TLS_TAG_LEN;
4063
4064 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4065 return 1;
4066
4067 return 0;
4068}
4069
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004070/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4071 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4072 * concatenated to the <pkt> DCID field.
4073 *
4074 * Returns the instance or NULL if not found.
4075 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004076static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004077 struct listener *l,
4078 struct sockaddr_storage *saddr)
4079{
4080 struct quic_conn *qc = NULL;
4081 struct ebmb_node *node;
4082 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004083 /* set if the quic_conn is found in the second DCID tree */
4084 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004085
4086 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
4087
4088 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4089 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4090 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4091 /* DCIDs of first packets coming from multiple clients may have
4092 * the same values. Let's distinguish them by concatenating the
4093 * socket addresses.
4094 */
4095 quic_cid_saddr_cat(&pkt->dcid, saddr);
4096 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
4097 pkt->dcid.len + pkt->dcid.addrlen);
4098 if (node) {
4099 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4100 goto end;
4101 }
4102 }
4103
4104 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4105 * also for INITIAL/0-RTT non-first packets with the final DCID in
4106 * used.
4107 */
4108 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
4109 if (!node)
4110 goto end;
4111
4112 id = ebmb_entry(node, struct quic_connection_id, node);
4113 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004114 found_in_dcid = 1;
4115
4116 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004117 if (qc)
4118 quic_conn_take(qc);
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004119 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004120
4121 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4122 * If already done, this is a noop.
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004123 *
4124 * node.leaf_p is first checked to avoid unnecessary locking.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004125 */
Amaury Denoyellec6fab982021-12-23 16:27:56 +01004126 if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) {
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004127 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
4128 ebmb_delete(&qc->odcid_node);
4129 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4130 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004131
4132 return qc;
4133}
4134
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004135/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4136 * will extract the parameters stored in the token : <odcid>.
4137 *
4138 * Returns 0 on success else non-zero.
4139 */
4140static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4141 struct quic_cid *odcid)
4142{
4143 uint64_t odcid_len;
4144
4145 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4146 return 1;
4147
4148 memcpy(odcid->data, token, odcid_len);
4149 odcid->len = odcid_len;
4150
4151 return 0;
4152}
4153
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004154static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004155 struct quic_rx_packet *pkt,
4156 struct quic_dgram_ctx *dgram_ctx,
4157 struct sockaddr_storage *saddr)
4158{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004159 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004160 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004161 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004162 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004163 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004164 size_t b_cspace;
4165 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004166
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004167 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004169 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004170 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004171 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4172 /* This ist only to please to traces and distinguish the
4173 * packet with parsed packet number from others.
4174 */
4175 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004176 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004177 goto err;
4178
4179 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004180 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004181 /* XXX TO BE DISCARDED */
4182 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4183 goto err;
4184 }
4185
4186 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004187 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004188 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004189 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004190 uint64_t len;
4191
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004192 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004193 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4194 goto err;
4195 }
4196
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004197 /* Retry of Version Negotiation packets are only sent by servers */
4198 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4199 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4200 goto err;
4201 }
4202
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004203 /* RFC9000 6. Version Negotiation */
4204 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004205 /* unsupported version, send Negotiation packet */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004206 if (send_version_negotiation(l->rx.fd, saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004207 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4208 goto err;
4209 }
4210
4211 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004212 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004213 }
4214
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 /* For Initial packets, and for servers (QUIC clients connections),
4216 * there is no Initial connection IDs storage.
4217 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004218 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004219 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004220
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004221 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4222 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004223 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4224 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004225 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004226
4227 /* XXX TO DO XXX 0 value means "the token is not present".
4228 * A server which sends an Initial packet must not set the token.
4229 * So, a client which receives an Initial packet with a token
4230 * MUST discard the packet or generate a connection error with
4231 * PROTOCOL_VIOLATION as type.
4232 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4233 */
4234 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004235
4236 /* TODO Retry should be automatically activated if
4237 * suspect network usage is detected.
4238 */
4239 if (!token_len && l->bind_conf->quic_force_retry) {
4240 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
4241 if (send_retry(l->rx.fd, saddr, pkt)) {
4242 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4243 goto err;
4244 }
4245
4246 goto err;
4247 }
4248 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004249 pkt->token = buf;
4250 buf += pkt->token_len;
4251 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004253 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004254 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004255 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4256 goto err;
4257 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004258 }
4259
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004260 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4261 end - buf < len) {
4262 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4263 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004264 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004265
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004266 payload = buf;
4267 pkt->len = len + payload - beg;
4268
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004269 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004270 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004271 int ipv4;
4272 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004273 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004274 const unsigned char *salt = initial_salt_v1;
4275 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004276
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004277 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004278 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004279 goto err;
4280 }
4281
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004282 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004283 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004284 qc = qc_new_conn(pkt->version, ipv4,
4285 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004286 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004287 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004288 goto err;
4289
4290 odcid = &qc->rx.params.original_destination_connection_id;
4291 /* Copy the transport parameters. */
4292 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004293
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004294 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004295 if (pkt->token_len) {
4296 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4297 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4298 goto err;
4299 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004300 /* Copy retry_source_connection_id transport parameter. */
4301 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4302 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004303 }
4304 else {
4305 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4306 odcid->len = pkt->dcid.len;
4307 }
4308
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004309 /* Copy the initial source connection ID. */
4310 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4311 qc->enc_params_len =
4312 quic_transport_params_encode(qc->enc_params,
4313 qc->enc_params + sizeof qc->enc_params,
4314 &qc->rx.params, 1);
4315 if (!qc->enc_params_len)
4316 goto err;
4317
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004318 /* NOTE: the socket address has been concatenated to the destination ID
4319 * chosen by the client for Initial packets.
4320 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004321 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4322 salt = initial_salt_draft_29;
4323 salt_len = sizeof initial_salt_draft_29;
4324 }
4325 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004326 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004327 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004328 goto err;
4329 }
4330
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004331 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004332 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004333 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4334 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004335
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004336 /* If the insertion failed, it means that another
4337 * thread has already allocated a QUIC connection for
4338 * the same CID. Liberate our allocated connection.
4339 */
4340 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004341 qc_to_purge = qc;
4342
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004343 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4344 pkt->qc = qc;
4345 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004346
4347 quic_conn_take(qc);
4348 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4349
4350 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004351 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004352 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004353 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4354 /* Try to accept a new connection. */
4355 listener_accept(l);
4356 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004357 else {
4358 quic_conn_drop(qc_to_purge);
4359 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004360 }
4361 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004362 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004363 }
4364 }
4365 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004366 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004367 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4368 goto err;
4369 }
4370
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004371 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004372 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004373 buf += QUIC_HAP_CID_LEN;
4374
4375 /* A short packet is the last one of a UDP datagram. */
4376 payload = buf;
4377 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004378
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004379 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004380 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004381 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004382 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4383 goto err;
4384 }
4385
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004386 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004387 }
4388
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004389
4390 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4391 * they must have the same DCID.
4392 *
4393 * This check must be done after the final update to pkt.len to
4394 * properly drop the packet on failure.
4395 */
4396 if (!dgram_ctx->dcid.len) {
4397 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004398 if (!quic_peer_validated_addr(qc) &&
4399 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4400 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4401 QUIC_EV_CONN_LPKT, qc);
4402 /* Reset the anti-amplification bit. It will be set again
4403 * when sending the next packet if reached again.
4404 */
4405 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4406 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4407 io_cb_wakeup = 1;
4408 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004409 }
4410 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004411 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412 goto err;
4413 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004414 dgram_ctx->qc = qc;
4415
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004416
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004417 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004418 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004419 goto out;
4420 }
4421
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004422 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004423 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004424 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004425 b_cspace = b_contig_space(&qc->rx.buf);
4426 if (b_cspace < pkt->len) {
4427 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004428 if (b_cspace) {
4429 b_putchr(&qc->rx.buf, 0x00);
4430 b_cspace--;
4431 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004432 b_add(&qc->rx.buf, b_cspace);
4433 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4434 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004435 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004436 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004437 goto err;
4438 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004439 }
4440
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004441 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004442 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004443 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004444 goto err;
4445 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004446
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004447 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004448 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004449 if (pkt->aad_len)
4450 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004451 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004452 /* Wake up the connection packet handler task from here only if all
4453 * the contexts have been initialized, especially the mux context
4454 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4455 * will start it if these contexts for the connection are not already
4456 * initialized.
4457 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004458 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4459 if (conn_ctx && conn_ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004460 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004461
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004462 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004463
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004464 if (qc)
4465 quic_conn_drop(qc);
4466
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467 return pkt->len;
4468
4469 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004470 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4471 * This cannot be done by this thread.
4472 */
4473 if (io_cb_wakeup) {
4474 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4475 if (conn_ctx && conn_ctx->wait_event.tasklet)
4476 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4477 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004478 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004479 if (!pkt->len)
4480 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004481 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004482 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004483
4484 if (qc)
4485 quic_conn_drop(qc);
4486
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004487 return -1;
4488}
4489
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004490/* This function builds into <buf> buffer a QUIC long packet header.
4491 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004492 */
4493static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4494 int type, size_t pn_len, struct quic_conn *conn)
4495{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004496 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004497 return 0;
4498
4499 /* #0 byte flags */
4500 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4501 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4502 /* Version */
4503 quic_write_uint32(buf, end, conn->version);
4504 *(*buf)++ = conn->dcid.len;
4505 /* Destination connection ID */
4506 if (conn->dcid.len) {
4507 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4508 *buf += conn->dcid.len;
4509 }
4510 /* Source connection ID */
4511 *(*buf)++ = conn->scid.len;
4512 if (conn->scid.len) {
4513 memcpy(*buf, conn->scid.data, conn->scid.len);
4514 *buf += conn->scid.len;
4515 }
4516
4517 return 1;
4518}
4519
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004520/* This function builds into <buf> buffer a QUIC short packet header.
4521 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004522 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004523static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4524 size_t pn_len, struct quic_conn *conn,
4525 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004526{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004527 if (end - *buf < 1 + conn->dcid.len)
4528 return 0;
4529
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004530 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004531 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4532 ((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 +01004533 /* Destination connection ID */
4534 if (conn->dcid.len) {
4535 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4536 *buf += conn->dcid.len;
4537 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004538
4539 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004540}
4541
4542/* Apply QUIC header protection to the packet with <buf> as first byte address,
4543 * <pn> as address of the Packet number field, <pnlen> being this field length
4544 * with <aead> as AEAD cipher and <key> as secret key.
4545 * Returns 1 if succeeded or 0 if failed.
4546 */
4547static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4548 const EVP_CIPHER *aead, const unsigned char *key)
4549{
4550 int i, ret, outlen;
4551 EVP_CIPHER_CTX *ctx;
4552 /* We need an IV of at least 5 bytes: one byte for bytes #0
4553 * and at most 4 bytes for the packet number
4554 */
4555 unsigned char mask[5] = {0};
4556
4557 ret = 0;
4558 ctx = EVP_CIPHER_CTX_new();
4559 if (!ctx)
4560 return 0;
4561
4562 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4563 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4564 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4565 goto out;
4566
4567 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4568 for (i = 0; i < pnlen; i++)
4569 pn[i] ^= mask[i + 1];
4570
4571 ret = 1;
4572
4573 out:
4574 EVP_CIPHER_CTX_free(ctx);
4575
4576 return ret;
4577}
4578
4579/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4580 * ACK ranges if needed to a value below <limit> in bytes.
4581 * Return 1 if succeeded, 0 if not.
4582 */
4583static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4584{
4585 size_t room, ack_delay_sz;
4586
4587 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4588 /* A frame is made of 1 byte for the frame type. */
4589 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004590 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004591 return 0;
4592
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004593 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004594}
4595
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004596/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4597 * 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 +01004598 * and <*len> the packet Length field initialized with the number of bytes already present
4599 * 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 +02004600 * <headlen> is the number of bytes already present in this packet before building frames.
4601 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004602 * Update consequently <*len> to reflect the size of these frames built
4603 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004604 * Return 1 if succeeded, 0 if not.
4605 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004606static inline int qc_build_frms(struct quic_tx_packet *pkt,
4607 size_t room, size_t *len, size_t headlen,
4608 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004609 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004610{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004611 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004612 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004613 struct mt_list *tmp1, tmp2;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004614 size_t remain = quic_path_prep_data(qc->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004615
Frédéric Lécailleea604992020-12-24 13:01:37 +01004616 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004617 if (*len > room || headlen > remain)
4618 return 0;
4619
Frédéric Lécailleea604992020-12-24 13:01:37 +01004620 /* If we are not probing we must take into an account the congestion
4621 * control window.
4622 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004623 if (!qel->pktns->tx.pto_probe)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004624 room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004625 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004626 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004627 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 +01004628 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004629 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004630
Frédéric Lécailleea604992020-12-24 13:01:37 +01004631 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004632 break;
4633
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004634 switch (cf->type) {
4635 case QUIC_FT_CRYPTO:
4636 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004637 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004638 /* Compute the length of this CRYPTO frame header */
4639 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4640 /* Compute the data length of this CRyPTO frame. */
4641 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4642 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004643 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004644 if (!dlen)
4645 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004646
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004647 /* CRYPTO frame length. */
4648 flen = hlen + quic_int_getsize(dlen) + dlen;
4649 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004650 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004651 /* Add the CRYPTO data length and its encoded length to the packet
4652 * length and the length of this length.
4653 */
4654 *len += flen;
4655 room -= flen;
4656 if (dlen == cf->crypto.len) {
4657 /* <cf> CRYPTO data have been consumed. */
4658 MT_LIST_DELETE_SAFE(tmp1);
4659 LIST_APPEND(&pkt->frms, &cf->list);
4660 }
4661 else {
4662 struct quic_frame *new_cf;
4663
4664 new_cf = pool_alloc(pool_head_quic_frame);
4665 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004666 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004667 return 0;
4668 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004669
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004670 new_cf->type = QUIC_FT_CRYPTO;
4671 new_cf->crypto.len = dlen;
4672 new_cf->crypto.offset = cf->crypto.offset;
4673 new_cf->crypto.qel = qel;
4674 LIST_APPEND(&pkt->frms, &new_cf->list);
4675 /* Consume <dlen> bytes of the current frame. */
4676 cf->crypto.len -= dlen;
4677 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004678 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004679 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004680
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004681 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004682 /* Note that these frames are accepted in short packets only without
4683 * "Length" packet field. Here, <*len> is used only to compute the
4684 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004685 *
4686 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004687 * excepting the variable ones. Note that +1 is for the type of this frame.
4688 */
4689 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004690 ((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 +02004691 /* Compute the data length of this STREAM frame. */
4692 avail_room = room - hlen - *len;
4693 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004694 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004695
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004696 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004697 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004698 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4699 dlen = max_available_room(avail_room, &dlen_sz);
4700 if (dlen > cf->stream.len) {
4701 dlen = cf->stream.len;
4702 }
4703 dlen_sz = quic_int_getsize(dlen);
4704 flen = hlen + dlen_sz + dlen;
4705 }
4706 else {
4707 dlen = QUIC_MIN(avail_room, cf->stream.len);
4708 flen = hlen + dlen;
4709 }
4710 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004711 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004712 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004713 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004714 /* Add the STREAM data length and its encoded length to the packet
4715 * length and the length of this length.
4716 */
4717 *len += flen;
4718 room -= flen;
4719 if (dlen == cf->stream.len) {
4720 /* <cf> STREAM data have been consumed. */
4721 MT_LIST_DELETE_SAFE(tmp1);
4722 LIST_APPEND(&pkt->frms, &cf->list);
4723 }
4724 else {
4725 struct quic_frame *new_cf;
4726
4727 new_cf = pool_zalloc(pool_head_quic_frame);
4728 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004729 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004730 return 0;
4731 }
4732
4733 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004734 new_cf->stream.qcs = cf->stream.qcs;
4735 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004736 new_cf->stream.id = cf->stream.id;
4737 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4738 new_cf->stream.offset = cf->stream.offset;
4739 new_cf->stream.len = dlen;
4740 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4741 /* FIN bit reset */
4742 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4743 new_cf->stream.data = cf->stream.data;
4744 LIST_APPEND(&pkt->frms, &new_cf->list);
4745 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4746 /* Consume <dlen> bytes of the current frame. */
4747 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004748 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004749 cf->stream.data += dlen;
4750 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004751 break;
4752
4753 default:
4754 flen = qc_frm_len(cf);
4755 BUG_ON(!flen);
4756 if (flen > room)
4757 continue;
4758
4759 *len += flen;
4760 room -= flen;
4761 MT_LIST_DELETE_SAFE(tmp1);
4762 LIST_APPEND(&pkt->frms, &cf->list);
4763 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004764 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004765 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004766 }
4767
Frédéric Lécailleea604992020-12-24 13:01:37 +01004768 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004769}
4770
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004771/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004772 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4773 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4774 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004775 * 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 +02004776 * 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 +02004777 * having returned from this function.
4778 * 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 +01004779 * number field in this packet. <pn_len> will also have the packet number
4780 * length as value.
4781 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004782 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004783 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004784static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4785 size_t dglen, struct quic_tx_packet *pkt,
4786 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4787 int ack, int padding, int cc, int nb_pto_dgrams,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004788 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004789{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004790 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004791 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004792 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4793 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004794 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004795 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004796 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004797 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004798
Frédéric Lécailleea604992020-12-24 13:01:37 +01004799 /* Length field value with CRYPTO frames if present. */
4800 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004801 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004802 /* When not probing and not acking, and no immediate close is required,
4803 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004804 * the congestion controller window. So, we do not limit the size of this
4805 * packet if we have an ACK frame to send because an ACK frame is not
4806 * ack-eliciting. This size will be limited if we have ack-eliciting
4807 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004808 */
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004809 if (!nb_pto_dgrams && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004810 size_t path_room;
4811
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004812 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004813 if (end - beg > path_room)
4814 end = beg + path_room;
4815 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004816
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004817 /* Ensure there is enough room for the TLS encryption tag and a zero token
4818 * length field if any.
4819 */
4820 if (end - pos < QUIC_TLS_TAG_LEN +
4821 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4822 goto no_room;
4823
4824 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004825 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004826 /* packet number length */
4827 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004828 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004829 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004830 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004831 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004832 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004833 goto no_room;
4834
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004835 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4836 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004837 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004838 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004839 /* Build an ACK frame if required. */
4840 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004841 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004843 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004844 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4845 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4846 * that from here, we do not know if we will have to send a PING frame.
4847 * This will be decided after having computed the ack-eliciting frames
4848 * to be added to this packet.
4849 */
4850 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 +01004851 if (!ack_frm_len)
4852 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004853 }
4854
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004855 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004856 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004857 len_frms = 0;
4858 if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004859 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004860
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004861 /* Initialize the length of the frames built below to <len>.
4862 * If any frame could be successfully built by qc_build_frms(),
4863 * we will have len_frms > len.
4864 */
4865 len_frms = len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004866 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004867 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004868 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004869 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004870 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004871
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004872 /* Length (of the remaining data). Must not fail because, the buffer size
4873 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4874 * for the encryption tag. It must be taken into an account for the length
4875 * of this packet.
4876 */
4877 if (len_frms)
4878 len = len_frms + QUIC_TLS_TAG_LEN;
4879 else
4880 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004881 /* CONNECTION_CLOSE frame */
4882 if (cc) {
4883 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004884
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004885 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004886 len += qc_frm_len(&cc_frm);
4887 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004888 add_ping_frm = 0;
4889 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004890 len_sz = quic_int_getsize(len);
4891 /* Add this packet size to <dglen> */
4892 dglen += head_len + len_sz + len;
4893 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4894 /* This is a maximum padding size */
4895 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4896 /* The length field value is of this packet is <len> + <padding_len>
4897 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004898 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004899 * sizes from <padding_len>.
4900 */
4901 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4902 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004903 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004904 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004905 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004906 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004907 add_ping_frm = 1;
4908 len += 1;
4909 }
4910 /* 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 +01004911 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004912 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4913 }
4914
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004915 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4916 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004917
4918 /* Packet number field address. */
4919 *buf_pn = pos;
4920
4921 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004922 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4923 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004924
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004925 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004926 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004927
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004928 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004929 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004930 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004931
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004932 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004933 list_for_each_entry(cf, &pkt->frms, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004934 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004935 ssize_t room = end - pos;
4936 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004937 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004938 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004939 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004940 }
4941 }
4942
4943 /* Build a PING frame if needed. */
4944 if (add_ping_frm) {
4945 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004946 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004947 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004948 }
4949
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004950 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004951 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004952 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004953
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004954 /* Build a PADDING frame if needed. */
4955 if (padding_len) {
4956 frm.type = QUIC_FT_PADDING;
4957 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004958 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004959 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004960 }
4961
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004962 /* If this packet is ack-eliciting and we are probing let's
4963 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004964 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004965 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
4966 qel->pktns->tx.pto_probe)
4967 qel->pktns->tx.pto_probe--;
4968
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004969 pkt->len = pos - beg;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004970
4971 return 1;
4972
4973 no_room:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004974 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004975 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004976}
4977
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004978static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004979{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004980 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004981 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004982 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004983 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004984 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004985 pkt->next = NULL;
4986 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004987}
4988
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004989/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004990static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4991{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004992 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004993
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004994 if (!pkt)
4995 return;
4996
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004998 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004999 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005000 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005001 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005002}
5003
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005004/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5005 * type for <qc> QUIC connection from <qel> encryption level.
5006 * Return -2 if the packet could not be allocated or encrypted for any reason,
5007 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005008 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005009static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5010 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005011 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005012 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005013 int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005014{
5015 /* The pointer to the packet number field. */
5016 unsigned char *buf_pn;
5017 unsigned char *beg, *end, *payload;
5018 int64_t pn;
5019 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005020 struct quic_tls_ctx *tls_ctx;
5021 struct quic_tx_packet *pkt;
5022
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005023 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005024 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005025 pkt = pool_alloc(pool_head_quic_tx_packet);
5026 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005027 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005028 *err = -2;
5029 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005030 }
5031
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005032 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005033 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005034 pn_len = 0;
5035 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005036
5037 pn = qel->pktns->tx.next_pn + 1;
5038 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
5039 ack, padding, cc, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005040 *err = -1;
5041 goto err;
5042 }
5043
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005044 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005045 payload = buf_pn + pn_len;
5046 payload_len = end - payload;
5047 aad_len = payload - beg;
5048
5049 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005050 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005051 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005052 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005053 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005054
5055 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005056 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005057 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5058 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005059 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005060 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005061 goto err;
5062 }
5063
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005064 /* Consume a packet number */
5065 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005066 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005067 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5068 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005069 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5070 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005071 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005072 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005073 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005074 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005075 pkt->in_flight_len = pkt->len;
5076 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005077 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005078 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005079 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005080
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005081 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082
5083 err:
5084 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005085 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005086 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005087}
5088
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005089/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
5090 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005091 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005092static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
5093 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005094{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005095 size_t try, done = 0;
5096
5097 if (!conn_ctrl_ready(conn))
5098 return 0;
5099
5100 if (!fd_recv_ready(conn->handle.fd))
5101 return 0;
5102
5103 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005104
5105 /* read the largest possible block. For this, we perform only one call
5106 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005107 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108 */
5109 while (count > 0) {
5110 try = b_contig_space(buf);
5111 if (!try)
5112 break;
5113
5114 if (try > count)
5115 try = count;
5116
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005117 b_add(buf, try);
5118 done += try;
5119 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005120 }
5121
5122 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5123 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5124
5125 leave:
5126 return done;
5127
5128 read0:
5129 conn_sock_read0(conn);
5130 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5131
5132 /* Now a final check for a possible asynchronous low-level error
5133 * report. This can happen when a connection receives a reset
5134 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
5135 * we might have come from there by just checking POLL_HUP instead
5136 * of recv()'s return value 0, so we have no way to tell there was
5137 * an error without checking.
5138 */
Willy Tarreauf5090652021-04-06 17:23:40 +02005139 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005140 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5141 goto leave;
5142}
5143
5144
5145/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5146 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5147 * other pending data for example, but this flag is ignored at the moment.
5148 * Only one call to send() is performed, unless the buffer wraps, in which case
5149 * a second call may be performed. The connection's flags are updated with
5150 * whatever special event is detected (error, empty). The caller is responsible
5151 * for taking care of those events and avoiding the call if inappropriate. The
5152 * function does not call the connection's polling update function, so the caller
5153 * is responsible for this. It's up to the caller to update the buffer's contents
5154 * based on the return value.
5155 */
5156static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5157{
5158 ssize_t ret;
5159 size_t try, done;
5160 int send_flag;
5161
5162 if (!conn_ctrl_ready(conn))
5163 return 0;
5164
5165 if (!fd_send_ready(conn->handle.fd))
5166 return 0;
5167
5168 done = 0;
5169 /* send the largest possible block. For this we perform only one call
5170 * to send() unless the buffer wraps and we exactly fill the first hunk,
5171 * in which case we accept to do it once again.
5172 */
5173 while (count) {
5174 try = b_contig_data(buf, done);
5175 if (try > count)
5176 try = count;
5177
5178 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5179 if (try < count || flags & CO_SFL_MSG_MORE)
5180 send_flag |= MSG_MORE;
5181
5182 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
5183 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
5184 if (ret > 0) {
5185 count -= ret;
5186 done += ret;
5187
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005188 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005189 conn->flags |= CO_FL_WAIT_L4L6;
5190 /* if the system buffer is full, don't insist */
5191 if (ret < try)
5192 break;
5193 }
5194 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
5195 /* nothing written, we need to poll for write first */
5196 fd_cant_send(conn->handle.fd);
5197 break;
5198 }
5199 else if (errno != EINTR) {
5200 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5201 break;
5202 }
5203 }
5204 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5205 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5206
5207 if (done > 0) {
5208 /* we count the total bytes sent, and the send rate for 32-byte
5209 * blocks. The reason for the latter is that freq_ctr are
5210 * limited to 4GB and that it's not enough per second.
5211 */
5212 _HA_ATOMIC_ADD(&global.out_bytes, done);
5213 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5214 }
5215 return done;
5216}
5217
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005218/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5219 * event subscriber <es> is not allowed to change from a previous call as long
5220 * as at least one event is still subscribed. The <event_type> must only be a
5221 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5222 */
5223static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5224{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005225 struct qcc *qcc = conn->qc->qcc;
5226
5227 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5228 BUG_ON(qcc->subs && qcc->subs != es);
5229
5230 es->events |= event_type;
5231 qcc->subs = es;
5232
5233 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005234 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005235
5236 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005237 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005238
5239 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005240}
5241
5242/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5243 * The <es> pointer is not allowed to differ from the one passed to the
5244 * subscribe() call. It always returns zero.
5245 */
5246static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5247{
5248 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5249}
5250
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005251/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5252 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5253 * parameters of this session.
5254 * This is the responsibility of the caller to check the validity of all the
5255 * pointers passed as parameter to this function.
5256 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5257 * CO_ER_SSL_NO_MEM.
5258 */
5259static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5260 unsigned char *params, size_t params_len)
5261{
5262 int retry;
5263
5264 retry = 1;
5265 retry:
5266 *ssl = SSL_new(ssl_ctx);
5267 if (!*ssl) {
5268 if (!retry--)
5269 goto err;
5270
5271 pool_gc(NULL);
5272 goto retry;
5273 }
5274
5275 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5276 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
5277 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5278 goto err;
5279
5280 SSL_free(*ssl);
5281 *ssl = NULL;
5282 if (!retry--)
5283 goto err;
5284
5285 pool_gc(NULL);
5286 goto retry;
5287 }
5288
5289 return 0;
5290
5291 err:
5292 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5293 return -1;
5294}
5295
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005296/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5297 * connection with <xprt_ctx> as address of the xprt context.
5298 * Returns 1 if succeeded, 0 if not.
5299 */
5300static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5301{
Amaury Denoyelle9979d0d2021-12-23 16:32:24 +01005302 struct ssl_sock_ctx *ctx = NULL;
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005303 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005304
5305 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5306
5307 if (*xprt_ctx)
5308 goto out;
5309
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005310 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005311 if (!ctx) {
5312 conn->err_code = CO_ER_SYS_MEMLIM;
5313 goto err;
5314 }
5315
5316 ctx->wait_event.tasklet = tasklet_new();
5317 if (!ctx->wait_event.tasklet) {
5318 conn->err_code = CO_ER_SYS_MEMLIM;
5319 goto err;
5320 }
5321
5322 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5323 ctx->wait_event.tasklet->context = ctx;
5324 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005325 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005326 ctx->subs = NULL;
5327 ctx->xprt_ctx = NULL;
5328
5329 ctx->xprt = xprt_get(XPRT_QUIC);
5330 if (objt_server(conn->target)) {
5331 /* Server */
5332 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005333 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005334 int ssl_err, ipv4;
5335
5336 ssl_err = SSL_ERROR_NONE;
5337 if (RAND_bytes(dcid, sizeof dcid) != 1)
5338 goto err;
5339
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005340 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005341 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005342 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005343 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005344 goto err;
5345
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005346 ctx->qc = qc;
5347
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005348 /* Insert our SCID, the connection ID for the QUIC client. */
5349 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5350
5351 conn->qc = qc;
5352 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005353 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5354 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005355 goto err;
5356
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005357 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005358 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005359 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5360 qc->enc_params_len =
5361 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5362 &qc->rx.params, 0);
5363 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005364 goto err;
5365
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005366 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5367 qc->enc_params, qc->enc_params_len) == -1)
5368 goto err;
5369
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005370 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005371 SSL_set_connect_state(ctx->ssl);
5372 ssl_err = SSL_do_handshake(ctx->ssl);
5373 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005374 int st;
5375
5376 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005377 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5378 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005379 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005380 }
5381 else {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005382 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005383 goto err;
5384 }
5385 }
5386 }
5387 else if (objt_listener(conn->target)) {
5388 /* Listener */
5389 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
5390
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005391 qc = ctx->conn->qc;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005392 ctx->qc = qc;
5393
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005394 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005395 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5396 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005397 goto err;
5398
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005399 /* Enabling 0-RTT */
5400 if (bc->ssl_conf.early_data)
5401 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5402
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005403 SSL_set_accept_state(ctx->ssl);
5404 }
5405
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005406 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005407
5408 /* Leave init state and start handshake */
5409 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005410
5411 out:
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005412 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005413 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005414
5415 return 0;
5416
5417 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005418 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005419 tasklet_free(ctx->wait_event.tasklet);
5420 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005421 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005422 return -1;
5423}
5424
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005425/* Start the QUIC transport layer */
5426static int qc_xprt_start(struct connection *conn, void *ctx)
5427{
5428 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005429 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005430
5431 qc = conn->qc;
5432 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005433 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005434 return 0;
5435 }
5436
5437 tasklet_wakeup(qctx->wait_event.tasklet);
5438 return 1;
5439}
5440
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005441/* transport-layer operations for QUIC connections. */
5442static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005443 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005444 .snd_buf = quic_conn_from_buf,
5445 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005446 .subscribe = quic_conn_subscribe,
5447 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005448 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005449 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005450 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5451 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005452 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005453 .name = "QUIC",
5454};
5455
5456__attribute__((constructor))
5457static void __quic_conn_init(void)
5458{
5459 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5460 xprt_register(XPRT_QUIC, &ssl_quic);
5461}
5462
5463__attribute__((destructor))
5464static void __quic_conn_deinit(void)
5465{
5466 BIO_meth_free(ha_quic_meth);
5467}
5468
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005469/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5470 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005471 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005472 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005473static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005474 struct sockaddr_storage *saddr, qpkt_read_func *func)
5475{
5476 unsigned char *pos;
5477 const unsigned char *end;
5478 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005479 .qc = NULL,
5480 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005481 .owner = owner,
5482 };
5483
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005484 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005485 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005486 do {
5487 int ret;
5488 struct quic_rx_packet *pkt;
5489
Willy Tarreaue4498932021-03-22 21:13:05 +01005490 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005491 if (!pkt)
5492 goto err;
5493
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005494 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005495 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5496 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005497 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005498 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005499 /* If the packet length could not be found, we cannot continue. */
5500 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005501 } while (pos < end);
5502
5503 /* Increasing the received bytes counter by the UDP datagram length
5504 * if this datagram could be associated to a connection.
5505 */
5506 if (dgram_ctx.qc)
5507 dgram_ctx.qc->rx.bytes += len;
5508
Amaury Denoyellece340fe2022-01-11 14:20:46 +01005509 return pos - (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005510
5511 err:
5512 return -1;
5513}
5514
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005515ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005516 struct sockaddr_storage *saddr)
5517{
5518 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5519}
5520
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005521/* Function to automatically activate QUIC traces on stdout.
5522 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5523 * Main use for now is in the docker image for QUIC interop testing.
5524 */
5525static void quic_init_stdout_traces(void)
5526{
5527#ifdef ENABLE_QUIC_STDOUT_TRACES
5528 trace_quic.sink = sink_find("stdout");
5529 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005530 trace_quic.state = TRACE_STATE_RUNNING;
5531#endif
5532}
5533INITCALL0(STG_INIT, quic_init_stdout_traces);
5534
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005535/*
5536 * Local variables:
5537 * c-indent-level: 8
5538 * c-basic-offset: 8
5539 * End:
5540 */