blob: 7247cd5971bc135a350ce42ea3c9464888d22b5f [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
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001595/* Release all the frames attached to <pktns> packet number space */
1596static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1597{
1598 struct quic_frame *frm, *frmbak;
1599
1600 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1601 LIST_DELETE(&frm->list);
1602 pool_free(pool_head_quic_frame, frm);
1603 }
1604}
1605
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001606/* Handle <pkts> list of lost packets detected at <now_us> handling
1607 * their TX frames.
1608 * Send a packet loss event to the congestion controller if
1609 * in flight packet have been lost.
1610 * Also frees the packet in <pkts> list.
1611 * Never fails.
1612 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001613static inline void qc_release_lost_pkts(struct quic_conn *qc,
1614 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001615 struct list *pkts,
1616 uint64_t now_us)
1617{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001618 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001619 uint64_t lost_bytes;
1620
1621 lost_bytes = 0;
1622 oldest_lost = newest_lost = NULL;
1623 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001624 struct list tmp = LIST_HEAD_INIT(tmp);
1625
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001626 lost_bytes += pkt->in_flight_len;
1627 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001628 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001630 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001631 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001632 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001633 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 if (!oldest_lost) {
1635 oldest_lost = newest_lost = pkt;
1636 }
1637 else {
1638 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 newest_lost = pkt;
1641 }
1642 }
1643
1644 if (lost_bytes) {
1645 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001646 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001647 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001648 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001649 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001650 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001651 }
1652}
1653
1654/* Look for packet loss from sent packets for <qel> encryption level of a
1655 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1656 * their tree if deemed as lost or set the <loss_time> value the packet number
1657 * space if any not deemed lost.
1658 * Should be called after having received an ACK frame with newly acknowledged
1659 * packets or when the the loss detection timer has expired.
1660 * Always succeeds.
1661 */
1662static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1663 struct quic_conn *qc,
1664 struct list *lost_pkts)
1665{
1666 struct eb_root *pkts;
1667 struct eb64_node *node;
1668 struct quic_loss *ql;
1669 unsigned int loss_delay;
1670
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001671 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001672 pkts = &pktns->tx.pkts;
1673 pktns->tx.loss_time = TICK_ETERNITY;
1674 if (eb_is_empty(pkts))
1675 goto out;
1676
1677 ql = &qc->path->loss;
1678 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1679 loss_delay += loss_delay >> 3;
1680 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1681
1682 node = eb64_first(pkts);
1683 while (node) {
1684 struct quic_tx_packet *pkt;
1685 int64_t largest_acked_pn;
1686 unsigned int loss_time_limit, time_sent;
1687
1688 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001689 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001690 node = eb64_next(node);
1691 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1692 break;
1693
1694 time_sent = pkt->time_sent;
1695 loss_time_limit = tick_add(time_sent, loss_delay);
1696 if (tick_is_le(time_sent, now_ms) ||
1697 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1698 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001699 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001700 }
1701 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001702 if (tick_isset(pktns->tx.loss_time))
1703 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1704 else
1705 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001706 }
1707 }
1708
1709 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001710 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711}
1712
1713/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1714 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001715 * 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 +01001716 * acked ack-eliciting packet.
1717 * Return 1, if succeeded, 0 if not.
1718 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001719static inline int qc_parse_ack_frm(struct quic_conn *qc,
1720 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001721 struct quic_enc_level *qel,
1722 unsigned int *rtt_sample,
1723 const unsigned char **pos, const unsigned char *end)
1724{
1725 struct quic_ack *ack = &frm->ack;
1726 uint64_t smallest, largest;
1727 struct eb_root *pkts;
1728 struct eb64_node *largest_node;
1729 unsigned int time_sent, pkt_flags;
1730 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1731 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1732
1733 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1734 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001735 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001736 goto err;
1737 }
1738
1739 if (ack->first_ack_range > ack->largest_ack) {
1740 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001741 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001742 goto err;
1743 }
1744
1745 largest = ack->largest_ack;
1746 smallest = largest - ack->first_ack_range;
1747 pkts = &qel->pktns->tx.pkts;
1748 pkt_flags = 0;
1749 largest_node = NULL;
1750 time_sent = 0;
1751
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001752 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 +01001753 largest_node = eb64_lookup(pkts, largest);
1754 if (!largest_node) {
1755 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001756 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001757 }
1758 else {
1759 time_sent = eb64_entry(&largest_node->node,
1760 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001761 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001762 }
1763
1764 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001765 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 do {
1767 uint64_t gap, ack_range;
1768
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001769 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1770 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001771 if (!ack->ack_range_num--)
1772 break;
1773
1774 if (!quic_dec_int(&gap, pos, end))
1775 goto err;
1776
1777 if (smallest < gap + 2) {
1778 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001779 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 goto err;
1781 }
1782
1783 largest = smallest - gap - 2;
1784 if (!quic_dec_int(&ack_range, pos, end))
1785 goto err;
1786
1787 if (largest < ack_range) {
1788 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001789 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001790 goto err;
1791 }
1792
1793 /* Do not use this node anymore. */
1794 largest_node = NULL;
1795 /* Next range */
1796 smallest = largest - ack_range;
1797
1798 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001799 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001800 } while (1);
1801
1802 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001803 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001804
1805 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1806 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001807 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001808 }
1809
1810 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1811 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001812 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001813 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001814 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001815 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001816 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1817 if (quic_peer_validated_addr(qc))
1818 qc->path->loss.pto_count = 0;
1819 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001820 }
1821
1822
1823 return 1;
1824
1825 err:
1826 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001827 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828 return 0;
1829}
1830
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001831/* This function gives the detail of the SSL error. It is used only
1832 * if the debug mode and the verbose mode are activated. It dump all
1833 * the SSL error until the stack was empty.
1834 */
1835static forceinline void qc_ssl_dump_errors(struct connection *conn)
1836{
1837 if (unlikely(global.mode & MODE_DEBUG)) {
1838 while (1) {
1839 unsigned long ret;
1840
1841 ret = ERR_get_error();
1842 if (!ret)
1843 return;
1844
1845 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1846 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1847 }
1848 }
1849}
1850
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001851int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1852 const char **str, int *len);
1853
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001854/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1855 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001856 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001857 * Return 1 if succeeded, 0 if not.
1858 */
1859static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001860 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861 const unsigned char *data, size_t len,
1862 struct quic_rx_packet *pkt,
1863 struct quic_rx_crypto_frm *cf)
1864{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001865 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001866 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001869 qc = ctx->qc;
1870
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001871 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1872
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001873 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1874 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001875 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001876 goto err;
1877 }
1878
1879 el->rx.crypto.offset += len;
1880 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001881 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001882
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001883 state = HA_ATOMIC_LOAD(&qc->state);
1884 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001885 ssl_err = SSL_do_handshake(ctx->ssl);
1886 if (ssl_err != 1) {
1887 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1888 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1889 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001890 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001891 goto out;
1892 }
1893
1894 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001895 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001896 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001897 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 goto err;
1899 }
1900
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001901 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01001902 if (qc_is_listener(ctx->qc))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001903 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001905 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001906 } else {
1907 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1908 if (ssl_err != 1) {
1909 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1910 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1911 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001912 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913 goto out;
1914 }
1915
1916 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001917 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001918 goto err;
1919 }
1920
1921 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001922 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001924
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001926 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001927 return 1;
1928
1929 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001930 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001931 return 0;
1932}
1933
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001934/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1935 * <pkt> RX packet.
1936 * Return it if succeeded, NULL if not.
1937 */
1938static inline
1939struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1940 struct quic_rx_packet *pkt)
1941{
1942 struct quic_rx_strm_frm *frm;
1943
1944 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1945 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001946 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001947 frm->len = stream_frm->len;
1948 frm->data = stream_frm->data;
1949 frm->pkt = pkt;
1950 }
1951
1952 return frm;
1953}
1954
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001955/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001956 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001957 */
1958static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1959{
1960 size_t ret;
1961
1962 ret = 0;
1963 while (strm_frm->len) {
1964 size_t try;
1965
1966 try = b_contig_space(buf);
1967 if (!try)
1968 break;
1969
1970 if (try > strm_frm->len)
1971 try = strm_frm->len;
1972 memcpy(b_tail(buf), strm_frm->data, try);
1973 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001974 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001975 b_add(buf, try);
1976 ret += try;
1977 }
1978
1979 return ret;
1980}
1981
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001982/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1983 * Also update <strm_frm> frame to reflect the data which have been consumed.
1984 */
1985static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1986 struct quic_rx_strm_frm *strm_frm)
1987{
1988 size_t ret;
1989
1990 ret = 0;
1991 while (strm_frm->len) {
1992 size_t try;
1993
1994 try = b_contig_space(buf);
1995 if (!try)
1996 break;
1997
1998 if (try > strm_frm->len)
1999 try = strm_frm->len;
2000 memcpy(b_tail(buf), strm_frm->data, try);
2001 strm_frm->len -= try;
2002 strm_frm->offset_node.key += try;
2003 b_add(buf, try);
2004 ret += try;
2005 }
2006
2007 return ret;
2008}
2009
2010/* Process as much as possible RX STREAM frames received for <qcs> */
2011static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2012{
2013 int total;
2014 struct eb64_node *frm_node;
2015
2016 total = 0;
2017 frm_node = eb64_first(&qcs->rx.frms);
2018 while (frm_node) {
2019 int ret;
2020 struct quic_rx_strm_frm *frm;
2021
2022 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2023 if (frm->offset_node.key != qcs->rx.offset)
2024 break;
2025
2026 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2027 qcs->rx.offset += ret;
2028 total += ret;
2029 if (frm->len) {
2030 /* If there is remaining data in this frame
2031 * this is because the destination buffer is full.
2032 */
2033 break;
2034 }
2035
2036 frm_node = eb64_next(frm_node);
2037 quic_rx_packet_refdec(frm->pkt);
2038 eb64_delete(&frm->offset_node);
2039 pool_free(pool_head_quic_rx_strm_frm, frm);
2040 }
2041
2042 return total;
2043}
2044
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002045/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2046 * streams may be open. The data are copied to the stream RX buffer if possible.
2047 * If not, the STREAM frame is stored to be treated again later.
2048 * We rely on the flow control so that not to store too much STREAM frames.
2049 * Return 1 if succeeded, 0 if not.
2050 */
2051static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2052 struct quic_stream *strm_frm,
2053 struct quic_conn *qc)
2054{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002055 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002056 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002057 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002058 struct quic_rx_strm_frm *frm;
2059
2060 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2061 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002062 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002063 return 0;
2064 }
2065
2066 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002067 if (strm_frm->offset.key < strm->rx.offset) {
2068 size_t diff;
2069
2070 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2071 TRACE_PROTO("Already received STREAM data",
2072 QUIC_EV_CONN_PSTRM, qc);
2073 goto out;
2074 }
2075
2076 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2077 diff = strm->rx.offset - strm_frm->offset.key;
2078 strm_frm->offset.key = strm->rx.offset;
2079 strm_frm->len -= diff;
2080 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002081 }
2082
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002083 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002084 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002085 int ret;
2086
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002087 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002088 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002089 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002090
2091 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002092 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002093 strm->rx.offset += ret;
2094 }
2095
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002096 total += qc_treat_rx_strm_frms(strm);
2097 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 +01002098 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002099 return 0;
2100 }
2101
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002102 if (!strm_frm->len)
2103 goto out;
2104
2105 store_frm:
2106 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2107 if (!frm) {
2108 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002109 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002110 return 0;
2111 }
2112
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002113 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002114 quic_rx_packet_refinc(pkt);
2115
2116 out:
2117 return 1;
2118}
2119
2120/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2121 * streams may be open. The data are copied to the stream RX buffer if possible.
2122 * If not, the STREAM frame is stored to be treated again later.
2123 * We rely on the flow control so that not to store too much STREAM frames.
2124 * Return 1 if succeeded, 0 if not.
2125 */
2126static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2127 struct quic_stream *strm_frm,
2128 struct quic_conn *qc)
2129{
2130 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002131 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002132 struct quic_rx_strm_frm *frm;
2133 size_t strm_frm_len;
2134
2135 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2136 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002137 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002138 return 0;
2139 }
2140
2141 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002142 if (strm_frm->offset.key < strm->rx.offset) {
2143 size_t diff;
2144
2145 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2146 TRACE_PROTO("Already received STREAM data",
2147 QUIC_EV_CONN_PSTRM, qc);
2148 goto out;
2149 }
2150
2151 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2152 diff = strm->rx.offset - strm_frm->offset.key;
2153 strm_frm->offset.key = strm->rx.offset;
2154 strm_frm->len -= diff;
2155 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002156 }
2157
2158 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002159 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002160 int ret;
2161
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002162 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002163 goto store_frm;
2164
2165 /* qc_strm_cpy() will modify the offset, depending on the number
2166 * of bytes copied.
2167 */
2168 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2169 /* Inform the application of the arrival of this new stream */
2170 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002171 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002172 return 0;
2173 }
2174
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002175 if (ret)
2176 qcs_notify_recv(strm);
2177
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002178 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002179 }
2180 /* Take this frame into an account for the stream flow control */
2181 strm->rx.offset += strm_frm_len;
2182 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002183 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002184 */
2185 if (!strm_frm->len)
2186 goto out;
2187
2188 store_frm:
2189 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2190 if (!frm) {
2191 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002192 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002193 return 0;
2194 }
2195
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002196 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002197 quic_rx_packet_refinc(pkt);
2198
2199 out:
2200 return 1;
2201}
2202
2203static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2204 struct quic_stream *strm_frm,
2205 struct quic_conn *qc)
2206{
2207 if (strm_frm->id & QCS_ID_DIR_BIT)
2208 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2209 else
2210 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2211}
2212
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002213/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002214static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2215 struct quic_conn *qc)
2216{
2217 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002218 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002219 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002220
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002221 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002222 pkts = &qel->pktns->tx.pkts;
2223 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002224 /* Skip the empty packet (they have already been retransmitted) */
2225 while (node) {
2226 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2227 if (!LIST_ISEMPTY(&pkt->frms))
2228 break;
2229 node = eb64_next(node);
2230 }
2231
2232 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002233 return;
2234
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002235 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002236}
2237
2238/* Prepare a fast retransmission during handshake after a client
2239 * has resent Initial packets. According to the RFC a server may retransmit
2240 * up to two datagrams of Initial packets if did not receive all Initial packets
2241 * and resend them coalescing with others (Handshake here).
2242 * (Listener only).
2243 */
2244static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2245{
2246 struct list itmp = LIST_HEAD_INIT(itmp);
2247 struct list htmp = LIST_HEAD_INIT(htmp);
2248 struct quic_frame *frm, *frmbak;
2249
2250 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2251 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2252 struct quic_enc_level *qel = iqel;
2253 struct eb_root *pkts;
2254 struct eb64_node *node;
2255 struct quic_tx_packet *pkt;
2256 struct list *tmp = &itmp;
2257
2258 start:
2259 pkt = NULL;
2260 pkts = &qel->pktns->tx.pkts;
2261 node = eb64_first(pkts);
2262 /* Skip the empty packet (they have already been retransmitted) */
2263 while (node) {
2264 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2265 if (!LIST_ISEMPTY(&pkt->frms))
2266 break;
2267 node = eb64_next(node);
2268 }
2269
2270 if (!pkt)
2271 goto end;
2272
2273 qel->pktns->tx.pto_probe += 1;
2274 requeue:
2275 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2276 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2277 LIST_DELETE(&frm->list);
2278 LIST_APPEND(tmp, &frm->list);
2279 }
2280
2281 if (qel == iqel) {
2282 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2283 pkt = pkt->next;
2284 tmp = &htmp;
2285 hqel->pktns->tx.pto_probe += 1;
2286 goto requeue;
2287 }
2288
2289 qel = hqel;
2290 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002291 goto start;
2292 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002293
2294 end:
2295 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2296 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002297}
2298
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002299/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2300 * as I/O handler context and <qel> as encryption level.
2301 * Returns 1 if succeeded, 0 if failed.
2302 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002303static 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 +01002304 struct quic_enc_level *qel)
2305{
2306 struct quic_frame frm;
2307 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002308 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002309 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002310
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002311 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312 /* Skip the AAD */
2313 pos = pkt->data + pkt->aad_len;
2314 end = pkt->data + pkt->len;
2315
2316 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002317 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 goto err;
2319
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002320 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002321 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002322 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002323 break;
2324 case QUIC_FT_PING:
2325 break;
2326 case QUIC_FT_ACK:
2327 {
2328 unsigned int rtt_sample;
2329
2330 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002331 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002332 goto err;
2333
2334 if (rtt_sample) {
2335 unsigned int ack_delay;
2336
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002337 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002338 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2339 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2340 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002341 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002342 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002343 break;
2344 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002345 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002346 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002347 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002348 {
2349 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002350
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002351 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2352 /* XXX TO DO: <cfdebug> is used only for the traces. */
2353 struct quic_rx_crypto_frm cfdebug = { };
2354
2355 cfdebug.offset_node.key = frm.crypto.offset;
2356 cfdebug.len = frm.crypto.len;
2357 TRACE_PROTO("CRYPTO data discarded",
2358 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2359 break;
2360 }
2361
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002362 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2363 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2364 /* XXX TO DO: <cfdebug> is used only for the traces. */
2365 struct quic_rx_crypto_frm cfdebug = { };
2366
2367 cfdebug.offset_node.key = frm.crypto.offset;
2368 cfdebug.len = frm.crypto.len;
2369 /* Nothing to do */
2370 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002371 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002372 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002373 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2374 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002375 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002377 else {
2378 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2379 /* XXX TO DO: <cfdebug> is used only for the traces. */
2380 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002381
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002382 cfdebug.offset_node.key = frm.crypto.offset;
2383 cfdebug.len = frm.crypto.len;
2384 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002385 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002386 frm.crypto.len -= diff;
2387 frm.crypto.data += diff;
2388 frm.crypto.offset = qel->rx.crypto.offset;
2389 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002390 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002391
2392 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002393 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002394 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002395
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002396 cfdebug.offset_node.key = frm.crypto.offset;
2397 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398 if (!qc_provide_cdata(qel, ctx,
2399 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002400 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002402
2403 break;
2404 }
2405
2406 /* frm.crypto.offset > qel->rx.crypto.offset */
2407 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2408 if (!cf) {
2409 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002410 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002411 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002413
2414 cf->offset_node.key = frm.crypto.offset;
2415 cf->len = frm.crypto.len;
2416 cf->data = frm.crypto.data;
2417 cf->pkt = pkt;
2418 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2419 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002420 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002421 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002422 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002423 {
2424 struct quic_stream *stream = &frm.stream;
2425
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002426 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002427 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2428 goto err;
2429 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2430 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002431
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002432 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002433 goto err;
2434
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002435 break;
2436 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002437 case QUIC_FT_MAX_DATA:
2438 case QUIC_FT_MAX_STREAM_DATA:
2439 case QUIC_FT_MAX_STREAMS_BIDI:
2440 case QUIC_FT_MAX_STREAMS_UNI:
2441 case QUIC_FT_DATA_BLOCKED:
2442 case QUIC_FT_STREAM_DATA_BLOCKED:
2443 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2444 case QUIC_FT_STREAMS_BLOCKED_UNI:
2445 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002446 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002447 case QUIC_FT_RETIRE_CONNECTION_ID:
2448 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002449 break;
2450 case QUIC_FT_CONNECTION_CLOSE:
2451 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002452 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002453 qc->qcc->flags |= QC_CF_CC_RECV;
2454 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002455 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002456 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002457 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002458 goto err;
2459
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002460 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002461 break;
2462 default:
2463 goto err;
2464 }
2465 }
2466
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002467 if (fast_retrans)
2468 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002469
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002470 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2471 * has successfully parse a Handshake packet. The Initial encryption must also
2472 * be discarded.
2473 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002474 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002475 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002476
2477 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002478 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002479 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002480 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002481 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002482 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002483 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002484 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002485 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002486 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002487 }
2488
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002489 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 return 1;
2491
2492 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002493 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494 return 0;
2495}
2496
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002497/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002498 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002499 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2500 * This function must be called only after having built a correct datagram.
2501 * Always succeeds.
2502 */
2503static inline void qc_set_dg(struct cbuf *cbuf,
2504 uint16_t dglen, struct quic_tx_packet *pkt)
2505{
2506 write_u16(cb_wr(cbuf), dglen);
2507 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2508 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2509}
2510
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002511/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002512 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2513 * several packets in the same datagram. A header made of two fields is added
2514 * to each datagram: the datagram length followed by the address of the first
2515 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002516 * Returns 1 if succeeded, or 0 if something wrong happened.
2517 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002518static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2519 enum quic_tls_enc_level tel,
2520 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002521{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002522 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002523 struct cbuf *cbuf;
2524 unsigned char *end_buf, *end, *pos, *spos;
2525 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2526 /* length of datagrams */
2527 uint16_t dglen;
2528 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002529 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002530 /* Each datagram is prepended with its length followed by the
2531 * address of the first packet in the datagram.
2532 */
2533 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002535 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2536
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002537 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002538 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002539 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002540 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002541 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002542 cbuf = qr->cbuf;
2543 spos = pos = cb_wr(cbuf);
2544 /* Leave at least <dglen> bytes at the end of this buffer
2545 * to ensure there is enough room to mark the end of prepared
2546 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002547 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002548 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2549 first_pkt = prv_pkt = NULL;
2550 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002551 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002552 enum quic_pkt_type pkt_type;
2553
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002554 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002555 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002556 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2557 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002558 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002559 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002560 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002561 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002562 * 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 +01002563 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002564 * and if there is no more CRYPTO data available or in flight
2565 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002566 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002567 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002568 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002569 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002570 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002571 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002572 /* Set the current datagram as prepared into <cbuf> if
2573 * the was already a correct packet which was previously written.
2574 */
2575 if (prv_pkt)
2576 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002577 /* Let's select the next encryption level */
2578 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2579 tel = next_tel;
2580 qel = &qc->els[tel];
2581 /* Build a new datagram */
2582 prv_pkt = NULL;
2583 continue;
2584 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585 break;
2586 }
2587
2588 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002589 if (!prv_pkt) {
2590 /* Leave room for the datagram header */
2591 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002592 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002593 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2594 }
2595 else {
2596 end = pos + qc->path->mtu;
2597 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002598 }
2599
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002600 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002601 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002602 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002603 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002604 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002605 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002606 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002607 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 case -2:
2609 goto err;
2610 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002611 /* If there was already a correct packet present, set the
2612 * current datagram as prepared into <cbuf>.
2613 */
2614 if (prv_pkt) {
2615 qc_set_dg(cbuf, dglen, first_pkt);
2616 goto stop_build;
2617 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002618 goto out;
2619 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002620 break;
2621 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002622
Frédéric Lécaille63556772021-12-29 17:18:21 +01002623 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2624 if (!cur_pkt)
2625 goto err;
2626
2627 total += cur_pkt->len;
2628 /* keep trace of the first packet in the datagram */
2629 if (!first_pkt)
2630 first_pkt = cur_pkt;
2631 /* Attach the current one to the previous one */
2632 if (prv_pkt)
2633 prv_pkt->next = cur_pkt;
2634 /* Let's say we have to build a new dgram */
2635 prv_pkt = NULL;
2636 dglen += cur_pkt->len;
2637 /* Client: discard the Initial encryption keys as soon as
2638 * a handshake packet could be built.
2639 */
2640 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2641 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2642 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2643 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2644 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2645 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002646 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002647 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002648 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2649 }
2650 /* If the data for the current encryption level have all been sent,
2651 * select the next level.
2652 */
2653 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002654 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002655 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2656 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2657 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2658 tel = next_tel;
2659 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002660 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002661 /* If there is data for the next level, do not
2662 * consume a datagram.
2663 */
2664 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002666 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002667 /* If we have to build a new datagram, set the current datagram as
2668 * prepared into <cbuf>.
2669 */
2670 if (!prv_pkt) {
2671 qc_set_dg(cbuf, dglen, first_pkt);
2672 first_pkt = NULL;
2673 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002674 padding = 0;
2675 }
2676 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002677 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002678 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2679 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002680 }
2681 }
2682
2683 stop_build:
2684 /* Reset <wr> writer index if in front of <rd> index */
2685 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2686 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2687
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002688 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002689 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2690 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2691 /* Mark the end of contiguous data for the reader */
2692 write_u16(cb_wr(cbuf), 0);
2693 cb_add(cbuf, sizeof(uint16_t));
2694 }
2695 }
2696
2697 if (rd && rd <= cbuf->wr) {
2698 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002699 /* Let's try to reuse this buffer */
2700 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002701 }
2702 }
2703
2704 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002705 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002706 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002707
2708 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002709 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002710 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002711}
2712
2713/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002714 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002715 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002716int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002717{
2718 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002719 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002720
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002721 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002722 cbuf = qr->cbuf;
2723 while (cb_contig_data(cbuf)) {
2724 unsigned char *pos;
2725 struct buffer tmpbuf = { };
2726 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2727 uint16_t dglen;
2728 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002729 unsigned int time_sent;
2730
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002731 pos = cb_rd(cbuf);
2732 dglen = read_u16(pos);
2733 /* End of prepared datagrams.
2734 * Reset the reader index only if in front of the writer index.
2735 */
2736 if (!dglen) {
2737 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2738
2739 if (wr && wr < cbuf->rd) {
2740 cb_rd_reset(cbuf);
2741 continue;
2742 }
2743 break;
2744 }
2745
2746 pos += sizeof dglen;
2747 first_pkt = read_ptr(pos);
2748 pos += sizeof first_pkt;
2749 tmpbuf.area = (char *)pos;
2750 tmpbuf.size = tmpbuf.data = dglen;
2751
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002752 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002753 for (pkt = first_pkt; pkt; pkt = pkt->next)
2754 quic_tx_packet_refinc(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002755 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
2756 &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002757 for (pkt = first_pkt; pkt; pkt = pkt->next)
2758 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002759 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002760 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002762 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002763 qc->tx.bytes += tmpbuf.data;
2764 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002765
2766 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2767 pkt->time_sent = time_sent;
2768 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2769 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002770 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002771 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002772 qc->path->in_flight += pkt->in_flight_len;
2773 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2774 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002775 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002776 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002777 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002778 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002779 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780 }
2781 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002782
2783 return 1;
2784}
2785
2786/* Build all the frames which must be sent just after the handshake have succeeded.
2787 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2788 * a HANDSHAKE_DONE frame.
2789 * Return 1 if succeeded, 0 if not.
2790 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002791static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792{
2793 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002794 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002795 struct quic_frame *frm;
2796
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002797 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002799 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002800 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002801 if (!frm)
2802 return 0;
2803
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002804 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002805 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002806 }
2807
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002808 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002809 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002810 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002811
2812 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002813 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814 goto err;
2815
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002816 cid = new_quic_cid(&qc->cids, qc, i);
2817 if (!cid)
2818 goto err;
2819
2820 /* insert the allocated CID in the receiver tree */
2821 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2822 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2823 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2824
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002825 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002826 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002828 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829
2830 return 1;
2831
2832 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 return 0;
2834}
2835
2836/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002837void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002838{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002839 struct eb64_node *n;
2840 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002841
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002842 n = eb64_first(&arngs->root);
2843 while (n) {
2844 struct eb64_node *next;
2845
2846 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2847 next = eb64_next(n);
2848 eb64_delete(n);
2849 free(ar);
2850 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002851 }
2852}
2853
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002854/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2855 * descending order.
2856 */
2857static inline size_t sack_gap(struct quic_arng_node *p,
2858 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002860 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861}
2862
2863
2864/* Remove the last elements of <ack_ranges> list of ack range updating its
2865 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002866 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002867 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002868static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002869{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002870 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002871
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002872 last = eb64_last(&arngs->root);
2873 while (last && arngs->enc_sz > limit) {
2874 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002875
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002876 prev = eb64_prev(last);
2877 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002878 return 0;
2879
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002880 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2881 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2882 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2883 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2884 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2885 --arngs->sz;
2886 eb64_delete(last);
2887 pool_free(pool_head_quic_arng, last);
2888 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002889 }
2890
2891 return 1;
2892}
2893
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002894/* Set the encoded size of <arngs> QUIC ack ranges. */
2895static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2896{
2897 struct eb64_node *node, *next;
2898 struct quic_arng_node *ar, *ar_next;
2899
2900 node = eb64_last(&arngs->root);
2901 if (!node)
2902 return;
2903
2904 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2905 arngs->enc_sz = quic_int_getsize(ar->last) +
2906 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2907
2908 while ((next = eb64_prev(node))) {
2909 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2910 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2911 quic_int_getsize(ar_next->last - ar_next->first.key);
2912 node = next;
2913 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2914 }
2915}
2916
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002917/* Insert <ar> ack range into <argns> tree of ack ranges.
2918 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002919 */
2920static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002921struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2922 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002923{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002924 struct quic_arng_node *new_ar;
2925
2926 new_ar = pool_alloc(pool_head_quic_arng);
2927 if (new_ar) {
2928 new_ar->first.key = ar->first;
2929 new_ar->last = ar->last;
2930 eb64_insert(&arngs->root, &new_ar->first);
2931 arngs->sz++;
2932 }
2933
2934 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002935}
2936
2937/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002938 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002939 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002940 *
2941 * Descending order
2942 * ------------->
2943 * range1 range2
2944 * ..........|--------|..............|--------|
2945 * ^ ^ ^ ^
2946 * | | | |
2947 * last1 first1 last2 first2
2948 * ..........+--------+--------------+--------+......
2949 * diff1 gap12 diff2
2950 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002951 * To encode the previous list of ranges we must encode integers as follows in
2952 * descending order:
2953 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002954 * with diff1 = last1 - first1
2955 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002956 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002957 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002958 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002959int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2960 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002961{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002962 struct eb64_node *le;
2963 struct quic_arng_node *new_node;
2964 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002965
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002966 new = NULL;
2967 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002968 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002969 if (!new_node)
2970 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002971
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002972 goto out;
2973 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002975 le = eb64_lookup_le(&arngs->root, ar->first);
2976 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002977 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002978 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002979 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002980
2981 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002982 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002983 else {
2984 struct quic_arng_node *le_ar =
2985 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002986
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002987 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002988 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002989 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002991 if (le_ar->last + 1 >= ar->first) {
2992 le_ar->last = ar->last;
2993 new = le;
2994 new_node = le_ar;
2995 }
2996 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002997 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002998 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003000
3001 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003002 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003003 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003005 /* Verify that the new inserted node does not overlap the nodes
3006 * which follow it.
3007 */
3008 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003009 struct eb64_node *next;
3010 struct quic_arng_node *next_node;
3011
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003012 while ((next = eb64_next(new))) {
3013 next_node =
3014 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003015 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003016 break;
3017
3018 if (next_node->last > new_node->last)
3019 new_node->last = next_node->last;
3020 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003021 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003022 /* Decrement the size of these ranges. */
3023 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003024 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025 }
3026
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003027 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003028 quic_arngs_set_enc_sz(arngs);
3029
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030 return 1;
3031}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003032/* Remove the header protection of packets at <el> encryption level.
3033 * Always succeeds.
3034 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003035static 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 +01003036{
3037 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003038 struct quic_rx_packet *pqpkt;
3039 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 struct quic_enc_level *app_qel;
3041
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003042 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3043 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003045 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003046 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003047 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003048 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003049 goto out;
3050 }
3051 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003052 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003053 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003054 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003055 pqpkt->data, pqpkt->data + pqpkt->len)) {
3056 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003057 /* XXX TO DO XXX */
3058 }
3059 else {
3060 /* The AAD includes the packet number field */
3061 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3062 /* Store the packet into the tree of packets to decrypt. */
3063 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003064 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003065 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3066 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003067 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003068 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003070 MT_LIST_DELETE_SAFE(pkttmp1);
3071 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003072 }
3073
3074 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003075 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003076}
3077
3078/* Process all the CRYPTO frame at <el> encryption level.
3079 * Return 1 if succeeded, 0 if not.
3080 */
3081static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003082 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003083{
3084 struct eb64_node *node;
3085
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086 node = eb64_first(&el->rx.crypto.frms);
3087 while (node) {
3088 struct quic_rx_crypto_frm *cf;
3089
3090 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3091 if (cf->offset_node.key != el->rx.crypto.offset)
3092 break;
3093
3094 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3095 goto err;
3096
3097 node = eb64_next(node);
3098 quic_rx_packet_refdec(cf->pkt);
3099 eb64_delete(&cf->offset_node);
3100 pool_free(pool_head_quic_rx_crypto_frm, cf);
3101 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003102 return 1;
3103
3104 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003105 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003106 return 0;
3107}
3108
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003109/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003110 * 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 +02003111 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003112 * Return 1 if succeeded, 0 if not.
3113 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003114int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3115 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003116{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003118 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003119 struct quic_conn *qc = ctx->conn->qc;
3120 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003121
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003122 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003123 qel = cur_el;
3124 next_tel:
3125 if (!qel)
3126 goto out;
3127
3128 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3129 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 while (node) {
3131 struct quic_rx_packet *pkt;
3132
3133 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003134 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003135 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003136 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003137 /* Drop the packet */
3138 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003139 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003140 }
3141 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003142 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003143 /* Drop the packet */
3144 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003145 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 }
3147 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003148 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3149
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003150 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003151 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003152 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003153 if (pkt->pn > largest_pn)
3154 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003156 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003157 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003158 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003159 }
3160 }
3161 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003162 eb64_delete(&pkt->pn_node);
3163 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003164 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003165 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003166
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003167 /* Update the largest packet number. */
3168 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003169 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3170 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003171 goto err;
3172
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003173 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003174 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003175 qel = next_el;
3176 goto next_tel;
3177 }
3178
3179 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003180 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003181 return 1;
3182
3183 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003184 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003185 return 0;
3186}
3187
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003188/* QUIC connection packet handler task. */
3189struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003190{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003191 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003192 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003193 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003194 enum quic_tls_enc_level tel, next_tel;
3195 struct quic_enc_level *qel, *next_qel;
3196 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003197 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003198 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003199
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003200 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003201 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003202 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003203 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003204 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003205 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003206 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3207 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3208 /* The I/O handler has been woken up by the dgram listener
3209 * after the anti-amplification was reached.
3210 */
3211 qc_set_timer(qc);
3212 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3213 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3214 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003216 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3217 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3218 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003219 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003220 if (st >= QUIC_HS_ST_COMPLETE &&
3221 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3222 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3223 /* There may be remaining Handshake packets to treat and acknowledge. */
3224 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3225 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3226 }
3227 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003228 goto err;
3229
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003230 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003231 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003232
3233 next_level:
3234 tls_ctx = &qel->tls_ctx;
3235
3236 /* If the header protection key for this level has been derived,
3237 * remove the packet header protections.
3238 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003239 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003241 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003242
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003243 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3244 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3245 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003246 goto err;
3247
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003248 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3249 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3250 qel = next_qel;
3251 next_qel = NULL;
3252 goto next_level;
3253 }
3254
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003255 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003256 if (st >= QUIC_HS_ST_COMPLETE) {
3257 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3258 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003259 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003260
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003261 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3262 QUIC_FL_TLS_SECRETS_DCD)) {
3263 /* Discard the Handshake keys. */
3264 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3265 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3266 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3267 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003268 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003269 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003270 }
3271
3272 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3273 /* There may be remaining handshake to build (acks) */
3274 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3275 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003276 }
3277
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003278 if (!qr)
3279 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003280 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3281 * be considered.
3282 */
3283 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003284 goto err;
3285 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003286 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003287 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003288 else if (ret == 0)
3289 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003291 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003292 goto err;
3293
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003294 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003295 /* Check if there is something to do for the next level.
3296 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003297 if (next_qel && next_qel != qel &&
3298 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003299 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003300 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003301 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003302 goto next_level;
3303 }
3304
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003305 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003306 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003307 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003308
3309 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003310 if (qr)
3311 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003312 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003313 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003314}
3315
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003316/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003317static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3318{
3319 int i;
3320
3321 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3322 if (qel->tx.crypto.bufs[i]) {
3323 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3324 qel->tx.crypto.bufs[i] = NULL;
3325 }
3326 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003327 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003328}
3329
3330/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003331 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003332 * Returns 1 if succeeded, 0 if not.
3333 */
3334static int quic_conn_enc_level_init(struct quic_conn *qc,
3335 enum quic_tls_enc_level level)
3336{
3337 struct quic_enc_level *qel;
3338
3339 qel = &qc->els[level];
3340 qel->level = quic_to_ssl_enc_level(level);
3341 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3342 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3343 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3344 qel->tls_ctx.rx.flags = 0;
3345 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003346 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003347
3348 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003349 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003350 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003351 qel->rx.crypto.offset = 0;
3352 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003353
3354 /* Allocate only one buffer. */
3355 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3356 if (!qel->tx.crypto.bufs)
3357 goto err;
3358
3359 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3360 if (!qel->tx.crypto.bufs[0])
3361 goto err;
3362
3363 qel->tx.crypto.bufs[0]->sz = 0;
3364 qel->tx.crypto.nb_buf = 1;
3365
3366 qel->tx.crypto.sz = 0;
3367 qel->tx.crypto.offset = 0;
3368
3369 return 1;
3370
3371 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003372 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003373 return 0;
3374}
3375
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003376/* Increment the <qc> refcount.
3377 *
3378 * This operation must be conducted when manipulating the quic_conn outside of
3379 * the connection pinned thread. These threads can only retrieve the connection
3380 * in the CID tree, so this function must be conducted under the CID lock.
3381 */
3382static inline void quic_conn_take(struct quic_conn *qc)
3383{
3384 HA_ATOMIC_INC(&qc->refcount);
3385}
3386
3387/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003388 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003389 */
3390static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003391{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003392 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003393 int i;
3394
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003395 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003396 return;
3397
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003398 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003399 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003400
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003401 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003402 if (conn_ctx) {
3403 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003404 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003405 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003406
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003408 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003409
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003410 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3411 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003412 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003413}
3414
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003415/* Release the quic_conn <qc>. It will decrement its refcount so that the
3416 * connection will be freed once all threads have finished to work with it. The
3417 * connection is removed from the CIDs tree and thus cannot be found by other
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003418 * threads after it. The connection tasklet is killed.
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003419 *
3420 * Do not use <qc> after it as it may be freed. This function must only be
3421 * called by the thread responsible of the quic_conn tasklet.
3422 */
3423static void quic_conn_release(struct quic_conn *qc)
3424{
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003425 struct ssl_sock_ctx *conn_ctx;
3426
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003427 /* remove the connection from receiver cids trees */
3428 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3429 ebmb_delete(&qc->odcid_node);
3430 ebmb_delete(&qc->scid_node);
3431 free_quic_conn_cids(qc);
3432 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3433
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003434 /* Kill the tasklet. Do not use tasklet_free as this is not thread safe
3435 * as other threads may call tasklet_wakeup after this.
3436 */
3437 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3438 if (conn_ctx)
3439 tasklet_kill(conn_ctx->wait_event.tasklet);
3440
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003441 quic_conn_drop(qc);
3442}
3443
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003444void quic_close(struct connection *conn, void *xprt_ctx)
3445{
3446 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3447 struct quic_conn *qc = conn_ctx->conn->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003448
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003449 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003450 /* This task must be deleted by the connection-pinned thread. */
3451 if (qc->timer_task) {
3452 task_destroy(qc->timer_task);
3453 qc->timer_task = NULL;
3454 }
3455
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003456 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003457
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003458 /* TODO for now release the quic_conn on notification by the upper
3459 * layer. It could be useful to delay it if there is remaining data to
3460 * send or data to be acked.
3461 */
3462 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003463}
3464
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003465/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003466static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003467{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003468 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003469 struct quic_conn *qc;
3470 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003471 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003472
3473 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003474 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003475 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003476 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003477 task->expire = TICK_ETERNITY;
3478 pktns = quic_loss_pktns(qc);
3479 if (tick_isset(pktns->tx.loss_time)) {
3480 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3481
3482 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3483 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003484 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3485 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486 goto out;
3487 }
3488
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003489 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003491 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003492 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3493 pktns->tx.pto_probe = 1;
3494 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3495 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3496 }
3497 else {
3498 pktns->tx.pto_probe = 2;
3499 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003500 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003501 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003502 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3503 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3504
3505 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3506 hel->pktns->tx.pto_probe = 1;
3507 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3508 iel->pktns->tx.pto_probe = 1;
3509 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003510
3511 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3512 int j;
3513
3514 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3515 continue;
3516
3517 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3518 qc_prep_fast_retrans(&qc->els[i], qc);
3519 }
3520
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3522 qc->path->loss.pto_count++;
3523
3524 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003525 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003526
3527 return task;
3528}
3529
3530/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3531 * connections used to identify the first Initial packets of client connecting
3532 * to listeners. This parameter must be NULL for QUIC connections attached
3533 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3534 * <scid> is the source connection ID with <scid_len> as length.
3535 * Returns 1 if succeeded, 0 if not.
3536 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003537static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003538 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003539 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003540{
3541 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003542 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003543 /* Initial CID. */
3544 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003545 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003546 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003548 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003549 qc = pool_zalloc(pool_head_quic_conn);
3550 if (!qc) {
3551 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3552 goto err;
3553 }
3554
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003555 HA_ATOMIC_STORE(&qc->refcount, 0);
3556
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003557 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3558 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003559 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003560 goto err;
3561 }
3562
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003563 qc->cids = EB_ROOT;
3564 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003565 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003566 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003567
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003568 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003569 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003570 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003571 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003572 qc->odcid.addrlen = dcid_addr_len;
3573 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003574
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003575 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003576 if (scid_len)
3577 memcpy(qc->dcid.data, scid, scid_len);
3578 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003579 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003580 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003581 }
3582 /* QUIC Client (outgoing connection to servers) */
3583 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003584 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003585 if (dcid_len)
3586 memcpy(qc->dcid.data, dcid, dcid_len);
3587 qc->dcid.len = dcid_len;
3588 }
3589
3590 /* Initialize the output buffer */
3591 qc->obuf.pos = qc->obuf.data;
3592
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003593 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003594 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003595 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003596 goto err;
3597 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003598
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003599 /* insert the allocated CID in the receiver tree */
3600 if (server) {
3601 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3602 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3603 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3604 }
3605
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003606 /* Select our SCID which is the first CID with 0 as sequence number. */
3607 qc->scid = icid->cid;
3608
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 /* Packet number spaces initialization. */
3610 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3611 quic_pktns_init(&qc->pktns[i]);
3612 /* QUIC encryption level context initialization. */
3613 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003614 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003615 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003616 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003617 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003618 /* Initialize the packet number space. */
3619 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3620 }
3621
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003622 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003623 qc->tps_tls_ext = qc->version & 0xff000000 ?
3624 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3625 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003626 /* TX part. */
3627 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003628 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3629 qc->tx.wbuf = qc->tx.rbuf = 0;
3630 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631 /* RX part. */
3632 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003633 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003634 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3635 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3636 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003637 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003638 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003639 goto err;
3640 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003641
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003642 /* XXX TO DO: Only one path at this time. */
3643 qc->path = &qc->paths[0];
3644 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3645
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003646 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003648 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003649
3650 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003651 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003652 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003653 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003654}
3655
3656/* Initialize the timer task of <qc> QUIC connection.
3657 * Returns 1 if succeeded, 0 if not.
3658 */
3659static int quic_conn_init_timer(struct quic_conn *qc)
3660{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003661 /* Attach this task to the same thread ID used for the connection */
3662 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663 if (!qc->timer_task)
3664 return 0;
3665
3666 qc->timer = TICK_ETERNITY;
3667 qc->timer_task->process = process_timer;
3668 qc->timer_task->context = qc->conn->xprt_ctx;
3669
3670 return 1;
3671}
3672
3673/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3674 * past one byte of this buffer.
3675 */
3676static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3677 struct quic_rx_packet *pkt)
3678{
3679 unsigned char dcid_len, scid_len;
3680
3681 /* Version */
3682 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3683 return 0;
3684
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003685 /* Destination Connection ID Length */
3686 dcid_len = *(*buf)++;
3687 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3688 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3689 /* XXX MUST BE DROPPED */
3690 return 0;
3691
3692 if (dcid_len) {
3693 /* Check that the length of this received DCID matches the CID lengths
3694 * of our implementation for non Initials packets only.
3695 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003696 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3697 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003698 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003699 return 0;
3700
3701 memcpy(pkt->dcid.data, *buf, dcid_len);
3702 }
3703
3704 pkt->dcid.len = dcid_len;
3705 *buf += dcid_len;
3706
3707 /* Source Connection ID Length */
3708 scid_len = *(*buf)++;
3709 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3710 /* XXX MUST BE DROPPED */
3711 return 0;
3712
3713 if (scid_len)
3714 memcpy(pkt->scid.data, *buf, scid_len);
3715 pkt->scid.len = scid_len;
3716 *buf += scid_len;
3717
3718 return 1;
3719}
3720
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003721/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3722 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3723 * encryption level matching with the packet type. <*qel> may be null if not found.
3724 * Note that <ctx> may be null (for Initial packets).
3725 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003726static 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 +02003727 struct quic_enc_level **qel)
3728{
3729 enum quic_tls_enc_level tel;
3730
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003731 tel = quic_packet_type_enc_level(pkt->type);
3732 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3733 *qel = NULL;
3734 return 0;
3735 }
3736
3737 *qel = &qc->els[tel];
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003738 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003739 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003740
3741 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003742 (tel != QUIC_TLS_ENC_LEVEL_APP ||
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003743 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003744 return 1;
3745
3746 return 0;
3747}
3748
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003749/* Insert <pkt> RX packet in its <qel> RX packets tree */
3750static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3751{
3752 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003753 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003754 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3755 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3756 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003757}
3758
3759/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003760 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3761 * byte past the end of the buffer containing this packet and <beg> the address of
3762 * the packet first byte.
3763 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3764 * Returns 1 if succeeded, 0 if not.
3765 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003766static inline int qc_try_rm_hp(struct quic_conn *qc,
3767 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003768 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003769 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003770 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003771{
3772 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003773 struct quic_enc_level *qel;
3774 /* Only for traces. */
3775 struct quic_rx_packet *qpkt_trace;
3776
3777 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003778 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003779 /* The packet number is here. This is also the start minus
3780 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3781 * protection.
3782 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003783 pn = buf;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003784 if (qc_pkt_may_rm_hp(qc, pkt, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785 /* Note that the following function enables us to unprotect the packet
3786 * number and its length subsequently used to decrypt the entire
3787 * packets.
3788 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003789 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3790 qel->pktns->rx.largest_pn, pn, beg, end)) {
3791 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 goto err;
3793 }
3794
3795 /* The AAD includes the packet number field found at <pn>. */
3796 pkt->aad_len = pn - beg + pkt->pnl;
3797 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003798 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003799 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003800 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3801 /* If the packet number space has been discarded, this packet
3802 * will be not parsed.
3803 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003804 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003805 goto out;
3806 }
3807
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003808 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003809 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003810 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3811 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003812 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003813 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003814 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003815 goto err;
3816 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003817
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003818 *el = qel;
3819 /* No reference counter incrementation here!!! */
3820 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3821 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3822 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3823 b_add(&qc->rx.buf, pkt->len);
3824 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003825 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003826 return 1;
3827
3828 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003829 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003830 return 0;
3831}
3832
3833/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3834 * Also set <*long_header> to 1 if this form is long, 0 if not.
3835 */
3836static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3837 unsigned char byte0, int *long_header)
3838{
3839 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3840 pkt->type =
3841 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3842 *long_header = 1;
3843 }
3844 else {
3845 pkt->type = QUIC_PACKET_TYPE_SHORT;
3846 *long_header = 0;
3847 }
3848}
3849
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003850/*
3851 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3852 */
3853static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3854{
3855 int j = 0, version;
3856
3857 do {
3858 version = quic_supported_version[j];
3859 if (version == pkt->version)
3860 return 1;
3861
3862 version = quic_supported_version[++j];
3863 } while(version);
3864
3865 return 0;
3866}
3867
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003868__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003869static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3870 struct quic_rx_packet *pkt,
3871 struct quic_dgram_ctx *dgram_ctx,
3872 struct sockaddr_storage *saddr)
3873{
3874 unsigned char *beg;
3875 uint64_t len;
3876 struct quic_conn *qc;
3877 struct eb_root *cids;
3878 struct ebmb_node *node;
3879 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003880 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003881 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003882 size_t b_cspace;
3883 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003884
3885 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003886 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003887 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3888 if (end <= *buf)
3889 goto err;
3890
3891 /* Fixed bit */
3892 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3893 /* XXX TO BE DISCARDED */
3894 goto err;
3895
3896 srv_conn = dgram_ctx->owner;
3897 beg = *buf;
3898 /* Header form */
3899 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3900 if (long_header) {
3901 size_t cid_lookup_len;
3902
3903 if (!quic_packet_read_long_header(buf, end, pkt))
3904 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003905
3906 /* unsupported QUIC version */
3907 if (!qc_pkt_is_supported_version(pkt)) {
3908 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3909 goto err;
3910 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003911
3912 /* For Initial packets, and for servers (QUIC clients connections),
3913 * there is no Initial connection IDs storage.
3914 */
3915 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3916 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3917 cid_lookup_len = pkt->dcid.len;
3918 }
3919 else {
3920 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003921 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003922 }
3923
3924 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3925 if (!node)
3926 goto err;
3927
3928 qc = ebmb_entry(node, struct quic_conn, scid_node);
3929
3930 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3931 qc->dcid.len = pkt->scid.len;
3932 if (pkt->scid.len)
3933 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3934 }
3935
3936 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3937 uint64_t token_len;
3938
3939 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3940 goto err;
3941
3942 /* XXX TO DO XXX 0 value means "the token is not present".
3943 * A server which sends an Initial packet must not set the token.
3944 * So, a client which receives an Initial packet with a token
3945 * MUST discard the packet or generate a connection error with
3946 * PROTOCOL_VIOLATION as type.
3947 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3948 */
3949 pkt->token_len = token_len;
3950 }
3951 }
3952 else {
3953 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003954 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003955 goto err;
3956
3957 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003958 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959 if (!node)
3960 goto err;
3961
3962 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003963 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003964 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003965
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003966 /* Only packets packets with long headers and not RETRY or VERSION as type
3967 * have a length field.
3968 */
3969 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3970 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3971 goto err;
3972
3973 pkt->len = len;
3974 }
3975 else if (!long_header) {
3976 /* A short packet is the last one of an UDP datagram. */
3977 pkt->len = end - *buf;
3978 }
3979
3980 conn_ctx = qc->conn->xprt_ctx;
3981
3982 /* Increase the total length of this packet by the header length. */
3983 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003984
3985 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3986 * they must have the same DCID.
3987 *
3988 * This check must be done after the final update to pkt.len to
3989 * properly drop the packet on failure.
3990 */
3991 if (!dgram_ctx->dcid.len) {
3992 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3993 }
3994 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003995 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003996 goto err;
3997 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003998 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003999
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004000 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
4001 b_cspace = b_contig_space(&qc->rx.buf);
4002 if (b_cspace < pkt->len) {
4003 /* Let us consume the remaining contiguous space. */
4004 b_add(&qc->rx.buf, b_cspace);
4005 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4006 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004007 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004008 goto err;
4009 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004010 }
4011
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004012 if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004013 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004014 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004015 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004016 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004017
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004018 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
4019 if (pkt->aad_len)
4020 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021 /* Wake the tasklet of the QUIC connection packet handler. */
4022 if (conn_ctx)
4023 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4024
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004025 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004026
4027 return pkt->len;
4028
4029 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004030 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004031 return -1;
4032}
4033
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004034/*
4035 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4036 * address <addr>.
4037 * Implementation of RFC9000 6. Version Negotiation
4038 *
4039 * TODO implement a rate-limiting sending of Version Negotiation packets
4040 *
4041 * Returns 0 on success else non-zero
4042 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004043static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4044 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004045{
4046 char buf[256];
4047 int i = 0, j, version;
4048 const socklen_t addrlen = get_addr_len(addr);
4049
4050 /*
4051 * header form
4052 * long header, fixed bit to 0 for Version Negotiation
4053 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004054 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4055 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004056
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004057 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004058 /* null version for Version Negotiation */
4059 buf[i++] = '\x00';
4060 buf[i++] = '\x00';
4061 buf[i++] = '\x00';
4062 buf[i++] = '\x00';
4063
4064 /* source connection id */
4065 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004066 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004067 i += pkt->scid.len;
4068
4069 /* destination connection id */
4070 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004071 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004072 i += pkt->dcid.len;
4073
4074 /* supported version */
4075 j = 0;
4076 do {
4077 version = htonl(quic_supported_version[j]);
4078 memcpy(&buf[i], &version, sizeof(version));
4079 i += sizeof(version);
4080
4081 version = quic_supported_version[++j];
4082 } while (version);
4083
4084 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4085 return 1;
4086
4087 return 0;
4088}
4089
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004090/* Generate the token to be used in Retry packets. The token is written to
4091 * <buf> which is expected to be <len> bytes.
4092 *
4093 * Various parameters are expected to be encoded in the token. For now, only
4094 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4095 * as this CID must be repeated by the server in the transport parameters.
4096 *
4097 * TODO add the client address to validate the token origin.
4098 *
4099 * Returns the length of the encoded token or 0 on error.
4100 */
4101static int generate_retry_token(unsigned char *buf, unsigned char len,
4102 struct quic_rx_packet *pkt)
4103{
4104 const size_t token_len = 1 + pkt->dcid.len;
4105 unsigned char i = 0;
4106
4107 if (token_len > len)
4108 return 0;
4109
4110 buf[i++] = pkt->dcid.len;
4111 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4112 i += pkt->dcid.len;
4113
4114 return i;
4115}
4116
4117/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4118 * the Initial <pkt> packet.
4119 *
4120 * Returns 0 on success else non-zero.
4121 */
4122static int send_retry(int fd, struct sockaddr_storage *addr,
4123 struct quic_rx_packet *pkt)
4124{
4125 unsigned char buf[128];
4126 int i = 0, token_len;
4127 const socklen_t addrlen = get_addr_len(addr);
4128 struct quic_cid scid;
4129
4130 /* long header + fixed bit + packet type 0x3 */
4131 buf[i++] = 0xf0;
4132 /* version */
4133 buf[i++] = 0x00;
4134 buf[i++] = 0x00;
4135 buf[i++] = 0x00;
4136 buf[i++] = 0x01;
4137
4138 /* Use the SCID from <pkt> for Retry DCID. */
4139 buf[i++] = pkt->scid.len;
4140 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4141 i += pkt->scid.len;
4142
4143 /* Generate a new CID to be used as SCID for the Retry packet. */
4144 scid.len = QUIC_HAP_CID_LEN;
4145 if (RAND_bytes(scid.data, scid.len) != 1)
4146 return 1;
4147
4148 buf[i++] = scid.len;
4149 memcpy(&buf[i], scid.data, scid.len);
4150 i += scid.len;
4151
4152 /* token */
4153 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4154 return 1;
4155 i += token_len;
4156
4157 /* token integrity tag */
4158 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4159 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4160 pkt->dcid.len, buf, i)) {
4161 return 1;
4162 }
4163
4164 i += QUIC_TLS_TAG_LEN;
4165
4166 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4167 return 1;
4168
4169 return 0;
4170}
4171
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004172/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4173 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4174 * concatenated to the <pkt> DCID field.
4175 *
4176 * Returns the instance or NULL if not found.
4177 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004178static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004179 struct listener *l,
4180 struct sockaddr_storage *saddr)
4181{
4182 struct quic_conn *qc = NULL;
4183 struct ebmb_node *node;
4184 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004185 /* set if the quic_conn is found in the second DCID tree */
4186 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004187
4188 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
4189
4190 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4191 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4192 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4193 /* DCIDs of first packets coming from multiple clients may have
4194 * the same values. Let's distinguish them by concatenating the
4195 * socket addresses.
4196 */
4197 quic_cid_saddr_cat(&pkt->dcid, saddr);
4198 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
4199 pkt->dcid.len + pkt->dcid.addrlen);
4200 if (node) {
4201 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4202 goto end;
4203 }
4204 }
4205
4206 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4207 * also for INITIAL/0-RTT non-first packets with the final DCID in
4208 * used.
4209 */
4210 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
4211 if (!node)
4212 goto end;
4213
4214 id = ebmb_entry(node, struct quic_connection_id, node);
4215 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004216 found_in_dcid = 1;
4217
4218 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004219 if (qc)
4220 quic_conn_take(qc);
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004221 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004222
4223 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4224 * If already done, this is a noop.
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004225 *
4226 * node.leaf_p is first checked to avoid unnecessary locking.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004227 */
Amaury Denoyellec6fab982021-12-23 16:27:56 +01004228 if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) {
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004229 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
4230 ebmb_delete(&qc->odcid_node);
4231 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4232 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004233
4234 return qc;
4235}
4236
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004237/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4238 * will extract the parameters stored in the token : <odcid>.
4239 *
4240 * Returns 0 on success else non-zero.
4241 */
4242static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4243 struct quic_cid *odcid)
4244{
4245 uint64_t odcid_len;
4246
4247 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4248 return 1;
4249
4250 memcpy(odcid->data, token, odcid_len);
4251 odcid->len = odcid_len;
4252
4253 return 0;
4254}
4255
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004256static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004257 struct quic_rx_packet *pkt,
4258 struct quic_dgram_ctx *dgram_ctx,
4259 struct sockaddr_storage *saddr)
4260{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004261 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004262 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004263 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004264 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004265 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004266 size_t b_cspace;
4267 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004268
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004269 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004270 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004271 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004272 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004273 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4274 /* This ist only to please to traces and distinguish the
4275 * packet with parsed packet number from others.
4276 */
4277 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004278 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004279 goto err;
4280
4281 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004282 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004283 /* XXX TO BE DISCARDED */
4284 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4285 goto err;
4286 }
4287
4288 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004289 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004290 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004291 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004292 uint64_t len;
4293
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004294 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004295 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4296 goto err;
4297 }
4298
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004299 /* Retry of Version Negotiation packets are only sent by servers */
4300 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4301 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4302 goto err;
4303 }
4304
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004305 /* RFC9000 6. Version Negotiation */
4306 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004307 /* unsupported version, send Negotiation packet */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004308 if (send_version_negotiation(l->rx.fd, saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004309 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4310 goto err;
4311 }
4312
4313 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004314 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004315 }
4316
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317 /* For Initial packets, and for servers (QUIC clients connections),
4318 * there is no Initial connection IDs storage.
4319 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004320 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004321 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004322
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004323 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4324 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004325 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4326 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004327 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004328
4329 /* XXX TO DO XXX 0 value means "the token is not present".
4330 * A server which sends an Initial packet must not set the token.
4331 * So, a client which receives an Initial packet with a token
4332 * MUST discard the packet or generate a connection error with
4333 * PROTOCOL_VIOLATION as type.
4334 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4335 */
4336 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004337
4338 /* TODO Retry should be automatically activated if
4339 * suspect network usage is detected.
4340 */
4341 if (!token_len && l->bind_conf->quic_force_retry) {
4342 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
4343 if (send_retry(l->rx.fd, saddr, pkt)) {
4344 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4345 goto err;
4346 }
4347
4348 goto err;
4349 }
4350 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004351 pkt->token = buf;
4352 buf += pkt->token_len;
4353 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004354 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004355 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004356 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004357 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4358 goto err;
4359 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004360 }
4361
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004362 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4363 end - buf < len) {
4364 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4365 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004366 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004367
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004368 payload = buf;
4369 pkt->len = len + payload - beg;
4370
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004371 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004372 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004373 int ipv4;
4374 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004375 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004376 const unsigned char *salt = initial_salt_v1;
4377 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004378
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004379 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004380 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004381 goto err;
4382 }
4383
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004384 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004385 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004386 qc = qc_new_conn(pkt->version, ipv4,
4387 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004388 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004389 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004390 goto err;
4391
4392 odcid = &qc->rx.params.original_destination_connection_id;
4393 /* Copy the transport parameters. */
4394 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004395
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004396 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004397 if (pkt->token_len) {
4398 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4399 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4400 goto err;
4401 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004402 /* Copy retry_source_connection_id transport parameter. */
4403 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4404 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004405 }
4406 else {
4407 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4408 odcid->len = pkt->dcid.len;
4409 }
4410
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004411 /* Copy the initial source connection ID. */
4412 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4413 qc->enc_params_len =
4414 quic_transport_params_encode(qc->enc_params,
4415 qc->enc_params + sizeof qc->enc_params,
4416 &qc->rx.params, 1);
4417 if (!qc->enc_params_len)
4418 goto err;
4419
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004420 /* NOTE: the socket address has been concatenated to the destination ID
4421 * chosen by the client for Initial packets.
4422 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004423 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4424 salt = initial_salt_draft_29;
4425 salt_len = sizeof initial_salt_draft_29;
4426 }
4427 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004428 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004429 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004430 goto err;
4431 }
4432
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004433 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004434 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004435 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4436 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004437
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004438 /* If the insertion failed, it means that another
4439 * thread has already allocated a QUIC connection for
4440 * the same CID. Liberate our allocated connection.
4441 */
4442 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004443 qc_to_purge = qc;
4444
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004445 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4446 pkt->qc = qc;
4447 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004448
4449 quic_conn_take(qc);
4450 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4451
4452 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004453 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004454 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004455 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4456 /* Try to accept a new connection. */
4457 listener_accept(l);
4458 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004459 else {
4460 quic_conn_drop(qc_to_purge);
4461 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004462 }
4463 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004464 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004465 }
4466 }
4467 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004468 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004469 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4470 goto err;
4471 }
4472
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004473 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004474 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004475 buf += QUIC_HAP_CID_LEN;
4476
4477 /* A short packet is the last one of a UDP datagram. */
4478 payload = buf;
4479 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004480
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004481 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004482 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004483 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004484 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4485 goto err;
4486 }
4487
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004488 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004489 }
4490
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004491
4492 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4493 * they must have the same DCID.
4494 *
4495 * This check must be done after the final update to pkt.len to
4496 * properly drop the packet on failure.
4497 */
4498 if (!dgram_ctx->dcid.len) {
4499 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004500 if (!quic_peer_validated_addr(qc) &&
4501 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4502 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4503 QUIC_EV_CONN_LPKT, qc);
4504 /* Reset the anti-amplification bit. It will be set again
4505 * when sending the next packet if reached again.
4506 */
4507 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4508 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4509 io_cb_wakeup = 1;
4510 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004511 }
4512 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004513 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004514 goto err;
4515 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004516 dgram_ctx->qc = qc;
4517
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004518
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004519 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004520 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004521 goto out;
4522 }
4523
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004524 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004525 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004526 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004527 b_cspace = b_contig_space(&qc->rx.buf);
4528 if (b_cspace < pkt->len) {
4529 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004530 if (b_cspace) {
4531 b_putchr(&qc->rx.buf, 0x00);
4532 b_cspace--;
4533 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004534 b_add(&qc->rx.buf, b_cspace);
4535 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4536 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004537 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004538 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004539 goto err;
4540 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004541 }
4542
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004543 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004544 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004545 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004546 goto err;
4547 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004548
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004549 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004550 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004551 if (pkt->aad_len)
4552 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004553 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004554 /* Wake up the connection packet handler task from here only if all
4555 * the contexts have been initialized, especially the mux context
4556 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4557 * will start it if these contexts for the connection are not already
4558 * initialized.
4559 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004560 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Frédéric Lécailleb80b20c2022-01-12 17:46:56 +01004561 if (conn_ctx && HA_ATOMIC_LOAD(&qc->qcc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004562 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004563
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004564 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004565
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004566 if (qc)
4567 quic_conn_drop(qc);
4568
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004569 return pkt->len;
4570
4571 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004572 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4573 * This cannot be done by this thread.
4574 */
4575 if (io_cb_wakeup) {
4576 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4577 if (conn_ctx && conn_ctx->wait_event.tasklet)
4578 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4579 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004580 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004581 if (!pkt->len)
4582 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004583 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004584 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004585
4586 if (qc)
4587 quic_conn_drop(qc);
4588
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004589 return -1;
4590}
4591
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004592/* This function builds into <buf> buffer a QUIC long packet header.
4593 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004594 */
4595static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4596 int type, size_t pn_len, struct quic_conn *conn)
4597{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004598 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004599 return 0;
4600
4601 /* #0 byte flags */
4602 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4603 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4604 /* Version */
4605 quic_write_uint32(buf, end, conn->version);
4606 *(*buf)++ = conn->dcid.len;
4607 /* Destination connection ID */
4608 if (conn->dcid.len) {
4609 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4610 *buf += conn->dcid.len;
4611 }
4612 /* Source connection ID */
4613 *(*buf)++ = conn->scid.len;
4614 if (conn->scid.len) {
4615 memcpy(*buf, conn->scid.data, conn->scid.len);
4616 *buf += conn->scid.len;
4617 }
4618
4619 return 1;
4620}
4621
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004622/* This function builds into <buf> buffer a QUIC short packet header.
4623 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004624 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004625static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4626 size_t pn_len, struct quic_conn *conn,
4627 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004628{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004629 if (end - *buf < 1 + conn->dcid.len)
4630 return 0;
4631
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004632 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004633 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4634 ((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 +01004635 /* Destination connection ID */
4636 if (conn->dcid.len) {
4637 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4638 *buf += conn->dcid.len;
4639 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004640
4641 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004642}
4643
4644/* Apply QUIC header protection to the packet with <buf> as first byte address,
4645 * <pn> as address of the Packet number field, <pnlen> being this field length
4646 * with <aead> as AEAD cipher and <key> as secret key.
4647 * Returns 1 if succeeded or 0 if failed.
4648 */
4649static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4650 const EVP_CIPHER *aead, const unsigned char *key)
4651{
4652 int i, ret, outlen;
4653 EVP_CIPHER_CTX *ctx;
4654 /* We need an IV of at least 5 bytes: one byte for bytes #0
4655 * and at most 4 bytes for the packet number
4656 */
4657 unsigned char mask[5] = {0};
4658
4659 ret = 0;
4660 ctx = EVP_CIPHER_CTX_new();
4661 if (!ctx)
4662 return 0;
4663
4664 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4665 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4666 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4667 goto out;
4668
4669 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4670 for (i = 0; i < pnlen; i++)
4671 pn[i] ^= mask[i + 1];
4672
4673 ret = 1;
4674
4675 out:
4676 EVP_CIPHER_CTX_free(ctx);
4677
4678 return ret;
4679}
4680
4681/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4682 * ACK ranges if needed to a value below <limit> in bytes.
4683 * Return 1 if succeeded, 0 if not.
4684 */
4685static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4686{
4687 size_t room, ack_delay_sz;
4688
4689 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4690 /* A frame is made of 1 byte for the frame type. */
4691 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004692 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004693 return 0;
4694
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004695 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004696}
4697
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004698/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4699 * 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 +01004700 * and <*len> the packet Length field initialized with the number of bytes already present
4701 * 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 +02004702 * <headlen> is the number of bytes already present in this packet before building frames.
4703 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004704 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004705 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004706 * Return 1 if succeeded, 0 if not.
4707 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004708static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004709 size_t room, size_t *len, size_t headlen,
4710 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004711 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004712{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004713 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004714 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004715
Frédéric Lécailleea604992020-12-24 13:01:37 +01004716 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004717 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004718 return 0;
4719
Frédéric Lécailleea604992020-12-24 13:01:37 +01004720 /* If we are not probing we must take into an account the congestion
4721 * control window.
4722 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004723 if (!qel->pktns->tx.pto_probe) {
4724 size_t remain = quic_path_prep_data(qc->path);
4725
4726 if (headlen > remain)
4727 return 0;
4728
4729 room = QUIC_MIN(room, remain - headlen);
4730 }
4731
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004732 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004733 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004734 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004735 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004736 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004737
Frédéric Lécailleea604992020-12-24 13:01:37 +01004738 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004739 break;
4740
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004741 switch (cf->type) {
4742 case QUIC_FT_CRYPTO:
4743 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004744 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004745 /* Compute the length of this CRYPTO frame header */
4746 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4747 /* Compute the data length of this CRyPTO frame. */
4748 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4749 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004750 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004751 if (!dlen)
4752 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004753
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004754 /* CRYPTO frame length. */
4755 flen = hlen + quic_int_getsize(dlen) + dlen;
4756 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004757 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004758 /* Add the CRYPTO data length and its encoded length to the packet
4759 * length and the length of this length.
4760 */
4761 *len += flen;
4762 room -= flen;
4763 if (dlen == cf->crypto.len) {
4764 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004765 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004766 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004767 }
4768 else {
4769 struct quic_frame *new_cf;
4770
4771 new_cf = pool_alloc(pool_head_quic_frame);
4772 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004773 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004774 return 0;
4775 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004776
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004777 new_cf->type = QUIC_FT_CRYPTO;
4778 new_cf->crypto.len = dlen;
4779 new_cf->crypto.offset = cf->crypto.offset;
4780 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004781 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004782 /* Consume <dlen> bytes of the current frame. */
4783 cf->crypto.len -= dlen;
4784 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004785 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004786 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004787
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004788 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004789 /* Note that these frames are accepted in short packets only without
4790 * "Length" packet field. Here, <*len> is used only to compute the
4791 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004792 *
4793 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004794 * excepting the variable ones. Note that +1 is for the type of this frame.
4795 */
4796 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004797 ((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 +02004798 /* Compute the data length of this STREAM frame. */
4799 avail_room = room - hlen - *len;
4800 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004801 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004802
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004803 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004804 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004805 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4806 dlen = max_available_room(avail_room, &dlen_sz);
4807 if (dlen > cf->stream.len) {
4808 dlen = cf->stream.len;
4809 }
4810 dlen_sz = quic_int_getsize(dlen);
4811 flen = hlen + dlen_sz + dlen;
4812 }
4813 else {
4814 dlen = QUIC_MIN(avail_room, cf->stream.len);
4815 flen = hlen + dlen;
4816 }
4817 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004818 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004819 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004820 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004821 /* Add the STREAM data length and its encoded length to the packet
4822 * length and the length of this length.
4823 */
4824 *len += flen;
4825 room -= flen;
4826 if (dlen == cf->stream.len) {
4827 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004828 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004829 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004830 }
4831 else {
4832 struct quic_frame *new_cf;
4833
4834 new_cf = pool_zalloc(pool_head_quic_frame);
4835 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004836 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004837 return 0;
4838 }
4839
4840 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004841 new_cf->stream.qcs = cf->stream.qcs;
4842 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004843 new_cf->stream.id = cf->stream.id;
4844 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4845 new_cf->stream.offset = cf->stream.offset;
4846 new_cf->stream.len = dlen;
4847 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4848 /* FIN bit reset */
4849 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4850 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004851 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004852 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4853 /* Consume <dlen> bytes of the current frame. */
4854 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004855 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004856 cf->stream.data += dlen;
4857 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004858 break;
4859
4860 default:
4861 flen = qc_frm_len(cf);
4862 BUG_ON(!flen);
4863 if (flen > room)
4864 continue;
4865
4866 *len += flen;
4867 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004868 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004869 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004870 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004871 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004872 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004873 }
4874
Frédéric Lécailleea604992020-12-24 13:01:37 +01004875 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004876}
4877
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004878/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004879 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4880 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4881 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004882 * 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 +02004883 * 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 +02004884 * having returned from this function.
4885 * 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 +01004886 * number field in this packet. <pn_len> will also have the packet number
4887 * length as value.
4888 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004889 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004890 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004891static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4892 size_t dglen, struct quic_tx_packet *pkt,
4893 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004894 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004895 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004896{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004897 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004898 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004899 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4900 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004901 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004902 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004903 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004904 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004905 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004906
Frédéric Lécailleea604992020-12-24 13:01:37 +01004907 /* Length field value with CRYPTO frames if present. */
4908 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004909 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004910 /* When not probing and not acking, and no immediate close is required,
4911 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004912 * the congestion controller window. So, we do not limit the size of this
4913 * packet if we have an ACK frame to send because an ACK frame is not
4914 * ack-eliciting. This size will be limited if we have ack-eliciting
4915 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004916 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004917 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004918 size_t path_room;
4919
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004920 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004921 if (end - beg > path_room)
4922 end = beg + path_room;
4923 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004924
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004925 /* Ensure there is enough room for the TLS encryption tag and a zero token
4926 * length field if any.
4927 */
4928 if (end - pos < QUIC_TLS_TAG_LEN +
4929 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4930 goto no_room;
4931
4932 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004933 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004934 /* packet number length */
4935 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004936 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004937 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004938 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004939 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004940 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004941 goto no_room;
4942
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004943 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4944 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004945 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004946 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004947 /* Build an ACK frame if required. */
4948 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004949 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004950 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004951 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004952 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4953 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4954 * that from here, we do not know if we will have to send a PING frame.
4955 * This will be decided after having computed the ack-eliciting frames
4956 * to be added to this packet.
4957 */
4958 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 +01004959 if (!ack_frm_len)
4960 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004961 }
4962
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004963 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004964 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004965 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004966 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004967 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004968
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004969 /* Initialize the length of the frames built below to <len>.
4970 * If any frame could be successfully built by qc_build_frms(),
4971 * we will have len_frms > len.
4972 */
4973 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004974 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004975 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004976 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004977 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004978 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004979
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004980 /* Length (of the remaining data). Must not fail because, the buffer size
4981 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4982 * for the encryption tag. It must be taken into an account for the length
4983 * of this packet.
4984 */
4985 if (len_frms)
4986 len = len_frms + QUIC_TLS_TAG_LEN;
4987 else
4988 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004989 /* CONNECTION_CLOSE frame */
4990 if (cc) {
4991 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004992
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004993 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004994 len += qc_frm_len(&cc_frm);
4995 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004996 add_ping_frm = 0;
4997 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004998 len_sz = quic_int_getsize(len);
4999 /* Add this packet size to <dglen> */
5000 dglen += head_len + len_sz + len;
5001 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5002 /* This is a maximum padding size */
5003 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5004 /* The length field value is of this packet is <len> + <padding_len>
5005 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005006 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005007 * sizes from <padding_len>.
5008 */
5009 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5010 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005011 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005012 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005013 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005014 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005015 add_ping_frm = 1;
5016 len += 1;
5017 }
5018 /* 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 +01005019 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005020 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5021 }
5022
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005023 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5024 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005025
5026 /* Packet number field address. */
5027 *buf_pn = pos;
5028
5029 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005030 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5031 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005032
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005033 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005034 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005036 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005037 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005038 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005039
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005040 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005041 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005042 ssize_t room = end - pos;
5043 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005044 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005045 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005046 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005047 }
5048 }
5049
5050 /* Build a PING frame if needed. */
5051 if (add_ping_frm) {
5052 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005053 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005054 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005055 }
5056
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005057 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005058 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005059 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005060
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005061 /* Build a PADDING frame if needed. */
5062 if (padding_len) {
5063 frm.type = QUIC_FT_PADDING;
5064 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005065 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005066 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005067 }
5068
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005069 /* If this packet is ack-eliciting and we are probing let's
5070 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005071 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005072 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5073 qel->pktns->tx.pto_probe)
5074 qel->pktns->tx.pto_probe--;
5075
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005076 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005077 LIST_SPLICE(&pkt->frms, &frm_list);
5078 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005079
5080 return 1;
5081
5082 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005083 /* Replace the pre-built frames which could not be add to this packet */
5084 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005085 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005086 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005087}
5088
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005089static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005090{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005091 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005092 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005093 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005094 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005095 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005096 pkt->next = NULL;
5097 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005098}
5099
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005100/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5102{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005103 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005104
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005105 if (!pkt)
5106 return;
5107
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005109 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005110 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005111 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005112 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005113}
5114
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005115/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5116 * type for <qc> QUIC connection from <qel> encryption level.
5117 * Return -2 if the packet could not be allocated or encrypted for any reason,
5118 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005119 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005120static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5121 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005122 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005123 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005124 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005125{
5126 /* The pointer to the packet number field. */
5127 unsigned char *buf_pn;
5128 unsigned char *beg, *end, *payload;
5129 int64_t pn;
5130 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005131 struct quic_tls_ctx *tls_ctx;
5132 struct quic_tx_packet *pkt;
5133
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005134 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005135 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136 pkt = pool_alloc(pool_head_quic_tx_packet);
5137 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005138 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005139 *err = -2;
5140 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005141 }
5142
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005143 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005144 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145 pn_len = 0;
5146 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005147
5148 pn = qel->pktns->tx.next_pn + 1;
5149 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 +01005150 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005151 *err = -1;
5152 goto err;
5153 }
5154
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005155 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005156 payload = buf_pn + pn_len;
5157 payload_len = end - payload;
5158 aad_len = payload - beg;
5159
5160 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005161 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005162 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005163 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005164 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005165
5166 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005167 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005168 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5169 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005170 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005171 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005172 goto err;
5173 }
5174
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005175 /* Consume a packet number */
5176 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005177 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005178 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5179 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005180 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5181 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005182 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005183 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005184 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005185 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005186 pkt->in_flight_len = pkt->len;
5187 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005188 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005189 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005190 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005191
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005192 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005193
5194 err:
5195 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005196 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005197 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005198}
5199
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005200/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
5201 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005202 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005203static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
5204 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005205{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005206 size_t try, done = 0;
5207
5208 if (!conn_ctrl_ready(conn))
5209 return 0;
5210
5211 if (!fd_recv_ready(conn->handle.fd))
5212 return 0;
5213
5214 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005215
5216 /* read the largest possible block. For this, we perform only one call
5217 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005218 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005219 */
5220 while (count > 0) {
5221 try = b_contig_space(buf);
5222 if (!try)
5223 break;
5224
5225 if (try > count)
5226 try = count;
5227
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005228 b_add(buf, try);
5229 done += try;
5230 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005231 }
5232
5233 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5234 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5235
5236 leave:
5237 return done;
5238
5239 read0:
5240 conn_sock_read0(conn);
5241 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5242
5243 /* Now a final check for a possible asynchronous low-level error
5244 * report. This can happen when a connection receives a reset
5245 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
5246 * we might have come from there by just checking POLL_HUP instead
5247 * of recv()'s return value 0, so we have no way to tell there was
5248 * an error without checking.
5249 */
Willy Tarreauf5090652021-04-06 17:23:40 +02005250 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005251 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5252 goto leave;
5253}
5254
5255
5256/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5257 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5258 * other pending data for example, but this flag is ignored at the moment.
5259 * Only one call to send() is performed, unless the buffer wraps, in which case
5260 * a second call may be performed. The connection's flags are updated with
5261 * whatever special event is detected (error, empty). The caller is responsible
5262 * for taking care of those events and avoiding the call if inappropriate. The
5263 * function does not call the connection's polling update function, so the caller
5264 * is responsible for this. It's up to the caller to update the buffer's contents
5265 * based on the return value.
5266 */
5267static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5268{
5269 ssize_t ret;
5270 size_t try, done;
5271 int send_flag;
5272
5273 if (!conn_ctrl_ready(conn))
5274 return 0;
5275
5276 if (!fd_send_ready(conn->handle.fd))
5277 return 0;
5278
5279 done = 0;
5280 /* send the largest possible block. For this we perform only one call
5281 * to send() unless the buffer wraps and we exactly fill the first hunk,
5282 * in which case we accept to do it once again.
5283 */
5284 while (count) {
5285 try = b_contig_data(buf, done);
5286 if (try > count)
5287 try = count;
5288
5289 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5290 if (try < count || flags & CO_SFL_MSG_MORE)
5291 send_flag |= MSG_MORE;
5292
5293 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
5294 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
5295 if (ret > 0) {
5296 count -= ret;
5297 done += ret;
5298
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005299 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005300 conn->flags |= CO_FL_WAIT_L4L6;
5301 /* if the system buffer is full, don't insist */
5302 if (ret < try)
5303 break;
5304 }
5305 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
5306 /* nothing written, we need to poll for write first */
5307 fd_cant_send(conn->handle.fd);
5308 break;
5309 }
5310 else if (errno != EINTR) {
5311 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5312 break;
5313 }
5314 }
5315 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5316 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5317
5318 if (done > 0) {
5319 /* we count the total bytes sent, and the send rate for 32-byte
5320 * blocks. The reason for the latter is that freq_ctr are
5321 * limited to 4GB and that it's not enough per second.
5322 */
5323 _HA_ATOMIC_ADD(&global.out_bytes, done);
5324 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5325 }
5326 return done;
5327}
5328
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005329/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5330 * event subscriber <es> is not allowed to change from a previous call as long
5331 * as at least one event is still subscribed. The <event_type> must only be a
5332 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5333 */
5334static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5335{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005336 struct qcc *qcc = conn->qc->qcc;
5337
5338 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5339 BUG_ON(qcc->subs && qcc->subs != es);
5340
5341 es->events |= event_type;
5342 qcc->subs = es;
5343
5344 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005345 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005346
5347 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005348 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005349
5350 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005351}
5352
5353/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5354 * The <es> pointer is not allowed to differ from the one passed to the
5355 * subscribe() call. It always returns zero.
5356 */
5357static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5358{
5359 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5360}
5361
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005362/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5363 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5364 * parameters of this session.
5365 * This is the responsibility of the caller to check the validity of all the
5366 * pointers passed as parameter to this function.
5367 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5368 * CO_ER_SSL_NO_MEM.
5369 */
5370static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5371 unsigned char *params, size_t params_len)
5372{
5373 int retry;
5374
5375 retry = 1;
5376 retry:
5377 *ssl = SSL_new(ssl_ctx);
5378 if (!*ssl) {
5379 if (!retry--)
5380 goto err;
5381
5382 pool_gc(NULL);
5383 goto retry;
5384 }
5385
5386 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5387 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
5388 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5389 goto err;
5390
5391 SSL_free(*ssl);
5392 *ssl = NULL;
5393 if (!retry--)
5394 goto err;
5395
5396 pool_gc(NULL);
5397 goto retry;
5398 }
5399
5400 return 0;
5401
5402 err:
5403 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5404 return -1;
5405}
5406
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005407/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5408 * connection with <xprt_ctx> as address of the xprt context.
5409 * Returns 1 if succeeded, 0 if not.
5410 */
5411static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5412{
Amaury Denoyelle9979d0d2021-12-23 16:32:24 +01005413 struct ssl_sock_ctx *ctx = NULL;
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005414 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005415
5416 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5417
5418 if (*xprt_ctx)
5419 goto out;
5420
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005421 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005422 if (!ctx) {
5423 conn->err_code = CO_ER_SYS_MEMLIM;
5424 goto err;
5425 }
5426
5427 ctx->wait_event.tasklet = tasklet_new();
5428 if (!ctx->wait_event.tasklet) {
5429 conn->err_code = CO_ER_SYS_MEMLIM;
5430 goto err;
5431 }
5432
5433 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5434 ctx->wait_event.tasklet->context = ctx;
5435 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005436 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005437 ctx->subs = NULL;
5438 ctx->xprt_ctx = NULL;
5439
5440 ctx->xprt = xprt_get(XPRT_QUIC);
5441 if (objt_server(conn->target)) {
5442 /* Server */
5443 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005444 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005445 int ssl_err, ipv4;
5446
5447 ssl_err = SSL_ERROR_NONE;
5448 if (RAND_bytes(dcid, sizeof dcid) != 1)
5449 goto err;
5450
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005451 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005452 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005453 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005454 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005455 goto err;
5456
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005457 ctx->qc = qc;
5458
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005459 /* Insert our SCID, the connection ID for the QUIC client. */
5460 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5461
5462 conn->qc = qc;
5463 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005464 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5465 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005466 goto err;
5467
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005468 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005469 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005470 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5471 qc->enc_params_len =
5472 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5473 &qc->rx.params, 0);
5474 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005475 goto err;
5476
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005477 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5478 qc->enc_params, qc->enc_params_len) == -1)
5479 goto err;
5480
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005481 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005482 SSL_set_connect_state(ctx->ssl);
5483 ssl_err = SSL_do_handshake(ctx->ssl);
5484 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005485 int st;
5486
5487 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005488 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5489 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005490 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005491 }
5492 else {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005493 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005494 goto err;
5495 }
5496 }
5497 }
5498 else if (objt_listener(conn->target)) {
5499 /* Listener */
5500 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
5501
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005502 qc = ctx->conn->qc;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005503 ctx->qc = qc;
5504
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005505 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005506 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5507 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005508 goto err;
5509
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005510 /* Enabling 0-RTT */
5511 if (bc->ssl_conf.early_data)
5512 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5513
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005514 SSL_set_accept_state(ctx->ssl);
5515 }
5516
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005517 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005518
5519 /* Leave init state and start handshake */
5520 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005521
5522 out:
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005523 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005524 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005525
5526 return 0;
5527
5528 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005529 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005530 tasklet_free(ctx->wait_event.tasklet);
5531 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005532 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005533 return -1;
5534}
5535
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005536/* Start the QUIC transport layer */
5537static int qc_xprt_start(struct connection *conn, void *ctx)
5538{
5539 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005540 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005541
5542 qc = conn->qc;
5543 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005544 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005545 return 0;
5546 }
5547
5548 tasklet_wakeup(qctx->wait_event.tasklet);
5549 return 1;
5550}
5551
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005552/* transport-layer operations for QUIC connections. */
5553static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005554 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005555 .snd_buf = quic_conn_from_buf,
5556 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005557 .subscribe = quic_conn_subscribe,
5558 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005559 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005560 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005561 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5562 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005563 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005564 .name = "QUIC",
5565};
5566
5567__attribute__((constructor))
5568static void __quic_conn_init(void)
5569{
5570 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5571 xprt_register(XPRT_QUIC, &ssl_quic);
5572}
5573
5574__attribute__((destructor))
5575static void __quic_conn_deinit(void)
5576{
5577 BIO_meth_free(ha_quic_meth);
5578}
5579
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005580/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5581 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005582 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005583 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005584static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005585 struct sockaddr_storage *saddr, qpkt_read_func *func)
5586{
5587 unsigned char *pos;
5588 const unsigned char *end;
5589 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005590 .qc = NULL,
5591 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005592 .owner = owner,
5593 };
5594
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005595 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005596 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005597 do {
5598 int ret;
5599 struct quic_rx_packet *pkt;
5600
Willy Tarreaue4498932021-03-22 21:13:05 +01005601 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005602 if (!pkt)
5603 goto err;
5604
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005605 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005606 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5607 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005608 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005609 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005610 /* If the packet length could not be found, we cannot continue. */
5611 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005612 } while (pos < end);
5613
5614 /* Increasing the received bytes counter by the UDP datagram length
5615 * if this datagram could be associated to a connection.
5616 */
5617 if (dgram_ctx.qc)
5618 dgram_ctx.qc->rx.bytes += len;
5619
Amaury Denoyellece340fe2022-01-11 14:20:46 +01005620 return pos - (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005621
5622 err:
5623 return -1;
5624}
5625
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005626ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005627 struct sockaddr_storage *saddr)
5628{
5629 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5630}
5631
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005632/* Function to automatically activate QUIC traces on stdout.
5633 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5634 * Main use for now is in the docker image for QUIC interop testing.
5635 */
5636static void quic_init_stdout_traces(void)
5637{
5638#ifdef ENABLE_QUIC_STDOUT_TRACES
5639 trace_quic.sink = sink_find("stdout");
5640 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005641 trace_quic.state = TRACE_STATE_RUNNING;
5642#endif
5643}
5644INITCALL0(STG_INIT, quic_init_stdout_traces);
5645
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005646/*
5647 * Local variables:
5648 * c-indent-level: 8
5649 * c-basic-offset: 8
5650 * End:
5651 */