blob: dc487b2c8bbf9529cb955416053a7e15ed8a574e [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>
Amaury Denoyellecfa2d562022-01-19 16:01:05 +010047#include <haproxy/quic_sock.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020048#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010049#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010051#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010052#include <haproxy/ssl_sock.h>
53#include <haproxy/stream_interface.h>
54#include <haproxy/task.h>
55#include <haproxy/trace.h>
56#include <haproxy/xprt_quic.h>
57
Amaury Denoyellea22d8602021-11-10 15:17:56 +010058/* list of supported QUIC versions by this implementation */
59static int quic_supported_version[] = {
60 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010061 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010062
63 /* placeholder, do not add entry after this */
64 0x0
65};
66
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020067/* This is the values of some QUIC transport parameters when absent.
68 * Should be used to initialize any transport parameters (local or remote)
69 * before updating them with customized values.
70 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010071struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020072 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020073 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
74 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020075 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010076};
77
78/* trace source and events */
79static void quic_trace(enum trace_level level, uint64_t mask, \
80 const struct trace_source *src,
81 const struct ist where, const struct ist func,
82 const void *a1, const void *a2, const void *a3, const void *a4);
83
84static const struct trace_event quic_trace_events[] = {
85 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
86 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
87 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
88 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
89 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
90 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
91 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050092 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
94 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
95 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
96 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
97 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
98 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
99 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
100 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
101 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
102 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
103 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
104 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
105 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
106 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
107 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
108 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
109 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
110 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
111 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100112 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
113 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
114 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
115 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
116 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
117 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100118 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200119 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
120 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100121 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
122 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100123 { /* end */ }
124};
125
126static const struct name_desc quic_trace_lockon_args[4] = {
127 /* arg1 */ { /* already used by the connection */ },
128 /* arg2 */ { .name="quic", .desc="QUIC transport" },
129 /* arg3 */ { },
130 /* arg4 */ { }
131};
132
133static const struct name_desc quic_trace_decoding[] = {
134#define QUIC_VERB_CLEAN 1
135 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
136 { /* end */ }
137};
138
139
140struct trace_source trace_quic = {
141 .name = IST("quic"),
142 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100143 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144 .default_cb = quic_trace,
145 .known_events = quic_trace_events,
146 .lockon_args = quic_trace_lockon_args,
147 .decoding = quic_trace_decoding,
148 .report_events = ~0, /* report everything by default */
149};
150
151#define TRACE_SOURCE &trace_quic
152INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
153
154static BIO_METHOD *ha_quic_meth;
155
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200156DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200157DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100158DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200160 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_POOL(pool_head_quic_connection_id,
163 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100164DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165DECLARE_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 +0100166DECLARE_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 +0100167DECLARE_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 +0100168DECLARE_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 +0100169DECLARE_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 +0200170DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100171DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100172
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200173static 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 +0200174 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100175 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +0100176 int padding, int ack, int probe, int cc, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100177
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100178/* Only for debug purpose */
179struct enc_debug_info {
180 unsigned char *payload;
181 size_t payload_len;
182 unsigned char *aad;
183 size_t aad_len;
184 uint64_t pn;
185};
186
187/* Initializes a enc_debug_info struct (only for debug purpose) */
188static inline void enc_debug_info_init(struct enc_debug_info *edi,
189 unsigned char *payload, size_t payload_len,
190 unsigned char *aad, size_t aad_len, uint64_t pn)
191{
192 edi->payload = payload;
193 edi->payload_len = payload_len;
194 edi->aad = aad;
195 edi->aad_len = aad_len;
196 edi->pn = pn;
197}
198
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100199/* Trace callback for QUIC.
200 * These traces always expect that arg1, if non-null, is of type connection.
201 */
202static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
203 const struct ist where, const struct ist func,
204 const void *a1, const void *a2, const void *a3, const void *a4)
205{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100206 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100208 if (qc) {
209 const struct quic_tls_secrets *secs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100210
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100211 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100212 if ((mask & QUIC_EV_CONN_INIT) && qc) {
213 chunk_appendf(&trace_buf, "\n odcid");
214 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100215 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100217 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100218 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100219 }
220
221 if (mask & QUIC_EV_CONN_ADDDATA) {
222 const enum ssl_encryption_level_t *level = a2;
223 const size_t *len = a3;
224
225 if (level) {
226 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
227
228 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
229 }
230 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100231 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100232 }
233 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
234 /* Initial read & write secrets. */
235 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
236 const unsigned char *rx_sec = a2;
237 const unsigned char *tx_sec = a3;
238
239 secs = &qc->els[level].tls_ctx.rx;
240 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
241 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
242 if (rx_sec)
243 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
244 quic_tls_keys_hexdump(&trace_buf, secs);
245 }
246 secs = &qc->els[level].tls_ctx.tx;
247 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
248 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
249 if (tx_sec)
250 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
251 quic_tls_keys_hexdump(&trace_buf, secs);
252 }
253 }
254 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
255 const enum ssl_encryption_level_t *level = a2;
256 const unsigned char *secret = a3;
257 const size_t *secret_len = a4;
258
259 if (level) {
260 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
261
262 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
263 if (secret && secret_len)
264 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
265 secs = &qc->els[lvl].tls_ctx.rx;
266 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
267 quic_tls_keys_hexdump(&trace_buf, secs);
268 }
269 }
270
271 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
272 const enum ssl_encryption_level_t *level = a2;
273 const unsigned char *secret = a3;
274 const size_t *secret_len = a4;
275
276 if (level) {
277 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
278
279 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
280 if (secret && secret_len)
281 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
282 secs = &qc->els[lvl].tls_ctx.tx;
283 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
284 quic_tls_keys_hexdump(&trace_buf, secs);
285 }
286
287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100289 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 const struct quic_tx_packet *pkt = a2;
291 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100292 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100293
294 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100295 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100296 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100297 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100298 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100299 (unsigned long long)qc->path->cwnd,
300 (unsigned long long)qc->path->prep_in_flight,
301 (unsigned long long)qc->path->in_flight,
302 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100303 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304 }
305 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200306 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100307 if (pkt->pn_node.key != (uint64_t)-1)
308 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100309 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100310 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100311 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
312 (unsigned long long)qc->rx.bytes,
313 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100314 }
315
316 if (room) {
317 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
318 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
319 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100320 }
321 }
322
323 if (mask & QUIC_EV_CONN_HDSHK) {
324 const enum quic_handshake_state *state = a2;
325 const int *err = a3;
326
327 if (state)
328 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
329 if (err)
330 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
331 }
332
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100333 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100334 const struct quic_rx_packet *pkt = a2;
335 const unsigned long *pktlen = a3;
336 const SSL *ssl = a4;
337
338 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100339 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100340 pkt, quic_packet_type_enc_level_char(pkt->type));
341 if (pkt->pnl)
342 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
343 (unsigned long long)pkt->pn);
344 if (pkt->token_len)
345 chunk_appendf(&trace_buf, " toklen=%llu",
346 (unsigned long long)pkt->token_len);
347 if (pkt->aad_len)
348 chunk_appendf(&trace_buf, " aadlen=%llu",
349 (unsigned long long)pkt->aad_len);
350 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
351 pkt->flags, (unsigned long long)pkt->len);
352 }
353 if (pktlen)
354 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
355 if (ssl) {
356 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
357 chunk_appendf(&trace_buf, " el=%c",
358 quic_enc_level_char(ssl_to_quic_enc_level(level)));
359 }
360 }
361
362 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
363 const struct quic_rx_packet *pkt = a2;
364 const struct quic_rx_crypto_frm *cf = a3;
365 const SSL *ssl = a4;
366
367 if (pkt)
368 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
369 quic_packet_type_enc_level_char(pkt->type),
370 (unsigned long long)pkt->pn);
371 if (cf)
372 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
373 (unsigned long long)cf->offset_node.key,
374 (unsigned long long)cf->len);
375 if (ssl) {
376 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200377 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100378 quic_enc_level_char(ssl_to_quic_enc_level(level)));
379 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100380
381 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100382 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100383 }
384
385 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
386 const struct quic_frame *frm = a2;
387
388 if (frm)
389 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
390 }
391
392 if (mask & QUIC_EV_CONN_PHPKTS) {
393 const struct quic_enc_level *qel = a2;
394
395 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100396 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100398 " 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 +0100399 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200400 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100401 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100402 (unsigned long long)qc->path->cwnd,
403 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100405 (unsigned long long)pktns->tx.in_flight,
406 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100407 }
408 }
409
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100410 if (mask & QUIC_EV_CONN_ENCPKT) {
411 const struct enc_debug_info *edi = a2;
412
413 if (edi)
414 chunk_appendf(&trace_buf,
415 " payload=@%p payload_len=%llu"
416 " aad=@%p aad_len=%llu pn=%llu",
417 edi->payload, (unsigned long long)edi->payload_len,
418 edi->aad, (unsigned long long)edi->aad_len,
419 (unsigned long long)edi->pn);
420 }
421
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100422 if (mask & QUIC_EV_CONN_RMHP) {
423 const struct quic_rx_packet *pkt = a2;
424
425 if (pkt) {
426 const int *ret = a3;
427
428 chunk_appendf(&trace_buf, " pkt@%p", pkt);
429 if (ret && *ret)
430 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
431 pkt->pnl, (unsigned long long)pkt->pn);
432 }
433 }
434
435 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200436 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100437 const unsigned long *val1 = a3;
438 const unsigned long *val2 = a4;
439
440 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100441 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100442 if (val1)
443 chunk_appendf(&trace_buf, " %lu", *val1);
444 if (val2)
445 chunk_appendf(&trace_buf, "..%lu", *val2);
446 }
447
448 if (mask & QUIC_EV_CONN_RTTUPDT) {
449 const unsigned int *rtt_sample = a2;
450 const unsigned int *ack_delay = a3;
451 const struct quic_loss *ql = a4;
452
453 if (rtt_sample)
454 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
455 if (ack_delay)
456 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
457 if (ql)
458 chunk_appendf(&trace_buf,
459 " srtt=%ums rttvar=%ums min_rtt=%ums",
460 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
461 }
462 if (mask & QUIC_EV_CONN_CC) {
463 const struct quic_cc_event *ev = a2;
464 const struct quic_cc *cc = a3;
465
466 if (a2)
467 quic_cc_event_trace(&trace_buf, ev);
468 if (a3)
469 quic_cc_state_trace(&trace_buf, cc);
470 }
471
472 if (mask & QUIC_EV_CONN_PKTLOSS) {
473 const struct quic_pktns *pktns = a2;
474 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100475
476 if (pktns) {
477 chunk_appendf(&trace_buf, " pktns=%s",
478 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
479 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
480 if (pktns->tx.loss_time)
481 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100482 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100483 }
484 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
485 struct quic_tx_packet *pkt;
486
487 chunk_appendf(&trace_buf, " lost_pkts:");
488 list_for_each_entry(pkt, lost_pkts, list)
489 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
490 }
491 }
492
493 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494 const struct quic_pktns *pktns = a2;
495 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100496 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100498 if (ifae_pkts)
499 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
500 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100501 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100502 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100504 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
505 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100506 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
507 if (pktns->tx.in_flight)
508 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509 if (pktns->tx.loss_time)
510 chunk_appendf(&trace_buf, " loss_time=%dms",
511 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
512 }
513 if (mask & QUIC_EV_CONN_SPTO) {
514 if (pktns->tx.time_of_last_eliciting)
515 chunk_appendf(&trace_buf, " tole=%dms",
516 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
517 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100518 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100519 }
520 }
521
522 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
523 chunk_appendf(&trace_buf,
524 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
525 }
526 }
527
528 if (mask & QUIC_EV_CONN_SPPKTS) {
529 const struct quic_tx_packet *pkt = a2;
530
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100531 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
532 (unsigned long long)qc->path->cwnd,
533 (unsigned long long)qc->path->prep_in_flight,
534 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100535 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100536 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100537 (unsigned long)pkt->pn_node.key,
538 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
539 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100540 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100541 }
542 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100543
544 if (mask & QUIC_EV_CONN_SSLALERT) {
545 const uint8_t *alert = a2;
546 const enum ssl_encryption_level_t *level = a3;
547
548 if (alert)
549 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
550 if (level)
551 chunk_appendf(&trace_buf, " el=%c",
552 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
553 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100554
555 if (mask & QUIC_EV_CONN_BCFRMS) {
556 const size_t *sz1 = a2;
557 const size_t *sz2 = a3;
558 const size_t *sz3 = a4;
559
560 if (sz1)
561 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
562 if (sz2)
563 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
564 if (sz3)
565 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
566 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100567
568 if (mask & QUIC_EV_CONN_PSTRM) {
569 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100570
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100571 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100572 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100573 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100574 }
575 if (mask & QUIC_EV_CONN_LPKT) {
576 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200577 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100579 if (pkt) {
580 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
581 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
582 if (pkt->pn_node.key != (uint64_t)-1)
583 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
584 }
585
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200586 if (len)
587 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100588 }
589
590}
591
592/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100593static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100594{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200595 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100597 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 return 1;
599
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200600 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
601 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
602 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
603 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200604 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100605 return 1;
606
607 return 0;
608}
609
610/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
611 * both loss detection and PTO and schedule the task assiated to this timer if needed.
612 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100613static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100614{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615 struct quic_pktns *pktns;
616 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200617 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100618
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100619 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100620 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100621
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622 pktns = quic_loss_pktns(qc);
623 if (tick_isset(pktns->tx.loss_time)) {
624 qc->timer = pktns->tx.loss_time;
625 goto out;
626 }
627
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100628 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100629 * cancelled for a server which reached the anti-amplification limit.
630 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100631 if (!quic_peer_validated_addr(qc) &&
632 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100633 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100634 qc->timer = TICK_ETERNITY;
635 goto out;
636 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100638 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100639 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100640 /* Timer cancellation. */
641 qc->timer = TICK_ETERNITY;
642 goto out;
643 }
644
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200645 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
646 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100647 if (tick_isset(pto))
648 qc->timer = pto;
649 out:
Amaury Denoyelle0a29e132021-12-23 15:06:56 +0100650 if (qc->timer_task && qc->timer != TICK_ETERNITY)
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200651 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100652 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100653}
654
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100655/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
656 * connection.
657 * Return 1 if succeeded, 0 if not.
658 */
659static int quic_tls_key_update(struct quic_conn *qc)
660{
661 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
662 struct quic_tls_secrets *rx, *tx;
663 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
664 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
665
666 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
667 rx = &tls_ctx->rx;
668 tx = &tls_ctx->tx;
669 nxt_rx = &qc->ku.nxt_rx;
670 nxt_tx = &qc->ku.nxt_tx;
671
672 /* Prepare new RX secrets */
673 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
674 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100675 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100676 return 0;
677 }
678
679 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
680 nxt_rx->key, nxt_rx->keylen,
681 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
682 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100683 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100684 return 0;
685 }
686
687 /* Prepare new TX secrets */
688 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
689 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100690 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100691 return 0;
692 }
693
694 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
695 nxt_tx->key, nxt_tx->keylen,
696 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
697 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100698 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100699 return 0;
700 }
701
702 return 1;
703}
704
705/* Rotate the Key Update information for <qc> QUIC connection.
706 * Must be used after having updated them.
707 * Always succeeds.
708 */
709static void quic_tls_rotate_keys(struct quic_conn *qc)
710{
711 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
712 unsigned char *curr_secret, *curr_iv, *curr_key;
713
714 /* Rotate the RX secrets */
715 curr_secret = tls_ctx->rx.secret;
716 curr_iv = tls_ctx->rx.iv;
717 curr_key = tls_ctx->rx.key;
718
719 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
720 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
721 tls_ctx->rx.key = qc->ku.nxt_rx.key;
722
723 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
724 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
725 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
726
727 qc->ku.prv_rx.secret = curr_secret;
728 qc->ku.prv_rx.iv = curr_iv;
729 qc->ku.prv_rx.key = curr_key;
730 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
731
732 /* Update the TX secrets */
733 curr_secret = tls_ctx->tx.secret;
734 curr_iv = tls_ctx->tx.iv;
735 curr_key = tls_ctx->tx.key;
736
737 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
738 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
739 tls_ctx->tx.key = qc->ku.nxt_tx.key;
740
741 qc->ku.nxt_tx.secret = curr_secret;
742 qc->ku.nxt_tx.iv = curr_iv;
743 qc->ku.nxt_tx.key = curr_key;
744}
745
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100746#ifndef OPENSSL_IS_BORINGSSL
747int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
748 const uint8_t *read_secret,
749 const uint8_t *write_secret, size_t secret_len)
750{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100751 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
752 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100753 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100754 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100755
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100756 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100757 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100758 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
759 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100760 goto out;
761 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100762
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100763 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100764 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100765 goto err;
766 }
767
768 rx = &tls_ctx->rx;
769 tx = &tls_ctx->tx;
770
771 rx->aead = tx->aead = tls_aead(cipher);
772 rx->md = tx->md = tls_md(cipher);
773 rx->hp = tx->hp = tls_hp(cipher);
774
775 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
776 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100777 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100778 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100779 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100780 }
781
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100782 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100783 /* Enqueue this connection asap if we could derive O-RTT secrets as
784 * listener. Note that a listener derives only RX secrets for this
785 * level.
786 */
787 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
788 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100789
790 if (!write_secret)
791 goto tp;
792
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100793 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
794 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100795 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100796 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100797 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100798 }
799
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100800 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100801 tp:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100802 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100803 const unsigned char *buf;
804 size_t buflen;
805
806 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
807 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100808 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100809
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100810 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100811 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100812 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100813
814 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100815 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
816 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
817 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
818
819 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
820 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100821 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100822 goto err;
823 }
824
825 memcpy(rx->secret, read_secret, secret_len);
826 rx->secretlen = secret_len;
827 memcpy(tx->secret, write_secret, secret_len);
828 tx->secretlen = secret_len;
829 /* Initialize all the secret keys lengths */
830 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
831 /* Prepare the next key update */
832 if (!quic_tls_key_update(qc))
833 goto err;
834 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100835 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100836 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100837 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100838
839 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100840 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100841 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100842}
843#else
844/* ->set_read_secret callback to derive the RX secrets at <level> encryption
845 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500846 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100847 */
848int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
849 const SSL_CIPHER *cipher,
850 const uint8_t *secret, size_t secret_len)
851{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100852 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100853 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100854 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100855
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100856 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
857 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
858 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100859 goto out;
860 }
861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 tls_ctx->rx.aead = tls_aead(cipher);
863 tls_ctx->rx.md = tls_md(cipher);
864 tls_ctx->rx.hp = tls_hp(cipher);
865
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100866 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
867 goto err;
868
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100869 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 +0100870 tls_ctx->rx.key, tls_ctx->rx.keylen,
871 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100872 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
873 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100874 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100875 goto err;
876 }
877
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100878 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100879 const unsigned char *buf;
880 size_t buflen;
881
882 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
883 if (!buflen)
884 goto err;
885
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100886 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100887 goto err;
888 }
889
890 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100891 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100892 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100893
894 return 1;
895
896 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100897 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100898 return 0;
899}
900
901/* ->set_write_secret callback to derive the TX secrets at <level>
902 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500903 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100904 */
905int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
906 const SSL_CIPHER *cipher,
907 const uint8_t *secret, size_t secret_len)
908{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100909 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
910 struct quic_tls_ctx *tls_ctx = &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100911
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100912 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
913 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
914 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100915 goto out;
916 }
917
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100918 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
919 goto err;
920
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100921 tls_ctx->tx.aead = tls_aead(cipher);
922 tls_ctx->tx.md = tls_md(cipher);
923 tls_ctx->tx.hp = tls_hp(cipher);
924
925 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 +0100926 tls_ctx->tx.key, tls_ctx->tx.keylen,
927 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100928 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
929 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100930 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100931 goto err;
932 }
933
934 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100935 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100936 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100937 return 1;
938
939 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100940 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100941 return 0;
942}
943#endif
944
945/* This function copies the CRYPTO data provided by the TLS stack found at <data>
946 * with <len> as size in CRYPTO buffers dedicated to store the information about
947 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
948 * It fails only if it could not managed to allocate enough CRYPTO buffers to
949 * store all the data.
950 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
951 */
952static int quic_crypto_data_cpy(struct quic_enc_level *qel,
953 const unsigned char *data, size_t len)
954{
955 struct quic_crypto_buf **qcb;
956 /* The remaining byte to store in CRYPTO buffers. */
957 size_t cf_offset, cf_len, *nb_buf;
958 unsigned char *pos;
959
960 nb_buf = &qel->tx.crypto.nb_buf;
961 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
962 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
963 cf_len = len;
964
965 while (len) {
966 size_t to_copy, room;
967
968 pos = (*qcb)->data + (*qcb)->sz;
969 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
970 to_copy = len > room ? room : len;
971 if (to_copy) {
972 memcpy(pos, data, to_copy);
973 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
974 qel->tx.crypto.sz += to_copy;
975 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100976 len -= to_copy;
977 data += to_copy;
978 }
979 else {
980 struct quic_crypto_buf **tmp;
981
982 tmp = realloc(qel->tx.crypto.bufs,
983 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
984 if (tmp) {
985 qel->tx.crypto.bufs = tmp;
986 qcb = &qel->tx.crypto.bufs[*nb_buf];
987 *qcb = pool_alloc(pool_head_quic_crypto_buf);
988 if (!*qcb)
989 return 0;
990
991 (*qcb)->sz = 0;
992 ++*nb_buf;
993 }
994 else {
995 break;
996 }
997 }
998 }
999
1000 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1001 * have been buffered.
1002 */
1003 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001004 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001005 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001006
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001007 /* There is at most one CRYPTO frame in this packet number
1008 * space. Let's look for it.
1009 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001010 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001011 if (frm->type != QUIC_FT_CRYPTO)
1012 continue;
1013
1014 /* Found */
1015 found = frm;
1016 break;
1017 }
1018
1019 if (found) {
1020 found->crypto.len += cf_len;
1021 }
1022 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001023 frm = pool_alloc(pool_head_quic_frame);
1024 if (!frm)
1025 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001026
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001027 frm->type = QUIC_FT_CRYPTO;
1028 frm->crypto.offset = cf_offset;
1029 frm->crypto.len = cf_len;
1030 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001031 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001032 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001033 }
1034
1035 return len == 0;
1036}
1037
1038
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001039/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1040void quic_set_tls_alert(struct quic_conn *qc, int alert)
1041{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001042 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001043 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001044 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001045}
1046
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001047/* Set the application for <qc> QUIC connection.
1048 * Return 1 if succeeded, 0 if not.
1049 */
1050int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1051{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001052 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1053 qc->app_ops = &h3_ops;
1054 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1055 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001056 else
1057 return 0;
1058
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001059 return 1;
1060}
1061
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001062/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1063 * wants to provide the QUIC layer with CRYPTO data.
1064 * Returns 1 if succeeded, 0 if not.
1065 */
1066int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1067 const uint8_t *data, size_t len)
1068{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001069 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001070 enum quic_tls_enc_level tel;
1071 struct quic_enc_level *qel;
1072
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001073 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1074 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1075 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1076 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001077 goto out;
1078 }
1079
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001080 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001081 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001082 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001083 goto err;
1084 }
1085
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001086 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001087 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001088 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089 goto err;
1090 }
1091
1092 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001093 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001094
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001095 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001096 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001097 return 1;
1098
1099 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001100 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 return 0;
1102}
1103
1104int ha_quic_flush_flight(SSL *ssl)
1105{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001106 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001108 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1109 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001110
1111 return 1;
1112}
1113
1114int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1115{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001116 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001118 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1119 quic_set_tls_alert(qc, alert);
1120 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001121 return 1;
1122}
1123
1124/* QUIC TLS methods */
1125static SSL_QUIC_METHOD ha_quic_method = {
1126#ifdef OPENSSL_IS_BORINGSSL
1127 .set_read_secret = ha_set_rsec,
1128 .set_write_secret = ha_set_wsec,
1129#else
1130 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1131#endif
1132 .add_handshake_data = ha_quic_add_handshake_data,
1133 .flush_flight = ha_quic_flush_flight,
1134 .send_alert = ha_quic_send_alert,
1135};
1136
1137/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1138 * Returns an error count.
1139 */
1140int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1141{
1142 struct proxy *curproxy = bind_conf->frontend;
1143 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1144 int cfgerr = 0;
1145
1146#if 0
1147 /* XXX Did not manage to use this. */
1148 const char *ciphers =
1149 "TLS_AES_128_GCM_SHA256:"
1150 "TLS_AES_256_GCM_SHA384:"
1151 "TLS_CHACHA20_POLY1305_SHA256:"
1152 "TLS_AES_128_CCM_SHA256";
1153#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001154 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155 long options =
1156 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1157 SSL_OP_SINGLE_ECDH_USE |
1158 SSL_OP_CIPHER_SERVER_PREFERENCE;
1159 SSL_CTX *ctx;
1160
1161 ctx = SSL_CTX_new(TLS_server_method());
1162 bind_conf->initial_ctx = ctx;
1163
1164 SSL_CTX_set_options(ctx, options);
1165#if 0
1166 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1167 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1168 "for bind '%s' at [%s:%d].\n",
1169 curproxy->id, ciphers,
1170 bind_conf->arg, bind_conf->file, bind_conf->line);
1171 cfgerr++;
1172 }
1173#endif
1174
1175 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1176 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1177 "for bind '%s' at [%s:%d].\n",
1178 curproxy->id, groups,
1179 bind_conf->arg, bind_conf->file, bind_conf->line);
1180 cfgerr++;
1181 }
1182
1183 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1184 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1185 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1186 SSL_CTX_set_default_verify_paths(ctx);
1187
1188#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1189#ifdef OPENSSL_IS_BORINGSSL
1190 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1191 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1192#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1193 if (bind_conf->ssl_conf.early_data) {
1194 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001195 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001196 }
1197 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1198 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1199#else
1200 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1201#endif
1202 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1203#endif
1204 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1205
1206 return cfgerr;
1207}
1208
1209/* Decode an expected packet number from <truncated_on> its truncated value,
1210 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1211 * the number of bits used to encode this packet number (its length in bytes * 8).
1212 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1213 */
1214static uint64_t decode_packet_number(uint64_t largest_pn,
1215 uint32_t truncated_pn, unsigned int pn_nbits)
1216{
1217 uint64_t expected_pn = largest_pn + 1;
1218 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1219 uint64_t pn_hwin = pn_win / 2;
1220 uint64_t pn_mask = pn_win - 1;
1221 uint64_t candidate_pn;
1222
1223
1224 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1225 /* Note that <pn_win> > <pn_hwin>. */
1226 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1227 candidate_pn + pn_hwin <= expected_pn)
1228 return candidate_pn + pn_win;
1229
1230 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1231 return candidate_pn - pn_win;
1232
1233 return candidate_pn;
1234}
1235
1236/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1237 * cryptographic context.
1238 * <largest_pn> is the largest received packet number and <pn> the address of
1239 * the packet number field for this packet with <byte0> address of its first byte.
1240 * <end> points to one byte past the end of this packet.
1241 * Returns 1 if succeeded, 0 if not.
1242 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001243static int qc_do_rm_hp(struct quic_conn *qc,
1244 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001245 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001246 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001247{
1248 int ret, outlen, i, pnlen;
1249 uint64_t packet_number;
1250 uint32_t truncated_pn = 0;
1251 unsigned char mask[5] = {0};
1252 unsigned char *sample;
1253 EVP_CIPHER_CTX *cctx;
1254 unsigned char *hp_key;
1255
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001256 /* Check there is enough data in this packet. */
1257 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001258 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001259 return 0;
1260 }
1261
1262 cctx = EVP_CIPHER_CTX_new();
1263 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001264 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 return 0;
1266 }
1267
1268 ret = 0;
1269 sample = pn + QUIC_PACKET_PN_MAXLEN;
1270
1271 hp_key = tls_ctx->rx.hp_key;
1272 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1273 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1274 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001275 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001276 goto out;
1277 }
1278
1279 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1280 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1281 for (i = 0; i < pnlen; i++) {
1282 pn[i] ^= mask[i + 1];
1283 truncated_pn = (truncated_pn << 8) | pn[i];
1284 }
1285
1286 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1287 /* Store remaining information for this unprotected header */
1288 pkt->pn = packet_number;
1289 pkt->pnl = pnlen;
1290
1291 ret = 1;
1292
1293 out:
1294 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295
1296 return ret;
1297}
1298
1299/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1300 * address, with <payload_len> as payload length, <aad> as address of
1301 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1302 * context.
1303 * Returns 1 if succeeded, 0 if not.
1304 */
1305static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1306 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001307 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308{
1309 unsigned char iv[12];
1310 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001311 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001312 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313
1314 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001315 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001316 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001317 }
1318
1319 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1320 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001321 TRACE_DEVEL("QUIC packet 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 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001326
1327 err:
1328 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001329 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001330 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001331}
1332
1333/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1334 * Returns 1 if succeeded, 0 if not.
1335 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001336static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001337{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001338 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001339 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001340 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001341 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001342 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1343 unsigned char *rx_key = tls_ctx->rx.key;
1344
1345 kp_changed = 0;
1346 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1347 /* The two tested bits are not at the same position,
1348 * this is why they are first both inversed.
1349 */
1350 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1351 if (pkt->pn < tls_ctx->rx.pn) {
1352 /* The lowest packet number of a previous key phase
1353 * cannot be null if it really stores previous key phase
1354 * secrets.
1355 */
1356 if (!pkt->qc->ku.prv_rx.pn)
1357 return 0;
1358
1359 rx_iv = pkt->qc->ku.prv_rx.iv;
1360 rx_key = pkt->qc->ku.prv_rx.key;
1361 }
1362 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1363 /* Next key phase */
1364 kp_changed = 1;
1365 rx_iv = pkt->qc->ku.nxt_rx.iv;
1366 rx_key = pkt->qc->ku.nxt_rx.key;
1367 }
1368 }
1369 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001370
1371 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1372 return 0;
1373
1374 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1375 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001376 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001377 if (!ret)
1378 return 0;
1379
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001380 /* Update the keys only if the packet decryption succeeded. */
1381 if (kp_changed) {
1382 quic_tls_rotate_keys(pkt->qc);
1383 /* Toggle the Key Phase bit */
1384 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1385 /* Store the lowest packet number received for the current key phase */
1386 tls_ctx->rx.pn = pkt->pn;
1387 /* Prepare the next key update */
1388 if (!quic_tls_key_update(pkt->qc))
1389 return 0;
1390 }
1391
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001392 /* Update the packet length (required to parse the frames). */
1393 pkt->len = pkt->aad_len + ret;
1394
1395 return 1;
1396}
1397
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001398/* Remove from <qcs> stream the acknowledged frames.
1399 * Never fails.
1400 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001401static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001402{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001403 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001404 struct eb64_node *frm_node;
1405
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001406 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001407 frm_node = eb64_first(&qcs->tx.acked_frms);
1408 while (frm_node) {
1409 struct quic_stream *strm;
1410
1411 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1412 if (strm->offset.key != qcs->tx.ack_offset)
1413 break;
1414
1415 b_del(strm->buf, strm->len);
1416 qcs->tx.ack_offset += strm->len;
1417 frm_node = eb64_next(frm_node);
1418 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001419 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001420 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001421
1422 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001423}
1424
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001425/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001426static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1427 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001428{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001429 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001430
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001431 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001432 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001433 switch (frm->type) {
1434 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1435 {
1436 struct qcs *qcs = frm->stream.qcs;
1437 struct quic_stream *strm = &frm->stream;
1438
1439 if (qcs->tx.ack_offset == strm->offset.key) {
1440 b_del(strm->buf, strm->len);
1441 qcs->tx.ack_offset += strm->len;
1442 LIST_DELETE(&frm->list);
1443 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001444 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001445 }
1446 else {
1447 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1448 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001449 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001450 }
1451 break;
1452 default:
1453 LIST_DELETE(&frm->list);
1454 pool_free(pool_head_quic_frame, frm);
1455 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001456
1457 if (stream_acked) {
1458 struct qcc *qcc = qc->qcc;
1459
1460 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1461 tasklet_wakeup(qcc->subs->tasklet);
1462 qcc->subs->events &= ~SUB_RETRY_SEND;
1463 if (!qcc->subs->events)
1464 qcc->subs = NULL;
1465 }
1466 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001467}
1468
1469/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1470 * deallocating them, and their TX frames.
1471 * Returns the last node reached to be used for the next range.
1472 * May be NULL if <largest> node could not be found.
1473 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001474static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1475 struct eb_root *pkts,
1476 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001477 struct list *newly_acked_pkts,
1478 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001479 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001480{
1481 struct eb64_node *node;
1482 struct quic_tx_packet *pkt;
1483
1484 if (largest_node)
1485 node = largest_node;
1486 else {
1487 node = eb64_lookup(pkts, largest);
1488 while (!node && largest > smallest) {
1489 node = eb64_lookup(pkts, --largest);
1490 }
1491 }
1492
1493 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001494 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001495
1496 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1497 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001498 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001499 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001500 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001501 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502 node = eb64_prev(node);
1503 eb64_delete(&pkt->pn_node);
1504 }
1505
1506 return node;
1507}
1508
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001509/* Remove all frames from <pkt_frm_list> and reinsert them in the
1510 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001511 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001512static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1513 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001514 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001515{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001516 struct quic_frame *frm, *frmbak;
1517 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001518
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001519 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1520 LIST_DELETE(&frm->list);
1521 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001522 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001523 }
1524
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001525 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001526}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001527
1528/* Free the TX packets of <pkts> list */
1529static inline void free_quic_tx_pkts(struct list *pkts)
1530{
1531 struct quic_tx_packet *pkt, *tmp;
1532
1533 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001534 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001535 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001536 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001537 }
1538}
1539
1540/* Send a packet loss event nofification to the congestion controller
1541 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1542 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1543 * at <now_us> current time.
1544 * Always succeeds.
1545 */
1546static inline void qc_cc_loss_event(struct quic_conn *qc,
1547 unsigned int lost_bytes,
1548 unsigned int newest_time_sent,
1549 unsigned int period,
1550 unsigned int now_us)
1551{
1552 struct quic_cc_event ev = {
1553 .type = QUIC_CC_EVT_LOSS,
1554 .loss.now_ms = now_ms,
1555 .loss.max_ack_delay = qc->max_ack_delay,
1556 .loss.lost_bytes = lost_bytes,
1557 .loss.newest_time_sent = newest_time_sent,
1558 .loss.period = period,
1559 };
1560
1561 quic_cc_event(&qc->path->cc, &ev);
1562}
1563
1564/* Send a packet ack event nofication for each newly acked packet of
1565 * <newly_acked_pkts> list and free them.
1566 * Always succeeds.
1567 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001568static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 struct list *newly_acked_pkts)
1570{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001571 struct quic_tx_packet *pkt, *tmp;
1572 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1573
1574 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1575 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001576 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001577 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001578 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001579 ev.ack.acked = pkt->in_flight_len;
1580 ev.ack.time_sent = pkt->time_sent;
1581 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001582 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001583 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001584 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585 }
1586
1587}
1588
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001589/* Release all the frames attached to <pktns> packet number space */
1590static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1591{
1592 struct quic_frame *frm, *frmbak;
1593
1594 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1595 LIST_DELETE(&frm->list);
1596 pool_free(pool_head_quic_frame, frm);
1597 }
1598}
1599
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600/* Handle <pkts> list of lost packets detected at <now_us> handling
1601 * their TX frames.
1602 * Send a packet loss event to the congestion controller if
1603 * in flight packet have been lost.
1604 * Also frees the packet in <pkts> list.
1605 * Never fails.
1606 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001607static inline void qc_release_lost_pkts(struct quic_conn *qc,
1608 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001609 struct list *pkts,
1610 uint64_t now_us)
1611{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001612 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001613 uint64_t lost_bytes;
1614
1615 lost_bytes = 0;
1616 oldest_lost = newest_lost = NULL;
1617 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001618 struct list tmp = LIST_HEAD_INIT(tmp);
1619
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620 lost_bytes += pkt->in_flight_len;
1621 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001622 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001623 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001624 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001625 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001626 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001627 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001628 if (!oldest_lost) {
1629 oldest_lost = newest_lost = pkt;
1630 }
1631 else {
1632 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001633 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 newest_lost = pkt;
1635 }
1636 }
1637
1638 if (lost_bytes) {
1639 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001640 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001641 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001642 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001643 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001644 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001645 }
1646}
1647
1648/* Look for packet loss from sent packets for <qel> encryption level of a
1649 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1650 * their tree if deemed as lost or set the <loss_time> value the packet number
1651 * space if any not deemed lost.
1652 * Should be called after having received an ACK frame with newly acknowledged
1653 * packets or when the the loss detection timer has expired.
1654 * Always succeeds.
1655 */
1656static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1657 struct quic_conn *qc,
1658 struct list *lost_pkts)
1659{
1660 struct eb_root *pkts;
1661 struct eb64_node *node;
1662 struct quic_loss *ql;
1663 unsigned int loss_delay;
1664
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001665 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001666 pkts = &pktns->tx.pkts;
1667 pktns->tx.loss_time = TICK_ETERNITY;
1668 if (eb_is_empty(pkts))
1669 goto out;
1670
1671 ql = &qc->path->loss;
1672 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1673 loss_delay += loss_delay >> 3;
1674 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1675
1676 node = eb64_first(pkts);
1677 while (node) {
1678 struct quic_tx_packet *pkt;
1679 int64_t largest_acked_pn;
1680 unsigned int loss_time_limit, time_sent;
1681
1682 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001683 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001684 node = eb64_next(node);
1685 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1686 break;
1687
1688 time_sent = pkt->time_sent;
1689 loss_time_limit = tick_add(time_sent, loss_delay);
1690 if (tick_is_le(time_sent, now_ms) ||
1691 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1692 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001693 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001694 }
1695 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001696 if (tick_isset(pktns->tx.loss_time))
1697 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1698 else
1699 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001700 }
1701 }
1702
1703 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001704 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001705}
1706
1707/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1708 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001709 * 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 +01001710 * acked ack-eliciting packet.
1711 * Return 1, if succeeded, 0 if not.
1712 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001713static inline int qc_parse_ack_frm(struct quic_conn *qc,
1714 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001715 struct quic_enc_level *qel,
1716 unsigned int *rtt_sample,
1717 const unsigned char **pos, const unsigned char *end)
1718{
1719 struct quic_ack *ack = &frm->ack;
1720 uint64_t smallest, largest;
1721 struct eb_root *pkts;
1722 struct eb64_node *largest_node;
1723 unsigned int time_sent, pkt_flags;
1724 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1725 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1726
1727 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1728 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001729 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001730 goto err;
1731 }
1732
1733 if (ack->first_ack_range > ack->largest_ack) {
1734 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001735 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001736 goto err;
1737 }
1738
1739 largest = ack->largest_ack;
1740 smallest = largest - ack->first_ack_range;
1741 pkts = &qel->pktns->tx.pkts;
1742 pkt_flags = 0;
1743 largest_node = NULL;
1744 time_sent = 0;
1745
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001746 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 +01001747 largest_node = eb64_lookup(pkts, largest);
1748 if (!largest_node) {
1749 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001750 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001751 }
1752 else {
1753 time_sent = eb64_entry(&largest_node->node,
1754 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001755 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001756 }
1757
1758 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001759 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001760 do {
1761 uint64_t gap, ack_range;
1762
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001763 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1764 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001765 if (!ack->ack_range_num--)
1766 break;
1767
1768 if (!quic_dec_int(&gap, pos, end))
1769 goto err;
1770
1771 if (smallest < gap + 2) {
1772 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001773 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001774 goto err;
1775 }
1776
1777 largest = smallest - gap - 2;
1778 if (!quic_dec_int(&ack_range, pos, end))
1779 goto err;
1780
1781 if (largest < ack_range) {
1782 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001783 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001784 goto err;
1785 }
1786
1787 /* Do not use this node anymore. */
1788 largest_node = NULL;
1789 /* Next range */
1790 smallest = largest - ack_range;
1791
1792 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001793 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001794 } while (1);
1795
1796 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001797 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001798
1799 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1800 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001801 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001802 }
1803
1804 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1805 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001806 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001807 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001808 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001809 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001810 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1811 if (quic_peer_validated_addr(qc))
1812 qc->path->loss.pto_count = 0;
1813 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001814 }
1815
1816
1817 return 1;
1818
1819 err:
1820 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001821 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001822 return 0;
1823}
1824
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001825/* This function gives the detail of the SSL error. It is used only
1826 * if the debug mode and the verbose mode are activated. It dump all
1827 * the SSL error until the stack was empty.
1828 */
1829static forceinline void qc_ssl_dump_errors(struct connection *conn)
1830{
1831 if (unlikely(global.mode & MODE_DEBUG)) {
1832 while (1) {
1833 unsigned long ret;
1834
1835 ret = ERR_get_error();
1836 if (!ret)
1837 return;
1838
1839 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1840 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1841 }
1842 }
1843}
1844
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001845int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1846 const char **str, int *len);
1847
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001848/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1849 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001850 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851 * Return 1 if succeeded, 0 if not.
1852 */
1853static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001854 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001855 const unsigned char *data, size_t len,
1856 struct quic_rx_packet *pkt,
1857 struct quic_rx_crypto_frm *cf)
1858{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001859 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001860 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001863 qc = ctx->qc;
1864
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001865 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1866
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001867 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1868 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001869 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001870 goto err;
1871 }
1872
1873 el->rx.crypto.offset += len;
1874 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001875 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001876
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001877 state = HA_ATOMIC_LOAD(&qc->state);
1878 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001879 ssl_err = SSL_do_handshake(ctx->ssl);
1880 if (ssl_err != 1) {
1881 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1882 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1883 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001884 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001885 goto out;
1886 }
1887
1888 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001889 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001890 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001891 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001892 goto err;
1893 }
1894
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001895 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001896 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001897 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001898 /* The connection is ready to be accepted. */
1899 quic_accept_push_qc(qc);
1900 }
1901 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001902 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001903 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 } else {
1905 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1906 if (ssl_err != 1) {
1907 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1908 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1909 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001910 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001911 goto out;
1912 }
1913
1914 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001915 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001916 goto err;
1917 }
1918
1919 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001920 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001921 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001922
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001924 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 return 1;
1926
1927 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001928 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001929 return 0;
1930}
1931
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001932/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1933 * <pkt> RX packet.
1934 * Return it if succeeded, NULL if not.
1935 */
1936static inline
1937struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1938 struct quic_rx_packet *pkt)
1939{
1940 struct quic_rx_strm_frm *frm;
1941
1942 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1943 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001944 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001945 frm->len = stream_frm->len;
1946 frm->data = stream_frm->data;
1947 frm->pkt = pkt;
1948 }
1949
1950 return frm;
1951}
1952
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001953/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001954 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001955 */
1956static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1957{
1958 size_t ret;
1959
1960 ret = 0;
1961 while (strm_frm->len) {
1962 size_t try;
1963
1964 try = b_contig_space(buf);
1965 if (!try)
1966 break;
1967
1968 if (try > strm_frm->len)
1969 try = strm_frm->len;
1970 memcpy(b_tail(buf), strm_frm->data, try);
1971 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001972 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001973 b_add(buf, try);
1974 ret += try;
1975 }
1976
1977 return ret;
1978}
1979
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001980/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1981 * Also update <strm_frm> frame to reflect the data which have been consumed.
1982 */
1983static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1984 struct quic_rx_strm_frm *strm_frm)
1985{
1986 size_t ret;
1987
1988 ret = 0;
1989 while (strm_frm->len) {
1990 size_t try;
1991
1992 try = b_contig_space(buf);
1993 if (!try)
1994 break;
1995
1996 if (try > strm_frm->len)
1997 try = strm_frm->len;
1998 memcpy(b_tail(buf), strm_frm->data, try);
1999 strm_frm->len -= try;
2000 strm_frm->offset_node.key += try;
2001 b_add(buf, try);
2002 ret += try;
2003 }
2004
2005 return ret;
2006}
2007
2008/* Process as much as possible RX STREAM frames received for <qcs> */
2009static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2010{
2011 int total;
2012 struct eb64_node *frm_node;
2013
2014 total = 0;
2015 frm_node = eb64_first(&qcs->rx.frms);
2016 while (frm_node) {
2017 int ret;
2018 struct quic_rx_strm_frm *frm;
2019
2020 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2021 if (frm->offset_node.key != qcs->rx.offset)
2022 break;
2023
2024 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2025 qcs->rx.offset += ret;
2026 total += ret;
2027 if (frm->len) {
2028 /* If there is remaining data in this frame
2029 * this is because the destination buffer is full.
2030 */
2031 break;
2032 }
2033
2034 frm_node = eb64_next(frm_node);
2035 quic_rx_packet_refdec(frm->pkt);
2036 eb64_delete(&frm->offset_node);
2037 pool_free(pool_head_quic_rx_strm_frm, frm);
2038 }
2039
2040 return total;
2041}
2042
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002043/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2044 * streams may be open. The data are copied to the stream RX buffer if possible.
2045 * If not, the STREAM frame is stored to be treated again later.
2046 * We rely on the flow control so that not to store too much STREAM frames.
2047 * Return 1 if succeeded, 0 if not.
2048 */
2049static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2050 struct quic_stream *strm_frm,
2051 struct quic_conn *qc)
2052{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002053 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002054 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002055 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002056 struct quic_rx_strm_frm *frm;
Amaury Denoyelle6a2c2f42022-02-15 10:57:16 +01002057 char fin = 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002058
2059 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2060 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002061 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002062 return 0;
2063 }
2064
2065 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002066 if (strm_frm->offset.key < strm->rx.offset) {
2067 size_t diff;
2068
2069 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2070 TRACE_PROTO("Already received STREAM data",
2071 QUIC_EV_CONN_PSTRM, qc);
2072 goto out;
2073 }
2074
2075 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2076 diff = strm->rx.offset - strm_frm->offset.key;
2077 strm_frm->offset.key = strm->rx.offset;
2078 strm_frm->len -= diff;
2079 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002080 }
2081
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002082 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002083 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002084 int ret;
2085
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002086 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002087 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002088 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002089
2090 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002091 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002092 strm->rx.offset += ret;
2093 }
2094
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002095 total += qc_treat_rx_strm_frms(strm);
Amaury Denoyelle6a2c2f42022-02-15 10:57:16 +01002096 /* FIN is set only if all data were copied. */
2097 fin = strm_frm->fin && !strm_frm->len;
2098 if (total && qc->qcc->app_ops->decode_qcs(strm, fin, qc->qcc->ctx) < 0) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002099 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002100 return 0;
2101 }
2102
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002103 if (!strm_frm->len)
2104 goto out;
2105
2106 store_frm:
2107 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2108 if (!frm) {
2109 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002110 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002111 return 0;
2112 }
2113
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002114 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002115 quic_rx_packet_refinc(pkt);
2116
2117 out:
2118 return 1;
2119}
2120
2121/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2122 * streams may be open. The data are copied to the stream RX buffer if possible.
2123 * If not, the STREAM frame is stored to be treated again later.
2124 * We rely on the flow control so that not to store too much STREAM frames.
2125 * Return 1 if succeeded, 0 if not.
2126 */
2127static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2128 struct quic_stream *strm_frm,
2129 struct quic_conn *qc)
2130{
2131 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002132 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002133 struct quic_rx_strm_frm *frm;
2134 size_t strm_frm_len;
2135
2136 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2137 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002138 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139 return 0;
2140 }
2141
2142 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002143 if (strm_frm->offset.key < strm->rx.offset) {
2144 size_t diff;
2145
2146 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2147 TRACE_PROTO("Already received STREAM data",
2148 QUIC_EV_CONN_PSTRM, qc);
2149 goto out;
2150 }
2151
2152 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2153 diff = strm->rx.offset - strm_frm->offset.key;
2154 strm_frm->offset.key = strm->rx.offset;
2155 strm_frm->len -= diff;
2156 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002157 }
2158
2159 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002160 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002161 int ret;
2162
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002163 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002164 goto store_frm;
2165
2166 /* qc_strm_cpy() will modify the offset, depending on the number
2167 * of bytes copied.
2168 */
2169 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2170 /* Inform the application of the arrival of this new stream */
2171 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002172 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002173 return 0;
2174 }
2175
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002176 if (ret)
2177 qcs_notify_recv(strm);
2178
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002179 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002180 }
2181 /* Take this frame into an account for the stream flow control */
2182 strm->rx.offset += strm_frm_len;
2183 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002184 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002185 */
2186 if (!strm_frm->len)
2187 goto out;
2188
2189 store_frm:
2190 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2191 if (!frm) {
2192 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002193 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002194 return 0;
2195 }
2196
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002197 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002198 quic_rx_packet_refinc(pkt);
2199
2200 out:
2201 return 1;
2202}
2203
2204static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2205 struct quic_stream *strm_frm,
2206 struct quic_conn *qc)
2207{
2208 if (strm_frm->id & QCS_ID_DIR_BIT)
2209 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2210 else
2211 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2212}
2213
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002214/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002215static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2216 struct quic_conn *qc)
2217{
2218 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002219 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002220 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002221
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002222 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002223 pkts = &qel->pktns->tx.pkts;
2224 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002225 /* Skip the empty packet (they have already been retransmitted) */
2226 while (node) {
2227 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2228 if (!LIST_ISEMPTY(&pkt->frms))
2229 break;
2230 node = eb64_next(node);
2231 }
2232
2233 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002234 return;
2235
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002236 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002237}
2238
2239/* Prepare a fast retransmission during handshake after a client
2240 * has resent Initial packets. According to the RFC a server may retransmit
2241 * up to two datagrams of Initial packets if did not receive all Initial packets
2242 * and resend them coalescing with others (Handshake here).
2243 * (Listener only).
2244 */
2245static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2246{
2247 struct list itmp = LIST_HEAD_INIT(itmp);
2248 struct list htmp = LIST_HEAD_INIT(htmp);
2249 struct quic_frame *frm, *frmbak;
2250
2251 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2252 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2253 struct quic_enc_level *qel = iqel;
2254 struct eb_root *pkts;
2255 struct eb64_node *node;
2256 struct quic_tx_packet *pkt;
2257 struct list *tmp = &itmp;
2258
2259 start:
2260 pkt = NULL;
2261 pkts = &qel->pktns->tx.pkts;
2262 node = eb64_first(pkts);
2263 /* Skip the empty packet (they have already been retransmitted) */
2264 while (node) {
2265 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2266 if (!LIST_ISEMPTY(&pkt->frms))
2267 break;
2268 node = eb64_next(node);
2269 }
2270
2271 if (!pkt)
2272 goto end;
2273
2274 qel->pktns->tx.pto_probe += 1;
2275 requeue:
2276 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2277 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2278 LIST_DELETE(&frm->list);
2279 LIST_APPEND(tmp, &frm->list);
2280 }
2281
2282 if (qel == iqel) {
2283 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2284 pkt = pkt->next;
2285 tmp = &htmp;
2286 hqel->pktns->tx.pto_probe += 1;
2287 goto requeue;
2288 }
2289
2290 qel = hqel;
2291 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002292 goto start;
2293 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002294
2295 end:
2296 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2297 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002298}
2299
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002300/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2301 * as I/O handler context and <qel> as encryption level.
2302 * Returns 1 if succeeded, 0 if failed.
2303 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002304static 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 +01002305 struct quic_enc_level *qel)
2306{
2307 struct quic_frame frm;
2308 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002309 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002310 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002311
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002312 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002313 /* Skip the AAD */
2314 pos = pkt->data + pkt->aad_len;
2315 end = pkt->data + pkt->len;
2316
2317 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002318 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002319 goto err;
2320
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002321 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002322 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002323 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002324 break;
2325 case QUIC_FT_PING:
2326 break;
2327 case QUIC_FT_ACK:
2328 {
2329 unsigned int rtt_sample;
2330
2331 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002332 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002333 goto err;
2334
2335 if (rtt_sample) {
2336 unsigned int ack_delay;
2337
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002338 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002339 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2340 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2341 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002342 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002343 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002344 break;
2345 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002346 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002347 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002348 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002349 {
2350 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002351
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002352 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2353 /* XXX TO DO: <cfdebug> is used only for the traces. */
2354 struct quic_rx_crypto_frm cfdebug = { };
2355
2356 cfdebug.offset_node.key = frm.crypto.offset;
2357 cfdebug.len = frm.crypto.len;
2358 TRACE_PROTO("CRYPTO data discarded",
2359 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2360 break;
2361 }
2362
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002363 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2364 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2365 /* XXX TO DO: <cfdebug> is used only for the traces. */
2366 struct quic_rx_crypto_frm cfdebug = { };
2367
2368 cfdebug.offset_node.key = frm.crypto.offset;
2369 cfdebug.len = frm.crypto.len;
2370 /* Nothing to do */
2371 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002372 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002373 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002374 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2375 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002376 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002377 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002378 else {
2379 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2380 /* XXX TO DO: <cfdebug> is used only for the traces. */
2381 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002382
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002383 cfdebug.offset_node.key = frm.crypto.offset;
2384 cfdebug.len = frm.crypto.len;
2385 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002386 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002387 frm.crypto.len -= diff;
2388 frm.crypto.data += diff;
2389 frm.crypto.offset = qel->rx.crypto.offset;
2390 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002391 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002392
2393 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002394 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002395 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002396
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002397 cfdebug.offset_node.key = frm.crypto.offset;
2398 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002399 if (!qc_provide_cdata(qel, ctx,
2400 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002401 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002402 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002403
2404 break;
2405 }
2406
2407 /* frm.crypto.offset > qel->rx.crypto.offset */
2408 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2409 if (!cf) {
2410 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002411 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002412 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002413 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002414
2415 cf->offset_node.key = frm.crypto.offset;
2416 cf->len = frm.crypto.len;
2417 cf->data = frm.crypto.data;
2418 cf->pkt = pkt;
2419 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2420 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002421 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002422 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002423 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002424 {
2425 struct quic_stream *stream = &frm.stream;
2426
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002427 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002428 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2429 goto err;
2430 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2431 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002432
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002433 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002434 goto err;
2435
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002436 break;
2437 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002438 case QUIC_FT_MAX_DATA:
2439 case QUIC_FT_MAX_STREAM_DATA:
2440 case QUIC_FT_MAX_STREAMS_BIDI:
2441 case QUIC_FT_MAX_STREAMS_UNI:
2442 case QUIC_FT_DATA_BLOCKED:
2443 case QUIC_FT_STREAM_DATA_BLOCKED:
2444 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2445 case QUIC_FT_STREAMS_BLOCKED_UNI:
2446 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002447 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002448 case QUIC_FT_RETIRE_CONNECTION_ID:
2449 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450 break;
2451 case QUIC_FT_CONNECTION_CLOSE:
2452 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002453 /* warn the mux to close the connection */
Amaury Denoyelle4af65952022-02-15 11:06:15 +01002454 if (qc->mux_state == QC_MUX_READY)
2455 qc->qcc->flags |= QC_CF_CC_RECV;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002456 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002457 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002458 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002459 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002460 goto err;
2461
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002462 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 break;
2464 default:
2465 goto err;
2466 }
2467 }
2468
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002469 if (fast_retrans)
2470 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002471
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2473 * has successfully parse a Handshake packet. The Initial encryption must also
2474 * be discarded.
2475 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002476 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002477 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002478
2479 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002480 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002481 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002482 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002483 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002484 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002485 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002486 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002487 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002488 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002489 }
2490
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002491 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 return 1;
2493
2494 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002495 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002496 return 0;
2497}
2498
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002499/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002500 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002501 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2502 * This function must be called only after having built a correct datagram.
2503 * Always succeeds.
2504 */
2505static inline void qc_set_dg(struct cbuf *cbuf,
2506 uint16_t dglen, struct quic_tx_packet *pkt)
2507{
2508 write_u16(cb_wr(cbuf), dglen);
2509 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2510 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2511}
2512
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002513/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002514 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2515 * several packets in the same datagram. A header made of two fields is added
2516 * to each datagram: the datagram length followed by the address of the first
2517 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002518 * Returns 1 if succeeded, or 0 if something wrong happened.
2519 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002520static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2521 enum quic_tls_enc_level tel,
2522 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002523{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002524 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002525 struct cbuf *cbuf;
2526 unsigned char *end_buf, *end, *pos, *spos;
2527 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2528 /* length of datagrams */
2529 uint16_t dglen;
2530 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002531 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002532 /* Each datagram is prepended with its length followed by the
2533 * address of the first packet in the datagram.
2534 */
2535 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002536
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002537 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2538
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002539 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002540 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002541 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002542 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002543 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002544 cbuf = qr->cbuf;
2545 spos = pos = cb_wr(cbuf);
2546 /* Leave at least <dglen> bytes at the end of this buffer
2547 * to ensure there is enough room to mark the end of prepared
2548 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002549 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002550 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2551 first_pkt = prv_pkt = NULL;
2552 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002553 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002554 enum quic_pkt_type pkt_type;
2555
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002556 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002557 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002558 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2559 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002560 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002561 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002562 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002563 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002564 * 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 +01002565 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002566 * and if there is no more CRYPTO data available or in flight
2567 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002568 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002569 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002570 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002571 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002572 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002573 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002574 /* Set the current datagram as prepared into <cbuf> if
2575 * the was already a correct packet which was previously written.
2576 */
2577 if (prv_pkt)
2578 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002579 /* Let's select the next encryption level */
2580 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2581 tel = next_tel;
2582 qel = &qc->els[tel];
2583 /* Build a new datagram */
2584 prv_pkt = NULL;
2585 continue;
2586 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 break;
2588 }
2589
2590 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002591 if (!prv_pkt) {
2592 /* Leave room for the datagram header */
2593 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002594 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002595 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2596 }
2597 else {
2598 end = pos + qc->path->mtu;
2599 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002600 }
2601
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002602 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002603 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002604 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002605 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002606 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002607 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002608 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002609 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002610 case -2:
2611 goto err;
2612 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002613 /* If there was already a correct packet present, set the
2614 * current datagram as prepared into <cbuf>.
2615 */
2616 if (prv_pkt) {
2617 qc_set_dg(cbuf, dglen, first_pkt);
2618 goto stop_build;
2619 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002620 goto out;
2621 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002622 break;
2623 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002624
Frédéric Lécaille63556772021-12-29 17:18:21 +01002625 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2626 if (!cur_pkt)
2627 goto err;
2628
2629 total += cur_pkt->len;
2630 /* keep trace of the first packet in the datagram */
2631 if (!first_pkt)
2632 first_pkt = cur_pkt;
2633 /* Attach the current one to the previous one */
2634 if (prv_pkt)
2635 prv_pkt->next = cur_pkt;
2636 /* Let's say we have to build a new dgram */
2637 prv_pkt = NULL;
2638 dglen += cur_pkt->len;
2639 /* Client: discard the Initial encryption keys as soon as
2640 * a handshake packet could be built.
2641 */
2642 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2643 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2644 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2645 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2646 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2647 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002648 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002649 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002650 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2651 }
2652 /* If the data for the current encryption level have all been sent,
2653 * select the next level.
2654 */
2655 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002656 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002657 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2658 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2659 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2660 tel = next_tel;
2661 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002662 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002663 /* If there is data for the next level, do not
2664 * consume a datagram.
2665 */
2666 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002668 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002669 /* If we have to build a new datagram, set the current datagram as
2670 * prepared into <cbuf>.
2671 */
2672 if (!prv_pkt) {
2673 qc_set_dg(cbuf, dglen, first_pkt);
2674 first_pkt = NULL;
2675 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002676 padding = 0;
2677 }
2678 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002679 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002680 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2681 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002682 }
2683 }
2684
2685 stop_build:
2686 /* Reset <wr> writer index if in front of <rd> index */
2687 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2688 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2689
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002690 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002691 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2692 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2693 /* Mark the end of contiguous data for the reader */
2694 write_u16(cb_wr(cbuf), 0);
2695 cb_add(cbuf, sizeof(uint16_t));
2696 }
2697 }
2698
2699 if (rd && rd <= cbuf->wr) {
2700 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002701 /* Let's try to reuse this buffer */
2702 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002703 }
2704 }
2705
2706 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002707 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002708 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002709
2710 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002711 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002712 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713}
2714
2715/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002716 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002717 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002718int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002719{
2720 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002721 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002722
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002723 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002724 cbuf = qr->cbuf;
2725 while (cb_contig_data(cbuf)) {
2726 unsigned char *pos;
2727 struct buffer tmpbuf = { };
2728 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2729 uint16_t dglen;
2730 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002731 unsigned int time_sent;
2732
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002733 pos = cb_rd(cbuf);
2734 dglen = read_u16(pos);
2735 /* End of prepared datagrams.
2736 * Reset the reader index only if in front of the writer index.
2737 */
2738 if (!dglen) {
2739 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2740
2741 if (wr && wr < cbuf->rd) {
2742 cb_rd_reset(cbuf);
2743 continue;
2744 }
2745 break;
2746 }
2747
2748 pos += sizeof dglen;
2749 first_pkt = read_ptr(pos);
2750 pos += sizeof first_pkt;
2751 tmpbuf.area = (char *)pos;
2752 tmpbuf.size = tmpbuf.data = dglen;
2753
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002754 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002755 for (pkt = first_pkt; pkt; pkt = pkt->next)
2756 quic_tx_packet_refinc(pkt);
Amaury Denoyelle58a77042022-02-09 15:43:07 +01002757 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002758 for (pkt = first_pkt; pkt; pkt = pkt->next)
2759 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002760 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002761 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002762
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002763 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002764 qc->tx.bytes += tmpbuf.data;
2765 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002766
2767 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2768 pkt->time_sent = time_sent;
2769 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2770 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002771 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002773 qc->path->in_flight += pkt->in_flight_len;
2774 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2775 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002776 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002777 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002778 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002779 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002780 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002781 }
2782 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783
2784 return 1;
2785}
2786
2787/* Build all the frames which must be sent just after the handshake have succeeded.
2788 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2789 * a HANDSHAKE_DONE frame.
2790 * Return 1 if succeeded, 0 if not.
2791 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002792static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002793{
2794 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002795 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002796 struct quic_frame *frm;
2797
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002798 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002800 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002801 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002802 if (!frm)
2803 return 0;
2804
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002805 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002806 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002807 }
2808
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002809 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810 struct quic_connection_id *cid;
2811
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 Denoyelle0442efd2022-01-28 16:02:13 +01002816 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002817 if (!cid)
2818 goto err;
2819
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002820 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01002821 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002822
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002824 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002825 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002826 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827
2828 return 1;
2829
2830 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002831 return 0;
2832}
2833
2834/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002835void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002836{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002837 struct eb64_node *n;
2838 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002839
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002840 n = eb64_first(&arngs->root);
2841 while (n) {
2842 struct eb64_node *next;
2843
2844 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2845 next = eb64_next(n);
2846 eb64_delete(n);
2847 free(ar);
2848 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002849 }
2850}
2851
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002852/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2853 * descending order.
2854 */
2855static inline size_t sack_gap(struct quic_arng_node *p,
2856 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002857{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002858 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859}
2860
2861
2862/* Remove the last elements of <ack_ranges> list of ack range updating its
2863 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002864 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002866static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002867{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002868 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002869
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002870 last = eb64_last(&arngs->root);
2871 while (last && arngs->enc_sz > limit) {
2872 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002874 prev = eb64_prev(last);
2875 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876 return 0;
2877
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002878 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2879 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2880 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2881 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2882 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2883 --arngs->sz;
2884 eb64_delete(last);
2885 pool_free(pool_head_quic_arng, last);
2886 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002887 }
2888
2889 return 1;
2890}
2891
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002892/* Set the encoded size of <arngs> QUIC ack ranges. */
2893static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2894{
2895 struct eb64_node *node, *next;
2896 struct quic_arng_node *ar, *ar_next;
2897
2898 node = eb64_last(&arngs->root);
2899 if (!node)
2900 return;
2901
2902 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2903 arngs->enc_sz = quic_int_getsize(ar->last) +
2904 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2905
2906 while ((next = eb64_prev(node))) {
2907 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2908 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2909 quic_int_getsize(ar_next->last - ar_next->first.key);
2910 node = next;
2911 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2912 }
2913}
2914
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002915/* Insert <ar> ack range into <argns> tree of ack ranges.
2916 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002917 */
2918static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002919struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2920 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002921{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002922 struct quic_arng_node *new_ar;
2923
2924 new_ar = pool_alloc(pool_head_quic_arng);
2925 if (new_ar) {
2926 new_ar->first.key = ar->first;
2927 new_ar->last = ar->last;
2928 eb64_insert(&arngs->root, &new_ar->first);
2929 arngs->sz++;
2930 }
2931
2932 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002933}
2934
2935/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002936 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002937 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002938 *
2939 * Descending order
2940 * ------------->
2941 * range1 range2
2942 * ..........|--------|..............|--------|
2943 * ^ ^ ^ ^
2944 * | | | |
2945 * last1 first1 last2 first2
2946 * ..........+--------+--------------+--------+......
2947 * diff1 gap12 diff2
2948 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002949 * To encode the previous list of ranges we must encode integers as follows in
2950 * descending order:
2951 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002952 * with diff1 = last1 - first1
2953 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002954 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002955 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002956 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002957int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2958 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002959{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002960 struct eb64_node *le;
2961 struct quic_arng_node *new_node;
2962 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002963
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002964 new = NULL;
2965 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002966 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002967 if (!new_node)
2968 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002969
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002970 goto out;
2971 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002972
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002973 le = eb64_lookup_le(&arngs->root, ar->first);
2974 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002975 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002976 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002977 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002978
2979 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002981 else {
2982 struct quic_arng_node *le_ar =
2983 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002984
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002985 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002986 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002987 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002988
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002989 if (le_ar->last + 1 >= ar->first) {
2990 le_ar->last = ar->last;
2991 new = le;
2992 new_node = le_ar;
2993 }
2994 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002995 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002996 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002997 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002998
2999 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003000 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003001 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003002
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003003 /* Verify that the new inserted node does not overlap the nodes
3004 * which follow it.
3005 */
3006 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003007 struct eb64_node *next;
3008 struct quic_arng_node *next_node;
3009
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003010 while ((next = eb64_next(new))) {
3011 next_node =
3012 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003013 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003014 break;
3015
3016 if (next_node->last > new_node->last)
3017 new_node->last = next_node->last;
3018 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003019 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003020 /* Decrement the size of these ranges. */
3021 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003023 }
3024
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003025 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003026 quic_arngs_set_enc_sz(arngs);
3027
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003028 return 1;
3029}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030/* Remove the header protection of packets at <el> encryption level.
3031 * Always succeeds.
3032 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003033static 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 +01003034{
3035 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003036 struct quic_rx_packet *pqpkt;
3037 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003038 struct quic_enc_level *app_qel;
3039
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003040 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3041 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003042 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003043 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003044 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003045 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003046 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003047 goto out;
3048 }
3049 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003050 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003051 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003052 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003053 pqpkt->data, pqpkt->data + pqpkt->len)) {
3054 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055 /* XXX TO DO XXX */
3056 }
3057 else {
3058 /* The AAD includes the packet number field */
3059 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3060 /* Store the packet into the tree of packets to decrypt. */
3061 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003062 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003063 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3064 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003065 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003066 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003067 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003068 MT_LIST_DELETE_SAFE(pkttmp1);
3069 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003070 }
3071
3072 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003073 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074}
3075
3076/* Process all the CRYPTO frame at <el> encryption level.
3077 * Return 1 if succeeded, 0 if not.
3078 */
3079static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003080 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003081{
3082 struct eb64_node *node;
3083
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003084 node = eb64_first(&el->rx.crypto.frms);
3085 while (node) {
3086 struct quic_rx_crypto_frm *cf;
3087
3088 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3089 if (cf->offset_node.key != el->rx.crypto.offset)
3090 break;
3091
3092 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3093 goto err;
3094
3095 node = eb64_next(node);
3096 quic_rx_packet_refdec(cf->pkt);
3097 eb64_delete(&cf->offset_node);
3098 pool_free(pool_head_quic_rx_crypto_frm, cf);
3099 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003100 return 1;
3101
3102 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003103 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003104 return 0;
3105}
3106
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003107/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003108 * 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 +02003109 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110 * Return 1 if succeeded, 0 if not.
3111 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003112int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3113 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003115 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003116 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003117 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003118 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003119
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003120 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003121 qel = cur_el;
3122 next_tel:
3123 if (!qel)
3124 goto out;
3125
3126 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3127 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003128 while (node) {
3129 struct quic_rx_packet *pkt;
3130
3131 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003132 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003133 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003134 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003135 /* Drop the packet */
3136 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003137 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003138 }
3139 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003140 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003141 /* Drop the packet */
3142 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003143 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003144 }
3145 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003146 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3147
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003148 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003149 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003150 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003151 if (pkt->pn > largest_pn)
3152 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003153 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003154 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003156 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003157 }
3158 }
3159 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003160 eb64_delete(&pkt->pn_node);
3161 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003162 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003163 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003164
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003165 /* Update the largest packet number. */
3166 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003167 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3168 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003169 goto err;
3170
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003171 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003172 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003173 qel = next_el;
3174 goto next_tel;
3175 }
3176
3177 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003178 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179 return 1;
3180
3181 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003182 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003183 return 0;
3184}
3185
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003186/* Check if it's possible to remove header protection for packets related to
3187 * encryption level <qel>. If <qel> is NULL, assume it's false.
3188 *
3189 * Return true if the operation is possible else false.
3190 */
3191static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3192{
3193 enum quic_tls_enc_level tel;
3194
3195 if (!qel)
3196 return 0;
3197
3198 tel = ssl_to_quic_enc_level(qel->level);
3199
3200 /* check if tls secrets are available */
3201 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
3202 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
3203
3204 if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET))
3205 return 0;
3206
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003207 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003208 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
3209 qc->mux_state != QC_MUX_READY)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003210 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003211
3212 return 1;
3213}
3214
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003215/* QUIC connection packet handler task. */
3216struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003217{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003218 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003219 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003220 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003221 enum quic_tls_enc_level tel, next_tel;
3222 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003223 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003224 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003226 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003227 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003228 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003229 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003230 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003231 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003232 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3233 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3234 /* The I/O handler has been woken up by the dgram listener
3235 * after the anti-amplification was reached.
3236 */
3237 qc_set_timer(qc);
3238 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3239 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3240 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003242 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3243 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3244 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003245 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003246 if (st >= QUIC_HS_ST_COMPLETE &&
3247 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3248 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3249 /* There may be remaining Handshake packets to treat and acknowledge. */
3250 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3251 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3252 }
3253 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003254 goto err;
3255
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003256 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003257 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003258
3259 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003260 /* Treat packets waiting for header packet protection decryption */
3261 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003262 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003263
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003264 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3265 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3266 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003267 goto err;
3268
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003269 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3270 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3271 qel = next_qel;
3272 next_qel = NULL;
3273 goto next_level;
3274 }
3275
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003276 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003277 if (st >= QUIC_HS_ST_COMPLETE) {
3278 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3279 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003280 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003281
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003282 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3283 QUIC_FL_TLS_SECRETS_DCD)) {
3284 /* Discard the Handshake keys. */
3285 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3286 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3287 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3288 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003289 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003290 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003291 }
3292
3293 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3294 /* There may be remaining handshake to build (acks) */
3295 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3296 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003297 }
3298
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003299 if (!qr)
3300 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003301 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3302 * be considered.
3303 */
3304 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003305 goto err;
3306 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003307 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003308 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003309 else if (ret == 0)
3310 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003311
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003312 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003313 goto err;
3314
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003315 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003316 /* Check if there is something to do for the next level.
3317 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003318 if (next_qel && next_qel != qel &&
3319 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003320 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003322 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003323 goto next_level;
3324 }
3325
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003326 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003327 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003328 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003329
3330 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003331 if (qr)
3332 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003333 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003334 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003335}
3336
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003337/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003338static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3339{
3340 int i;
3341
3342 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3343 if (qel->tx.crypto.bufs[i]) {
3344 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3345 qel->tx.crypto.bufs[i] = NULL;
3346 }
3347 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003348 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003349}
3350
3351/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003352 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003353 * Returns 1 if succeeded, 0 if not.
3354 */
3355static int quic_conn_enc_level_init(struct quic_conn *qc,
3356 enum quic_tls_enc_level level)
3357{
3358 struct quic_enc_level *qel;
3359
3360 qel = &qc->els[level];
3361 qel->level = quic_to_ssl_enc_level(level);
3362 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3363 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3364 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3365 qel->tls_ctx.rx.flags = 0;
3366 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003367 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368
3369 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003370 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003371 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003372 qel->rx.crypto.offset = 0;
3373 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003374
3375 /* Allocate only one buffer. */
3376 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3377 if (!qel->tx.crypto.bufs)
3378 goto err;
3379
3380 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3381 if (!qel->tx.crypto.bufs[0])
3382 goto err;
3383
3384 qel->tx.crypto.bufs[0]->sz = 0;
3385 qel->tx.crypto.nb_buf = 1;
3386
3387 qel->tx.crypto.sz = 0;
3388 qel->tx.crypto.offset = 0;
3389
3390 return 1;
3391
3392 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003393 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003394 return 0;
3395}
3396
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003397/* Increment the <qc> refcount.
3398 *
3399 * This operation must be conducted when manipulating the quic_conn outside of
3400 * the connection pinned thread. These threads can only retrieve the connection
3401 * in the CID tree, so this function must be conducted under the CID lock.
3402 */
3403static inline void quic_conn_take(struct quic_conn *qc)
3404{
3405 HA_ATOMIC_INC(&qc->refcount);
3406}
3407
3408/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003409 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003410 */
3411static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003413 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003414 int i;
3415
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003416 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003417 return;
3418
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003419 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003420 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003421
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003422 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003423 if (conn_ctx) {
3424 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003425 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003426 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003427
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003429 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003430
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003431 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3432 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003433 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003434}
3435
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003436/* Release the quic_conn <qc>. It will decrement its refcount so that the
3437 * connection will be freed once all threads have finished to work with it. The
3438 * connection is removed from the CIDs tree and thus cannot be found by other
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003439 * threads after it. The connection tasklet is killed.
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003440 *
3441 * Do not use <qc> after it as it may be freed. This function must only be
3442 * called by the thread responsible of the quic_conn tasklet.
3443 */
3444static void quic_conn_release(struct quic_conn *qc)
3445{
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003446 struct ssl_sock_ctx *conn_ctx;
3447
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003448 /* remove the connection from receiver cids trees */
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003449 ebmb_delete(&qc->odcid_node);
3450 ebmb_delete(&qc->scid_node);
3451 free_quic_conn_cids(qc);
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003452
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003453 /* Kill the tasklet. Do not use tasklet_free as this is not thread safe
3454 * as other threads may call tasklet_wakeup after this.
3455 */
3456 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3457 if (conn_ctx)
3458 tasklet_kill(conn_ctx->wait_event.tasklet);
3459
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003460 quic_conn_drop(qc);
3461}
3462
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003463void quic_close(struct connection *conn, void *xprt_ctx)
3464{
3465 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003466 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003467
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003468 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003469 /* This task must be deleted by the connection-pinned thread. */
3470 if (qc->timer_task) {
3471 task_destroy(qc->timer_task);
3472 qc->timer_task = NULL;
3473 }
3474
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003475 /* Next application data can be dropped. */
3476 qc->mux_state = QC_MUX_RELEASED;
3477
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003478 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003479
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003480 /* TODO for now release the quic_conn on notification by the upper
3481 * layer. It could be useful to delay it if there is remaining data to
3482 * send or data to be acked.
3483 */
3484 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003485}
3486
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003487/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003488static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003489{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003490 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003491 struct quic_conn *qc;
3492 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003493 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494
3495 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003496 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003497 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003498 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003499 task->expire = TICK_ETERNITY;
3500 pktns = quic_loss_pktns(qc);
3501 if (tick_isset(pktns->tx.loss_time)) {
3502 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3503
3504 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3505 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003506 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3507 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003508 goto out;
3509 }
3510
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003511 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003512 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003513 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003514 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3515 pktns->tx.pto_probe = 1;
3516 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3517 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3518 }
3519 else {
3520 pktns->tx.pto_probe = 2;
3521 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003522 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003523 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3525 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3526
3527 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3528 hel->pktns->tx.pto_probe = 1;
3529 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3530 iel->pktns->tx.pto_probe = 1;
3531 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003532
3533 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3534 int j;
3535
3536 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3537 continue;
3538
3539 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3540 qc_prep_fast_retrans(&qc->els[i], qc);
3541 }
3542
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003543 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3544 qc->path->loss.pto_count++;
3545
3546 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003547 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548
3549 return task;
3550}
3551
3552/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3553 * connections used to identify the first Initial packets of client connecting
3554 * to listeners. This parameter must be NULL for QUIC connections attached
3555 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3556 * <scid> is the source connection ID with <scid_len> as length.
3557 * Returns 1 if succeeded, 0 if not.
3558 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003559static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003560 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003561 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003562{
3563 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003564 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003565 /* Initial CID. */
3566 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003567 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003568 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003570 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003571 qc = pool_zalloc(pool_head_quic_conn);
3572 if (!qc) {
3573 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3574 goto err;
3575 }
3576
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003577 HA_ATOMIC_STORE(&qc->refcount, 0);
3578
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003579 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3580 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003581 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003582 goto err;
3583 }
3584
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003585 qc->cids = EB_ROOT;
3586 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003587 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003588 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003589
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003590 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003591 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003592 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003594 qc->odcid.addrlen = dcid_addr_len;
3595 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003596
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003597 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003598 if (scid_len)
3599 memcpy(qc->dcid.data, scid, scid_len);
3600 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003601 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003602 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003603 }
3604 /* QUIC Client (outgoing connection to servers) */
3605 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003606 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003607 if (dcid_len)
3608 memcpy(qc->dcid.data, dcid, dcid_len);
3609 qc->dcid.len = dcid_len;
3610 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003611 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612
3613 /* Initialize the output buffer */
3614 qc->obuf.pos = qc->obuf.data;
3615
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003616 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003617 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003618 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003619 goto err;
3620 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003621
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003622 /* insert the allocated CID in the receiver datagram handler tree */
3623 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003624 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003625
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003626 /* Select our SCID which is the first CID with 0 as sequence number. */
3627 qc->scid = icid->cid;
3628
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003629 /* Packet number spaces initialization. */
3630 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3631 quic_pktns_init(&qc->pktns[i]);
3632 /* QUIC encryption level context initialization. */
3633 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003634 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003635 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003636 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003637 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003638 /* Initialize the packet number space. */
3639 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3640 }
3641
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003642 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003643 qc->tps_tls_ext = qc->version & 0xff000000 ?
3644 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3645 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003646 /* TX part. */
3647 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003648 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3649 qc->tx.wbuf = qc->tx.rbuf = 0;
3650 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003651 /* RX part. */
3652 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003653 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003654 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003655 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003656 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003657 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003658 goto err;
3659 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003661 /* XXX TO DO: Only one path at this time. */
3662 qc->path = &qc->paths[0];
3663 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3664
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003665 /* required to use MTLIST_IN_LIST */
3666 MT_LIST_INIT(&qc->accept_list);
3667
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003668 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003670 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003671
3672 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003673 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003674 pool_free(pool_head_quic_conn_rxbuf, buf_area);
3675 if (qc)
3676 qc->rx.buf.area = NULL;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003677 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003678 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003679}
3680
3681/* Initialize the timer task of <qc> QUIC connection.
3682 * Returns 1 if succeeded, 0 if not.
3683 */
3684static int quic_conn_init_timer(struct quic_conn *qc)
3685{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003686 /* Attach this task to the same thread ID used for the connection */
3687 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003688 if (!qc->timer_task)
3689 return 0;
3690
3691 qc->timer = TICK_ETERNITY;
3692 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01003693 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003694
3695 return 1;
3696}
3697
3698/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3699 * past one byte of this buffer.
3700 */
3701static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3702 struct quic_rx_packet *pkt)
3703{
3704 unsigned char dcid_len, scid_len;
3705
3706 /* Version */
3707 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3708 return 0;
3709
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003710 /* Destination Connection ID Length */
3711 dcid_len = *(*buf)++;
3712 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3713 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3714 /* XXX MUST BE DROPPED */
3715 return 0;
3716
3717 if (dcid_len) {
3718 /* Check that the length of this received DCID matches the CID lengths
3719 * of our implementation for non Initials packets only.
3720 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003721 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3722 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003723 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724 return 0;
3725
3726 memcpy(pkt->dcid.data, *buf, dcid_len);
3727 }
3728
3729 pkt->dcid.len = dcid_len;
3730 *buf += dcid_len;
3731
3732 /* Source Connection ID Length */
3733 scid_len = *(*buf)++;
3734 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3735 /* XXX MUST BE DROPPED */
3736 return 0;
3737
3738 if (scid_len)
3739 memcpy(pkt->scid.data, *buf, scid_len);
3740 pkt->scid.len = scid_len;
3741 *buf += scid_len;
3742
3743 return 1;
3744}
3745
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003746/* Insert <pkt> RX packet in its <qel> RX packets tree */
3747static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3748{
3749 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003750 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003751 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3752 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3753 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003754}
3755
3756/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003757 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3758 * byte past the end of the buffer containing this packet and <beg> the address of
3759 * the packet first byte.
3760 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3761 * Returns 1 if succeeded, 0 if not.
3762 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003763static inline int qc_try_rm_hp(struct quic_conn *qc,
3764 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003765 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003766 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003767 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003768{
3769 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003770 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003771 struct quic_enc_level *qel;
3772 /* Only for traces. */
3773 struct quic_rx_packet *qpkt_trace;
3774
3775 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003776 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003777 /* The packet number is here. This is also the start minus
3778 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3779 * protection.
3780 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003781 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003782
3783 tel = quic_packet_type_enc_level(pkt->type);
3784 qel = &qc->els[tel];
3785
3786 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787 /* Note that the following function enables us to unprotect the packet
3788 * number and its length subsequently used to decrypt the entire
3789 * packets.
3790 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003791 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3792 qel->pktns->rx.largest_pn, pn, beg, end)) {
3793 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794 goto err;
3795 }
3796
3797 /* The AAD includes the packet number field found at <pn>. */
3798 pkt->aad_len = pn - beg + pkt->pnl;
3799 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003800 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003801 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003802 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3803 /* If the packet number space has been discarded, this packet
3804 * will be not parsed.
3805 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003806 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003807 goto out;
3808 }
3809
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003810 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003811 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003812 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3813 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003815 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003816 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003817 goto err;
3818 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003819
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003820 *el = qel;
3821 /* No reference counter incrementation here!!! */
3822 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3823 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3824 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3825 b_add(&qc->rx.buf, pkt->len);
3826 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003827 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003828 return 1;
3829
3830 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003831 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003832 return 0;
3833}
3834
3835/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3836 * Also set <*long_header> to 1 if this form is long, 0 if not.
3837 */
3838static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3839 unsigned char byte0, int *long_header)
3840{
3841 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3842 pkt->type =
3843 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3844 *long_header = 1;
3845 }
3846 else {
3847 pkt->type = QUIC_PACKET_TYPE_SHORT;
3848 *long_header = 0;
3849 }
3850}
3851
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003852/*
3853 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3854 */
3855static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3856{
3857 int j = 0, version;
3858
3859 do {
3860 version = quic_supported_version[j];
3861 if (version == pkt->version)
3862 return 1;
3863
3864 version = quic_supported_version[++j];
3865 } while(version);
3866
3867 return 0;
3868}
3869
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003870/*
3871 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3872 * address <addr>.
3873 * Implementation of RFC9000 6. Version Negotiation
3874 *
3875 * TODO implement a rate-limiting sending of Version Negotiation packets
3876 *
3877 * Returns 0 on success else non-zero
3878 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003879static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3880 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003881{
3882 char buf[256];
3883 int i = 0, j, version;
3884 const socklen_t addrlen = get_addr_len(addr);
3885
3886 /*
3887 * header form
3888 * long header, fixed bit to 0 for Version Negotiation
3889 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003890 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3891 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003892
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003893 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003894 /* null version for Version Negotiation */
3895 buf[i++] = '\x00';
3896 buf[i++] = '\x00';
3897 buf[i++] = '\x00';
3898 buf[i++] = '\x00';
3899
3900 /* source connection id */
3901 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003902 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003903 i += pkt->scid.len;
3904
3905 /* destination connection id */
3906 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003907 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003908 i += pkt->dcid.len;
3909
3910 /* supported version */
3911 j = 0;
3912 do {
3913 version = htonl(quic_supported_version[j]);
3914 memcpy(&buf[i], &version, sizeof(version));
3915 i += sizeof(version);
3916
3917 version = quic_supported_version[++j];
3918 } while (version);
3919
3920 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3921 return 1;
3922
3923 return 0;
3924}
3925
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003926/* Generate the token to be used in Retry packets. The token is written to
3927 * <buf> which is expected to be <len> bytes.
3928 *
3929 * Various parameters are expected to be encoded in the token. For now, only
3930 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
3931 * as this CID must be repeated by the server in the transport parameters.
3932 *
3933 * TODO add the client address to validate the token origin.
3934 *
3935 * Returns the length of the encoded token or 0 on error.
3936 */
3937static int generate_retry_token(unsigned char *buf, unsigned char len,
3938 struct quic_rx_packet *pkt)
3939{
3940 const size_t token_len = 1 + pkt->dcid.len;
3941 unsigned char i = 0;
3942
3943 if (token_len > len)
3944 return 0;
3945
3946 buf[i++] = pkt->dcid.len;
3947 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
3948 i += pkt->dcid.len;
3949
3950 return i;
3951}
3952
3953/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
3954 * the Initial <pkt> packet.
3955 *
3956 * Returns 0 on success else non-zero.
3957 */
3958static int send_retry(int fd, struct sockaddr_storage *addr,
3959 struct quic_rx_packet *pkt)
3960{
3961 unsigned char buf[128];
3962 int i = 0, token_len;
3963 const socklen_t addrlen = get_addr_len(addr);
3964 struct quic_cid scid;
3965
3966 /* long header + fixed bit + packet type 0x3 */
3967 buf[i++] = 0xf0;
3968 /* version */
3969 buf[i++] = 0x00;
3970 buf[i++] = 0x00;
3971 buf[i++] = 0x00;
3972 buf[i++] = 0x01;
3973
3974 /* Use the SCID from <pkt> for Retry DCID. */
3975 buf[i++] = pkt->scid.len;
3976 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
3977 i += pkt->scid.len;
3978
3979 /* Generate a new CID to be used as SCID for the Retry packet. */
3980 scid.len = QUIC_HAP_CID_LEN;
3981 if (RAND_bytes(scid.data, scid.len) != 1)
3982 return 1;
3983
3984 buf[i++] = scid.len;
3985 memcpy(&buf[i], scid.data, scid.len);
3986 i += scid.len;
3987
3988 /* token */
3989 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
3990 return 1;
3991 i += token_len;
3992
3993 /* token integrity tag */
3994 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
3995 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
3996 pkt->dcid.len, buf, i)) {
3997 return 1;
3998 }
3999
4000 i += QUIC_TLS_TAG_LEN;
4001
4002 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4003 return 1;
4004
4005 return 0;
4006}
4007
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004008/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4009 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4010 * concatenated to the <pkt> DCID field.
4011 *
4012 * Returns the instance or NULL if not found.
4013 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004014static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004015 struct listener *l,
4016 struct sockaddr_storage *saddr)
4017{
4018 struct quic_conn *qc = NULL;
4019 struct ebmb_node *node;
4020 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004021 /* set if the quic_conn is found in the second DCID tree */
4022 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004023
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004024 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4025 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4026 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4027 /* DCIDs of first packets coming from multiple clients may have
4028 * the same values. Let's distinguish them by concatenating the
4029 * socket addresses.
4030 */
4031 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004032 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004033 pkt->dcid.len + pkt->dcid.addrlen);
4034 if (node) {
4035 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4036 goto end;
4037 }
4038 }
4039
4040 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4041 * also for INITIAL/0-RTT non-first packets with the final DCID in
4042 * used.
4043 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004044 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004045 if (!node)
4046 goto end;
4047
4048 id = ebmb_entry(node, struct quic_connection_id, node);
4049 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004050 found_in_dcid = 1;
4051
4052 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004053 if (qc)
4054 quic_conn_take(qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004055
4056 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4057 * If already done, this is a noop.
4058 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004059 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004060 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004061
4062 return qc;
4063}
4064
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004065/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4066 * will extract the parameters stored in the token : <odcid>.
4067 *
4068 * Returns 0 on success else non-zero.
4069 */
4070static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4071 struct quic_cid *odcid)
4072{
4073 uint64_t odcid_len;
4074
4075 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4076 return 1;
4077
4078 memcpy(odcid->data, token, odcid_len);
4079 odcid->len = odcid_len;
4080
4081 return 0;
4082}
4083
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004084/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4085 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4086 * parameters of this session.
4087 * This is the responsibility of the caller to check the validity of all the
4088 * pointers passed as parameter to this function.
4089 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4090 * CO_ER_SSL_NO_MEM.
4091 */
4092static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4093 unsigned char *params, size_t params_len)
4094{
4095 int retry;
4096
4097 retry = 1;
4098 retry:
4099 *ssl = SSL_new(ssl_ctx);
4100 if (!*ssl) {
4101 if (!retry--)
4102 goto err;
4103
4104 pool_gc(NULL);
4105 goto retry;
4106 }
4107
4108 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4109 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4110 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4111 goto err;
4112
4113 SSL_free(*ssl);
4114 *ssl = NULL;
4115 if (!retry--)
4116 goto err;
4117
4118 pool_gc(NULL);
4119 goto retry;
4120 }
4121
4122 return 0;
4123
4124 err:
4125 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4126 return -1;
4127}
4128
4129/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4130 * used to process <qc> received packets. The allocated context is stored in
4131 * <qc.xprt_ctx>.
4132 *
4133 * Returns 0 on success else non-zero.
4134 */
4135int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4136{
4137 struct bind_conf *bc = qc->li->bind_conf;
4138 struct ssl_sock_ctx *ctx = NULL;
4139
4140 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4141 if (!ctx)
4142 goto err;
4143
4144 ctx->wait_event.tasklet = tasklet_new();
4145 if (!ctx->wait_event.tasklet)
4146 goto err;
4147
4148 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4149 ctx->wait_event.tasklet->context = ctx;
4150 ctx->wait_event.events = 0;
4151 ctx->subs = NULL;
4152 ctx->xprt_ctx = NULL;
4153 ctx->qc = qc;
4154
4155 /* Set tasklet tid based on the SCID selected by us for this
4156 * connection. The upper layer will also be binded on the same thread.
4157 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004158 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004159
4160 if (qc_is_listener(qc)) {
4161 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4162 qc->enc_params, qc->enc_params_len) == -1) {
4163 goto err;
4164 }
4165
4166 /* Enabling 0-RTT */
4167 if (bc->ssl_conf.early_data)
4168 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4169
4170 SSL_set_accept_state(ctx->ssl);
4171 }
4172
4173 ctx->xprt = xprt_get(XPRT_QUIC);
4174
4175 /* Store the allocated context in <qc>. */
4176 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4177
4178 return 0;
4179
4180 err:
4181 if (ctx && ctx->wait_event.tasklet)
4182 tasklet_free(ctx->wait_event.tasklet);
4183 pool_free(pool_head_quic_conn_ctx, ctx);
4184
4185 return 1;
4186}
4187
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004188static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004189 struct quic_rx_packet *pkt, int first_pkt,
4190 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004192 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004193 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004195 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004196 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004197 size_t b_cspace;
4198 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004200 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004201 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004202 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004203 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004204 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4205 /* This ist only to please to traces and distinguish the
4206 * packet with parsed packet number from others.
4207 */
4208 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004209 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004210 goto err;
4211
4212 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004213 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004214 /* XXX TO BE DISCARDED */
4215 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4216 goto err;
4217 }
4218
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004219 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004220 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004221 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004222 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004223 uint64_t len;
4224
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004225 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004226 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4227 goto err;
4228 }
4229
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004230 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4231 * they must have the same DCID.
4232 */
4233 if (!first_pkt &&
4234 (pkt->dcid.len != dgram->dcid_len ||
4235 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4236 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4237 goto err;
4238 }
4239
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004240 /* Retry of Version Negotiation packets are only sent by servers */
4241 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4242 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4243 goto err;
4244 }
4245
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004246 /* RFC9000 6. Version Negotiation */
4247 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004248 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004249 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004250 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4251 goto err;
4252 }
4253
4254 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004255 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004256 }
4257
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004258 /* For Initial packets, and for servers (QUIC clients connections),
4259 * there is no Initial connection IDs storage.
4260 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004261 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004262 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004263
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004264 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4265 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004266 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4267 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004268 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004269
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004270 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4271 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004272 */
4273 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004274
4275 /* TODO Retry should be automatically activated if
4276 * suspect network usage is detected.
4277 */
4278 if (!token_len && l->bind_conf->quic_force_retry) {
4279 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004280 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004281 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4282 goto err;
4283 }
4284
4285 goto err;
4286 }
4287 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004288 pkt->token = buf;
4289 buf += pkt->token_len;
4290 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004291 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004292 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004293 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004294 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4295 goto err;
4296 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004297 }
4298
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004299 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4300 end - buf < len) {
4301 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4302 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004303 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004304
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004305 payload = buf;
4306 pkt->len = len + payload - beg;
4307
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004308 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004309 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004310 int ipv4;
4311 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004312 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004313 const unsigned char *salt = initial_salt_v1;
4314 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004315
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004316 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004317 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004318 goto err;
4319 }
4320
Frédéric Lécailledc364042022-01-27 16:51:54 +01004321 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4322 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4323 goto err;
4324 }
4325
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004326 pkt->saddr = dgram->saddr;
4327 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004328 qc = qc_new_conn(pkt->version, ipv4,
4329 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004330 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004331 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004332 goto err;
4333
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004334 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4335
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004336 odcid = &qc->rx.params.original_destination_connection_id;
4337 /* Copy the transport parameters. */
4338 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004339
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004340 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004341 if (pkt->token_len) {
4342 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4343 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4344 goto err;
4345 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004346 /* Copy retry_source_connection_id transport parameter. */
4347 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4348 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004349 }
4350 else {
4351 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4352 odcid->len = pkt->dcid.len;
4353 }
4354
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004355 /* Copy the initial source connection ID. */
4356 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4357 qc->enc_params_len =
4358 quic_transport_params_encode(qc->enc_params,
4359 qc->enc_params + sizeof qc->enc_params,
4360 &qc->rx.params, 1);
4361 if (!qc->enc_params_len)
4362 goto err;
4363
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004364 if (qc_conn_alloc_ssl_ctx(qc))
4365 goto err;
4366
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004367 if (!quic_conn_init_timer(qc))
4368 goto err;
4369
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004370 /* NOTE: the socket address has been concatenated to the destination ID
4371 * chosen by the client for Initial packets.
4372 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004373 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4374 salt = initial_salt_draft_29;
4375 salt_len = sizeof initial_salt_draft_29;
4376 }
4377 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004378 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004379 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004380 goto err;
4381 }
4382
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004383 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004384 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004385 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004386
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004387 /* If the insertion failed, it means that another
4388 * thread has already allocated a QUIC connection for
4389 * the same CID. Liberate our allocated connection.
4390 */
4391 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004392 qc_to_purge = qc;
4393
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004394 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4395 pkt->qc = qc;
4396 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004397
4398 quic_conn_take(qc);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004399
4400 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004401 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004402 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004403 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004404 else {
4405 quic_conn_drop(qc_to_purge);
4406 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004407 }
4408 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004409 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410 }
4411 }
4412 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004413 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004414 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4415 goto err;
4416 }
4417
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004418 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004419 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004420
4421 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4422 * they must have the same DCID.
4423 */
4424 if (!first_pkt &&
4425 (pkt->dcid.len != dgram->dcid_len ||
4426 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4427 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4428 goto err;
4429 }
4430
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004431 buf += QUIC_HAP_CID_LEN;
4432
4433 /* A short packet is the last one of a UDP datagram. */
4434 payload = buf;
4435 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004436
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004437 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004438 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004439 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004440 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4441 goto err;
4442 }
4443
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004444 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004445 }
4446
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004447
4448 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4449 * they must have the same DCID.
4450 *
4451 * This check must be done after the final update to pkt.len to
4452 * properly drop the packet on failure.
4453 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004454 if (first_pkt && !quic_peer_validated_addr(qc) &&
4455 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4456 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4457 QUIC_EV_CONN_LPKT, qc);
4458 /* Reset the anti-amplification bit. It will be set again
4459 * when sending the next packet if reached again.
4460 */
4461 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4462 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4463 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004464 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004465
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004466 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004468 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004469 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004470 goto out;
4471 }
4472
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004473 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004474 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004475 b_cspace = b_contig_space(&qc->rx.buf);
4476 if (b_cspace < pkt->len) {
4477 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004478 if (b_cspace) {
4479 b_putchr(&qc->rx.buf, 0x00);
4480 b_cspace--;
4481 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004482 b_add(&qc->rx.buf, b_cspace);
4483 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004484 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004485 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004486 goto err;
4487 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004488 }
4489
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004490 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004491 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004492 goto err;
4493 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004494
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004495 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004496 if (pkt->aad_len)
4497 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004498 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004499 /* Wake up the connection packet handler task from here only if all
4500 * the contexts have been initialized, especially the mux context
4501 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4502 * will start it if these contexts for the connection are not already
4503 * initialized.
4504 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004505 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004506 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004507 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004508
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004509 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004511 if (qc)
4512 quic_conn_drop(qc);
4513
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004514 return pkt->len;
4515
4516 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004517 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4518 * This cannot be done by this thread.
4519 */
4520 if (io_cb_wakeup) {
4521 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4522 if (conn_ctx && conn_ctx->wait_event.tasklet)
4523 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4524 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004525 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004526 if (!pkt->len)
4527 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004528 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004529 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004530
4531 if (qc)
4532 quic_conn_drop(qc);
4533
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004534 return -1;
4535}
4536
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004537/* This function builds into <buf> buffer a QUIC long packet header.
4538 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004539 */
4540static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4541 int type, size_t pn_len, struct quic_conn *conn)
4542{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004543 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004544 return 0;
4545
4546 /* #0 byte flags */
4547 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4548 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4549 /* Version */
4550 quic_write_uint32(buf, end, conn->version);
4551 *(*buf)++ = conn->dcid.len;
4552 /* Destination connection ID */
4553 if (conn->dcid.len) {
4554 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4555 *buf += conn->dcid.len;
4556 }
4557 /* Source connection ID */
4558 *(*buf)++ = conn->scid.len;
4559 if (conn->scid.len) {
4560 memcpy(*buf, conn->scid.data, conn->scid.len);
4561 *buf += conn->scid.len;
4562 }
4563
4564 return 1;
4565}
4566
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004567/* This function builds into <buf> buffer a QUIC short packet header.
4568 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004569 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004570static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4571 size_t pn_len, struct quic_conn *conn,
4572 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004573{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004574 if (end - *buf < 1 + conn->dcid.len)
4575 return 0;
4576
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004577 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004578 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4579 ((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 +01004580 /* Destination connection ID */
4581 if (conn->dcid.len) {
4582 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4583 *buf += conn->dcid.len;
4584 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004585
4586 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004587}
4588
4589/* Apply QUIC header protection to the packet with <buf> as first byte address,
4590 * <pn> as address of the Packet number field, <pnlen> being this field length
4591 * with <aead> as AEAD cipher and <key> as secret key.
4592 * Returns 1 if succeeded or 0 if failed.
4593 */
4594static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4595 const EVP_CIPHER *aead, const unsigned char *key)
4596{
4597 int i, ret, outlen;
4598 EVP_CIPHER_CTX *ctx;
4599 /* We need an IV of at least 5 bytes: one byte for bytes #0
4600 * and at most 4 bytes for the packet number
4601 */
4602 unsigned char mask[5] = {0};
4603
4604 ret = 0;
4605 ctx = EVP_CIPHER_CTX_new();
4606 if (!ctx)
4607 return 0;
4608
4609 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4610 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4611 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4612 goto out;
4613
4614 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4615 for (i = 0; i < pnlen; i++)
4616 pn[i] ^= mask[i + 1];
4617
4618 ret = 1;
4619
4620 out:
4621 EVP_CIPHER_CTX_free(ctx);
4622
4623 return ret;
4624}
4625
4626/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4627 * ACK ranges if needed to a value below <limit> in bytes.
4628 * Return 1 if succeeded, 0 if not.
4629 */
4630static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4631{
4632 size_t room, ack_delay_sz;
4633
4634 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4635 /* A frame is made of 1 byte for the frame type. */
4636 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004637 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004638 return 0;
4639
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004640 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004641}
4642
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004643/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4644 * 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 +01004645 * and <*len> the packet Length field initialized with the number of bytes already present
4646 * 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 +02004647 * <headlen> is the number of bytes already present in this packet before building frames.
4648 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004649 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004650 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004651 * Return 1 if succeeded, 0 if not.
4652 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004653static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004654 size_t room, size_t *len, size_t headlen,
4655 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004656 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004657{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004658 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004659 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660
Frédéric Lécailleea604992020-12-24 13:01:37 +01004661 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004662 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004663 return 0;
4664
Frédéric Lécailleea604992020-12-24 13:01:37 +01004665 /* If we are not probing we must take into an account the congestion
4666 * control window.
4667 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004668 if (!qel->pktns->tx.pto_probe) {
4669 size_t remain = quic_path_prep_data(qc->path);
4670
4671 if (headlen > remain)
4672 return 0;
4673
4674 room = QUIC_MIN(room, remain - headlen);
4675 }
4676
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004677 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004678 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004679 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004680 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004681 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004682
Frédéric Lécailleea604992020-12-24 13:01:37 +01004683 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004684 break;
4685
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004686 switch (cf->type) {
4687 case QUIC_FT_CRYPTO:
4688 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004689 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004690 /* Compute the length of this CRYPTO frame header */
4691 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4692 /* Compute the data length of this CRyPTO frame. */
4693 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4694 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004695 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004696 if (!dlen)
4697 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004698
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004699 /* CRYPTO frame length. */
4700 flen = hlen + quic_int_getsize(dlen) + dlen;
4701 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004702 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004703 /* Add the CRYPTO data length and its encoded length to the packet
4704 * length and the length of this length.
4705 */
4706 *len += flen;
4707 room -= flen;
4708 if (dlen == cf->crypto.len) {
4709 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004710 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004711 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004712 }
4713 else {
4714 struct quic_frame *new_cf;
4715
4716 new_cf = pool_alloc(pool_head_quic_frame);
4717 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004718 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004719 return 0;
4720 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004722 new_cf->type = QUIC_FT_CRYPTO;
4723 new_cf->crypto.len = dlen;
4724 new_cf->crypto.offset = cf->crypto.offset;
4725 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004726 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004727 /* Consume <dlen> bytes of the current frame. */
4728 cf->crypto.len -= dlen;
4729 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004730 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004731 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004732
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004733 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004734 /* Note that these frames are accepted in short packets only without
4735 * "Length" packet field. Here, <*len> is used only to compute the
4736 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004737 *
4738 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004739 * excepting the variable ones. Note that +1 is for the type of this frame.
4740 */
4741 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004742 ((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 +02004743 /* Compute the data length of this STREAM frame. */
4744 avail_room = room - hlen - *len;
4745 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004746 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004747
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004748 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004749 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004750 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4751 dlen = max_available_room(avail_room, &dlen_sz);
4752 if (dlen > cf->stream.len) {
4753 dlen = cf->stream.len;
4754 }
4755 dlen_sz = quic_int_getsize(dlen);
4756 flen = hlen + dlen_sz + dlen;
4757 }
4758 else {
4759 dlen = QUIC_MIN(avail_room, cf->stream.len);
4760 flen = hlen + dlen;
4761 }
4762 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004763 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004764 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004765 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004766 /* Add the STREAM data length and its encoded length to the packet
4767 * length and the length of this length.
4768 */
4769 *len += flen;
4770 room -= flen;
4771 if (dlen == cf->stream.len) {
4772 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004773 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004774 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004775 }
4776 else {
4777 struct quic_frame *new_cf;
4778
4779 new_cf = pool_zalloc(pool_head_quic_frame);
4780 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004781 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004782 return 0;
4783 }
4784
4785 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004786 new_cf->stream.qcs = cf->stream.qcs;
4787 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004788 new_cf->stream.id = cf->stream.id;
4789 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4790 new_cf->stream.offset = cf->stream.offset;
4791 new_cf->stream.len = dlen;
4792 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4793 /* FIN bit reset */
4794 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4795 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004796 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004797 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4798 /* Consume <dlen> bytes of the current frame. */
4799 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004800 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004801 cf->stream.data += dlen;
4802 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004803 break;
4804
4805 default:
4806 flen = qc_frm_len(cf);
4807 BUG_ON(!flen);
4808 if (flen > room)
4809 continue;
4810
4811 *len += flen;
4812 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004813 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004814 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004815 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004816 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004817 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004818 }
4819
Frédéric Lécailleea604992020-12-24 13:01:37 +01004820 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004821}
4822
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004823/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004824 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4825 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4826 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004827 * 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 +02004828 * 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 +02004829 * having returned from this function.
4830 * 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 +01004831 * number field in this packet. <pn_len> will also have the packet number
4832 * length as value.
4833 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004834 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004835 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004836static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4837 size_t dglen, struct quic_tx_packet *pkt,
4838 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004839 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004840 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004841{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004842 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004843 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004844 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4845 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004846 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004847 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004848 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004849 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004850 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004851
Frédéric Lécailleea604992020-12-24 13:01:37 +01004852 /* Length field value with CRYPTO frames if present. */
4853 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004854 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004855 /* When not probing and not acking, and no immediate close is required,
4856 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004857 * the congestion controller window. So, we do not limit the size of this
4858 * packet if we have an ACK frame to send because an ACK frame is not
4859 * ack-eliciting. This size will be limited if we have ack-eliciting
4860 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004861 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004862 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004863 size_t path_room;
4864
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004865 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004866 if (end - beg > path_room)
4867 end = beg + path_room;
4868 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004869
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004870 /* Ensure there is enough room for the TLS encryption tag and a zero token
4871 * length field if any.
4872 */
4873 if (end - pos < QUIC_TLS_TAG_LEN +
4874 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4875 goto no_room;
4876
4877 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004878 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004879 /* packet number length */
4880 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004881 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004882 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004883 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004884 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004885 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004886 goto no_room;
4887
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004888 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4889 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004890 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004891 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004892 /* Build an ACK frame if required. */
4893 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004894 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004895 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004896 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004897 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4898 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4899 * that from here, we do not know if we will have to send a PING frame.
4900 * This will be decided after having computed the ack-eliciting frames
4901 * to be added to this packet.
4902 */
4903 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 +01004904 if (!ack_frm_len)
4905 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004906 }
4907
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004908 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004909 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004910 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004911 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004912 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004913
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004914 /* Initialize the length of the frames built below to <len>.
4915 * If any frame could be successfully built by qc_build_frms(),
4916 * we will have len_frms > len.
4917 */
4918 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004919 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004920 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004921 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004922 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004923 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004924
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004925 /* Length (of the remaining data). Must not fail because, the buffer size
4926 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4927 * for the encryption tag. It must be taken into an account for the length
4928 * of this packet.
4929 */
4930 if (len_frms)
4931 len = len_frms + QUIC_TLS_TAG_LEN;
4932 else
4933 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004934 /* CONNECTION_CLOSE frame */
4935 if (cc) {
4936 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004937
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004938 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004939 len += qc_frm_len(&cc_frm);
4940 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004941 add_ping_frm = 0;
4942 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004943 len_sz = quic_int_getsize(len);
4944 /* Add this packet size to <dglen> */
4945 dglen += head_len + len_sz + len;
4946 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4947 /* This is a maximum padding size */
4948 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4949 /* The length field value is of this packet is <len> + <padding_len>
4950 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004951 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004952 * sizes from <padding_len>.
4953 */
4954 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4955 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004956 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004957 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004958 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004959 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004960 add_ping_frm = 1;
4961 len += 1;
4962 }
4963 /* 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 +01004964 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004965 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4966 }
4967
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004968 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4969 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004970
4971 /* Packet number field address. */
4972 *buf_pn = pos;
4973
4974 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004975 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4976 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004977
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004978 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004979 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004980
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004981 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004982 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004983 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004984
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004985 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004986 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004987 ssize_t room = end - pos;
4988 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004989 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004990 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004991 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004992 }
4993 }
4994
4995 /* Build a PING frame if needed. */
4996 if (add_ping_frm) {
4997 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004998 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004999 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005000 }
5001
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005002 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005003 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005004 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005005
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005006 /* Build a PADDING frame if needed. */
5007 if (padding_len) {
5008 frm.type = QUIC_FT_PADDING;
5009 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005010 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005011 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005012 }
5013
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005014 /* If this packet is ack-eliciting and we are probing let's
5015 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005016 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005017 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5018 qel->pktns->tx.pto_probe)
5019 qel->pktns->tx.pto_probe--;
5020
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005021 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005022 LIST_SPLICE(&pkt->frms, &frm_list);
5023 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005024
5025 return 1;
5026
5027 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005028 /* Replace the pre-built frames which could not be add to this packet */
5029 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005030 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005031 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005032}
5033
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005034static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005036 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005037 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005038 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005039 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005040 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005041 pkt->next = NULL;
5042 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005043}
5044
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005045/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005046static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5047{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005048 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005049
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005050 if (!pkt)
5051 return;
5052
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005053 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005054 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005055 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005056 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005057 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005058}
5059
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005060/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5061 * type for <qc> QUIC connection from <qel> encryption level.
5062 * Return -2 if the packet could not be allocated or encrypted for any reason,
5063 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005064 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005065static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5066 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005067 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005068 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005069 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005070{
5071 /* The pointer to the packet number field. */
5072 unsigned char *buf_pn;
5073 unsigned char *beg, *end, *payload;
5074 int64_t pn;
5075 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005076 struct quic_tls_ctx *tls_ctx;
5077 struct quic_tx_packet *pkt;
5078
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005079 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005080 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005081 pkt = pool_alloc(pool_head_quic_tx_packet);
5082 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005083 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005084 *err = -2;
5085 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005086 }
5087
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005088 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005089 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005090 pn_len = 0;
5091 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005092
5093 pn = qel->pktns->tx.next_pn + 1;
5094 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 +01005095 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005096 *err = -1;
5097 goto err;
5098 }
5099
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005100 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101 payload = buf_pn + pn_len;
5102 payload_len = end - payload;
5103 aad_len = payload - beg;
5104
5105 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005106 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005107 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005109 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005110
5111 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005112 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005113 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5114 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005115 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005116 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005117 goto err;
5118 }
5119
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005120 /* Consume a packet number */
5121 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005122 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005123 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5124 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005125 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5126 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005127 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005128 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005129 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005130 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005131 pkt->in_flight_len = pkt->len;
5132 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005133 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005135 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005137 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005138
5139 err:
5140 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005141 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005142 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005143}
5144
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005146/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5147 * event subscriber <es> is not allowed to change from a previous call as long
5148 * as at least one event is still subscribed. The <event_type> must only be a
5149 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5150 */
5151static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5152{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005153 struct qcc *qcc = conn->qc->qcc;
5154
5155 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5156 BUG_ON(qcc->subs && qcc->subs != es);
5157
5158 es->events |= event_type;
5159 qcc->subs = es;
5160
5161 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005162 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005163
5164 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005165 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005166
5167 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005168}
5169
5170/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5171 * The <es> pointer is not allowed to differ from the one passed to the
5172 * subscribe() call. It always returns zero.
5173 */
5174static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5175{
5176 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5177}
5178
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005179/* Store in <xprt_ctx> the context attached to <conn>.
5180 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005181 */
5182static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5183{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005184 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005185
5186 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5187
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005188 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005189 if (*xprt_ctx)
5190 goto out;
5191
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005192 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005193
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005194 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005195 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005196
5197 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005198}
5199
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005200/* Start the QUIC transport layer */
5201static int qc_xprt_start(struct connection *conn, void *ctx)
5202{
5203 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005204 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005205
5206 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005207 quic_mux_transport_params_update(qc->qcc);
5208 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5209 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5210 return 0;
5211 }
5212
5213 /* mux-quic can now be considered ready. */
5214 qc->mux_state = QC_MUX_READY;
5215
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005216 tasklet_wakeup(qctx->wait_event.tasklet);
5217 return 1;
5218}
5219
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005220/* transport-layer operations for QUIC connections. */
5221static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005222 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005223 .subscribe = quic_conn_subscribe,
5224 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005225 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005226 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005227 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5228 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005229 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005230 .name = "QUIC",
5231};
5232
5233__attribute__((constructor))
5234static void __quic_conn_init(void)
5235{
5236 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5237 xprt_register(XPRT_QUIC, &ssl_quic);
5238}
5239
5240__attribute__((destructor))
5241static void __quic_conn_deinit(void)
5242{
5243 BIO_meth_free(ha_quic_meth);
5244}
5245
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005246/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5247 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005248 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005249 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005250struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005251{
5252 unsigned char *pos;
5253 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005254 struct quic_dghdlr *dghdlr = ctx;
5255 struct quic_dgram *dgram;
5256 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005257
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005258 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
5259 if (!dgram)
5260 goto err;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005261
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005262 pos = dgram->buf;
5263 end = pos + dgram->len;
5264 do {
5265 int ret;
5266 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005267
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005268 pkt = pool_zalloc(pool_head_quic_rx_packet);
5269 if (!pkt)
5270 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005271
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005272 quic_rx_packet_refinc(pkt);
5273 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5274 first_pkt = 0;
5275 pos += pkt->len;
5276 quic_rx_packet_refdec(pkt);
5277 if (ret == -1)
5278 /* If the packet length could not be found, we cannot continue. */
5279 break;
5280 } while (pos < end);
5281
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005282 /* Increasing the received bytes counter by the UDP datagram length
5283 * if this datagram could be associated to a connection.
5284 */
5285 if (dgram->qc)
5286 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005287
5288 /* Mark this datagram as consumed */
5289 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005290 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005291
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005292 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005293
5294 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005295 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005296}
5297
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005298/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5299 * Returns 1 if succeeded, 0 if not.
5300 */
5301static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5302 unsigned char **dcid, size_t *dcid_len)
5303{
5304 int long_header;
5305 size_t minlen, skip;
5306
5307 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5308 goto err;
5309
5310 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5311 minlen = long_header ?
5312 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5313 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005314 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005315 goto err;
5316
5317 buf += skip;
5318 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5319 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5320 goto err;
5321
5322 *dcid = buf;
5323
5324 return 1;
5325
5326 err:
5327 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5328 return 0;
5329}
5330
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005331/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5332 * correct datagram handler.
5333 * Return 1 if a correct datagram could be found, 0 if not.
5334 */
5335int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5336 struct sockaddr_storage *saddr,
5337 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005338{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005339 struct quic_dgram *dgram;
5340 unsigned char *dcid;
5341 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005342 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005343
5344 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005345 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005346
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005347 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005348 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005349 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005350
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005351 cid_tid = quic_get_cid_tid(dcid);
5352
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005353 /* All the members must be initialized! */
5354 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005355 dgram->buf = buf;
5356 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005357 dgram->dcid = dcid;
5358 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005359 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005360 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005361 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005362 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005363
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005364 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005365
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005366 return 1;
5367
5368 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005369 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005370}
5371
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005372/* Function to automatically activate QUIC traces on stdout.
5373 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5374 * Main use for now is in the docker image for QUIC interop testing.
5375 */
5376static void quic_init_stdout_traces(void)
5377{
5378#ifdef ENABLE_QUIC_STDOUT_TRACES
5379 trace_quic.sink = sink_find("stdout");
5380 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005381 trace_quic.state = TRACE_STATE_RUNNING;
5382#endif
5383}
5384INITCALL0(STG_INIT, quic_init_stdout_traces);
5385
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005386/*
5387 * Local variables:
5388 * c-indent-level: 8
5389 * c-basic-offset: 8
5390 * End:
5391 */