blob: e5b7c7d62b305d59e0ef8cbdf5e465bc89e5da38 [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écaillece6602d2022-01-17 11:06:10 +0100173 int padding, int ack, int probe, 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écaille8b6ea172022-01-17 10:51:43 +0100308 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
309 (unsigned long long)qc->rx.bytes,
310 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 }
312
313 if (room) {
314 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
315 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
316 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100317 }
318 }
319
320 if (mask & QUIC_EV_CONN_HDSHK) {
321 const enum quic_handshake_state *state = a2;
322 const int *err = a3;
323
324 if (state)
325 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
326 if (err)
327 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
328 }
329
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100330 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 const struct quic_rx_packet *pkt = a2;
332 const unsigned long *pktlen = a3;
333 const SSL *ssl = a4;
334
335 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100336 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100337 pkt, quic_packet_type_enc_level_char(pkt->type));
338 if (pkt->pnl)
339 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
340 (unsigned long long)pkt->pn);
341 if (pkt->token_len)
342 chunk_appendf(&trace_buf, " toklen=%llu",
343 (unsigned long long)pkt->token_len);
344 if (pkt->aad_len)
345 chunk_appendf(&trace_buf, " aadlen=%llu",
346 (unsigned long long)pkt->aad_len);
347 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
348 pkt->flags, (unsigned long long)pkt->len);
349 }
350 if (pktlen)
351 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
352 if (ssl) {
353 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
354 chunk_appendf(&trace_buf, " el=%c",
355 quic_enc_level_char(ssl_to_quic_enc_level(level)));
356 }
357 }
358
359 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
360 const struct quic_rx_packet *pkt = a2;
361 const struct quic_rx_crypto_frm *cf = a3;
362 const SSL *ssl = a4;
363
364 if (pkt)
365 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
366 quic_packet_type_enc_level_char(pkt->type),
367 (unsigned long long)pkt->pn);
368 if (cf)
369 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
370 (unsigned long long)cf->offset_node.key,
371 (unsigned long long)cf->len);
372 if (ssl) {
373 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200374 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100375 quic_enc_level_char(ssl_to_quic_enc_level(level)));
376 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100377
378 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100379 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100380 }
381
382 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
383 const struct quic_frame *frm = a2;
384
385 if (frm)
386 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
387 }
388
389 if (mask & QUIC_EV_CONN_PHPKTS) {
390 const struct quic_enc_level *qel = a2;
391
392 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100393 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100394 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100395 " 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 +0100396 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200397 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100398 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100399 (unsigned long long)qc->path->cwnd,
400 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100402 (unsigned long long)pktns->tx.in_flight,
403 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 }
405 }
406
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100407 if (mask & QUIC_EV_CONN_ENCPKT) {
408 const struct enc_debug_info *edi = a2;
409
410 if (edi)
411 chunk_appendf(&trace_buf,
412 " payload=@%p payload_len=%llu"
413 " aad=@%p aad_len=%llu pn=%llu",
414 edi->payload, (unsigned long long)edi->payload_len,
415 edi->aad, (unsigned long long)edi->aad_len,
416 (unsigned long long)edi->pn);
417 }
418
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100419 if (mask & QUIC_EV_CONN_RMHP) {
420 const struct quic_rx_packet *pkt = a2;
421
422 if (pkt) {
423 const int *ret = a3;
424
425 chunk_appendf(&trace_buf, " pkt@%p", pkt);
426 if (ret && *ret)
427 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
428 pkt->pnl, (unsigned long long)pkt->pn);
429 }
430 }
431
432 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200433 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100434 const unsigned long *val1 = a3;
435 const unsigned long *val2 = a4;
436
437 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100438 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100439 if (val1)
440 chunk_appendf(&trace_buf, " %lu", *val1);
441 if (val2)
442 chunk_appendf(&trace_buf, "..%lu", *val2);
443 }
444
445 if (mask & QUIC_EV_CONN_RTTUPDT) {
446 const unsigned int *rtt_sample = a2;
447 const unsigned int *ack_delay = a3;
448 const struct quic_loss *ql = a4;
449
450 if (rtt_sample)
451 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
452 if (ack_delay)
453 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
454 if (ql)
455 chunk_appendf(&trace_buf,
456 " srtt=%ums rttvar=%ums min_rtt=%ums",
457 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
458 }
459 if (mask & QUIC_EV_CONN_CC) {
460 const struct quic_cc_event *ev = a2;
461 const struct quic_cc *cc = a3;
462
463 if (a2)
464 quic_cc_event_trace(&trace_buf, ev);
465 if (a3)
466 quic_cc_state_trace(&trace_buf, cc);
467 }
468
469 if (mask & QUIC_EV_CONN_PKTLOSS) {
470 const struct quic_pktns *pktns = a2;
471 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100472
473 if (pktns) {
474 chunk_appendf(&trace_buf, " pktns=%s",
475 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
476 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
477 if (pktns->tx.loss_time)
478 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100479 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100480 }
481 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
482 struct quic_tx_packet *pkt;
483
484 chunk_appendf(&trace_buf, " lost_pkts:");
485 list_for_each_entry(pkt, lost_pkts, list)
486 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
487 }
488 }
489
490 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100491 const struct quic_pktns *pktns = a2;
492 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100493 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100495 if (ifae_pkts)
496 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
497 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100498 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100499 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100501 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
502 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100503 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
504 if (pktns->tx.in_flight)
505 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506 if (pktns->tx.loss_time)
507 chunk_appendf(&trace_buf, " loss_time=%dms",
508 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
509 }
510 if (mask & QUIC_EV_CONN_SPTO) {
511 if (pktns->tx.time_of_last_eliciting)
512 chunk_appendf(&trace_buf, " tole=%dms",
513 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
514 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100515 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100516 }
517 }
518
519 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
520 chunk_appendf(&trace_buf,
521 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
522 }
523 }
524
525 if (mask & QUIC_EV_CONN_SPPKTS) {
526 const struct quic_tx_packet *pkt = a2;
527
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100528 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
529 (unsigned long long)qc->path->cwnd,
530 (unsigned long long)qc->path->prep_in_flight,
531 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100533 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 (unsigned long)pkt->pn_node.key,
535 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
536 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100537 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100538 }
539 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100540
541 if (mask & QUIC_EV_CONN_SSLALERT) {
542 const uint8_t *alert = a2;
543 const enum ssl_encryption_level_t *level = a3;
544
545 if (alert)
546 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
547 if (level)
548 chunk_appendf(&trace_buf, " el=%c",
549 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
550 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100551
552 if (mask & QUIC_EV_CONN_BCFRMS) {
553 const size_t *sz1 = a2;
554 const size_t *sz2 = a3;
555 const size_t *sz3 = a4;
556
557 if (sz1)
558 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
559 if (sz2)
560 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
561 if (sz3)
562 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
563 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100564
565 if (mask & QUIC_EV_CONN_PSTRM) {
566 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100567
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100568 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100569 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100570 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100571 }
572 if (mask & QUIC_EV_CONN_LPKT) {
573 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200574 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100575
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100576 if (pkt) {
577 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
578 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
579 if (pkt->pn_node.key != (uint64_t)-1)
580 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
581 }
582
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200583 if (len)
584 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100585 }
586
587}
588
589/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100590static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100591{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200592 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100594 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100595 return 1;
596
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200597 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
598 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
599 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
600 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200601 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100602 return 1;
603
604 return 0;
605}
606
607/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
608 * both loss detection and PTO and schedule the task assiated to this timer if needed.
609 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100610static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 struct quic_pktns *pktns;
613 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200614 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100616 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100617 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100618
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100619 pktns = quic_loss_pktns(qc);
620 if (tick_isset(pktns->tx.loss_time)) {
621 qc->timer = pktns->tx.loss_time;
622 goto out;
623 }
624
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100625 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626 * cancelled for a server which reached the anti-amplification limit.
627 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100628 if (!quic_peer_validated_addr(qc) &&
629 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100630 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100631 qc->timer = TICK_ETERNITY;
632 goto out;
633 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100634
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100635 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100636 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637 /* Timer cancellation. */
638 qc->timer = TICK_ETERNITY;
639 goto out;
640 }
641
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200642 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
643 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100644 if (tick_isset(pto))
645 qc->timer = pto;
646 out:
Amaury Denoyelle0a29e132021-12-23 15:06:56 +0100647 if (qc->timer_task && qc->timer != TICK_ETERNITY)
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200648 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100649 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100650}
651
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100652/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
653 * connection.
654 * Return 1 if succeeded, 0 if not.
655 */
656static int quic_tls_key_update(struct quic_conn *qc)
657{
658 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
659 struct quic_tls_secrets *rx, *tx;
660 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
661 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
662
663 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
664 rx = &tls_ctx->rx;
665 tx = &tls_ctx->tx;
666 nxt_rx = &qc->ku.nxt_rx;
667 nxt_tx = &qc->ku.nxt_tx;
668
669 /* Prepare new RX secrets */
670 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
671 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100672 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100673 return 0;
674 }
675
676 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
677 nxt_rx->key, nxt_rx->keylen,
678 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
679 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100680 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100681 return 0;
682 }
683
684 /* Prepare new TX secrets */
685 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
686 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100687 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100688 return 0;
689 }
690
691 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
692 nxt_tx->key, nxt_tx->keylen,
693 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
694 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100695 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100696 return 0;
697 }
698
699 return 1;
700}
701
702/* Rotate the Key Update information for <qc> QUIC connection.
703 * Must be used after having updated them.
704 * Always succeeds.
705 */
706static void quic_tls_rotate_keys(struct quic_conn *qc)
707{
708 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
709 unsigned char *curr_secret, *curr_iv, *curr_key;
710
711 /* Rotate the RX secrets */
712 curr_secret = tls_ctx->rx.secret;
713 curr_iv = tls_ctx->rx.iv;
714 curr_key = tls_ctx->rx.key;
715
716 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
717 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
718 tls_ctx->rx.key = qc->ku.nxt_rx.key;
719
720 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
721 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
722 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
723
724 qc->ku.prv_rx.secret = curr_secret;
725 qc->ku.prv_rx.iv = curr_iv;
726 qc->ku.prv_rx.key = curr_key;
727 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
728
729 /* Update the TX secrets */
730 curr_secret = tls_ctx->tx.secret;
731 curr_iv = tls_ctx->tx.iv;
732 curr_key = tls_ctx->tx.key;
733
734 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
735 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
736 tls_ctx->tx.key = qc->ku.nxt_tx.key;
737
738 qc->ku.nxt_tx.secret = curr_secret;
739 qc->ku.nxt_tx.iv = curr_iv;
740 qc->ku.nxt_tx.key = curr_key;
741}
742
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100743#ifndef OPENSSL_IS_BORINGSSL
744int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
745 const uint8_t *read_secret,
746 const uint8_t *write_secret, size_t secret_len)
747{
748 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
749 struct quic_tls_ctx *tls_ctx =
750 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
751 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100752 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100753
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100754 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100755 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100756 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100757 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100758 goto out;
759 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100760
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100761 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100762 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100763 goto err;
764 }
765
766 rx = &tls_ctx->rx;
767 tx = &tls_ctx->tx;
768
769 rx->aead = tx->aead = tls_aead(cipher);
770 rx->md = tx->md = tls_md(cipher);
771 rx->hp = tx->hp = tls_hp(cipher);
772
773 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
774 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100775 read_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100776 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100777 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100778 }
779
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100780 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100781
782 if (!write_secret)
783 goto tp;
784
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100785 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
786 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100787 write_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100788 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100789 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100790 }
791
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100792 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100793 tp:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100794 if (objt_server(conn->target) && level == ssl_encryption_application) {
795 const unsigned char *buf;
796 size_t buflen;
797
798 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
799 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100800 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100801
802 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100803 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100804 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100805
806 if (level == ssl_encryption_application) {
807 struct quic_conn *qc = conn->qc;
808 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
809 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
810 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
811
812 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
813 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100814 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100815 goto err;
816 }
817
818 memcpy(rx->secret, read_secret, secret_len);
819 rx->secretlen = secret_len;
820 memcpy(tx->secret, write_secret, secret_len);
821 tx->secretlen = secret_len;
822 /* Initialize all the secret keys lengths */
823 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
824 /* Prepare the next key update */
825 if (!quic_tls_key_update(qc))
826 goto err;
827 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100828 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100829 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100830 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100831
832 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100833 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100834 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100835}
836#else
837/* ->set_read_secret callback to derive the RX secrets at <level> encryption
838 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500839 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100840 */
841int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
842 const SSL_CIPHER *cipher,
843 const uint8_t *secret, size_t secret_len)
844{
845 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
846 struct quic_tls_ctx *tls_ctx =
847 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
848
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100849 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100850 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100851 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100852 goto out;
853 }
854
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100855 tls_ctx->rx.aead = tls_aead(cipher);
856 tls_ctx->rx.md = tls_md(cipher);
857 tls_ctx->rx.hp = tls_hp(cipher);
858
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100859 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
860 goto err;
861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100863 tls_ctx->rx.key, tls_ctx->rx.keylen,
864 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100865 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
866 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100867 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100868 goto err;
869 }
870
871 if (objt_server(conn->target) && level == ssl_encryption_application) {
872 const unsigned char *buf;
873 size_t buflen;
874
875 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
876 if (!buflen)
877 goto err;
878
879 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
880 goto err;
881 }
882
883 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100884 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100885 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100886
887 return 1;
888
889 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100890 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891 return 0;
892}
893
894/* ->set_write_secret callback to derive the TX secrets at <level>
895 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500896 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897 */
898int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
899 const SSL_CIPHER *cipher,
900 const uint8_t *secret, size_t secret_len)
901{
902 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
903 struct quic_tls_ctx *tls_ctx =
904 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
905
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100906 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100907 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100908 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100909 goto out;
910 }
911
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100912 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
913 goto err;
914
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100915 tls_ctx->tx.aead = tls_aead(cipher);
916 tls_ctx->tx.md = tls_md(cipher);
917 tls_ctx->tx.hp = tls_hp(cipher);
918
919 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 +0100920 tls_ctx->tx.key, tls_ctx->tx.keylen,
921 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
923 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100924 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100925 goto err;
926 }
927
928 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100929 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100930 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100931 return 1;
932
933 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100934 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935 return 0;
936}
937#endif
938
939/* This function copies the CRYPTO data provided by the TLS stack found at <data>
940 * with <len> as size in CRYPTO buffers dedicated to store the information about
941 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
942 * It fails only if it could not managed to allocate enough CRYPTO buffers to
943 * store all the data.
944 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
945 */
946static int quic_crypto_data_cpy(struct quic_enc_level *qel,
947 const unsigned char *data, size_t len)
948{
949 struct quic_crypto_buf **qcb;
950 /* The remaining byte to store in CRYPTO buffers. */
951 size_t cf_offset, cf_len, *nb_buf;
952 unsigned char *pos;
953
954 nb_buf = &qel->tx.crypto.nb_buf;
955 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
956 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
957 cf_len = len;
958
959 while (len) {
960 size_t to_copy, room;
961
962 pos = (*qcb)->data + (*qcb)->sz;
963 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
964 to_copy = len > room ? room : len;
965 if (to_copy) {
966 memcpy(pos, data, to_copy);
967 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
968 qel->tx.crypto.sz += to_copy;
969 (*qcb)->sz += to_copy;
970 pos += to_copy;
971 len -= to_copy;
972 data += to_copy;
973 }
974 else {
975 struct quic_crypto_buf **tmp;
976
977 tmp = realloc(qel->tx.crypto.bufs,
978 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
979 if (tmp) {
980 qel->tx.crypto.bufs = tmp;
981 qcb = &qel->tx.crypto.bufs[*nb_buf];
982 *qcb = pool_alloc(pool_head_quic_crypto_buf);
983 if (!*qcb)
984 return 0;
985
986 (*qcb)->sz = 0;
987 ++*nb_buf;
988 }
989 else {
990 break;
991 }
992 }
993 }
994
995 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
996 * have been buffered.
997 */
998 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200999 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001000 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001001
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001002 /* There is at most one CRYPTO frame in this packet number
1003 * space. Let's look for it.
1004 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001005 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001006 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;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001026 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001027 }
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
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001515/* Remove all frames from <pkt_frm_list> and reinsert them in the
1516 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001517 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001518static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1519 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001520 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001521{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001522 struct quic_frame *frm, *frmbak;
1523 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001524
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001525 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1526 LIST_DELETE(&frm->list);
1527 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001528 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001529 }
1530
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001531 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001532}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533
1534/* Free the TX packets of <pkts> list */
1535static inline void free_quic_tx_pkts(struct list *pkts)
1536{
1537 struct quic_tx_packet *pkt, *tmp;
1538
1539 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001540 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001541 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001542 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001543 }
1544}
1545
1546/* Send a packet loss event nofification to the congestion controller
1547 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1548 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1549 * at <now_us> current time.
1550 * Always succeeds.
1551 */
1552static inline void qc_cc_loss_event(struct quic_conn *qc,
1553 unsigned int lost_bytes,
1554 unsigned int newest_time_sent,
1555 unsigned int period,
1556 unsigned int now_us)
1557{
1558 struct quic_cc_event ev = {
1559 .type = QUIC_CC_EVT_LOSS,
1560 .loss.now_ms = now_ms,
1561 .loss.max_ack_delay = qc->max_ack_delay,
1562 .loss.lost_bytes = lost_bytes,
1563 .loss.newest_time_sent = newest_time_sent,
1564 .loss.period = period,
1565 };
1566
1567 quic_cc_event(&qc->path->cc, &ev);
1568}
1569
1570/* Send a packet ack event nofication for each newly acked packet of
1571 * <newly_acked_pkts> list and free them.
1572 * Always succeeds.
1573 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001574static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001575 struct list *newly_acked_pkts)
1576{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001577 struct quic_tx_packet *pkt, *tmp;
1578 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1579
1580 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1581 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001582 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001583 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001584 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585 ev.ack.acked = pkt->in_flight_len;
1586 ev.ack.time_sent = pkt->time_sent;
1587 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001588 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001589 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001590 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001591 }
1592
1593}
1594
1595/* Handle <pkts> list of lost packets detected at <now_us> handling
1596 * their TX frames.
1597 * Send a packet loss event to the congestion controller if
1598 * in flight packet have been lost.
1599 * Also frees the packet in <pkts> list.
1600 * Never fails.
1601 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001602static inline void qc_release_lost_pkts(struct quic_conn *qc,
1603 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001604 struct list *pkts,
1605 uint64_t now_us)
1606{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001607 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608 uint64_t lost_bytes;
1609
1610 lost_bytes = 0;
1611 oldest_lost = newest_lost = NULL;
1612 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001613 struct list tmp = LIST_HEAD_INIT(tmp);
1614
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001615 lost_bytes += pkt->in_flight_len;
1616 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001617 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001618 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001619 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001621 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001622 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001623 if (!oldest_lost) {
1624 oldest_lost = newest_lost = pkt;
1625 }
1626 else {
1627 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001628 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629 newest_lost = pkt;
1630 }
1631 }
1632
1633 if (lost_bytes) {
1634 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001635 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001636 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001637 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001638 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001639 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001640 }
1641}
1642
1643/* Look for packet loss from sent packets for <qel> encryption level of a
1644 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1645 * their tree if deemed as lost or set the <loss_time> value the packet number
1646 * space if any not deemed lost.
1647 * Should be called after having received an ACK frame with newly acknowledged
1648 * packets or when the the loss detection timer has expired.
1649 * Always succeeds.
1650 */
1651static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1652 struct quic_conn *qc,
1653 struct list *lost_pkts)
1654{
1655 struct eb_root *pkts;
1656 struct eb64_node *node;
1657 struct quic_loss *ql;
1658 unsigned int loss_delay;
1659
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001660 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001661 pkts = &pktns->tx.pkts;
1662 pktns->tx.loss_time = TICK_ETERNITY;
1663 if (eb_is_empty(pkts))
1664 goto out;
1665
1666 ql = &qc->path->loss;
1667 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1668 loss_delay += loss_delay >> 3;
1669 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1670
1671 node = eb64_first(pkts);
1672 while (node) {
1673 struct quic_tx_packet *pkt;
1674 int64_t largest_acked_pn;
1675 unsigned int loss_time_limit, time_sent;
1676
1677 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001678 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001679 node = eb64_next(node);
1680 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1681 break;
1682
1683 time_sent = pkt->time_sent;
1684 loss_time_limit = tick_add(time_sent, loss_delay);
1685 if (tick_is_le(time_sent, now_ms) ||
1686 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1687 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001688 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001689 }
1690 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001691 if (tick_isset(pktns->tx.loss_time))
1692 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1693 else
1694 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001695 }
1696 }
1697
1698 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001699 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001700}
1701
1702/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1703 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001704 * if the largest acked packet was newly acked and if there was at least one newly
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001705 * acked ack-eliciting packet.
1706 * Return 1, if succeeded, 0 if not.
1707 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001708static inline int qc_parse_ack_frm(struct quic_conn *qc,
1709 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001710 struct quic_enc_level *qel,
1711 unsigned int *rtt_sample,
1712 const unsigned char **pos, const unsigned char *end)
1713{
1714 struct quic_ack *ack = &frm->ack;
1715 uint64_t smallest, largest;
1716 struct eb_root *pkts;
1717 struct eb64_node *largest_node;
1718 unsigned int time_sent, pkt_flags;
1719 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1720 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1721
1722 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1723 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001724 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001725 goto err;
1726 }
1727
1728 if (ack->first_ack_range > ack->largest_ack) {
1729 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001730 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001731 goto err;
1732 }
1733
1734 largest = ack->largest_ack;
1735 smallest = largest - ack->first_ack_range;
1736 pkts = &qel->pktns->tx.pkts;
1737 pkt_flags = 0;
1738 largest_node = NULL;
1739 time_sent = 0;
1740
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001741 if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001742 largest_node = eb64_lookup(pkts, largest);
1743 if (!largest_node) {
1744 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001745 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001746 }
1747 else {
1748 time_sent = eb64_entry(&largest_node->node,
1749 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001750 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001751 }
1752
1753 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001754 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001755 do {
1756 uint64_t gap, ack_range;
1757
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001758 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1759 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001760 if (!ack->ack_range_num--)
1761 break;
1762
1763 if (!quic_dec_int(&gap, pos, end))
1764 goto err;
1765
1766 if (smallest < gap + 2) {
1767 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001768 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001769 goto err;
1770 }
1771
1772 largest = smallest - gap - 2;
1773 if (!quic_dec_int(&ack_range, pos, end))
1774 goto err;
1775
1776 if (largest < ack_range) {
1777 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001778 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001779 goto err;
1780 }
1781
1782 /* Do not use this node anymore. */
1783 largest_node = NULL;
1784 /* Next range */
1785 smallest = largest - ack_range;
1786
1787 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001788 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001789 } while (1);
1790
1791 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001792 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001793
1794 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1795 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001796 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001797 }
1798
1799 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1800 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001801 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001802 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001803 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001804 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001805 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1806 if (quic_peer_validated_addr(qc))
1807 qc->path->loss.pto_count = 0;
1808 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001809 }
1810
1811
1812 return 1;
1813
1814 err:
1815 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001816 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001817 return 0;
1818}
1819
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001820/* This function gives the detail of the SSL error. It is used only
1821 * if the debug mode and the verbose mode are activated. It dump all
1822 * the SSL error until the stack was empty.
1823 */
1824static forceinline void qc_ssl_dump_errors(struct connection *conn)
1825{
1826 if (unlikely(global.mode & MODE_DEBUG)) {
1827 while (1) {
1828 unsigned long ret;
1829
1830 ret = ERR_get_error();
1831 if (!ret)
1832 return;
1833
1834 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1835 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1836 }
1837 }
1838}
1839
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001840int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1841 const char **str, int *len);
1842
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001843/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1844 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001845 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001846 * Return 1 if succeeded, 0 if not.
1847 */
1848static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001849 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001850 const unsigned char *data, size_t len,
1851 struct quic_rx_packet *pkt,
1852 struct quic_rx_crypto_frm *cf)
1853{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001854 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001855 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001856
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001857 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001858 qc = ctx->qc;
1859
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001860 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1863 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001864 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865 goto err;
1866 }
1867
1868 el->rx.crypto.offset += len;
1869 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001870 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001872 state = HA_ATOMIC_LOAD(&qc->state);
1873 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874 ssl_err = SSL_do_handshake(ctx->ssl);
1875 if (ssl_err != 1) {
1876 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1877 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1878 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001879 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880 goto out;
1881 }
1882
1883 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001884 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001885 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001886 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887 goto err;
1888 }
1889
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001890 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01001891 if (qc_is_listener(ctx->qc))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001892 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001893 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001894 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001895 } else {
1896 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1897 if (ssl_err != 1) {
1898 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1899 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1900 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001901 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001902 goto out;
1903 }
1904
1905 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001906 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001907 goto err;
1908 }
1909
1910 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001911 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001912 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001913
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001914 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001915 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001916 return 1;
1917
1918 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001919 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 return 0;
1921}
1922
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001923/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1924 * <pkt> RX packet.
1925 * Return it if succeeded, NULL if not.
1926 */
1927static inline
1928struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1929 struct quic_rx_packet *pkt)
1930{
1931 struct quic_rx_strm_frm *frm;
1932
1933 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1934 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001935 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001936 frm->len = stream_frm->len;
1937 frm->data = stream_frm->data;
1938 frm->pkt = pkt;
1939 }
1940
1941 return frm;
1942}
1943
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001944/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001945 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001946 */
1947static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1948{
1949 size_t ret;
1950
1951 ret = 0;
1952 while (strm_frm->len) {
1953 size_t try;
1954
1955 try = b_contig_space(buf);
1956 if (!try)
1957 break;
1958
1959 if (try > strm_frm->len)
1960 try = strm_frm->len;
1961 memcpy(b_tail(buf), strm_frm->data, try);
1962 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001963 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001964 b_add(buf, try);
1965 ret += try;
1966 }
1967
1968 return ret;
1969}
1970
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001971/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1972 * Also update <strm_frm> frame to reflect the data which have been consumed.
1973 */
1974static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1975 struct quic_rx_strm_frm *strm_frm)
1976{
1977 size_t ret;
1978
1979 ret = 0;
1980 while (strm_frm->len) {
1981 size_t try;
1982
1983 try = b_contig_space(buf);
1984 if (!try)
1985 break;
1986
1987 if (try > strm_frm->len)
1988 try = strm_frm->len;
1989 memcpy(b_tail(buf), strm_frm->data, try);
1990 strm_frm->len -= try;
1991 strm_frm->offset_node.key += try;
1992 b_add(buf, try);
1993 ret += try;
1994 }
1995
1996 return ret;
1997}
1998
1999/* Process as much as possible RX STREAM frames received for <qcs> */
2000static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2001{
2002 int total;
2003 struct eb64_node *frm_node;
2004
2005 total = 0;
2006 frm_node = eb64_first(&qcs->rx.frms);
2007 while (frm_node) {
2008 int ret;
2009 struct quic_rx_strm_frm *frm;
2010
2011 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2012 if (frm->offset_node.key != qcs->rx.offset)
2013 break;
2014
2015 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2016 qcs->rx.offset += ret;
2017 total += ret;
2018 if (frm->len) {
2019 /* If there is remaining data in this frame
2020 * this is because the destination buffer is full.
2021 */
2022 break;
2023 }
2024
2025 frm_node = eb64_next(frm_node);
2026 quic_rx_packet_refdec(frm->pkt);
2027 eb64_delete(&frm->offset_node);
2028 pool_free(pool_head_quic_rx_strm_frm, frm);
2029 }
2030
2031 return total;
2032}
2033
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002034/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2035 * streams may be open. The data are copied to the stream RX buffer if possible.
2036 * If not, the STREAM frame is stored to be treated again later.
2037 * We rely on the flow control so that not to store too much STREAM frames.
2038 * Return 1 if succeeded, 0 if not.
2039 */
2040static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2041 struct quic_stream *strm_frm,
2042 struct quic_conn *qc)
2043{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002044 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002045 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002046 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002047 struct quic_rx_strm_frm *frm;
2048
2049 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2050 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002051 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002052 return 0;
2053 }
2054
2055 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002056 if (strm_frm->offset.key < strm->rx.offset) {
2057 size_t diff;
2058
2059 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2060 TRACE_PROTO("Already received STREAM data",
2061 QUIC_EV_CONN_PSTRM, qc);
2062 goto out;
2063 }
2064
2065 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2066 diff = strm->rx.offset - strm_frm->offset.key;
2067 strm_frm->offset.key = strm->rx.offset;
2068 strm_frm->len -= diff;
2069 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002070 }
2071
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002072 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002073 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002074 int ret;
2075
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002076 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002077 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002078 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002079
2080 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002081 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002082 strm->rx.offset += ret;
2083 }
2084
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002085 total += qc_treat_rx_strm_frms(strm);
2086 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 +01002087 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002088 return 0;
2089 }
2090
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002091 if (!strm_frm->len)
2092 goto out;
2093
2094 store_frm:
2095 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2096 if (!frm) {
2097 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002098 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002099 return 0;
2100 }
2101
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002102 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002103 quic_rx_packet_refinc(pkt);
2104
2105 out:
2106 return 1;
2107}
2108
2109/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2110 * streams may be open. The data are copied to the stream RX buffer if possible.
2111 * If not, the STREAM frame is stored to be treated again later.
2112 * We rely on the flow control so that not to store too much STREAM frames.
2113 * Return 1 if succeeded, 0 if not.
2114 */
2115static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2116 struct quic_stream *strm_frm,
2117 struct quic_conn *qc)
2118{
2119 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002120 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002121 struct quic_rx_strm_frm *frm;
2122 size_t strm_frm_len;
2123
2124 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2125 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002126 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002127 return 0;
2128 }
2129
2130 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002131 if (strm_frm->offset.key < strm->rx.offset) {
2132 size_t diff;
2133
2134 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2135 TRACE_PROTO("Already received STREAM data",
2136 QUIC_EV_CONN_PSTRM, qc);
2137 goto out;
2138 }
2139
2140 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2141 diff = strm->rx.offset - strm_frm->offset.key;
2142 strm_frm->offset.key = strm->rx.offset;
2143 strm_frm->len -= diff;
2144 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002145 }
2146
2147 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002148 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002149 int ret;
2150
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002151 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002152 goto store_frm;
2153
2154 /* qc_strm_cpy() will modify the offset, depending on the number
2155 * of bytes copied.
2156 */
2157 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2158 /* Inform the application of the arrival of this new stream */
2159 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002160 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002161 return 0;
2162 }
2163
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002164 if (ret)
2165 qcs_notify_recv(strm);
2166
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002167 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002168 }
2169 /* Take this frame into an account for the stream flow control */
2170 strm->rx.offset += strm_frm_len;
2171 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002172 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002173 */
2174 if (!strm_frm->len)
2175 goto out;
2176
2177 store_frm:
2178 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2179 if (!frm) {
2180 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002181 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002182 return 0;
2183 }
2184
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002185 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002186 quic_rx_packet_refinc(pkt);
2187
2188 out:
2189 return 1;
2190}
2191
2192static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2193 struct quic_stream *strm_frm,
2194 struct quic_conn *qc)
2195{
2196 if (strm_frm->id & QCS_ID_DIR_BIT)
2197 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2198 else
2199 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2200}
2201
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002202/* Prepare a fast retransmission from <qel> encryption level
2203 * which must be Initial encryption level.
2204 */
2205static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2206 struct quic_conn *qc)
2207{
2208 struct eb_root *pkts = &qel->pktns->tx.pkts;
2209 struct eb64_node *node = eb64_first(pkts);
2210 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002211
2212 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002213 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002214 pkts = &qel->pktns->tx.pkts;
2215 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002216 /* Skip the empty packet (they have already been retransmitted) */
2217 while (node) {
2218 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2219 if (!LIST_ISEMPTY(&pkt->frms))
2220 break;
2221 node = eb64_next(node);
2222 }
2223
2224 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002225 return;
2226
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002227 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002228 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] &&
2229 qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight) {
2230 qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.pto_probe = 1;
2231 qel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2232 goto start;
2233 }
2234}
2235
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002236/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2237 * as I/O handler context and <qel> as encryption level.
2238 * Returns 1 if succeeded, 0 if failed.
2239 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002240static 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 +01002241 struct quic_enc_level *qel)
2242{
2243 struct quic_frame frm;
2244 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002245 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002246 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002247
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002248 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 /* Skip the AAD */
2250 pos = pkt->data + pkt->aad_len;
2251 end = pkt->data + pkt->len;
2252
2253 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002254 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002255 goto err;
2256
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002257 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002258 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002259 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002260 break;
2261 case QUIC_FT_PING:
2262 break;
2263 case QUIC_FT_ACK:
2264 {
2265 unsigned int rtt_sample;
2266
2267 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002268 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002269 goto err;
2270
2271 if (rtt_sample) {
2272 unsigned int ack_delay;
2273
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002274 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002275 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2276 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2277 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002278 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002279 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002280 break;
2281 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002282 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002283 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002284 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002285 {
2286 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002287
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002288 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2289 /* XXX TO DO: <cfdebug> is used only for the traces. */
2290 struct quic_rx_crypto_frm cfdebug = { };
2291
2292 cfdebug.offset_node.key = frm.crypto.offset;
2293 cfdebug.len = frm.crypto.len;
2294 TRACE_PROTO("CRYPTO data discarded",
2295 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2296 break;
2297 }
2298
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002299 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2300 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2301 /* XXX TO DO: <cfdebug> is used only for the traces. */
2302 struct quic_rx_crypto_frm cfdebug = { };
2303
2304 cfdebug.offset_node.key = frm.crypto.offset;
2305 cfdebug.len = frm.crypto.len;
2306 /* Nothing to do */
2307 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002308 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002309 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002310 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2311 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002312 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002313 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002314 else {
2315 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2316 /* XXX TO DO: <cfdebug> is used only for the traces. */
2317 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002319 cfdebug.offset_node.key = frm.crypto.offset;
2320 cfdebug.len = frm.crypto.len;
2321 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002322 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002323 frm.crypto.len -= diff;
2324 frm.crypto.data += diff;
2325 frm.crypto.offset = qel->rx.crypto.offset;
2326 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002327 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002328
2329 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002330 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002331 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002332
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002333 cfdebug.offset_node.key = frm.crypto.offset;
2334 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002335 if (!qc_provide_cdata(qel, ctx,
2336 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002337 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002338 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002339
2340 break;
2341 }
2342
2343 /* frm.crypto.offset > qel->rx.crypto.offset */
2344 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2345 if (!cf) {
2346 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002347 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002348 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002350
2351 cf->offset_node.key = frm.crypto.offset;
2352 cf->len = frm.crypto.len;
2353 cf->data = frm.crypto.data;
2354 cf->pkt = pkt;
2355 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2356 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002357 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002358 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002359 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002360 {
2361 struct quic_stream *stream = &frm.stream;
2362
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002363 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002364 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2365 goto err;
2366 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2367 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002368
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002369 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002370 goto err;
2371
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002372 break;
2373 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002374 case QUIC_FT_MAX_DATA:
2375 case QUIC_FT_MAX_STREAM_DATA:
2376 case QUIC_FT_MAX_STREAMS_BIDI:
2377 case QUIC_FT_MAX_STREAMS_UNI:
2378 case QUIC_FT_DATA_BLOCKED:
2379 case QUIC_FT_STREAM_DATA_BLOCKED:
2380 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2381 case QUIC_FT_STREAMS_BLOCKED_UNI:
2382 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002383 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002384 break;
2385 case QUIC_FT_CONNECTION_CLOSE:
2386 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002387 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002388 qc->qcc->flags |= QC_CF_CC_RECV;
2389 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002390 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002391 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002392 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002393 goto err;
2394
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002395 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002396 break;
2397 default:
2398 goto err;
2399 }
2400 }
2401
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002402 if (fast_retrans) {
2403 qc_prep_fast_retrans(qel, qc);
2404 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->tx.in_flight)
2405 qc_prep_fast_retrans(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE], qc);
2406 }
2407
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002408 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2409 * has successfully parse a Handshake packet. The Initial encryption must also
2410 * be discarded.
2411 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002412 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002413 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002414
2415 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002416 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002417 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002418 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002419 qc_set_timer(ctx->qc);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002420 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002421 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002422 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002423 }
2424
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002425 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002426 return 1;
2427
2428 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002429 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002430 return 0;
2431}
2432
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002433/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002434 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002435 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2436 * This function must be called only after having built a correct datagram.
2437 * Always succeeds.
2438 */
2439static inline void qc_set_dg(struct cbuf *cbuf,
2440 uint16_t dglen, struct quic_tx_packet *pkt)
2441{
2442 write_u16(cb_wr(cbuf), dglen);
2443 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2444 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2445}
2446
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002447/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002448 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2449 * several packets in the same datagram. A header made of two fields is added
2450 * to each datagram: the datagram length followed by the address of the first
2451 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002452 * Returns 1 if succeeded, or 0 if something wrong happened.
2453 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002454static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2455 enum quic_tls_enc_level tel,
2456 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002457{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002458 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002459 struct cbuf *cbuf;
2460 unsigned char *end_buf, *end, *pos, *spos;
2461 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2462 /* length of datagrams */
2463 uint16_t dglen;
2464 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002465 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002466 /* Each datagram is prepended with its length followed by the
2467 * address of the first packet in the datagram.
2468 */
2469 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002470
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002471 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2472
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002473 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002474 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002475 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002476 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002477 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002478 cbuf = qr->cbuf;
2479 spos = pos = cb_wr(cbuf);
2480 /* Leave at least <dglen> bytes at the end of this buffer
2481 * to ensure there is enough room to mark the end of prepared
2482 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002483 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002484 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2485 first_pkt = prv_pkt = NULL;
2486 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002487 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002488 enum quic_pkt_type pkt_type;
2489
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002490 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002491 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002492 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2493 if (!cc) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002494 if (!prv_pkt)
2495 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002496 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002497 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002498 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002499 * 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 +01002500 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002501 * and if there is no more CRYPTO data available or in flight
2502 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002503 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002504 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002505 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002506 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002507 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002508 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002509 /* Set the current datagram as prepared into <cbuf> if
2510 * the was already a correct packet which was previously written.
2511 */
2512 if (prv_pkt)
2513 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 break;
2515 }
2516
2517 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002518 if (!prv_pkt) {
2519 /* Leave room for the datagram header */
2520 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002521 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002522 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2523 }
2524 else {
2525 end = pos + qc->path->mtu;
2526 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002527 }
2528
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002529 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002530 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002531 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002532 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002533 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002534 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002535 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002536 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002537 case -2:
2538 goto err;
2539 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002540 /* If there was already a correct packet present, set the
2541 * current datagram as prepared into <cbuf>.
2542 */
2543 if (prv_pkt) {
2544 qc_set_dg(cbuf, dglen, first_pkt);
2545 goto stop_build;
2546 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002547 goto out;
2548 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002549 break;
2550 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002551
Frédéric Lécaille63556772021-12-29 17:18:21 +01002552 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2553 if (!cur_pkt)
2554 goto err;
2555
2556 total += cur_pkt->len;
2557 /* keep trace of the first packet in the datagram */
2558 if (!first_pkt)
2559 first_pkt = cur_pkt;
2560 /* Attach the current one to the previous one */
2561 if (prv_pkt)
2562 prv_pkt->next = cur_pkt;
2563 /* Let's say we have to build a new dgram */
2564 prv_pkt = NULL;
2565 dglen += cur_pkt->len;
2566 /* Client: discard the Initial encryption keys as soon as
2567 * a handshake packet could be built.
2568 */
2569 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2570 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2571 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2572 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2573 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2574 qc_set_timer(qc);
2575 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2576 }
2577 /* If the data for the current encryption level have all been sent,
2578 * select the next level.
2579 */
2580 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002581 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002582 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2583 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2584 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2585 tel = next_tel;
2586 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002587 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002588 /* If there is data for the next level, do not
2589 * consume a datagram.
2590 */
2591 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002592 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002593 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002594 /* If we have to build a new datagram, set the current datagram as
2595 * prepared into <cbuf>.
2596 */
2597 if (!prv_pkt) {
2598 qc_set_dg(cbuf, dglen, first_pkt);
2599 first_pkt = NULL;
2600 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002601 padding = 0;
2602 }
2603 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002604 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002605 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2606 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002607 }
2608 }
2609
2610 stop_build:
2611 /* Reset <wr> writer index if in front of <rd> index */
2612 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2613 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2614
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002615 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002616 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2617 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2618 /* Mark the end of contiguous data for the reader */
2619 write_u16(cb_wr(cbuf), 0);
2620 cb_add(cbuf, sizeof(uint16_t));
2621 }
2622 }
2623
2624 if (rd && rd <= cbuf->wr) {
2625 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002626 /* Let's try to reuse this buffer */
2627 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002628 }
2629 }
2630
2631 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002632 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002633 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002634
2635 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002636 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002637 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638}
2639
2640/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002641 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002642 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002643int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002644{
2645 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002646 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002647
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002648 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002649 cbuf = qr->cbuf;
2650 while (cb_contig_data(cbuf)) {
2651 unsigned char *pos;
2652 struct buffer tmpbuf = { };
2653 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2654 uint16_t dglen;
2655 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002656 unsigned int time_sent;
2657
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002658 pos = cb_rd(cbuf);
2659 dglen = read_u16(pos);
2660 /* End of prepared datagrams.
2661 * Reset the reader index only if in front of the writer index.
2662 */
2663 if (!dglen) {
2664 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2665
2666 if (wr && wr < cbuf->rd) {
2667 cb_rd_reset(cbuf);
2668 continue;
2669 }
2670 break;
2671 }
2672
2673 pos += sizeof dglen;
2674 first_pkt = read_ptr(pos);
2675 pos += sizeof first_pkt;
2676 tmpbuf.area = (char *)pos;
2677 tmpbuf.size = tmpbuf.data = dglen;
2678
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002679 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002680 for (pkt = first_pkt; pkt; pkt = pkt->next)
2681 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002682 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002683 &tmpbuf, tmpbuf.data, 0) <= 0) {
2684 for (pkt = first_pkt; pkt; pkt = pkt->next)
2685 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002686 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002687 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002688
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002689 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002690 qc->tx.bytes += tmpbuf.data;
2691 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002692
2693 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2694 pkt->time_sent = time_sent;
2695 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2696 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002697 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002698 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002699 qc->path->in_flight += pkt->in_flight_len;
2700 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2701 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002702 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002703 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002704 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002705 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002706 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002707 }
2708 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002709
2710 return 1;
2711}
2712
2713/* Build all the frames which must be sent just after the handshake have succeeded.
2714 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2715 * a HANDSHAKE_DONE frame.
2716 * Return 1 if succeeded, 0 if not.
2717 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002718static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002719{
2720 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002721 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002722 struct quic_frame *frm;
2723
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002724 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002725 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002726 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002727 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002728 if (!frm)
2729 return 0;
2730
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002731 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002732 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002733 }
2734
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002735 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002736 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002737 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002738
2739 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002740 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002741 goto err;
2742
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002743 cid = new_quic_cid(&qc->cids, qc, i);
2744 if (!cid)
2745 goto err;
2746
2747 /* insert the allocated CID in the receiver tree */
2748 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2749 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2750 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2751
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002753 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002754 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002755 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756
2757 return 1;
2758
2759 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 return 0;
2761}
2762
2763/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002764void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002765{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002766 struct eb64_node *n;
2767 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002768
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002769 n = eb64_first(&arngs->root);
2770 while (n) {
2771 struct eb64_node *next;
2772
2773 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2774 next = eb64_next(n);
2775 eb64_delete(n);
2776 free(ar);
2777 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002778 }
2779}
2780
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002781/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2782 * descending order.
2783 */
2784static inline size_t sack_gap(struct quic_arng_node *p,
2785 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002786{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002787 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788}
2789
2790
2791/* Remove the last elements of <ack_ranges> list of ack range updating its
2792 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002793 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002795static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002796{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002797 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002799 last = eb64_last(&arngs->root);
2800 while (last && arngs->enc_sz > limit) {
2801 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002802
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002803 prev = eb64_prev(last);
2804 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002805 return 0;
2806
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002807 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2808 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2809 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2810 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2811 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2812 --arngs->sz;
2813 eb64_delete(last);
2814 pool_free(pool_head_quic_arng, last);
2815 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002816 }
2817
2818 return 1;
2819}
2820
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002821/* Set the encoded size of <arngs> QUIC ack ranges. */
2822static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2823{
2824 struct eb64_node *node, *next;
2825 struct quic_arng_node *ar, *ar_next;
2826
2827 node = eb64_last(&arngs->root);
2828 if (!node)
2829 return;
2830
2831 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2832 arngs->enc_sz = quic_int_getsize(ar->last) +
2833 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2834
2835 while ((next = eb64_prev(node))) {
2836 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2837 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2838 quic_int_getsize(ar_next->last - ar_next->first.key);
2839 node = next;
2840 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2841 }
2842}
2843
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002844/* Insert <ar> ack range into <argns> tree of ack ranges.
2845 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002846 */
2847static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002848struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2849 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002850{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002851 struct quic_arng_node *new_ar;
2852
2853 new_ar = pool_alloc(pool_head_quic_arng);
2854 if (new_ar) {
2855 new_ar->first.key = ar->first;
2856 new_ar->last = ar->last;
2857 eb64_insert(&arngs->root, &new_ar->first);
2858 arngs->sz++;
2859 }
2860
2861 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002862}
2863
2864/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002866 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002867 *
2868 * Descending order
2869 * ------------->
2870 * range1 range2
2871 * ..........|--------|..............|--------|
2872 * ^ ^ ^ ^
2873 * | | | |
2874 * last1 first1 last2 first2
2875 * ..........+--------+--------------+--------+......
2876 * diff1 gap12 diff2
2877 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002878 * To encode the previous list of ranges we must encode integers as follows in
2879 * descending order:
2880 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002881 * with diff1 = last1 - first1
2882 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002883 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002884 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002885 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002886int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2887 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002889 struct eb64_node *le;
2890 struct quic_arng_node *new_node;
2891 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002892
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002893 new = NULL;
2894 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002895 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002896 if (!new_node)
2897 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002898
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002899 goto out;
2900 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002901
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002902 le = eb64_lookup_le(&arngs->root, ar->first);
2903 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002904 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002905 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002906 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002907
2908 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002909 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002910 else {
2911 struct quic_arng_node *le_ar =
2912 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002913
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002914 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002915 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002916 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002917
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002918 if (le_ar->last + 1 >= ar->first) {
2919 le_ar->last = ar->last;
2920 new = le;
2921 new_node = le_ar;
2922 }
2923 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002924 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002925 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002926 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002927
2928 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002929 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002930 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002931
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002932 /* Verify that the new inserted node does not overlap the nodes
2933 * which follow it.
2934 */
2935 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002936 struct eb64_node *next;
2937 struct quic_arng_node *next_node;
2938
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002939 while ((next = eb64_next(new))) {
2940 next_node =
2941 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002942 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002943 break;
2944
2945 if (next_node->last > new_node->last)
2946 new_node->last = next_node->last;
2947 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002948 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002949 /* Decrement the size of these ranges. */
2950 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002951 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002952 }
2953
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002954 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002955 quic_arngs_set_enc_sz(arngs);
2956
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002957 return 1;
2958}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002959/* Remove the header protection of packets at <el> encryption level.
2960 * Always succeeds.
2961 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002962static 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 +01002963{
2964 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002965 struct quic_rx_packet *pqpkt;
2966 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002967 struct quic_enc_level *app_qel;
2968
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002969 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
2970 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002971 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002972 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002973 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002975 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002976 goto out;
2977 }
2978 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002979 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002980 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002981 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002982 pqpkt->data, pqpkt->data + pqpkt->len)) {
2983 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002984 /* XXX TO DO XXX */
2985 }
2986 else {
2987 /* The AAD includes the packet number field */
2988 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2989 /* Store the packet into the tree of packets to decrypt. */
2990 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002991 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002992 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2993 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002994 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002995 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002997 MT_LIST_DELETE_SAFE(pkttmp1);
2998 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999 }
3000
3001 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003002 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003003}
3004
3005/* Process all the CRYPTO frame at <el> encryption level.
3006 * Return 1 if succeeded, 0 if not.
3007 */
3008static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003009 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003010{
3011 struct eb64_node *node;
3012
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013 node = eb64_first(&el->rx.crypto.frms);
3014 while (node) {
3015 struct quic_rx_crypto_frm *cf;
3016
3017 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3018 if (cf->offset_node.key != el->rx.crypto.offset)
3019 break;
3020
3021 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3022 goto err;
3023
3024 node = eb64_next(node);
3025 quic_rx_packet_refdec(cf->pkt);
3026 eb64_delete(&cf->offset_node);
3027 pool_free(pool_head_quic_rx_crypto_frm, cf);
3028 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029 return 1;
3030
3031 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003032 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003033 return 0;
3034}
3035
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003036/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003037 * 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 +02003038 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039 * Return 1 if succeeded, 0 if not.
3040 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003041int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3042 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003045 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003046 struct quic_conn *qc = ctx->conn->qc;
3047 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003048
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003049 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003050 qel = cur_el;
3051 next_tel:
3052 if (!qel)
3053 goto out;
3054
3055 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3056 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003057 while (node) {
3058 struct quic_rx_packet *pkt;
3059
3060 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003061 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003062 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003063 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003064 /* Drop the packet */
3065 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003066 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003067 }
3068 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003069 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003070 /* Drop the packet */
3071 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003072 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003073 }
3074 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003075 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3076
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003077 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003078 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003079 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003080 if (pkt->pn > largest_pn)
3081 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003082 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003083 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003084 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003085 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086 }
3087 }
3088 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003089 eb64_delete(&pkt->pn_node);
3090 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003092 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003093
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003094 /* Update the largest packet number. */
3095 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003096 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3097 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003098 goto err;
3099
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003100 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003101 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003102 qel = next_el;
3103 goto next_tel;
3104 }
3105
3106 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003107 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108 return 1;
3109
3110 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003111 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003112 return 0;
3113}
3114
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003115/* QUIC connection packet handler task. */
3116struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003118 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003119 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003120 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003121 enum quic_tls_enc_level tel, next_tel;
3122 struct quic_enc_level *qel, *next_qel;
3123 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003124 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003125 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003126
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003127 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003128 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003129 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003130 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003131 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003132 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003133 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3134 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3135 /* The I/O handler has been woken up by the dgram listener
3136 * after the anti-amplification was reached.
3137 */
3138 qc_set_timer(qc);
3139 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3140 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3141 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003142 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003143 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3144 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3145 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003146 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003147 if (st >= QUIC_HS_ST_COMPLETE &&
3148 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3149 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3150 /* There may be remaining Handshake packets to treat and acknowledge. */
3151 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3152 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3153 }
3154 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155 goto err;
3156
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003157 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003158 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003159
3160 next_level:
3161 tls_ctx = &qel->tls_ctx;
3162
3163 /* If the header protection key for this level has been derived,
3164 * remove the packet header protections.
3165 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003166 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003167 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003168 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003169
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003170 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3171 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3172 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003173 goto err;
3174
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003175 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3176 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3177 qel = next_qel;
3178 next_qel = NULL;
3179 goto next_level;
3180 }
3181
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003182 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003183 if (st >= QUIC_HS_ST_COMPLETE) {
3184 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3185 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003186 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003187
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003188 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3189 QUIC_FL_TLS_SECRETS_DCD)) {
3190 /* Discard the Handshake keys. */
3191 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3192 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3193 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3194 qc_set_timer(qc);
3195 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3196 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3197 }
3198
3199 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3200 /* There may be remaining handshake to build (acks) */
3201 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3202 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003203 }
3204
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003205 if (!qr)
3206 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003207 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3208 * be considered.
3209 */
3210 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003211 goto err;
3212 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003213 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003214 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003215 else if (ret == 0)
3216 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003217
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003218 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003219 goto err;
3220
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003221 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003222 /* Check if there is something to do for the next level.
3223 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003224 if (next_qel && next_qel != qel &&
3225 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003226 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003227 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003228 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003229 goto next_level;
3230 }
3231
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003232 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003233 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003234 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003235
3236 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003237 if (qr)
3238 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003239 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003240 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241}
3242
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003243/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003244static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3245{
3246 int i;
3247
3248 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3249 if (qel->tx.crypto.bufs[i]) {
3250 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3251 qel->tx.crypto.bufs[i] = NULL;
3252 }
3253 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003254 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003255}
3256
3257/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003258 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003259 * Returns 1 if succeeded, 0 if not.
3260 */
3261static int quic_conn_enc_level_init(struct quic_conn *qc,
3262 enum quic_tls_enc_level level)
3263{
3264 struct quic_enc_level *qel;
3265
3266 qel = &qc->els[level];
3267 qel->level = quic_to_ssl_enc_level(level);
3268 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3269 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3270 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3271 qel->tls_ctx.rx.flags = 0;
3272 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003273 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003274
3275 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003276 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003277 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003278 qel->rx.crypto.offset = 0;
3279 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280
3281 /* Allocate only one buffer. */
3282 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3283 if (!qel->tx.crypto.bufs)
3284 goto err;
3285
3286 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3287 if (!qel->tx.crypto.bufs[0])
3288 goto err;
3289
3290 qel->tx.crypto.bufs[0]->sz = 0;
3291 qel->tx.crypto.nb_buf = 1;
3292
3293 qel->tx.crypto.sz = 0;
3294 qel->tx.crypto.offset = 0;
3295
3296 return 1;
3297
3298 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003299 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003300 return 0;
3301}
3302
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003303/* Increment the <qc> refcount.
3304 *
3305 * This operation must be conducted when manipulating the quic_conn outside of
3306 * the connection pinned thread. These threads can only retrieve the connection
3307 * in the CID tree, so this function must be conducted under the CID lock.
3308 */
3309static inline void quic_conn_take(struct quic_conn *qc)
3310{
3311 HA_ATOMIC_INC(&qc->refcount);
3312}
3313
3314/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003315 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003316 */
3317static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003318{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003319 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320 int i;
3321
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003322 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003323 return;
3324
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003325 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3326 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) {
3327 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3328 return;
3329 }
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003330
3331 /* remove the connection from receiver cids trees */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003332 ebmb_delete(&qc->odcid_node);
3333 ebmb_delete(&qc->scid_node);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003334 free_quic_conn_cids(qc);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003335 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003336
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003337 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003338 if (conn_ctx)
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003339 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003340
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003341 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003342 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003343
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003344 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3345 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003346 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003347}
3348
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003349void quic_close(struct connection *conn, void *xprt_ctx)
3350{
3351 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3352 struct quic_conn *qc = conn_ctx->conn->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003353
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003354 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003355 /* This task must be deleted by the connection-pinned thread. */
3356 if (qc->timer_task) {
3357 task_destroy(qc->timer_task);
3358 qc->timer_task = NULL;
3359 }
3360
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003361 if (conn_ctx->wait_event.tasklet) {
3362 tasklet_free(conn_ctx->wait_event.tasklet);
3363 conn_ctx->wait_event.tasklet = NULL;
3364 }
3365
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003366 quic_conn_drop(qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003367 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003368}
3369
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003370/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003371static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003372{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003373 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003374 struct quic_conn *qc;
3375 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003376 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003377
3378 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003379 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003380 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003381 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003382 task->expire = TICK_ETERNITY;
3383 pktns = quic_loss_pktns(qc);
3384 if (tick_isset(pktns->tx.loss_time)) {
3385 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3386
3387 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3388 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003389 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3390 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003391 goto out;
3392 }
3393
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003394 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003395 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003396 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003397 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3398 pktns->tx.pto_probe = 1;
3399 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3400 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3401 }
3402 else {
3403 pktns->tx.pto_probe = 2;
3404 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003405 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003406 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3408 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3409
3410 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3411 hel->pktns->tx.pto_probe = 1;
3412 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3413 iel->pktns->tx.pto_probe = 1;
3414 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003415
3416 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3417 int j;
3418
3419 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3420 continue;
3421
3422 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3423 qc_prep_fast_retrans(&qc->els[i], qc);
3424 }
3425
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003426 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3427 qc->path->loss.pto_count++;
3428
3429 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003430 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003431
3432 return task;
3433}
3434
3435/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3436 * connections used to identify the first Initial packets of client connecting
3437 * to listeners. This parameter must be NULL for QUIC connections attached
3438 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3439 * <scid> is the source connection ID with <scid_len> as length.
3440 * Returns 1 if succeeded, 0 if not.
3441 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003442static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003443 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003444 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003445{
3446 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003447 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003448 /* Initial CID. */
3449 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003450 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003451 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003452
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003453 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003454 qc = pool_zalloc(pool_head_quic_conn);
3455 if (!qc) {
3456 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3457 goto err;
3458 }
3459
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003460 HA_ATOMIC_STORE(&qc->refcount, 0);
3461
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003462 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3463 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003464 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003465 goto err;
3466 }
3467
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003468 qc->cids = EB_ROOT;
3469 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003470 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003471 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003472
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003473 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003474 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003475 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003477 qc->odcid.addrlen = dcid_addr_len;
3478 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003479
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003480 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003481 if (scid_len)
3482 memcpy(qc->dcid.data, scid, scid_len);
3483 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003484 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003485 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486 }
3487 /* QUIC Client (outgoing connection to servers) */
3488 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003489 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490 if (dcid_len)
3491 memcpy(qc->dcid.data, dcid, dcid_len);
3492 qc->dcid.len = dcid_len;
3493 }
3494
3495 /* Initialize the output buffer */
3496 qc->obuf.pos = qc->obuf.data;
3497
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003498 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003499 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003500 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003501 goto err;
3502 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003503
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003504 /* insert the allocated CID in the receiver tree */
3505 if (server) {
3506 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3507 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3508 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3509 }
3510
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003511 /* Select our SCID which is the first CID with 0 as sequence number. */
3512 qc->scid = icid->cid;
3513
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514 /* Packet number spaces initialization. */
3515 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3516 quic_pktns_init(&qc->pktns[i]);
3517 /* QUIC encryption level context initialization. */
3518 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003519 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003520 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003522 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003523 /* Initialize the packet number space. */
3524 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3525 }
3526
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003527 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003528 qc->tps_tls_ext = qc->version & 0xff000000 ?
3529 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3530 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003531 /* TX part. */
3532 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003533 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3534 qc->tx.wbuf = qc->tx.rbuf = 0;
3535 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003536 /* RX part. */
3537 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003538 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003539 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3540 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3541 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003542 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003543 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003544 goto err;
3545 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003546
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547 /* XXX TO DO: Only one path at this time. */
3548 qc->path = &qc->paths[0];
3549 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3550
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003551 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003552
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003553 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003554
3555 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003556 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003557 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003558 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003559}
3560
3561/* Initialize the timer task of <qc> QUIC connection.
3562 * Returns 1 if succeeded, 0 if not.
3563 */
3564static int quic_conn_init_timer(struct quic_conn *qc)
3565{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003566 /* Attach this task to the same thread ID used for the connection */
3567 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003568 if (!qc->timer_task)
3569 return 0;
3570
3571 qc->timer = TICK_ETERNITY;
3572 qc->timer_task->process = process_timer;
3573 qc->timer_task->context = qc->conn->xprt_ctx;
3574
3575 return 1;
3576}
3577
3578/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3579 * past one byte of this buffer.
3580 */
3581static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3582 struct quic_rx_packet *pkt)
3583{
3584 unsigned char dcid_len, scid_len;
3585
3586 /* Version */
3587 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3588 return 0;
3589
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 /* Destination Connection ID Length */
3591 dcid_len = *(*buf)++;
3592 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3593 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3594 /* XXX MUST BE DROPPED */
3595 return 0;
3596
3597 if (dcid_len) {
3598 /* Check that the length of this received DCID matches the CID lengths
3599 * of our implementation for non Initials packets only.
3600 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003601 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3602 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003603 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003604 return 0;
3605
3606 memcpy(pkt->dcid.data, *buf, dcid_len);
3607 }
3608
3609 pkt->dcid.len = dcid_len;
3610 *buf += dcid_len;
3611
3612 /* Source Connection ID Length */
3613 scid_len = *(*buf)++;
3614 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3615 /* XXX MUST BE DROPPED */
3616 return 0;
3617
3618 if (scid_len)
3619 memcpy(pkt->scid.data, *buf, scid_len);
3620 pkt->scid.len = scid_len;
3621 *buf += scid_len;
3622
3623 return 1;
3624}
3625
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003626/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3627 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3628 * encryption level matching with the packet type. <*qel> may be null if not found.
3629 * Note that <ctx> may be null (for Initial packets).
3630 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003631static 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 +02003632 struct quic_enc_level **qel)
3633{
3634 enum quic_tls_enc_level tel;
3635
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003636 tel = quic_packet_type_enc_level(pkt->type);
3637 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3638 *qel = NULL;
3639 return 0;
3640 }
3641
3642 *qel = &qc->els[tel];
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003643 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003644 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003645
3646 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003647 (tel != QUIC_TLS_ENC_LEVEL_APP ||
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003648 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003649 return 1;
3650
3651 return 0;
3652}
3653
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003654/* Insert <pkt> RX packet in its <qel> RX packets tree */
3655static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3656{
3657 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003658 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003659 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3660 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3661 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003662}
3663
3664/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003665 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3666 * byte past the end of the buffer containing this packet and <beg> the address of
3667 * the packet first byte.
3668 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3669 * Returns 1 if succeeded, 0 if not.
3670 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003671static inline int qc_try_rm_hp(struct quic_conn *qc,
3672 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003673 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003674 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003675 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676{
3677 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003678 struct quic_enc_level *qel;
3679 /* Only for traces. */
3680 struct quic_rx_packet *qpkt_trace;
3681
3682 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003683 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684 /* The packet number is here. This is also the start minus
3685 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3686 * protection.
3687 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003688 pn = buf;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003689 if (qc_pkt_may_rm_hp(qc, pkt, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003690 /* Note that the following function enables us to unprotect the packet
3691 * number and its length subsequently used to decrypt the entire
3692 * packets.
3693 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003694 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3695 qel->pktns->rx.largest_pn, pn, beg, end)) {
3696 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003697 goto err;
3698 }
3699
3700 /* The AAD includes the packet number field found at <pn>. */
3701 pkt->aad_len = pn - beg + pkt->pnl;
3702 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003703 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003704 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003705 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3706 /* If the packet number space has been discarded, this packet
3707 * will be not parsed.
3708 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003709 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003710 goto out;
3711 }
3712
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003713 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003714 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003715 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3716 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003717 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003718 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003719 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003720 goto err;
3721 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003723 *el = qel;
3724 /* No reference counter incrementation here!!! */
3725 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3726 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3727 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3728 b_add(&qc->rx.buf, pkt->len);
3729 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003730 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003731 return 1;
3732
3733 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003734 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003735 return 0;
3736}
3737
3738/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3739 * Also set <*long_header> to 1 if this form is long, 0 if not.
3740 */
3741static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3742 unsigned char byte0, int *long_header)
3743{
3744 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3745 pkt->type =
3746 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3747 *long_header = 1;
3748 }
3749 else {
3750 pkt->type = QUIC_PACKET_TYPE_SHORT;
3751 *long_header = 0;
3752 }
3753}
3754
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003755/*
3756 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3757 */
3758static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3759{
3760 int j = 0, version;
3761
3762 do {
3763 version = quic_supported_version[j];
3764 if (version == pkt->version)
3765 return 1;
3766
3767 version = quic_supported_version[++j];
3768 } while(version);
3769
3770 return 0;
3771}
3772
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003773__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003774static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3775 struct quic_rx_packet *pkt,
3776 struct quic_dgram_ctx *dgram_ctx,
3777 struct sockaddr_storage *saddr)
3778{
3779 unsigned char *beg;
3780 uint64_t len;
3781 struct quic_conn *qc;
3782 struct eb_root *cids;
3783 struct ebmb_node *node;
3784 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003785 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003786 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003787 size_t b_cspace;
3788 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789
3790 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003791 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3793 if (end <= *buf)
3794 goto err;
3795
3796 /* Fixed bit */
3797 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3798 /* XXX TO BE DISCARDED */
3799 goto err;
3800
3801 srv_conn = dgram_ctx->owner;
3802 beg = *buf;
3803 /* Header form */
3804 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3805 if (long_header) {
3806 size_t cid_lookup_len;
3807
3808 if (!quic_packet_read_long_header(buf, end, pkt))
3809 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003810
3811 /* unsupported QUIC version */
3812 if (!qc_pkt_is_supported_version(pkt)) {
3813 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3814 goto err;
3815 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816
3817 /* For Initial packets, and for servers (QUIC clients connections),
3818 * there is no Initial connection IDs storage.
3819 */
3820 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3821 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3822 cid_lookup_len = pkt->dcid.len;
3823 }
3824 else {
3825 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003826 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003827 }
3828
3829 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3830 if (!node)
3831 goto err;
3832
3833 qc = ebmb_entry(node, struct quic_conn, scid_node);
3834
3835 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3836 qc->dcid.len = pkt->scid.len;
3837 if (pkt->scid.len)
3838 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3839 }
3840
3841 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3842 uint64_t token_len;
3843
3844 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3845 goto err;
3846
3847 /* XXX TO DO XXX 0 value means "the token is not present".
3848 * A server which sends an Initial packet must not set the token.
3849 * So, a client which receives an Initial packet with a token
3850 * MUST discard the packet or generate a connection error with
3851 * PROTOCOL_VIOLATION as type.
3852 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3853 */
3854 pkt->token_len = token_len;
3855 }
3856 }
3857 else {
3858 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003859 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003860 goto err;
3861
3862 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003863 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003864 if (!node)
3865 goto err;
3866
3867 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003868 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003869 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003870
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003871 /* Only packets packets with long headers and not RETRY or VERSION as type
3872 * have a length field.
3873 */
3874 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3875 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3876 goto err;
3877
3878 pkt->len = len;
3879 }
3880 else if (!long_header) {
3881 /* A short packet is the last one of an UDP datagram. */
3882 pkt->len = end - *buf;
3883 }
3884
3885 conn_ctx = qc->conn->xprt_ctx;
3886
3887 /* Increase the total length of this packet by the header length. */
3888 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003889
3890 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3891 * they must have the same DCID.
3892 *
3893 * This check must be done after the final update to pkt.len to
3894 * properly drop the packet on failure.
3895 */
3896 if (!dgram_ctx->dcid.len) {
3897 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3898 }
3899 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003900 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003901 goto err;
3902 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003903 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003904
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003905 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3906 b_cspace = b_contig_space(&qc->rx.buf);
3907 if (b_cspace < pkt->len) {
3908 /* Let us consume the remaining contiguous space. */
3909 b_add(&qc->rx.buf, b_cspace);
3910 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3911 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003912 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003913 goto err;
3914 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003915 }
3916
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003917 if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003918 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003919 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003920 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003921 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003922
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003923 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3924 if (pkt->aad_len)
3925 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003926 /* Wake the tasklet of the QUIC connection packet handler. */
3927 if (conn_ctx)
3928 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3929
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003930 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003931
3932 return pkt->len;
3933
3934 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003935 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003936 return -1;
3937}
3938
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003939/*
3940 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3941 * address <addr>.
3942 * Implementation of RFC9000 6. Version Negotiation
3943 *
3944 * TODO implement a rate-limiting sending of Version Negotiation packets
3945 *
3946 * Returns 0 on success else non-zero
3947 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003948static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3949 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003950{
3951 char buf[256];
3952 int i = 0, j, version;
3953 const socklen_t addrlen = get_addr_len(addr);
3954
3955 /*
3956 * header form
3957 * long header, fixed bit to 0 for Version Negotiation
3958 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003959 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3960 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003961
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003962 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003963 /* null version for Version Negotiation */
3964 buf[i++] = '\x00';
3965 buf[i++] = '\x00';
3966 buf[i++] = '\x00';
3967 buf[i++] = '\x00';
3968
3969 /* source connection id */
3970 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003971 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003972 i += pkt->scid.len;
3973
3974 /* destination connection id */
3975 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003976 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003977 i += pkt->dcid.len;
3978
3979 /* supported version */
3980 j = 0;
3981 do {
3982 version = htonl(quic_supported_version[j]);
3983 memcpy(&buf[i], &version, sizeof(version));
3984 i += sizeof(version);
3985
3986 version = quic_supported_version[++j];
3987 } while (version);
3988
3989 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3990 return 1;
3991
3992 return 0;
3993}
3994
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003995/* Generate the token to be used in Retry packets. The token is written to
3996 * <buf> which is expected to be <len> bytes.
3997 *
3998 * Various parameters are expected to be encoded in the token. For now, only
3999 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4000 * as this CID must be repeated by the server in the transport parameters.
4001 *
4002 * TODO add the client address to validate the token origin.
4003 *
4004 * Returns the length of the encoded token or 0 on error.
4005 */
4006static int generate_retry_token(unsigned char *buf, unsigned char len,
4007 struct quic_rx_packet *pkt)
4008{
4009 const size_t token_len = 1 + pkt->dcid.len;
4010 unsigned char i = 0;
4011
4012 if (token_len > len)
4013 return 0;
4014
4015 buf[i++] = pkt->dcid.len;
4016 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4017 i += pkt->dcid.len;
4018
4019 return i;
4020}
4021
4022/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4023 * the Initial <pkt> packet.
4024 *
4025 * Returns 0 on success else non-zero.
4026 */
4027static int send_retry(int fd, struct sockaddr_storage *addr,
4028 struct quic_rx_packet *pkt)
4029{
4030 unsigned char buf[128];
4031 int i = 0, token_len;
4032 const socklen_t addrlen = get_addr_len(addr);
4033 struct quic_cid scid;
4034
4035 /* long header + fixed bit + packet type 0x3 */
4036 buf[i++] = 0xf0;
4037 /* version */
4038 buf[i++] = 0x00;
4039 buf[i++] = 0x00;
4040 buf[i++] = 0x00;
4041 buf[i++] = 0x01;
4042
4043 /* Use the SCID from <pkt> for Retry DCID. */
4044 buf[i++] = pkt->scid.len;
4045 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4046 i += pkt->scid.len;
4047
4048 /* Generate a new CID to be used as SCID for the Retry packet. */
4049 scid.len = QUIC_HAP_CID_LEN;
4050 if (RAND_bytes(scid.data, scid.len) != 1)
4051 return 1;
4052
4053 buf[i++] = scid.len;
4054 memcpy(&buf[i], scid.data, scid.len);
4055 i += scid.len;
4056
4057 /* token */
4058 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4059 return 1;
4060 i += token_len;
4061
4062 /* token integrity tag */
4063 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4064 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4065 pkt->dcid.len, buf, i)) {
4066 return 1;
4067 }
4068
4069 i += QUIC_TLS_TAG_LEN;
4070
4071 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4072 return 1;
4073
4074 return 0;
4075}
4076
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004077/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4078 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4079 * concatenated to the <pkt> DCID field.
4080 *
4081 * Returns the instance or NULL if not found.
4082 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004083static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004084 struct listener *l,
4085 struct sockaddr_storage *saddr)
4086{
4087 struct quic_conn *qc = NULL;
4088 struct ebmb_node *node;
4089 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004090 /* set if the quic_conn is found in the second DCID tree */
4091 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004092
4093 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
4094
4095 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4096 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4097 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4098 /* DCIDs of first packets coming from multiple clients may have
4099 * the same values. Let's distinguish them by concatenating the
4100 * socket addresses.
4101 */
4102 quic_cid_saddr_cat(&pkt->dcid, saddr);
4103 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
4104 pkt->dcid.len + pkt->dcid.addrlen);
4105 if (node) {
4106 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4107 goto end;
4108 }
4109 }
4110
4111 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4112 * also for INITIAL/0-RTT non-first packets with the final DCID in
4113 * used.
4114 */
4115 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
4116 if (!node)
4117 goto end;
4118
4119 id = ebmb_entry(node, struct quic_connection_id, node);
4120 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004121 found_in_dcid = 1;
4122
4123 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004124 if (qc)
4125 quic_conn_take(qc);
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004126 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004127
4128 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4129 * If already done, this is a noop.
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004130 *
4131 * node.leaf_p is first checked to avoid unnecessary locking.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004132 */
Amaury Denoyellec6fab982021-12-23 16:27:56 +01004133 if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) {
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004134 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
4135 ebmb_delete(&qc->odcid_node);
4136 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4137 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004138
4139 return qc;
4140}
4141
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004142/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4143 * will extract the parameters stored in the token : <odcid>.
4144 *
4145 * Returns 0 on success else non-zero.
4146 */
4147static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4148 struct quic_cid *odcid)
4149{
4150 uint64_t odcid_len;
4151
4152 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4153 return 1;
4154
4155 memcpy(odcid->data, token, odcid_len);
4156 odcid->len = odcid_len;
4157
4158 return 0;
4159}
4160
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004161static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004162 struct quic_rx_packet *pkt,
4163 struct quic_dgram_ctx *dgram_ctx,
4164 struct sockaddr_storage *saddr)
4165{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004166 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004167 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004169 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004170 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004171 size_t b_cspace;
4172 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004173
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004174 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004175 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004176 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004177 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004178 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4179 /* This ist only to please to traces and distinguish the
4180 * packet with parsed packet number from others.
4181 */
4182 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004183 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004184 goto err;
4185
4186 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004187 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188 /* XXX TO BE DISCARDED */
4189 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4190 goto err;
4191 }
4192
4193 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004195 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004197 uint64_t len;
4198
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004199 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004200 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4201 goto err;
4202 }
4203
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004204 /* Retry of Version Negotiation packets are only sent by servers */
4205 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4206 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4207 goto err;
4208 }
4209
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004210 /* RFC9000 6. Version Negotiation */
4211 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004212 /* unsupported version, send Negotiation packet */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004213 if (send_version_negotiation(l->rx.fd, saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004214 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4215 goto err;
4216 }
4217
4218 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004219 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004220 }
4221
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004222 /* For Initial packets, and for servers (QUIC clients connections),
4223 * there is no Initial connection IDs storage.
4224 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004225 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004226 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004227
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004228 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4229 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004230 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4231 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004232 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004233
4234 /* XXX TO DO XXX 0 value means "the token is not present".
4235 * A server which sends an Initial packet must not set the token.
4236 * So, a client which receives an Initial packet with a token
4237 * MUST discard the packet or generate a connection error with
4238 * PROTOCOL_VIOLATION as type.
4239 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4240 */
4241 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004242
4243 /* TODO Retry should be automatically activated if
4244 * suspect network usage is detected.
4245 */
4246 if (!token_len && l->bind_conf->quic_force_retry) {
4247 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
4248 if (send_retry(l->rx.fd, saddr, pkt)) {
4249 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4250 goto err;
4251 }
4252
4253 goto err;
4254 }
4255 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004256 pkt->token = buf;
4257 buf += pkt->token_len;
4258 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004259 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004260 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004261 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004262 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4263 goto err;
4264 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004265 }
4266
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004267 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4268 end - buf < len) {
4269 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4270 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004271 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004272
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004273 payload = buf;
4274 pkt->len = len + payload - beg;
4275
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004276 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004277 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004278 int ipv4;
4279 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004280 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004281 const unsigned char *salt = initial_salt_v1;
4282 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004283
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004284 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004285 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004286 goto err;
4287 }
4288
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004289 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004290 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004291 qc = qc_new_conn(pkt->version, ipv4,
4292 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004293 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004294 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004295 goto err;
4296
4297 odcid = &qc->rx.params.original_destination_connection_id;
4298 /* Copy the transport parameters. */
4299 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004300
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004301 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004302 if (pkt->token_len) {
4303 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4304 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4305 goto err;
4306 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004307 /* Copy retry_source_connection_id transport parameter. */
4308 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4309 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004310 }
4311 else {
4312 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4313 odcid->len = pkt->dcid.len;
4314 }
4315
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004316 /* Copy the initial source connection ID. */
4317 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4318 qc->enc_params_len =
4319 quic_transport_params_encode(qc->enc_params,
4320 qc->enc_params + sizeof qc->enc_params,
4321 &qc->rx.params, 1);
4322 if (!qc->enc_params_len)
4323 goto err;
4324
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004325 /* NOTE: the socket address has been concatenated to the destination ID
4326 * chosen by the client for Initial packets.
4327 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004328 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4329 salt = initial_salt_draft_29;
4330 salt_len = sizeof initial_salt_draft_29;
4331 }
4332 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004333 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004334 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004335 goto err;
4336 }
4337
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004338 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004339 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004340 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4341 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004342
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004343 /* If the insertion failed, it means that another
4344 * thread has already allocated a QUIC connection for
4345 * the same CID. Liberate our allocated connection.
4346 */
4347 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004348 qc_to_purge = qc;
4349
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004350 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4351 pkt->qc = qc;
4352 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004353
4354 quic_conn_take(qc);
4355 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4356
4357 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004358 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004359 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004360 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4361 /* Try to accept a new connection. */
4362 listener_accept(l);
4363 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004364 else {
4365 quic_conn_drop(qc_to_purge);
4366 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004367 }
4368 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004369 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004370 }
4371 }
4372 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004373 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004374 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4375 goto err;
4376 }
4377
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004378 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004379 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004380 buf += QUIC_HAP_CID_LEN;
4381
4382 /* A short packet is the last one of a UDP datagram. */
4383 payload = buf;
4384 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004385
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004386 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004387 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004388 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004389 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4390 goto err;
4391 }
4392
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004393 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004394 }
4395
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004396
4397 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4398 * they must have the same DCID.
4399 *
4400 * This check must be done after the final update to pkt.len to
4401 * properly drop the packet on failure.
4402 */
4403 if (!dgram_ctx->dcid.len) {
4404 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004405 if (!quic_peer_validated_addr(qc) &&
4406 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4407 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4408 QUIC_EV_CONN_LPKT, qc);
4409 /* Reset the anti-amplification bit. It will be set again
4410 * when sending the next packet if reached again.
4411 */
4412 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4413 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4414 io_cb_wakeup = 1;
4415 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004416 }
4417 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004418 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004419 goto err;
4420 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004421 dgram_ctx->qc = qc;
4422
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004423
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004424 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004425 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004426 goto out;
4427 }
4428
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004429 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004430 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004431 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004432 b_cspace = b_contig_space(&qc->rx.buf);
4433 if (b_cspace < pkt->len) {
4434 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004435 if (b_cspace) {
4436 b_putchr(&qc->rx.buf, 0x00);
4437 b_cspace--;
4438 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004439 b_add(&qc->rx.buf, b_cspace);
4440 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4441 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004442 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004443 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004444 goto err;
4445 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004446 }
4447
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004448 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004449 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004450 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004451 goto err;
4452 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004454 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004455 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004456 if (pkt->aad_len)
4457 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004458 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004459 /* Wake up the connection packet handler task from here only if all
4460 * the contexts have been initialized, especially the mux context
4461 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4462 * will start it if these contexts for the connection are not already
4463 * initialized.
4464 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004465 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Frédéric Lécailleb80b20c2022-01-12 17:46:56 +01004466 if (conn_ctx && HA_ATOMIC_LOAD(&qc->qcc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004468
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004469 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004470
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004471 if (qc)
4472 quic_conn_drop(qc);
4473
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004474 return pkt->len;
4475
4476 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004477 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4478 * This cannot be done by this thread.
4479 */
4480 if (io_cb_wakeup) {
4481 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4482 if (conn_ctx && conn_ctx->wait_event.tasklet)
4483 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4484 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004485 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004486 if (!pkt->len)
4487 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004488 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004489 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004490
4491 if (qc)
4492 quic_conn_drop(qc);
4493
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004494 return -1;
4495}
4496
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004497/* This function builds into <buf> buffer a QUIC long packet header.
4498 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004499 */
4500static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4501 int type, size_t pn_len, struct quic_conn *conn)
4502{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004503 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004504 return 0;
4505
4506 /* #0 byte flags */
4507 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4508 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4509 /* Version */
4510 quic_write_uint32(buf, end, conn->version);
4511 *(*buf)++ = conn->dcid.len;
4512 /* Destination connection ID */
4513 if (conn->dcid.len) {
4514 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4515 *buf += conn->dcid.len;
4516 }
4517 /* Source connection ID */
4518 *(*buf)++ = conn->scid.len;
4519 if (conn->scid.len) {
4520 memcpy(*buf, conn->scid.data, conn->scid.len);
4521 *buf += conn->scid.len;
4522 }
4523
4524 return 1;
4525}
4526
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004527/* This function builds into <buf> buffer a QUIC short packet header.
4528 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004529 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004530static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4531 size_t pn_len, struct quic_conn *conn,
4532 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004533{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004534 if (end - *buf < 1 + conn->dcid.len)
4535 return 0;
4536
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004537 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004538 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4539 ((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 +01004540 /* Destination connection ID */
4541 if (conn->dcid.len) {
4542 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4543 *buf += conn->dcid.len;
4544 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004545
4546 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004547}
4548
4549/* Apply QUIC header protection to the packet with <buf> as first byte address,
4550 * <pn> as address of the Packet number field, <pnlen> being this field length
4551 * with <aead> as AEAD cipher and <key> as secret key.
4552 * Returns 1 if succeeded or 0 if failed.
4553 */
4554static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4555 const EVP_CIPHER *aead, const unsigned char *key)
4556{
4557 int i, ret, outlen;
4558 EVP_CIPHER_CTX *ctx;
4559 /* We need an IV of at least 5 bytes: one byte for bytes #0
4560 * and at most 4 bytes for the packet number
4561 */
4562 unsigned char mask[5] = {0};
4563
4564 ret = 0;
4565 ctx = EVP_CIPHER_CTX_new();
4566 if (!ctx)
4567 return 0;
4568
4569 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4570 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4571 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4572 goto out;
4573
4574 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4575 for (i = 0; i < pnlen; i++)
4576 pn[i] ^= mask[i + 1];
4577
4578 ret = 1;
4579
4580 out:
4581 EVP_CIPHER_CTX_free(ctx);
4582
4583 return ret;
4584}
4585
4586/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4587 * ACK ranges if needed to a value below <limit> in bytes.
4588 * Return 1 if succeeded, 0 if not.
4589 */
4590static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4591{
4592 size_t room, ack_delay_sz;
4593
4594 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4595 /* A frame is made of 1 byte for the frame type. */
4596 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004597 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004598 return 0;
4599
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004600 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004601}
4602
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004603/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4604 * 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 +01004605 * and <*len> the packet Length field initialized with the number of bytes already present
4606 * 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 +02004607 * <headlen> is the number of bytes already present in this packet before building frames.
4608 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004609 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004610 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004611 * Return 1 if succeeded, 0 if not.
4612 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004613static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004614 size_t room, size_t *len, size_t headlen,
4615 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004616 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004617{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004618 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004619 struct quic_frame *cf, *cfbak;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004620 size_t remain = quic_path_prep_data(qc->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004621
Frédéric Lécailleea604992020-12-24 13:01:37 +01004622 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004623 if (*len > room || headlen > remain)
4624 return 0;
4625
Frédéric Lécailleea604992020-12-24 13:01:37 +01004626 /* If we are not probing we must take into an account the congestion
4627 * control window.
4628 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004629 if (!qel->pktns->tx.pto_probe)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004630 room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004631 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004632 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004633 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004634 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004635 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004636
Frédéric Lécailleea604992020-12-24 13:01:37 +01004637 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004638 break;
4639
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004640 switch (cf->type) {
4641 case QUIC_FT_CRYPTO:
4642 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004643 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004644 /* Compute the length of this CRYPTO frame header */
4645 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4646 /* Compute the data length of this CRyPTO frame. */
4647 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4648 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004649 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004650 if (!dlen)
4651 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004652
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004653 /* CRYPTO frame length. */
4654 flen = hlen + quic_int_getsize(dlen) + dlen;
4655 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004656 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004657 /* Add the CRYPTO data length and its encoded length to the packet
4658 * length and the length of this length.
4659 */
4660 *len += flen;
4661 room -= flen;
4662 if (dlen == cf->crypto.len) {
4663 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004664 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004665 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004666 }
4667 else {
4668 struct quic_frame *new_cf;
4669
4670 new_cf = pool_alloc(pool_head_quic_frame);
4671 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004672 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004673 return 0;
4674 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004675
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004676 new_cf->type = QUIC_FT_CRYPTO;
4677 new_cf->crypto.len = dlen;
4678 new_cf->crypto.offset = cf->crypto.offset;
4679 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004680 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004681 /* Consume <dlen> bytes of the current frame. */
4682 cf->crypto.len -= dlen;
4683 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004684 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004685 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004686
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004687 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004688 /* Note that these frames are accepted in short packets only without
4689 * "Length" packet field. Here, <*len> is used only to compute the
4690 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004691 *
4692 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004693 * excepting the variable ones. Note that +1 is for the type of this frame.
4694 */
4695 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004696 ((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 +02004697 /* Compute the data length of this STREAM frame. */
4698 avail_room = room - hlen - *len;
4699 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004700 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004701
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004702 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004703 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004704 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4705 dlen = max_available_room(avail_room, &dlen_sz);
4706 if (dlen > cf->stream.len) {
4707 dlen = cf->stream.len;
4708 }
4709 dlen_sz = quic_int_getsize(dlen);
4710 flen = hlen + dlen_sz + dlen;
4711 }
4712 else {
4713 dlen = QUIC_MIN(avail_room, cf->stream.len);
4714 flen = hlen + dlen;
4715 }
4716 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004717 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004718 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004719 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004720 /* Add the STREAM data length and its encoded length to the packet
4721 * length and the length of this length.
4722 */
4723 *len += flen;
4724 room -= flen;
4725 if (dlen == cf->stream.len) {
4726 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004727 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004728 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004729 }
4730 else {
4731 struct quic_frame *new_cf;
4732
4733 new_cf = pool_zalloc(pool_head_quic_frame);
4734 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004735 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004736 return 0;
4737 }
4738
4739 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004740 new_cf->stream.qcs = cf->stream.qcs;
4741 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004742 new_cf->stream.id = cf->stream.id;
4743 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4744 new_cf->stream.offset = cf->stream.offset;
4745 new_cf->stream.len = dlen;
4746 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4747 /* FIN bit reset */
4748 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4749 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004750 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004751 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4752 /* Consume <dlen> bytes of the current frame. */
4753 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004754 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004755 cf->stream.data += dlen;
4756 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004757 break;
4758
4759 default:
4760 flen = qc_frm_len(cf);
4761 BUG_ON(!flen);
4762 if (flen > room)
4763 continue;
4764
4765 *len += flen;
4766 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004767 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004768 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004769 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004770 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004771 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004772 }
4773
Frédéric Lécailleea604992020-12-24 13:01:37 +01004774 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004775}
4776
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004777/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004778 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4779 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4780 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004781 * 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 +02004782 * 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 +02004783 * having returned from this function.
4784 * 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 +01004785 * number field in this packet. <pn_len> will also have the packet number
4786 * length as value.
4787 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004788 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004789 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004790static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4791 size_t dglen, struct quic_tx_packet *pkt,
4792 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004793 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004794 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004795{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004796 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004797 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004798 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4799 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004800 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004801 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004802 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004803 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004804 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004805
Frédéric Lécailleea604992020-12-24 13:01:37 +01004806 /* Length field value with CRYPTO frames if present. */
4807 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004808 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004809 /* When not probing and not acking, and no immediate close is required,
4810 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004811 * the congestion controller window. So, we do not limit the size of this
4812 * packet if we have an ACK frame to send because an ACK frame is not
4813 * ack-eliciting. This size will be limited if we have ack-eliciting
4814 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004815 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004816 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004817 size_t path_room;
4818
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004819 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004820 if (end - beg > path_room)
4821 end = beg + path_room;
4822 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004823
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004824 /* Ensure there is enough room for the TLS encryption tag and a zero token
4825 * length field if any.
4826 */
4827 if (end - pos < QUIC_TLS_TAG_LEN +
4828 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4829 goto no_room;
4830
4831 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004832 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004833 /* packet number length */
4834 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004835 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004836 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004837 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004838 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004839 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004840 goto no_room;
4841
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004842 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4843 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004844 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004845 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004846 /* Build an ACK frame if required. */
4847 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004848 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004849 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004850 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004851 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4852 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4853 * that from here, we do not know if we will have to send a PING frame.
4854 * This will be decided after having computed the ack-eliciting frames
4855 * to be added to this packet.
4856 */
4857 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 +01004858 if (!ack_frm_len)
4859 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004860 }
4861
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004862 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004863 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004864 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004865 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004866 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004867
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004868 /* Initialize the length of the frames built below to <len>.
4869 * If any frame could be successfully built by qc_build_frms(),
4870 * we will have len_frms > len.
4871 */
4872 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004873 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004874 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004875 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004876 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004877 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004878
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004879 /* Length (of the remaining data). Must not fail because, the buffer size
4880 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4881 * for the encryption tag. It must be taken into an account for the length
4882 * of this packet.
4883 */
4884 if (len_frms)
4885 len = len_frms + QUIC_TLS_TAG_LEN;
4886 else
4887 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004888 /* CONNECTION_CLOSE frame */
4889 if (cc) {
4890 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004891
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004892 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004893 len += qc_frm_len(&cc_frm);
4894 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004895 add_ping_frm = 0;
4896 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004897 len_sz = quic_int_getsize(len);
4898 /* Add this packet size to <dglen> */
4899 dglen += head_len + len_sz + len;
4900 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4901 /* This is a maximum padding size */
4902 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4903 /* The length field value is of this packet is <len> + <padding_len>
4904 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004905 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004906 * sizes from <padding_len>.
4907 */
4908 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4909 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004910 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004911 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004912 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004913 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004914 add_ping_frm = 1;
4915 len += 1;
4916 }
4917 /* 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 +01004918 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004919 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4920 }
4921
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004922 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4923 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004924
4925 /* Packet number field address. */
4926 *buf_pn = pos;
4927
4928 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004929 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4930 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004931
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004932 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004933 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004934
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004935 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004936 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004937 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004938
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004939 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004940 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004941 ssize_t room = end - pos;
4942 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004943 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004944 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004945 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004946 }
4947 }
4948
4949 /* Build a PING frame if needed. */
4950 if (add_ping_frm) {
4951 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004952 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004953 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004954 }
4955
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004956 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004957 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004958 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004959
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004960 /* Build a PADDING frame if needed. */
4961 if (padding_len) {
4962 frm.type = QUIC_FT_PADDING;
4963 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004964 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004965 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004966 }
4967
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004968 /* If this packet is ack-eliciting and we are probing let's
4969 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004970 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004971 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
4972 qel->pktns->tx.pto_probe)
4973 qel->pktns->tx.pto_probe--;
4974
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004975 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004976 LIST_SPLICE(&pkt->frms, &frm_list);
4977 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004978
4979 return 1;
4980
4981 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004982 /* Replace the pre-built frames which could not be add to this packet */
4983 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004984 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004985 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004986}
4987
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004988static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004989{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004990 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004991 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004992 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004993 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004994 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004995 pkt->next = NULL;
4996 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997}
4998
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004999/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005000static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5001{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005002 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005003
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005004 if (!pkt)
5005 return;
5006
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005007 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005008 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005009 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005010 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005011 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005012}
5013
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005014/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5015 * type for <qc> QUIC connection from <qel> encryption level.
5016 * Return -2 if the packet could not be allocated or encrypted for any reason,
5017 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005018 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005019static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5020 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005021 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005022 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005023 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005024{
5025 /* The pointer to the packet number field. */
5026 unsigned char *buf_pn;
5027 unsigned char *beg, *end, *payload;
5028 int64_t pn;
5029 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005030 struct quic_tls_ctx *tls_ctx;
5031 struct quic_tx_packet *pkt;
5032
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005033 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005034 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035 pkt = pool_alloc(pool_head_quic_tx_packet);
5036 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005037 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005038 *err = -2;
5039 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005040 }
5041
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005042 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005043 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005044 pn_len = 0;
5045 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005046
5047 pn = qel->pktns->tx.next_pn + 1;
5048 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005049 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005050 *err = -1;
5051 goto err;
5052 }
5053
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005054 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005055 payload = buf_pn + pn_len;
5056 payload_len = end - payload;
5057 aad_len = payload - beg;
5058
5059 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005060 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005061 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005062 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005063 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005064
5065 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005066 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005067 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5068 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005069 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005070 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005071 goto err;
5072 }
5073
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005074 /* Consume a packet number */
5075 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005076 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005077 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5078 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005079 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5080 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005081 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005082 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005083 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005084 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005085 pkt->in_flight_len = pkt->len;
5086 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005087 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005088 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005089 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005090
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005091 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005092
5093 err:
5094 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005095 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005096 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005097}
5098
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005099/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
5100 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005102static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
5103 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005104{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005105 size_t try, done = 0;
5106
5107 if (!conn_ctrl_ready(conn))
5108 return 0;
5109
5110 if (!fd_recv_ready(conn->handle.fd))
5111 return 0;
5112
5113 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005114
5115 /* read the largest possible block. For this, we perform only one call
5116 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005117 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005118 */
5119 while (count > 0) {
5120 try = b_contig_space(buf);
5121 if (!try)
5122 break;
5123
5124 if (try > count)
5125 try = count;
5126
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005127 b_add(buf, try);
5128 done += try;
5129 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005130 }
5131
5132 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5133 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5134
5135 leave:
5136 return done;
5137
5138 read0:
5139 conn_sock_read0(conn);
5140 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5141
5142 /* Now a final check for a possible asynchronous low-level error
5143 * report. This can happen when a connection receives a reset
5144 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
5145 * we might have come from there by just checking POLL_HUP instead
5146 * of recv()'s return value 0, so we have no way to tell there was
5147 * an error without checking.
5148 */
Willy Tarreauf5090652021-04-06 17:23:40 +02005149 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005150 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5151 goto leave;
5152}
5153
5154
5155/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5156 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5157 * other pending data for example, but this flag is ignored at the moment.
5158 * Only one call to send() is performed, unless the buffer wraps, in which case
5159 * a second call may be performed. The connection's flags are updated with
5160 * whatever special event is detected (error, empty). The caller is responsible
5161 * for taking care of those events and avoiding the call if inappropriate. The
5162 * function does not call the connection's polling update function, so the caller
5163 * is responsible for this. It's up to the caller to update the buffer's contents
5164 * based on the return value.
5165 */
5166static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5167{
5168 ssize_t ret;
5169 size_t try, done;
5170 int send_flag;
5171
5172 if (!conn_ctrl_ready(conn))
5173 return 0;
5174
5175 if (!fd_send_ready(conn->handle.fd))
5176 return 0;
5177
5178 done = 0;
5179 /* send the largest possible block. For this we perform only one call
5180 * to send() unless the buffer wraps and we exactly fill the first hunk,
5181 * in which case we accept to do it once again.
5182 */
5183 while (count) {
5184 try = b_contig_data(buf, done);
5185 if (try > count)
5186 try = count;
5187
5188 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5189 if (try < count || flags & CO_SFL_MSG_MORE)
5190 send_flag |= MSG_MORE;
5191
5192 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
5193 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
5194 if (ret > 0) {
5195 count -= ret;
5196 done += ret;
5197
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005198 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005199 conn->flags |= CO_FL_WAIT_L4L6;
5200 /* if the system buffer is full, don't insist */
5201 if (ret < try)
5202 break;
5203 }
5204 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
5205 /* nothing written, we need to poll for write first */
5206 fd_cant_send(conn->handle.fd);
5207 break;
5208 }
5209 else if (errno != EINTR) {
5210 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5211 break;
5212 }
5213 }
5214 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5215 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5216
5217 if (done > 0) {
5218 /* we count the total bytes sent, and the send rate for 32-byte
5219 * blocks. The reason for the latter is that freq_ctr are
5220 * limited to 4GB and that it's not enough per second.
5221 */
5222 _HA_ATOMIC_ADD(&global.out_bytes, done);
5223 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5224 }
5225 return done;
5226}
5227
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005228/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5229 * event subscriber <es> is not allowed to change from a previous call as long
5230 * as at least one event is still subscribed. The <event_type> must only be a
5231 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5232 */
5233static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5234{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005235 struct qcc *qcc = conn->qc->qcc;
5236
5237 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5238 BUG_ON(qcc->subs && qcc->subs != es);
5239
5240 es->events |= event_type;
5241 qcc->subs = es;
5242
5243 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005244 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005245
5246 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005247 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005248
5249 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005250}
5251
5252/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5253 * The <es> pointer is not allowed to differ from the one passed to the
5254 * subscribe() call. It always returns zero.
5255 */
5256static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5257{
5258 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5259}
5260
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005261/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5262 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5263 * parameters of this session.
5264 * This is the responsibility of the caller to check the validity of all the
5265 * pointers passed as parameter to this function.
5266 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5267 * CO_ER_SSL_NO_MEM.
5268 */
5269static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5270 unsigned char *params, size_t params_len)
5271{
5272 int retry;
5273
5274 retry = 1;
5275 retry:
5276 *ssl = SSL_new(ssl_ctx);
5277 if (!*ssl) {
5278 if (!retry--)
5279 goto err;
5280
5281 pool_gc(NULL);
5282 goto retry;
5283 }
5284
5285 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5286 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
5287 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5288 goto err;
5289
5290 SSL_free(*ssl);
5291 *ssl = NULL;
5292 if (!retry--)
5293 goto err;
5294
5295 pool_gc(NULL);
5296 goto retry;
5297 }
5298
5299 return 0;
5300
5301 err:
5302 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5303 return -1;
5304}
5305
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005306/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5307 * connection with <xprt_ctx> as address of the xprt context.
5308 * Returns 1 if succeeded, 0 if not.
5309 */
5310static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5311{
Amaury Denoyelle9979d0d2021-12-23 16:32:24 +01005312 struct ssl_sock_ctx *ctx = NULL;
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005313 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005314
5315 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5316
5317 if (*xprt_ctx)
5318 goto out;
5319
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005320 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005321 if (!ctx) {
5322 conn->err_code = CO_ER_SYS_MEMLIM;
5323 goto err;
5324 }
5325
5326 ctx->wait_event.tasklet = tasklet_new();
5327 if (!ctx->wait_event.tasklet) {
5328 conn->err_code = CO_ER_SYS_MEMLIM;
5329 goto err;
5330 }
5331
5332 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5333 ctx->wait_event.tasklet->context = ctx;
5334 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005335 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005336 ctx->subs = NULL;
5337 ctx->xprt_ctx = NULL;
5338
5339 ctx->xprt = xprt_get(XPRT_QUIC);
5340 if (objt_server(conn->target)) {
5341 /* Server */
5342 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005343 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005344 int ssl_err, ipv4;
5345
5346 ssl_err = SSL_ERROR_NONE;
5347 if (RAND_bytes(dcid, sizeof dcid) != 1)
5348 goto err;
5349
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005350 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005351 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005352 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005353 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005354 goto err;
5355
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005356 ctx->qc = qc;
5357
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005358 /* Insert our SCID, the connection ID for the QUIC client. */
5359 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5360
5361 conn->qc = qc;
5362 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005363 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5364 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005365 goto err;
5366
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005367 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005368 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005369 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5370 qc->enc_params_len =
5371 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5372 &qc->rx.params, 0);
5373 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005374 goto err;
5375
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005376 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5377 qc->enc_params, qc->enc_params_len) == -1)
5378 goto err;
5379
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005380 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005381 SSL_set_connect_state(ctx->ssl);
5382 ssl_err = SSL_do_handshake(ctx->ssl);
5383 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005384 int st;
5385
5386 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005387 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5388 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005389 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005390 }
5391 else {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005392 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005393 goto err;
5394 }
5395 }
5396 }
5397 else if (objt_listener(conn->target)) {
5398 /* Listener */
5399 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
5400
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005401 qc = ctx->conn->qc;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005402 ctx->qc = qc;
5403
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005404 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005405 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5406 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005407 goto err;
5408
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005409 /* Enabling 0-RTT */
5410 if (bc->ssl_conf.early_data)
5411 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5412
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005413 SSL_set_accept_state(ctx->ssl);
5414 }
5415
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005416 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005417
5418 /* Leave init state and start handshake */
5419 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005420
5421 out:
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005422 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005423 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005424
5425 return 0;
5426
5427 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005428 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005429 tasklet_free(ctx->wait_event.tasklet);
5430 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005431 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005432 return -1;
5433}
5434
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005435/* Start the QUIC transport layer */
5436static int qc_xprt_start(struct connection *conn, void *ctx)
5437{
5438 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005439 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005440
5441 qc = conn->qc;
5442 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005443 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005444 return 0;
5445 }
5446
5447 tasklet_wakeup(qctx->wait_event.tasklet);
5448 return 1;
5449}
5450
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005451/* transport-layer operations for QUIC connections. */
5452static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005453 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005454 .snd_buf = quic_conn_from_buf,
5455 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005456 .subscribe = quic_conn_subscribe,
5457 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005458 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005459 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005460 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5461 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005462 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005463 .name = "QUIC",
5464};
5465
5466__attribute__((constructor))
5467static void __quic_conn_init(void)
5468{
5469 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5470 xprt_register(XPRT_QUIC, &ssl_quic);
5471}
5472
5473__attribute__((destructor))
5474static void __quic_conn_deinit(void)
5475{
5476 BIO_meth_free(ha_quic_meth);
5477}
5478
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005479/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5480 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005481 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005482 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005483static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005484 struct sockaddr_storage *saddr, qpkt_read_func *func)
5485{
5486 unsigned char *pos;
5487 const unsigned char *end;
5488 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005489 .qc = NULL,
5490 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005491 .owner = owner,
5492 };
5493
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005494 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005495 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005496 do {
5497 int ret;
5498 struct quic_rx_packet *pkt;
5499
Willy Tarreaue4498932021-03-22 21:13:05 +01005500 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005501 if (!pkt)
5502 goto err;
5503
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005504 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005505 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5506 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005507 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005508 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005509 /* If the packet length could not be found, we cannot continue. */
5510 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005511 } while (pos < end);
5512
5513 /* Increasing the received bytes counter by the UDP datagram length
5514 * if this datagram could be associated to a connection.
5515 */
5516 if (dgram_ctx.qc)
5517 dgram_ctx.qc->rx.bytes += len;
5518
Amaury Denoyellece340fe2022-01-11 14:20:46 +01005519 return pos - (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005520
5521 err:
5522 return -1;
5523}
5524
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005525ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005526 struct sockaddr_storage *saddr)
5527{
5528 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5529}
5530
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005531/* Function to automatically activate QUIC traces on stdout.
5532 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5533 * Main use for now is in the docker image for QUIC interop testing.
5534 */
5535static void quic_init_stdout_traces(void)
5536{
5537#ifdef ENABLE_QUIC_STDOUT_TRACES
5538 trace_quic.sink = sink_find("stdout");
5539 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005540 trace_quic.state = TRACE_STATE_RUNNING;
5541#endif
5542}
5543INITCALL0(STG_INIT, quic_init_stdout_traces);
5544
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005545/*
5546 * Local variables:
5547 * c-indent-level: 8
5548 * c-basic-offset: 8
5549 * End:
5550 */