blob: 9e31a75bbe22f762b347db73eaa1b0940cd08f15 [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" },
Frédéric Lécaille00e24002022-02-18 17:13:45 +010096 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processin" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010097 { .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écaille324ecda2021-11-02 10:14:44 +0100157DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200159 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_POOL(pool_head_quic_connection_id,
162 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100163DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164DECLARE_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 +0100165DECLARE_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 +0100166DECLARE_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 +0100167DECLARE_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 +0100168DECLARE_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 +0200169DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100170DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100171
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200172static 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 +0200173 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100174 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +0100175 int padding, int ack, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100176static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
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
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100323 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324 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:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100650 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
651 if (tick_is_expired(qc->timer, now_ms))
652 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
653 else
654 task_schedule(qc->timer_task, qc->timer);
655 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100656 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100657}
658
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100659/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
660 * connection.
661 * Return 1 if succeeded, 0 if not.
662 */
663static int quic_tls_key_update(struct quic_conn *qc)
664{
665 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
666 struct quic_tls_secrets *rx, *tx;
667 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
668 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
669
670 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
671 rx = &tls_ctx->rx;
672 tx = &tls_ctx->tx;
673 nxt_rx = &qc->ku.nxt_rx;
674 nxt_tx = &qc->ku.nxt_tx;
675
676 /* Prepare new RX secrets */
677 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
678 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100679 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100680 return 0;
681 }
682
683 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
684 nxt_rx->key, nxt_rx->keylen,
685 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
686 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100687 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100688 return 0;
689 }
690
691 /* Prepare new TX secrets */
692 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
693 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100694 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100695 return 0;
696 }
697
698 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
699 nxt_tx->key, nxt_tx->keylen,
700 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
701 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100702 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100703 return 0;
704 }
705
706 return 1;
707}
708
709/* Rotate the Key Update information for <qc> QUIC connection.
710 * Must be used after having updated them.
711 * Always succeeds.
712 */
713static void quic_tls_rotate_keys(struct quic_conn *qc)
714{
715 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
716 unsigned char *curr_secret, *curr_iv, *curr_key;
717
718 /* Rotate the RX secrets */
719 curr_secret = tls_ctx->rx.secret;
720 curr_iv = tls_ctx->rx.iv;
721 curr_key = tls_ctx->rx.key;
722
723 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
724 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
725 tls_ctx->rx.key = qc->ku.nxt_rx.key;
726
727 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
728 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
729 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
730
731 qc->ku.prv_rx.secret = curr_secret;
732 qc->ku.prv_rx.iv = curr_iv;
733 qc->ku.prv_rx.key = curr_key;
734 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
735
736 /* Update the TX secrets */
737 curr_secret = tls_ctx->tx.secret;
738 curr_iv = tls_ctx->tx.iv;
739 curr_key = tls_ctx->tx.key;
740
741 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
742 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
743 tls_ctx->tx.key = qc->ku.nxt_tx.key;
744
745 qc->ku.nxt_tx.secret = curr_secret;
746 qc->ku.nxt_tx.iv = curr_iv;
747 qc->ku.nxt_tx.key = curr_key;
748}
749
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100750#ifndef OPENSSL_IS_BORINGSSL
751int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
752 const uint8_t *read_secret,
753 const uint8_t *write_secret, size_t secret_len)
754{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100755 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
756 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 +0100757 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100758 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100759
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100760 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100761 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100762 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
763 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100764 goto out;
765 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100766
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100767 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100768 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100769 goto err;
770 }
771
772 rx = &tls_ctx->rx;
773 tx = &tls_ctx->tx;
774
775 rx->aead = tx->aead = tls_aead(cipher);
776 rx->md = tx->md = tls_md(cipher);
777 rx->hp = tx->hp = tls_hp(cipher);
778
779 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
780 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100781 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100782 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100783 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100784 }
785
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100786 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100787 /* Enqueue this connection asap if we could derive O-RTT secrets as
788 * listener. Note that a listener derives only RX secrets for this
789 * level.
790 */
791 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
792 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100793
794 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100795 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100796
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100797 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
798 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100799 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100800 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100801 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100802 }
803
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100804 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100805 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100806 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
807 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
808 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
809
810 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
811 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100812 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100813 goto err;
814 }
815
816 memcpy(rx->secret, read_secret, secret_len);
817 rx->secretlen = secret_len;
818 memcpy(tx->secret, write_secret, secret_len);
819 tx->secretlen = secret_len;
820 /* Initialize all the secret keys lengths */
821 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
822 /* Prepare the next key update */
823 if (!quic_tls_key_update(qc))
824 goto err;
825 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100826
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100827 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100828 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100829 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100830
831 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100832 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100833 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100834}
835#else
836/* ->set_read_secret callback to derive the RX secrets at <level> encryption
837 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500838 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100839 */
840int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
841 const SSL_CIPHER *cipher,
842 const uint8_t *secret, size_t secret_len)
843{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100844 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100845 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100846 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100847
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100848 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
849 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
850 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100851 goto out;
852 }
853
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100854 tls_ctx->rx.aead = tls_aead(cipher);
855 tls_ctx->rx.md = tls_md(cipher);
856 tls_ctx->rx.hp = tls_hp(cipher);
857
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100858 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
859 goto err;
860
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100861 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 +0100862 tls_ctx->rx.key, tls_ctx->rx.keylen,
863 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100864 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
865 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100866 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100867 goto err;
868 }
869
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100870 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100871 const unsigned char *buf;
872 size_t buflen;
873
874 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
875 if (!buflen)
876 goto err;
877
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100878 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100879 goto err;
880 }
881
882 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100883 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100884 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100885
886 return 1;
887
888 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100889 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100890 return 0;
891}
892
893/* ->set_write_secret callback to derive the TX secrets at <level>
894 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500895 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100896 */
897int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
898 const SSL_CIPHER *cipher,
899 const uint8_t *secret, size_t secret_len)
900{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100901 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
902 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 +0100903
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100904 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
905 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
906 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100907 goto out;
908 }
909
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100910 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
911 goto err;
912
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100913 tls_ctx->tx.aead = tls_aead(cipher);
914 tls_ctx->tx.md = tls_md(cipher);
915 tls_ctx->tx.hp = tls_hp(cipher);
916
917 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100918 tls_ctx->tx.key, tls_ctx->tx.keylen,
919 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
921 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100922 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100923 goto err;
924 }
925
926 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100927 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100928 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100929 return 1;
930
931 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100932 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100933 return 0;
934}
935#endif
936
937/* This function copies the CRYPTO data provided by the TLS stack found at <data>
938 * with <len> as size in CRYPTO buffers dedicated to store the information about
939 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
940 * It fails only if it could not managed to allocate enough CRYPTO buffers to
941 * store all the data.
942 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
943 */
944static int quic_crypto_data_cpy(struct quic_enc_level *qel,
945 const unsigned char *data, size_t len)
946{
947 struct quic_crypto_buf **qcb;
948 /* The remaining byte to store in CRYPTO buffers. */
949 size_t cf_offset, cf_len, *nb_buf;
950 unsigned char *pos;
951
952 nb_buf = &qel->tx.crypto.nb_buf;
953 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
954 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
955 cf_len = len;
956
957 while (len) {
958 size_t to_copy, room;
959
960 pos = (*qcb)->data + (*qcb)->sz;
961 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
962 to_copy = len > room ? room : len;
963 if (to_copy) {
964 memcpy(pos, data, to_copy);
965 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
966 qel->tx.crypto.sz += to_copy;
967 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100968 len -= to_copy;
969 data += to_copy;
970 }
971 else {
972 struct quic_crypto_buf **tmp;
973
974 tmp = realloc(qel->tx.crypto.bufs,
975 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
976 if (tmp) {
977 qel->tx.crypto.bufs = tmp;
978 qcb = &qel->tx.crypto.bufs[*nb_buf];
979 *qcb = pool_alloc(pool_head_quic_crypto_buf);
980 if (!*qcb)
981 return 0;
982
983 (*qcb)->sz = 0;
984 ++*nb_buf;
985 }
986 else {
987 break;
988 }
989 }
990 }
991
992 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
993 * have been buffered.
994 */
995 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200996 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100997 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100998
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100999 /* There is at most one CRYPTO frame in this packet number
1000 * space. Let's look for it.
1001 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001002 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001003 if (frm->type != QUIC_FT_CRYPTO)
1004 continue;
1005
1006 /* Found */
1007 found = frm;
1008 break;
1009 }
1010
1011 if (found) {
1012 found->crypto.len += cf_len;
1013 }
1014 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001015 frm = pool_alloc(pool_head_quic_frame);
1016 if (!frm)
1017 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001018
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001019 frm->type = QUIC_FT_CRYPTO;
1020 frm->crypto.offset = cf_offset;
1021 frm->crypto.len = cf_len;
1022 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001023 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001024 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001025 }
1026
1027 return len == 0;
1028}
1029
1030
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001031/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1032void quic_set_tls_alert(struct quic_conn *qc, int alert)
1033{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001034 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001035 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001036 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001037}
1038
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001039/* Set the application for <qc> QUIC connection.
1040 * Return 1 if succeeded, 0 if not.
1041 */
1042int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1043{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001044 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1045 qc->app_ops = &h3_ops;
1046 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1047 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001048 else
1049 return 0;
1050
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001051 return 1;
1052}
1053
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001054/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1055 * wants to provide the QUIC layer with CRYPTO data.
1056 * Returns 1 if succeeded, 0 if not.
1057 */
1058int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1059 const uint8_t *data, size_t len)
1060{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001061 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001062 enum quic_tls_enc_level tel;
1063 struct quic_enc_level *qel;
1064
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001065 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1066 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1067 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1068 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001069 goto out;
1070 }
1071
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001072 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001073 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001074 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001075 goto err;
1076 }
1077
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001078 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001079 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001080 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001081 goto err;
1082 }
1083
1084 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001085 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001086
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001087 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001088 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089 return 1;
1090
1091 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001092 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093 return 0;
1094}
1095
1096int ha_quic_flush_flight(SSL *ssl)
1097{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001098 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001100 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1101 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001102
1103 return 1;
1104}
1105
1106int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1107{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001108 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001109
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001110 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1111 quic_set_tls_alert(qc, alert);
1112 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113 return 1;
1114}
1115
1116/* QUIC TLS methods */
1117static SSL_QUIC_METHOD ha_quic_method = {
1118#ifdef OPENSSL_IS_BORINGSSL
1119 .set_read_secret = ha_set_rsec,
1120 .set_write_secret = ha_set_wsec,
1121#else
1122 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1123#endif
1124 .add_handshake_data = ha_quic_add_handshake_data,
1125 .flush_flight = ha_quic_flush_flight,
1126 .send_alert = ha_quic_send_alert,
1127};
1128
1129/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1130 * Returns an error count.
1131 */
1132int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1133{
1134 struct proxy *curproxy = bind_conf->frontend;
1135 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1136 int cfgerr = 0;
1137
1138#if 0
1139 /* XXX Did not manage to use this. */
1140 const char *ciphers =
1141 "TLS_AES_128_GCM_SHA256:"
1142 "TLS_AES_256_GCM_SHA384:"
1143 "TLS_CHACHA20_POLY1305_SHA256:"
1144 "TLS_AES_128_CCM_SHA256";
1145#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001146 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001147 long options =
1148 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1149 SSL_OP_SINGLE_ECDH_USE |
1150 SSL_OP_CIPHER_SERVER_PREFERENCE;
1151 SSL_CTX *ctx;
1152
1153 ctx = SSL_CTX_new(TLS_server_method());
1154 bind_conf->initial_ctx = ctx;
1155
1156 SSL_CTX_set_options(ctx, options);
1157#if 0
1158 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1159 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1160 "for bind '%s' at [%s:%d].\n",
1161 curproxy->id, ciphers,
1162 bind_conf->arg, bind_conf->file, bind_conf->line);
1163 cfgerr++;
1164 }
1165#endif
1166
1167 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1168 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1169 "for bind '%s' at [%s:%d].\n",
1170 curproxy->id, groups,
1171 bind_conf->arg, bind_conf->file, bind_conf->line);
1172 cfgerr++;
1173 }
1174
1175 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1176 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1177 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1178 SSL_CTX_set_default_verify_paths(ctx);
1179
1180#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1181#ifdef OPENSSL_IS_BORINGSSL
1182 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1183 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1184#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1185 if (bind_conf->ssl_conf.early_data) {
1186 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001187 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001188 }
1189 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1190 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1191#else
1192 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1193#endif
1194 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1195#endif
1196 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1197
1198 return cfgerr;
1199}
1200
1201/* Decode an expected packet number from <truncated_on> its truncated value,
1202 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1203 * the number of bits used to encode this packet number (its length in bytes * 8).
1204 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1205 */
1206static uint64_t decode_packet_number(uint64_t largest_pn,
1207 uint32_t truncated_pn, unsigned int pn_nbits)
1208{
1209 uint64_t expected_pn = largest_pn + 1;
1210 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1211 uint64_t pn_hwin = pn_win / 2;
1212 uint64_t pn_mask = pn_win - 1;
1213 uint64_t candidate_pn;
1214
1215
1216 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1217 /* Note that <pn_win> > <pn_hwin>. */
1218 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1219 candidate_pn + pn_hwin <= expected_pn)
1220 return candidate_pn + pn_win;
1221
1222 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1223 return candidate_pn - pn_win;
1224
1225 return candidate_pn;
1226}
1227
1228/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1229 * cryptographic context.
1230 * <largest_pn> is the largest received packet number and <pn> the address of
1231 * the packet number field for this packet with <byte0> address of its first byte.
1232 * <end> points to one byte past the end of this packet.
1233 * Returns 1 if succeeded, 0 if not.
1234 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001235static int qc_do_rm_hp(struct quic_conn *qc,
1236 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001237 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001238 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001239{
1240 int ret, outlen, i, pnlen;
1241 uint64_t packet_number;
1242 uint32_t truncated_pn = 0;
1243 unsigned char mask[5] = {0};
1244 unsigned char *sample;
1245 EVP_CIPHER_CTX *cctx;
1246 unsigned char *hp_key;
1247
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001248 /* Check there is enough data in this packet. */
1249 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001250 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 return 0;
1252 }
1253
1254 cctx = EVP_CIPHER_CTX_new();
1255 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001256 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 return 0;
1258 }
1259
1260 ret = 0;
1261 sample = pn + QUIC_PACKET_PN_MAXLEN;
1262
1263 hp_key = tls_ctx->rx.hp_key;
1264 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1265 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1266 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001267 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001268 goto out;
1269 }
1270
1271 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1272 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1273 for (i = 0; i < pnlen; i++) {
1274 pn[i] ^= mask[i + 1];
1275 truncated_pn = (truncated_pn << 8) | pn[i];
1276 }
1277
1278 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1279 /* Store remaining information for this unprotected header */
1280 pkt->pn = packet_number;
1281 pkt->pnl = pnlen;
1282
1283 ret = 1;
1284
1285 out:
1286 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001287
1288 return ret;
1289}
1290
1291/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1292 * address, with <payload_len> as payload length, <aad> as address of
1293 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1294 * context.
1295 * Returns 1 if succeeded, 0 if not.
1296 */
1297static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1298 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001299 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300{
1301 unsigned char iv[12];
1302 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001303 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001304 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001305
1306 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001307 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001308 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001309 }
1310
1311 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1312 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001313 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001314 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001315 }
1316
1317 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001318
1319 err:
1320 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001321 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001322 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323}
1324
1325/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1326 * Returns 1 if succeeded, 0 if not.
1327 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001328static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001330 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001331 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001332 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001333 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001334 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1335 unsigned char *rx_key = tls_ctx->rx.key;
1336
1337 kp_changed = 0;
1338 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1339 /* The two tested bits are not at the same position,
1340 * this is why they are first both inversed.
1341 */
1342 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1343 if (pkt->pn < tls_ctx->rx.pn) {
1344 /* The lowest packet number of a previous key phase
1345 * cannot be null if it really stores previous key phase
1346 * secrets.
1347 */
1348 if (!pkt->qc->ku.prv_rx.pn)
1349 return 0;
1350
1351 rx_iv = pkt->qc->ku.prv_rx.iv;
1352 rx_key = pkt->qc->ku.prv_rx.key;
1353 }
1354 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1355 /* Next key phase */
1356 kp_changed = 1;
1357 rx_iv = pkt->qc->ku.nxt_rx.iv;
1358 rx_key = pkt->qc->ku.nxt_rx.key;
1359 }
1360 }
1361 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001362
1363 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1364 return 0;
1365
1366 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1367 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001368 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001369 if (!ret)
1370 return 0;
1371
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001372 /* Update the keys only if the packet decryption succeeded. */
1373 if (kp_changed) {
1374 quic_tls_rotate_keys(pkt->qc);
1375 /* Toggle the Key Phase bit */
1376 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1377 /* Store the lowest packet number received for the current key phase */
1378 tls_ctx->rx.pn = pkt->pn;
1379 /* Prepare the next key update */
1380 if (!quic_tls_key_update(pkt->qc))
1381 return 0;
1382 }
1383
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001384 /* Update the packet length (required to parse the frames). */
1385 pkt->len = pkt->aad_len + ret;
1386
1387 return 1;
1388}
1389
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001390/* Remove from <qcs> stream the acknowledged frames.
1391 * Never fails.
1392 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001393static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001394{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001395 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001396 struct eb64_node *frm_node;
1397
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001398 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001399 frm_node = eb64_first(&qcs->tx.acked_frms);
1400 while (frm_node) {
1401 struct quic_stream *strm;
1402
1403 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001404 if (strm->offset.key > qcs->tx.ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001405 break;
1406
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001407 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1408 const size_t diff = strm->offset.key + strm->len -
1409 qcs->tx.ack_offset;
1410 qcs->tx.ack_offset += diff;
1411 b_del(strm->buf, diff);
1412 ret = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001413
1414 if (!b_data(strm->buf)) {
1415 b_free(strm->buf);
1416 offer_buffers(NULL, 1);
1417 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001418 }
1419
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001420 frm_node = eb64_next(frm_node);
1421 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001422
1423 /* TODO
1424 *
1425 * memleak: The quic_frame container of the quic_stream should
1426 * be liberated here, as in qc_treat_acked_tx_frm. However this
1427 * code seems to cause a bug which can lead to interrupted
1428 * transfers.
1429 *
1430 * struct quic_frame frm = container_of(strm, struct quic_frame, stream);
1431 * LIST_DELETE(&frm->list);
1432 * pool_free(pool_head_quic_frame, frm);
1433 */
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001434 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001435
1436 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001437}
1438
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001439/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001440static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1441 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001442{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001443 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001444
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001445 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001446 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001447 switch (frm->type) {
1448 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1449 {
1450 struct qcs *qcs = frm->stream.qcs;
1451 struct quic_stream *strm = &frm->stream;
1452
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001453 if (strm->offset.key <= qcs->tx.ack_offset) {
1454 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1455 const size_t diff = strm->offset.key + strm->len -
1456 qcs->tx.ack_offset;
1457 qcs->tx.ack_offset += diff;
1458 b_del(strm->buf, diff);
1459 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001460
1461 if (!b_data(strm->buf)) {
1462 b_free(strm->buf);
1463 offer_buffers(NULL, 1);
1464 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001465 }
1466
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001467 LIST_DELETE(&frm->list);
1468 pool_free(pool_head_quic_frame, frm);
1469 }
1470 else {
1471 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1472 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001473
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001474 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001475 }
1476 break;
1477 default:
1478 LIST_DELETE(&frm->list);
1479 pool_free(pool_head_quic_frame, frm);
1480 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001481
1482 if (stream_acked) {
1483 struct qcc *qcc = qc->qcc;
1484
1485 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1486 tasklet_wakeup(qcc->subs->tasklet);
1487 qcc->subs->events &= ~SUB_RETRY_SEND;
1488 if (!qcc->subs->events)
1489 qcc->subs = NULL;
1490 }
1491 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001492}
1493
1494/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1495 * deallocating them, and their TX frames.
1496 * Returns the last node reached to be used for the next range.
1497 * May be NULL if <largest> node could not be found.
1498 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001499static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1500 struct eb_root *pkts,
1501 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502 struct list *newly_acked_pkts,
1503 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001504 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001505{
1506 struct eb64_node *node;
1507 struct quic_tx_packet *pkt;
1508
1509 if (largest_node)
1510 node = largest_node;
1511 else {
1512 node = eb64_lookup(pkts, largest);
1513 while (!node && largest > smallest) {
1514 node = eb64_lookup(pkts, --largest);
1515 }
1516 }
1517
1518 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001519 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001520
1521 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1522 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001523 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001524 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001525 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001526 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001527 node = eb64_prev(node);
1528 eb64_delete(&pkt->pn_node);
1529 }
1530
1531 return node;
1532}
1533
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001534/* Remove all frames from <pkt_frm_list> and reinsert them in the
1535 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001536 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001537static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1538 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001539 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001540{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001541 struct quic_frame *frm, *frmbak;
1542 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001543
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001544 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1545 LIST_DELETE(&frm->list);
1546 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001547 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001548 }
1549
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001550 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001551}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001552
1553/* Free the TX packets of <pkts> list */
1554static inline void free_quic_tx_pkts(struct list *pkts)
1555{
1556 struct quic_tx_packet *pkt, *tmp;
1557
1558 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001559 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001560 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001561 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001562 }
1563}
1564
1565/* Send a packet loss event nofification to the congestion controller
1566 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1567 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1568 * at <now_us> current time.
1569 * Always succeeds.
1570 */
1571static inline void qc_cc_loss_event(struct quic_conn *qc,
1572 unsigned int lost_bytes,
1573 unsigned int newest_time_sent,
1574 unsigned int period,
1575 unsigned int now_us)
1576{
1577 struct quic_cc_event ev = {
1578 .type = QUIC_CC_EVT_LOSS,
1579 .loss.now_ms = now_ms,
1580 .loss.max_ack_delay = qc->max_ack_delay,
1581 .loss.lost_bytes = lost_bytes,
1582 .loss.newest_time_sent = newest_time_sent,
1583 .loss.period = period,
1584 };
1585
1586 quic_cc_event(&qc->path->cc, &ev);
1587}
1588
1589/* Send a packet ack event nofication for each newly acked packet of
1590 * <newly_acked_pkts> list and free them.
1591 * Always succeeds.
1592 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001593static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001594 struct list *newly_acked_pkts)
1595{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001596 struct quic_tx_packet *pkt, *tmp;
1597 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1598
1599 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1600 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001601 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001602 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001603 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001604 ev.ack.acked = pkt->in_flight_len;
1605 ev.ack.time_sent = pkt->time_sent;
1606 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001607 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001609 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001610 }
1611
1612}
1613
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001614/* Release all the frames attached to <pktns> packet number space */
1615static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1616{
1617 struct quic_frame *frm, *frmbak;
1618
1619 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1620 LIST_DELETE(&frm->list);
1621 pool_free(pool_head_quic_frame, frm);
1622 }
1623}
1624
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001625/* Handle <pkts> list of lost packets detected at <now_us> handling
1626 * their TX frames.
1627 * Send a packet loss event to the congestion controller if
1628 * in flight packet have been lost.
1629 * Also frees the packet in <pkts> list.
1630 * Never fails.
1631 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001632static inline void qc_release_lost_pkts(struct quic_conn *qc,
1633 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 struct list *pkts,
1635 uint64_t now_us)
1636{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001637 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001638 uint64_t lost_bytes;
1639
1640 lost_bytes = 0;
1641 oldest_lost = newest_lost = NULL;
1642 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001643 struct list tmp = LIST_HEAD_INIT(tmp);
1644
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001645 lost_bytes += pkt->in_flight_len;
1646 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001647 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001648 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001649 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001650 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001651 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001652 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653 if (!oldest_lost) {
1654 oldest_lost = newest_lost = pkt;
1655 }
1656 else {
1657 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001658 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001659 newest_lost = pkt;
1660 }
1661 }
1662
1663 if (lost_bytes) {
1664 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001665 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001666 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001667 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001668 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001669 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001670 }
1671}
1672
1673/* Look for packet loss from sent packets for <qel> encryption level of a
1674 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1675 * their tree if deemed as lost or set the <loss_time> value the packet number
1676 * space if any not deemed lost.
1677 * Should be called after having received an ACK frame with newly acknowledged
1678 * packets or when the the loss detection timer has expired.
1679 * Always succeeds.
1680 */
1681static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1682 struct quic_conn *qc,
1683 struct list *lost_pkts)
1684{
1685 struct eb_root *pkts;
1686 struct eb64_node *node;
1687 struct quic_loss *ql;
1688 unsigned int loss_delay;
1689
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001690 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001691 pkts = &pktns->tx.pkts;
1692 pktns->tx.loss_time = TICK_ETERNITY;
1693 if (eb_is_empty(pkts))
1694 goto out;
1695
1696 ql = &qc->path->loss;
1697 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001698 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1699
1700 node = eb64_first(pkts);
1701 while (node) {
1702 struct quic_tx_packet *pkt;
1703 int64_t largest_acked_pn;
1704 unsigned int loss_time_limit, time_sent;
1705
1706 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001707 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001708 node = eb64_next(node);
1709 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1710 break;
1711
1712 time_sent = pkt->time_sent;
1713 loss_time_limit = tick_add(time_sent, loss_delay);
1714 if (tick_is_le(time_sent, now_ms) ||
1715 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1716 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001717 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001718 }
1719 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001720 if (tick_isset(pktns->tx.loss_time))
1721 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1722 else
1723 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001724 }
1725 }
1726
1727 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001728 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001729}
1730
1731/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1732 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001733 * 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 +01001734 * acked ack-eliciting packet.
1735 * Return 1, if succeeded, 0 if not.
1736 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001737static inline int qc_parse_ack_frm(struct quic_conn *qc,
1738 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001739 struct quic_enc_level *qel,
1740 unsigned int *rtt_sample,
1741 const unsigned char **pos, const unsigned char *end)
1742{
1743 struct quic_ack *ack = &frm->ack;
1744 uint64_t smallest, largest;
1745 struct eb_root *pkts;
1746 struct eb64_node *largest_node;
1747 unsigned int time_sent, pkt_flags;
1748 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1749 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1750
1751 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1752 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001753 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001754 goto err;
1755 }
1756
1757 if (ack->first_ack_range > ack->largest_ack) {
1758 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001759 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001760 goto err;
1761 }
1762
1763 largest = ack->largest_ack;
1764 smallest = largest - ack->first_ack_range;
1765 pkts = &qel->pktns->tx.pkts;
1766 pkt_flags = 0;
1767 largest_node = NULL;
1768 time_sent = 0;
1769
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001770 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 +01001771 largest_node = eb64_lookup(pkts, largest);
1772 if (!largest_node) {
1773 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001774 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001775 }
1776 else {
1777 time_sent = eb64_entry(&largest_node->node,
1778 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001779 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 }
1781
1782 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001783 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001784 do {
1785 uint64_t gap, ack_range;
1786
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001787 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1788 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001789 if (!ack->ack_range_num--)
1790 break;
1791
1792 if (!quic_dec_int(&gap, pos, end))
1793 goto err;
1794
1795 if (smallest < gap + 2) {
1796 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001797 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001798 goto err;
1799 }
1800
1801 largest = smallest - gap - 2;
1802 if (!quic_dec_int(&ack_range, pos, end))
1803 goto err;
1804
1805 if (largest < ack_range) {
1806 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001807 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001808 goto err;
1809 }
1810
1811 /* Do not use this node anymore. */
1812 largest_node = NULL;
1813 /* Next range */
1814 smallest = largest - ack_range;
1815
1816 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001817 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001818 } while (1);
1819
1820 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001821 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001822
1823 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1824 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001825 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 }
1827
1828 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1829 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001830 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001831 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001832 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001834 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1835 if (quic_peer_validated_addr(qc))
1836 qc->path->loss.pto_count = 0;
1837 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001838 }
1839
1840
1841 return 1;
1842
1843 err:
1844 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001845 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001846 return 0;
1847}
1848
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001849/* This function gives the detail of the SSL error. It is used only
1850 * if the debug mode and the verbose mode are activated. It dump all
1851 * the SSL error until the stack was empty.
1852 */
1853static forceinline void qc_ssl_dump_errors(struct connection *conn)
1854{
1855 if (unlikely(global.mode & MODE_DEBUG)) {
1856 while (1) {
1857 unsigned long ret;
1858
1859 ret = ERR_get_error();
1860 if (!ret)
1861 return;
1862
1863 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1864 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1865 }
1866 }
1867}
1868
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001869int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1870 const char **str, int *len);
1871
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001872/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1873 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001874 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001875 * Return 1 if succeeded, 0 if not.
1876 */
1877static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001878 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001879 const unsigned char *data, size_t len,
1880 struct quic_rx_packet *pkt,
1881 struct quic_rx_crypto_frm *cf)
1882{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001883 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001884 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001885
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001887 qc = ctx->qc;
1888
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001889 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1890
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001891 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1892 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001893 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001894 goto err;
1895 }
1896
1897 el->rx.crypto.offset += len;
1898 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001899 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001900
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001901 state = HA_ATOMIC_LOAD(&qc->state);
1902 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001903 ssl_err = SSL_do_handshake(ctx->ssl);
1904 if (ssl_err != 1) {
1905 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1906 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1907 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001908 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001909 goto out;
1910 }
1911
1912 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001913 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001914 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001915 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001916 goto err;
1917 }
1918
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001919 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
1920 /* I/O callback switch */
1921 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001922 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001923 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001924 /* The connection is ready to be accepted. */
1925 quic_accept_push_qc(qc);
1926 }
1927 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001928 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001929 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001930 } else {
1931 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1932 if (ssl_err != 1) {
1933 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1934 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1935 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001936 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001937 goto out;
1938 }
1939
1940 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001941 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 goto err;
1943 }
1944
1945 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001946 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001947 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001948
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001949 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001950 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001951 return 1;
1952
1953 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001954 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001955 return 0;
1956}
1957
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001958/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1959 * <pkt> RX packet.
1960 * Return it if succeeded, NULL if not.
1961 */
1962static inline
1963struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1964 struct quic_rx_packet *pkt)
1965{
1966 struct quic_rx_strm_frm *frm;
1967
1968 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1969 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001970 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001971 frm->len = stream_frm->len;
1972 frm->data = stream_frm->data;
1973 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01001974 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001975 }
1976
1977 return frm;
1978}
1979
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001980/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001981 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001982 */
1983static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1984{
1985 size_t ret;
1986
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01001987 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
1988 strm_frm->len -= ret;
1989 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001990
1991 return ret;
1992}
1993
1994/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1995 * streams may be open. The data are copied to the stream RX buffer if possible.
1996 * If not, the STREAM frame is stored to be treated again later.
1997 * We rely on the flow control so that not to store too much STREAM frames.
1998 * Return 1 if succeeded, 0 if not.
1999 */
2000static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2001 struct quic_stream *strm_frm,
2002 struct quic_conn *qc)
2003{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002004 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002005 struct eb64_node *frm_node;
2006 struct qcs *qcs = NULL;
2007 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002008
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002009 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2010 strm_frm->offset.key, strm_frm->fin,
2011 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002012
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002013 /* invalid or already received frame */
2014 if (ret == 1)
2015 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002016
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002017 if (ret == 2) {
2018 /* frame cannot be parsed at the moment and should be
2019 * buffered.
2020 */
2021 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2022 if (!frm) {
2023 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002024 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002025 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002026 }
2027
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002028 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2029 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002030
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002031 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002032 }
2033
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002034 /* Frame correctly received by the mux.
2035 * If there is buffered frame for next offset, it may be possible to
2036 * receive them now.
2037 */
2038 frm_node = eb64_first(&qcs->rx.frms);
2039 while (frm_node) {
2040 frm = eb64_entry(&frm_node->node,
2041 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002042
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002043 ret = qcc_recv(qc->qcc, qcs->by_id.key, frm->len,
2044 frm->offset_node.key, frm->fin,
2045 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002046
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002047 /* interrupt the parsing if the frame cannot be handled for the
2048 * moment only by the MUX.
2049 */
2050 if (ret == 2)
2051 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002052
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002053 /* Remove a newly received frame or an invalid one. */
2054 frm_node = eb64_next(frm_node);
2055 eb64_delete(&frm->offset_node);
2056 quic_rx_packet_refdec(frm->pkt);
2057 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002058 }
2059
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002060 /* Decode the received data. */
2061 if (qcc_decode_qcs(qc->qcc, qcs))
2062 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002063
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002064 return 1;
2065}
2066
2067/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2068 * streams may be open. The data are copied to the stream RX buffer if possible.
2069 * If not, the STREAM frame is stored to be treated again later.
2070 * We rely on the flow control so that not to store too much STREAM frames.
2071 * Return 1 if succeeded, 0 if not.
2072 */
2073static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2074 struct quic_stream *strm_frm,
2075 struct quic_conn *qc)
2076{
2077 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002078 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002079 struct quic_rx_strm_frm *frm;
2080 size_t strm_frm_len;
2081
2082 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2083 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002084 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002085 return 0;
2086 }
2087
2088 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002089 if (strm_frm->offset.key < strm->rx.offset) {
2090 size_t diff;
2091
2092 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2093 TRACE_PROTO("Already received STREAM data",
2094 QUIC_EV_CONN_PSTRM, qc);
2095 goto out;
2096 }
2097
2098 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2099 diff = strm->rx.offset - strm_frm->offset.key;
2100 strm_frm->offset.key = strm->rx.offset;
2101 strm_frm->len -= diff;
2102 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002103 }
2104
2105 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002106 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002107 int ret;
2108
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002109 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002110 goto store_frm;
2111
2112 /* qc_strm_cpy() will modify the offset, depending on the number
2113 * of bytes copied.
2114 */
2115 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2116 /* Inform the application of the arrival of this new stream */
2117 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002118 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002119 return 0;
2120 }
2121
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002122 if (ret)
2123 qcs_notify_recv(strm);
2124
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002125 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002126 }
2127 /* Take this frame into an account for the stream flow control */
2128 strm->rx.offset += strm_frm_len;
2129 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002130 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002131 */
2132 if (!strm_frm->len)
2133 goto out;
2134
2135 store_frm:
2136 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2137 if (!frm) {
2138 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002139 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002140 return 0;
2141 }
2142
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002143 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002144 quic_rx_packet_refinc(pkt);
2145
2146 out:
2147 return 1;
2148}
2149
2150static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2151 struct quic_stream *strm_frm,
2152 struct quic_conn *qc)
2153{
2154 if (strm_frm->id & QCS_ID_DIR_BIT)
2155 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2156 else
2157 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2158}
2159
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002160/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002161static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2162 struct quic_conn *qc)
2163{
2164 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002165 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002166 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002167
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002168 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002169 pkts = &qel->pktns->tx.pkts;
2170 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002171 /* Skip the empty packet (they have already been retransmitted) */
2172 while (node) {
2173 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2174 if (!LIST_ISEMPTY(&pkt->frms))
2175 break;
2176 node = eb64_next(node);
2177 }
2178
2179 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002180 return;
2181
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002182 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002183}
2184
2185/* Prepare a fast retransmission during handshake after a client
2186 * has resent Initial packets. According to the RFC a server may retransmit
2187 * up to two datagrams of Initial packets if did not receive all Initial packets
2188 * and resend them coalescing with others (Handshake here).
2189 * (Listener only).
2190 */
2191static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2192{
2193 struct list itmp = LIST_HEAD_INIT(itmp);
2194 struct list htmp = LIST_HEAD_INIT(htmp);
2195 struct quic_frame *frm, *frmbak;
2196
2197 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2198 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2199 struct quic_enc_level *qel = iqel;
2200 struct eb_root *pkts;
2201 struct eb64_node *node;
2202 struct quic_tx_packet *pkt;
2203 struct list *tmp = &itmp;
2204
2205 start:
2206 pkt = NULL;
2207 pkts = &qel->pktns->tx.pkts;
2208 node = eb64_first(pkts);
2209 /* Skip the empty packet (they have already been retransmitted) */
2210 while (node) {
2211 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2212 if (!LIST_ISEMPTY(&pkt->frms))
2213 break;
2214 node = eb64_next(node);
2215 }
2216
2217 if (!pkt)
2218 goto end;
2219
2220 qel->pktns->tx.pto_probe += 1;
2221 requeue:
2222 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2223 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2224 LIST_DELETE(&frm->list);
2225 LIST_APPEND(tmp, &frm->list);
2226 }
2227
2228 if (qel == iqel) {
2229 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2230 pkt = pkt->next;
2231 tmp = &htmp;
2232 hqel->pktns->tx.pto_probe += 1;
2233 goto requeue;
2234 }
2235
2236 qel = hqel;
2237 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002238 goto start;
2239 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002240
2241 end:
2242 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2243 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002244}
2245
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002246/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2247 * as I/O handler context and <qel> as encryption level.
2248 * Returns 1 if succeeded, 0 if failed.
2249 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002250static 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 +01002251 struct quic_enc_level *qel)
2252{
2253 struct quic_frame frm;
2254 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002255 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002256 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002257
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002258 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002259 /* Skip the AAD */
2260 pos = pkt->data + pkt->aad_len;
2261 end = pkt->data + pkt->len;
2262
2263 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002264 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002265 goto err;
2266
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002267 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002268 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002269 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002270 break;
2271 case QUIC_FT_PING:
2272 break;
2273 case QUIC_FT_ACK:
2274 {
2275 unsigned int rtt_sample;
2276
2277 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002278 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002279 goto err;
2280
2281 if (rtt_sample) {
2282 unsigned int ack_delay;
2283
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002284 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002285 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2286 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2287 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002288 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002289 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002290 break;
2291 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002292 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002293 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002294 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002295 {
2296 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002297
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002298 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2299 /* XXX TO DO: <cfdebug> is used only for the traces. */
2300 struct quic_rx_crypto_frm cfdebug = { };
2301
2302 cfdebug.offset_node.key = frm.crypto.offset;
2303 cfdebug.len = frm.crypto.len;
2304 TRACE_PROTO("CRYPTO data discarded",
2305 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2306 break;
2307 }
2308
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002309 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2310 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2311 /* XXX TO DO: <cfdebug> is used only for the traces. */
2312 struct quic_rx_crypto_frm cfdebug = { };
2313
2314 cfdebug.offset_node.key = frm.crypto.offset;
2315 cfdebug.len = frm.crypto.len;
2316 /* Nothing to do */
2317 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002318 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002319 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002320 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2321 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002322 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002324 else {
2325 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2326 /* XXX TO DO: <cfdebug> is used only for the traces. */
2327 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002328
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002329 cfdebug.offset_node.key = frm.crypto.offset;
2330 cfdebug.len = frm.crypto.len;
2331 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002332 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002333 frm.crypto.len -= diff;
2334 frm.crypto.data += diff;
2335 frm.crypto.offset = qel->rx.crypto.offset;
2336 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002338
2339 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002340 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002341 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002342
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002343 cfdebug.offset_node.key = frm.crypto.offset;
2344 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002345 if (!qc_provide_cdata(qel, ctx,
2346 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002347 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002348 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002349
2350 break;
2351 }
2352
2353 /* frm.crypto.offset > qel->rx.crypto.offset */
2354 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2355 if (!cf) {
2356 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002357 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002358 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002359 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002360
2361 cf->offset_node.key = frm.crypto.offset;
2362 cf->len = frm.crypto.len;
2363 cf->data = frm.crypto.data;
2364 cf->pkt = pkt;
2365 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2366 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002367 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002368 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002369 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002370 {
2371 struct quic_stream *stream = &frm.stream;
2372
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002373 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002374 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2375 goto err;
2376 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2377 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002378
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002379 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002380 goto err;
2381
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002382 break;
2383 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002384 case QUIC_FT_MAX_DATA:
2385 case QUIC_FT_MAX_STREAM_DATA:
2386 case QUIC_FT_MAX_STREAMS_BIDI:
2387 case QUIC_FT_MAX_STREAMS_UNI:
2388 case QUIC_FT_DATA_BLOCKED:
2389 case QUIC_FT_STREAM_DATA_BLOCKED:
2390 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2391 case QUIC_FT_STREAMS_BLOCKED_UNI:
2392 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002393 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002394 case QUIC_FT_RETIRE_CONNECTION_ID:
2395 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002396 break;
2397 case QUIC_FT_CONNECTION_CLOSE:
2398 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002399 /* warn the mux to close the connection */
Amaury Denoyelle4af65952022-02-15 11:06:15 +01002400 if (qc->mux_state == QC_MUX_READY)
2401 qc->qcc->flags |= QC_CF_CC_RECV;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002402 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002403 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002405 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406 goto err;
2407
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002408 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002409 break;
2410 default:
2411 goto err;
2412 }
2413 }
2414
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002415 if (fast_retrans)
2416 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002417
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002418 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2419 * has successfully parse a Handshake packet. The Initial encryption must also
2420 * be discarded.
2421 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002422 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002423 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002424
2425 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002426 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002427 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002428 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002429 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002430 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002431 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002432 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002433 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002434 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002435 }
2436
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002437 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002438 return 1;
2439
2440 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002441 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442 return 0;
2443}
2444
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002445/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002446 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002447 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2448 * This function must be called only after having built a correct datagram.
2449 * Always succeeds.
2450 */
2451static inline void qc_set_dg(struct cbuf *cbuf,
2452 uint16_t dglen, struct quic_tx_packet *pkt)
2453{
2454 write_u16(cb_wr(cbuf), dglen);
2455 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2456 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2457}
2458
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002459/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002460 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2461 * several packets in the same datagram. A header made of two fields is added
2462 * to each datagram: the datagram length followed by the address of the first
2463 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002464 * Returns 1 if succeeded, or 0 if something wrong happened.
2465 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002466static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2467 enum quic_tls_enc_level tel,
2468 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002469{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002470 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002471 struct cbuf *cbuf;
2472 unsigned char *end_buf, *end, *pos, *spos;
2473 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2474 /* length of datagrams */
2475 uint16_t dglen;
2476 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002477 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002478 /* Each datagram is prepended with its length followed by the
2479 * address of the first packet in the datagram.
2480 */
2481 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002482
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002483 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2484
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002485 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002486 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002487 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002488 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002489 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002490 cbuf = qr->cbuf;
2491 spos = pos = cb_wr(cbuf);
2492 /* Leave at least <dglen> bytes at the end of this buffer
2493 * to ensure there is enough room to mark the end of prepared
2494 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002495 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002496 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2497 first_pkt = prv_pkt = NULL;
2498 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002499 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002500 enum quic_pkt_type pkt_type;
2501
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002502 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002503 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002504 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2505 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002506 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002507 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002508 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002509 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002510 * 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 +01002511 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002512 * and if there is no more CRYPTO data available or in flight
2513 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002515 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002516 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002517 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002518 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002519 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002520 /* Set the current datagram as prepared into <cbuf> if
2521 * the was already a correct packet which was previously written.
2522 */
2523 if (prv_pkt)
2524 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002525 /* Let's select the next encryption level */
2526 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2527 tel = next_tel;
2528 qel = &qc->els[tel];
2529 /* Build a new datagram */
2530 prv_pkt = NULL;
2531 continue;
2532 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533 break;
2534 }
2535
2536 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002537 if (!prv_pkt) {
2538 /* Leave room for the datagram header */
2539 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002540 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002541 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2542 }
2543 else {
2544 end = pos + qc->path->mtu;
2545 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002546 }
2547
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002548 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002549 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002550 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002551 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002552 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002553 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002554 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002555 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002556 case -2:
2557 goto err;
2558 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002559 /* If there was already a correct packet present, set the
2560 * current datagram as prepared into <cbuf>.
2561 */
2562 if (prv_pkt) {
2563 qc_set_dg(cbuf, dglen, first_pkt);
2564 goto stop_build;
2565 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002566 goto out;
2567 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002568 break;
2569 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002570
Frédéric Lécaille63556772021-12-29 17:18:21 +01002571 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2572 if (!cur_pkt)
2573 goto err;
2574
2575 total += cur_pkt->len;
2576 /* keep trace of the first packet in the datagram */
2577 if (!first_pkt)
2578 first_pkt = cur_pkt;
2579 /* Attach the current one to the previous one */
2580 if (prv_pkt)
2581 prv_pkt->next = cur_pkt;
2582 /* Let's say we have to build a new dgram */
2583 prv_pkt = NULL;
2584 dglen += cur_pkt->len;
2585 /* Client: discard the Initial encryption keys as soon as
2586 * a handshake packet could be built.
2587 */
2588 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2589 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2590 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2591 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2592 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2593 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002594 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002595 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002596 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2597 }
2598 /* If the data for the current encryption level have all been sent,
2599 * select the next level.
2600 */
2601 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002602 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002603 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2604 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2605 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2606 tel = next_tel;
2607 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002608 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002609 /* If there is data for the next level, do not
2610 * consume a datagram.
2611 */
2612 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002613 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002614 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002615 /* If we have to build a new datagram, set the current datagram as
2616 * prepared into <cbuf>.
2617 */
2618 if (!prv_pkt) {
2619 qc_set_dg(cbuf, dglen, first_pkt);
2620 first_pkt = NULL;
2621 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002622 padding = 0;
2623 }
2624 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002625 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002626 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2627 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002628 }
2629 }
2630
2631 stop_build:
2632 /* Reset <wr> writer index if in front of <rd> index */
2633 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2634 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2635
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002636 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002637 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2638 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2639 /* Mark the end of contiguous data for the reader */
2640 write_u16(cb_wr(cbuf), 0);
2641 cb_add(cbuf, sizeof(uint16_t));
2642 }
2643 }
2644
2645 if (rd && rd <= cbuf->wr) {
2646 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002647 /* Let's try to reuse this buffer */
2648 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002649 }
2650 }
2651
2652 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002653 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002654 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002655
2656 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002657 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002658 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002659}
2660
2661/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002662 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002663 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002664int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665{
2666 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002667 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002669 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002670 cbuf = qr->cbuf;
2671 while (cb_contig_data(cbuf)) {
2672 unsigned char *pos;
2673 struct buffer tmpbuf = { };
2674 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2675 uint16_t dglen;
2676 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002677 unsigned int time_sent;
2678
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002679 pos = cb_rd(cbuf);
2680 dglen = read_u16(pos);
2681 /* End of prepared datagrams.
2682 * Reset the reader index only if in front of the writer index.
2683 */
2684 if (!dglen) {
2685 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2686
2687 if (wr && wr < cbuf->rd) {
2688 cb_rd_reset(cbuf);
2689 continue;
2690 }
2691 break;
2692 }
2693
2694 pos += sizeof dglen;
2695 first_pkt = read_ptr(pos);
2696 pos += sizeof first_pkt;
2697 tmpbuf.area = (char *)pos;
2698 tmpbuf.size = tmpbuf.data = dglen;
2699
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002700 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002701 for (pkt = first_pkt; pkt; pkt = pkt->next)
2702 quic_tx_packet_refinc(pkt);
Amaury Denoyelle58a77042022-02-09 15:43:07 +01002703 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002704 for (pkt = first_pkt; pkt; pkt = pkt->next)
2705 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002706 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002707 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002709 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002710 qc->tx.bytes += tmpbuf.data;
2711 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002712
2713 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2714 pkt->time_sent = time_sent;
2715 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2716 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002717 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002718 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002719 qc->path->in_flight += pkt->in_flight_len;
2720 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2721 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002722 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002723 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002724 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002725 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002726 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002727 }
2728 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002729
2730 return 1;
2731}
2732
2733/* Build all the frames which must be sent just after the handshake have succeeded.
2734 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2735 * a HANDSHAKE_DONE frame.
2736 * Return 1 if succeeded, 0 if not.
2737 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002738static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002739{
2740 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002741 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002742 struct quic_frame *frm;
2743
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002744 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002745 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002746 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002747 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002748 if (!frm)
2749 return 0;
2750
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002751 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002752 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002753 }
2754
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002755 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756 struct quic_connection_id *cid;
2757
2758 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002759 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 goto err;
2761
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01002762 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002763 if (!cid)
2764 goto err;
2765
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002766 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01002767 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002768
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002770 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002771 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002772 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002773
2774 return 1;
2775
2776 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002777 return 0;
2778}
2779
2780/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002781void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002782{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002783 struct eb64_node *n;
2784 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002786 n = eb64_first(&arngs->root);
2787 while (n) {
2788 struct eb64_node *next;
2789
2790 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2791 next = eb64_next(n);
2792 eb64_delete(n);
2793 free(ar);
2794 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002795 }
2796}
2797
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002798/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2799 * descending order.
2800 */
2801static inline size_t sack_gap(struct quic_arng_node *p,
2802 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002803{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002804 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002805}
2806
2807
2808/* Remove the last elements of <ack_ranges> list of ack range updating its
2809 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002810 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002811 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002812static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002814 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002815
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002816 last = eb64_last(&arngs->root);
2817 while (last && arngs->enc_sz > limit) {
2818 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002819
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002820 prev = eb64_prev(last);
2821 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002822 return 0;
2823
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002824 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2825 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2826 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2827 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2828 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2829 --arngs->sz;
2830 eb64_delete(last);
2831 pool_free(pool_head_quic_arng, last);
2832 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 }
2834
2835 return 1;
2836}
2837
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002838/* Set the encoded size of <arngs> QUIC ack ranges. */
2839static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2840{
2841 struct eb64_node *node, *next;
2842 struct quic_arng_node *ar, *ar_next;
2843
2844 node = eb64_last(&arngs->root);
2845 if (!node)
2846 return;
2847
2848 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2849 arngs->enc_sz = quic_int_getsize(ar->last) +
2850 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2851
2852 while ((next = eb64_prev(node))) {
2853 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2854 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2855 quic_int_getsize(ar_next->last - ar_next->first.key);
2856 node = next;
2857 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2858 }
2859}
2860
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002861/* Insert <ar> ack range into <argns> tree of ack ranges.
2862 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002863 */
2864static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002865struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2866 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002867{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002868 struct quic_arng_node *new_ar;
2869
2870 new_ar = pool_alloc(pool_head_quic_arng);
2871 if (new_ar) {
2872 new_ar->first.key = ar->first;
2873 new_ar->last = ar->last;
2874 eb64_insert(&arngs->root, &new_ar->first);
2875 arngs->sz++;
2876 }
2877
2878 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002879}
2880
2881/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002882 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002883 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002884 *
2885 * Descending order
2886 * ------------->
2887 * range1 range2
2888 * ..........|--------|..............|--------|
2889 * ^ ^ ^ ^
2890 * | | | |
2891 * last1 first1 last2 first2
2892 * ..........+--------+--------------+--------+......
2893 * diff1 gap12 diff2
2894 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002895 * To encode the previous list of ranges we must encode integers as follows in
2896 * descending order:
2897 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002898 * with diff1 = last1 - first1
2899 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002900 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002901 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002902 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002903int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2904 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002905{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002906 struct eb64_node *le;
2907 struct quic_arng_node *new_node;
2908 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002909
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002910 new = NULL;
2911 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002912 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002913 if (!new_node)
2914 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002915
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002916 goto out;
2917 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002918
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002919 le = eb64_lookup_le(&arngs->root, ar->first);
2920 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002921 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002922 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002923 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002924
2925 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002926 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002927 else {
2928 struct quic_arng_node *le_ar =
2929 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002930
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002931 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002932 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002933 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002934
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002935 if (le_ar->last + 1 >= ar->first) {
2936 le_ar->last = ar->last;
2937 new = le;
2938 new_node = le_ar;
2939 }
2940 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002941 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002942 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002943 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002944
2945 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002946 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002947 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002948
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002949 /* Verify that the new inserted node does not overlap the nodes
2950 * which follow it.
2951 */
2952 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002953 struct eb64_node *next;
2954 struct quic_arng_node *next_node;
2955
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002956 while ((next = eb64_next(new))) {
2957 next_node =
2958 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002959 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002960 break;
2961
2962 if (next_node->last > new_node->last)
2963 new_node->last = next_node->last;
2964 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002965 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002966 /* Decrement the size of these ranges. */
2967 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002968 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002969 }
2970
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002971 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002972 quic_arngs_set_enc_sz(arngs);
2973
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974 return 1;
2975}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002976/* Remove the header protection of packets at <el> encryption level.
2977 * Always succeeds.
2978 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002979static 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 +01002980{
2981 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002982 struct quic_rx_packet *pqpkt;
2983 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002984 struct quic_enc_level *app_qel;
2985
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002986 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
2987 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002988 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002989 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002990 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002991 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002992 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 goto out;
2994 }
2995 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002996 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002997 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002999 pqpkt->data, pqpkt->data + pqpkt->len)) {
3000 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001 /* XXX TO DO XXX */
3002 }
3003 else {
3004 /* The AAD includes the packet number field */
3005 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3006 /* Store the packet into the tree of packets to decrypt. */
3007 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003008 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003009 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3010 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003011 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003012 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003014 MT_LIST_DELETE_SAFE(pkttmp1);
3015 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003016 }
3017
3018 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003019 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020}
3021
3022/* Process all the CRYPTO frame at <el> encryption level.
3023 * Return 1 if succeeded, 0 if not.
3024 */
3025static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003026 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027{
3028 struct eb64_node *node;
3029
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030 node = eb64_first(&el->rx.crypto.frms);
3031 while (node) {
3032 struct quic_rx_crypto_frm *cf;
3033
3034 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3035 if (cf->offset_node.key != el->rx.crypto.offset)
3036 break;
3037
3038 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3039 goto err;
3040
3041 node = eb64_next(node);
3042 quic_rx_packet_refdec(cf->pkt);
3043 eb64_delete(&cf->offset_node);
3044 pool_free(pool_head_quic_rx_crypto_frm, cf);
3045 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 return 1;
3047
3048 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003049 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003050 return 0;
3051}
3052
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003053/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003054 * 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 +02003055 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003056 * Return 1 if succeeded, 0 if not.
3057 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003058int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3059 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003061 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003062 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003063 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003064 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003066 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003067 qel = cur_el;
3068 next_tel:
3069 if (!qel)
3070 goto out;
3071
3072 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3073 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074 while (node) {
3075 struct quic_rx_packet *pkt;
3076
3077 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003078 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003079 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003080 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003081 /* Drop the packet */
3082 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003083 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003084 }
3085 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003086 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003087 /* Drop the packet */
3088 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003089 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003090 }
3091 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003092 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3093
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003094 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003095 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003096 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003097 if (pkt->pn > largest_pn)
3098 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003099 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003100 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003101 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003102 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003103 }
3104 }
3105 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003106 eb64_delete(&pkt->pn_node);
3107 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003109 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003111 /* Update the largest packet number. */
3112 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003113 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3114 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003115 goto err;
3116
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003117 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003118 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003119 qel = next_el;
3120 goto next_tel;
3121 }
3122
3123 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003124 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003125 return 1;
3126
3127 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003128 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003129 return 0;
3130}
3131
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003132/* Check if it's possible to remove header protection for packets related to
3133 * encryption level <qel>. If <qel> is NULL, assume it's false.
3134 *
3135 * Return true if the operation is possible else false.
3136 */
3137static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3138{
3139 enum quic_tls_enc_level tel;
3140
3141 if (!qel)
3142 return 0;
3143
3144 tel = ssl_to_quic_enc_level(qel->level);
3145
3146 /* check if tls secrets are available */
3147 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
3148 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
3149
3150 if (!(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET))
3151 return 0;
3152
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003153 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003154 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
3155 qc->mux_state != QC_MUX_READY)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003156 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003157
3158 return 1;
3159}
3160
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003161/* Sends application level packets from <qc> QUIC connection */
3162static int qc_send_app_pkts(struct quic_conn *qc)
3163{
3164 int ret;
3165 struct qring *qr;
3166
3167 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3168 if (!qr)
3169 /* Never happens */
3170 return 1;
3171
3172 ret = qc_prep_pkts(qc, qr, QUIC_TLS_ENC_LEVEL_APP, QUIC_TLS_ENC_LEVEL_NONE);
3173 if (ret == -1)
3174 goto err;
3175 else if (ret == 0)
3176 goto out;
3177
3178 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3179 goto err;
3180
3181 out:
3182 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3183 return 1;
3184
3185 err:
3186 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3187 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3188 return 0;
3189}
3190
3191/* QUIC connection packet handler task (post handshake) */
3192static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3193{
3194 struct ssl_sock_ctx *ctx;
3195 struct quic_conn *qc;
3196 struct quic_enc_level *qel;
3197 int st;
3198
3199
3200 ctx = context;
3201 qc = ctx->qc;
3202 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3203 st = HA_ATOMIC_LOAD(&qc->state);
3204
3205 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
3206
3207 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3208 qc_rm_hp_pkts(qc, qel);
3209
3210 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3211 goto err;
3212
3213 if (!qc_send_app_pkts(qc))
3214 goto err;
3215
3216 return t;
3217
3218 err:
3219 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st);
3220 return t;
3221}
3222
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003223/* QUIC connection packet handler task. */
3224struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003226 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003227 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003228 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003229 enum quic_tls_enc_level tel, next_tel;
3230 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003231 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003232 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003234 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003235 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003236 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003237 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003238 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003239 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003240 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3241 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3242 /* The I/O handler has been woken up by the dgram listener
3243 * after the anti-amplification was reached.
3244 */
3245 qc_set_timer(qc);
3246 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3247 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3248 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003249 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003250 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3251 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3252 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003253 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003254 if (st >= QUIC_HS_ST_COMPLETE &&
3255 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3256 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3257 /* There may be remaining Handshake packets to treat and acknowledge. */
3258 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3259 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3260 }
3261 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003262 goto err;
3263
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003264 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003265 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003266
3267 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003268 /* Treat packets waiting for header packet protection decryption */
3269 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003270 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003271
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003272 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3273 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3274 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003275 goto err;
3276
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003277 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3278 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3279 qel = next_qel;
3280 next_qel = NULL;
3281 goto next_level;
3282 }
3283
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003284 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003285 if (st >= QUIC_HS_ST_COMPLETE) {
3286 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3287 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003288 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003289
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003290 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3291 QUIC_FL_TLS_SECRETS_DCD)) {
3292 /* Discard the Handshake keys. */
3293 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3294 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3295 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3296 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003297 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003298 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003299 }
3300
3301 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3302 /* There may be remaining handshake to build (acks) */
3303 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3304 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003305 }
3306
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003307 if (!qr)
3308 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003309 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3310 * be considered.
3311 */
3312 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003313 goto err;
3314 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003315 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003316 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003317 else if (ret == 0)
3318 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003319
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003320 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321 goto err;
3322
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003323 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003324 /* Check if there is something to do for the next level.
3325 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003326 if (next_qel && next_qel != qel &&
3327 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003328 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003329 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003330 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003331 goto next_level;
3332 }
3333
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003334 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003335 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003336 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003337
3338 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003339 if (qr)
3340 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003341 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003342 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343}
3344
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003345/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003346static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3347{
3348 int i;
3349
3350 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3351 if (qel->tx.crypto.bufs[i]) {
3352 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3353 qel->tx.crypto.bufs[i] = NULL;
3354 }
3355 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003356 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003357}
3358
3359/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003360 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003361 * Returns 1 if succeeded, 0 if not.
3362 */
3363static int quic_conn_enc_level_init(struct quic_conn *qc,
3364 enum quic_tls_enc_level level)
3365{
3366 struct quic_enc_level *qel;
3367
3368 qel = &qc->els[level];
3369 qel->level = quic_to_ssl_enc_level(level);
3370 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3371 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3372 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3373 qel->tls_ctx.rx.flags = 0;
3374 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003375 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003376
3377 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003378 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003379 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003380 qel->rx.crypto.offset = 0;
3381 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003382
3383 /* Allocate only one buffer. */
3384 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3385 if (!qel->tx.crypto.bufs)
3386 goto err;
3387
3388 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3389 if (!qel->tx.crypto.bufs[0])
3390 goto err;
3391
3392 qel->tx.crypto.bufs[0]->sz = 0;
3393 qel->tx.crypto.nb_buf = 1;
3394
3395 qel->tx.crypto.sz = 0;
3396 qel->tx.crypto.offset = 0;
3397
3398 return 1;
3399
3400 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003401 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402 return 0;
3403}
3404
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003405/* Increment the <qc> refcount.
3406 *
3407 * This operation must be conducted when manipulating the quic_conn outside of
3408 * the connection pinned thread. These threads can only retrieve the connection
3409 * in the CID tree, so this function must be conducted under the CID lock.
3410 */
3411static inline void quic_conn_take(struct quic_conn *qc)
3412{
3413 HA_ATOMIC_INC(&qc->refcount);
3414}
3415
3416/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003417 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003418 */
3419static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003421 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003422 int i;
3423
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003424 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003425 return;
3426
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003427 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003428 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003429
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003430 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003431 if (conn_ctx) {
3432 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003433 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003434 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003435
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003437 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003438
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003439 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3440 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003441 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003442}
3443
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003444/* Release the quic_conn <qc>. It will decrement its refcount so that the
3445 * connection will be freed once all threads have finished to work with it. The
3446 * connection is removed from the CIDs tree and thus cannot be found by other
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003447 * threads after it. The connection tasklet is killed.
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003448 *
3449 * Do not use <qc> after it as it may be freed. This function must only be
3450 * called by the thread responsible of the quic_conn tasklet.
3451 */
3452static void quic_conn_release(struct quic_conn *qc)
3453{
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003454 struct ssl_sock_ctx *conn_ctx;
3455
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003456 /* remove the connection from receiver cids trees */
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003457 ebmb_delete(&qc->odcid_node);
3458 ebmb_delete(&qc->scid_node);
3459 free_quic_conn_cids(qc);
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003460
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003461 /* Kill the tasklet. Do not use tasklet_free as this is not thread safe
3462 * as other threads may call tasklet_wakeup after this.
3463 */
3464 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3465 if (conn_ctx)
3466 tasklet_kill(conn_ctx->wait_event.tasklet);
3467
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003468 quic_conn_drop(qc);
3469}
3470
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003471void quic_close(struct connection *conn, void *xprt_ctx)
3472{
3473 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003474 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003475
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003476 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003477 /* This task must be deleted by the connection-pinned thread. */
3478 if (qc->timer_task) {
3479 task_destroy(qc->timer_task);
3480 qc->timer_task = NULL;
3481 }
3482
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003483 /* Next application data can be dropped. */
3484 qc->mux_state = QC_MUX_RELEASED;
3485
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003486 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003487
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003488 /* TODO for now release the quic_conn on notification by the upper
3489 * layer. It could be useful to delay it if there is remaining data to
3490 * send or data to be acked.
3491 */
3492 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003493}
3494
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003495/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003496static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003497{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003498 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003499 struct quic_conn *qc;
3500 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003501 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003502
3503 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003504 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003505 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003506 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003507 task->expire = TICK_ETERNITY;
3508 pktns = quic_loss_pktns(qc);
3509 if (tick_isset(pktns->tx.loss_time)) {
3510 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3511
3512 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3513 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003514 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3515 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516 goto out;
3517 }
3518
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003519 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003520 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003521 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003522 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3523 pktns->tx.pto_probe = 1;
3524 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3525 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3526 }
3527 else {
3528 pktns->tx.pto_probe = 2;
3529 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003530 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003531 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003532 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3533 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3534
3535 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3536 hel->pktns->tx.pto_probe = 1;
3537 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3538 iel->pktns->tx.pto_probe = 1;
3539 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003540
3541 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003542 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3543 continue;
3544
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003545 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003546 }
3547
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3549 qc->path->loss.pto_count++;
3550
3551 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003552 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003553
3554 return task;
3555}
3556
3557/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3558 * connections used to identify the first Initial packets of client connecting
3559 * to listeners. This parameter must be NULL for QUIC connections attached
3560 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3561 * <scid> is the source connection ID with <scid_len> as length.
3562 * Returns 1 if succeeded, 0 if not.
3563 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003564static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003565 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003566 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003567{
3568 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003569 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003570 /* Initial CID. */
3571 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003572 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003573 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003574
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003575 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003576 qc = pool_zalloc(pool_head_quic_conn);
3577 if (!qc) {
3578 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3579 goto err;
3580 }
3581
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003582 HA_ATOMIC_STORE(&qc->refcount, 0);
3583
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003584 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3585 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003586 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003587 goto err;
3588 }
3589
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 qc->cids = EB_ROOT;
3591 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003592 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003593 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003594
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003595 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003596 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003597 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003598 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003599 qc->odcid.addrlen = dcid_addr_len;
3600 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003601
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003602 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003603 if (scid_len)
3604 memcpy(qc->dcid.data, scid, scid_len);
3605 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003606 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003607 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003608 }
3609 /* QUIC Client (outgoing connection to servers) */
3610 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003611 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612 if (dcid_len)
3613 memcpy(qc->dcid.data, dcid, dcid_len);
3614 qc->dcid.len = dcid_len;
3615 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003616 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003617
3618 /* Initialize the output buffer */
3619 qc->obuf.pos = qc->obuf.data;
3620
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003621 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003622 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003623 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003624 goto err;
3625 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003626
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003627 /* insert the allocated CID in the receiver datagram handler tree */
3628 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003629 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003630
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631 /* Select our SCID which is the first CID with 0 as sequence number. */
3632 qc->scid = icid->cid;
3633
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003634 /* Packet number spaces initialization. */
3635 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3636 quic_pktns_init(&qc->pktns[i]);
3637 /* QUIC encryption level context initialization. */
3638 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003639 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003640 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003641 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003642 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003643 /* Initialize the packet number space. */
3644 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3645 }
3646
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003647 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003648 qc->tps_tls_ext = qc->version & 0xff000000 ?
3649 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3650 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003651 /* TX part. */
3652 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3654 qc->tx.wbuf = qc->tx.rbuf = 0;
3655 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003656 /* RX part. */
3657 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003658 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003659 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003660 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003661 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003662 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003663 goto err;
3664 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003665
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666 /* XXX TO DO: Only one path at this time. */
3667 qc->path = &qc->paths[0];
3668 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3669
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003670 /* required to use MTLIST_IN_LIST */
3671 MT_LIST_INIT(&qc->accept_list);
3672
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003673 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003674
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003675 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676
3677 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003678 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003679 pool_free(pool_head_quic_conn_rxbuf, buf_area);
3680 if (qc)
3681 qc->rx.buf.area = NULL;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003682 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003683 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684}
3685
3686/* Initialize the timer task of <qc> QUIC connection.
3687 * Returns 1 if succeeded, 0 if not.
3688 */
3689static int quic_conn_init_timer(struct quic_conn *qc)
3690{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003691 /* Attach this task to the same thread ID used for the connection */
3692 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693 if (!qc->timer_task)
3694 return 0;
3695
3696 qc->timer = TICK_ETERNITY;
3697 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01003698 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003699
3700 return 1;
3701}
3702
3703/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3704 * past one byte of this buffer.
3705 */
3706static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3707 struct quic_rx_packet *pkt)
3708{
3709 unsigned char dcid_len, scid_len;
3710
3711 /* Version */
3712 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3713 return 0;
3714
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003715 /* Destination Connection ID Length */
3716 dcid_len = *(*buf)++;
3717 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3718 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3719 /* XXX MUST BE DROPPED */
3720 return 0;
3721
3722 if (dcid_len) {
3723 /* Check that the length of this received DCID matches the CID lengths
3724 * of our implementation for non Initials packets only.
3725 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003726 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3727 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003728 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003729 return 0;
3730
3731 memcpy(pkt->dcid.data, *buf, dcid_len);
3732 }
3733
3734 pkt->dcid.len = dcid_len;
3735 *buf += dcid_len;
3736
3737 /* Source Connection ID Length */
3738 scid_len = *(*buf)++;
3739 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3740 /* XXX MUST BE DROPPED */
3741 return 0;
3742
3743 if (scid_len)
3744 memcpy(pkt->scid.data, *buf, scid_len);
3745 pkt->scid.len = scid_len;
3746 *buf += scid_len;
3747
3748 return 1;
3749}
3750
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003751/* Insert <pkt> RX packet in its <qel> RX packets tree */
3752static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3753{
3754 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003755 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003756 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3757 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3758 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003759}
3760
3761/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003762 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3763 * byte past the end of the buffer containing this packet and <beg> the address of
3764 * the packet first byte.
3765 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3766 * Returns 1 if succeeded, 0 if not.
3767 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003768static inline int qc_try_rm_hp(struct quic_conn *qc,
3769 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003770 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003771 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003772 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003773{
3774 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003775 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003776 struct quic_enc_level *qel;
3777 /* Only for traces. */
3778 struct quic_rx_packet *qpkt_trace;
3779
3780 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003781 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782 /* The packet number is here. This is also the start minus
3783 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3784 * protection.
3785 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003786 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003787
3788 tel = quic_packet_type_enc_level(pkt->type);
3789 qel = &qc->els[tel];
3790
3791 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 /* Note that the following function enables us to unprotect the packet
3793 * number and its length subsequently used to decrypt the entire
3794 * packets.
3795 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003796 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3797 qel->pktns->rx.largest_pn, pn, beg, end)) {
3798 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003799 goto err;
3800 }
3801
3802 /* The AAD includes the packet number field found at <pn>. */
3803 pkt->aad_len = pn - beg + pkt->pnl;
3804 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003805 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003806 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003807 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3808 /* If the packet number space has been discarded, this packet
3809 * will be not parsed.
3810 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003811 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003812 goto out;
3813 }
3814
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003815 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003817 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3818 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003819 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003820 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003821 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003822 goto err;
3823 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003824
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003825 *el = qel;
3826 /* No reference counter incrementation here!!! */
3827 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3828 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3829 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3830 b_add(&qc->rx.buf, pkt->len);
3831 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003832 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003833 return 1;
3834
3835 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003836 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003837 return 0;
3838}
3839
3840/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3841 * Also set <*long_header> to 1 if this form is long, 0 if not.
3842 */
3843static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3844 unsigned char byte0, int *long_header)
3845{
3846 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3847 pkt->type =
3848 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3849 *long_header = 1;
3850 }
3851 else {
3852 pkt->type = QUIC_PACKET_TYPE_SHORT;
3853 *long_header = 0;
3854 }
3855}
3856
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003857/*
3858 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3859 */
3860static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3861{
3862 int j = 0, version;
3863
3864 do {
3865 version = quic_supported_version[j];
3866 if (version == pkt->version)
3867 return 1;
3868
3869 version = quic_supported_version[++j];
3870 } while(version);
3871
3872 return 0;
3873}
3874
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003875/*
3876 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3877 * address <addr>.
3878 * Implementation of RFC9000 6. Version Negotiation
3879 *
3880 * TODO implement a rate-limiting sending of Version Negotiation packets
3881 *
3882 * Returns 0 on success else non-zero
3883 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003884static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3885 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003886{
3887 char buf[256];
3888 int i = 0, j, version;
3889 const socklen_t addrlen = get_addr_len(addr);
3890
3891 /*
3892 * header form
3893 * long header, fixed bit to 0 for Version Negotiation
3894 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003895 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3896 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003897
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003898 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003899 /* null version for Version Negotiation */
3900 buf[i++] = '\x00';
3901 buf[i++] = '\x00';
3902 buf[i++] = '\x00';
3903 buf[i++] = '\x00';
3904
3905 /* source connection id */
3906 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003907 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003908 i += pkt->scid.len;
3909
3910 /* destination connection id */
3911 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003912 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003913 i += pkt->dcid.len;
3914
3915 /* supported version */
3916 j = 0;
3917 do {
3918 version = htonl(quic_supported_version[j]);
3919 memcpy(&buf[i], &version, sizeof(version));
3920 i += sizeof(version);
3921
3922 version = quic_supported_version[++j];
3923 } while (version);
3924
3925 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3926 return 1;
3927
3928 return 0;
3929}
3930
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01003931/* Generate the token to be used in Retry packets. The token is written to
3932 * <buf> which is expected to be <len> bytes.
3933 *
3934 * Various parameters are expected to be encoded in the token. For now, only
3935 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
3936 * as this CID must be repeated by the server in the transport parameters.
3937 *
3938 * TODO add the client address to validate the token origin.
3939 *
3940 * Returns the length of the encoded token or 0 on error.
3941 */
3942static int generate_retry_token(unsigned char *buf, unsigned char len,
3943 struct quic_rx_packet *pkt)
3944{
3945 const size_t token_len = 1 + pkt->dcid.len;
3946 unsigned char i = 0;
3947
3948 if (token_len > len)
3949 return 0;
3950
3951 buf[i++] = pkt->dcid.len;
3952 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
3953 i += pkt->dcid.len;
3954
3955 return i;
3956}
3957
3958/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
3959 * the Initial <pkt> packet.
3960 *
3961 * Returns 0 on success else non-zero.
3962 */
3963static int send_retry(int fd, struct sockaddr_storage *addr,
3964 struct quic_rx_packet *pkt)
3965{
3966 unsigned char buf[128];
3967 int i = 0, token_len;
3968 const socklen_t addrlen = get_addr_len(addr);
3969 struct quic_cid scid;
3970
3971 /* long header + fixed bit + packet type 0x3 */
3972 buf[i++] = 0xf0;
3973 /* version */
3974 buf[i++] = 0x00;
3975 buf[i++] = 0x00;
3976 buf[i++] = 0x00;
3977 buf[i++] = 0x01;
3978
3979 /* Use the SCID from <pkt> for Retry DCID. */
3980 buf[i++] = pkt->scid.len;
3981 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
3982 i += pkt->scid.len;
3983
3984 /* Generate a new CID to be used as SCID for the Retry packet. */
3985 scid.len = QUIC_HAP_CID_LEN;
3986 if (RAND_bytes(scid.data, scid.len) != 1)
3987 return 1;
3988
3989 buf[i++] = scid.len;
3990 memcpy(&buf[i], scid.data, scid.len);
3991 i += scid.len;
3992
3993 /* token */
3994 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
3995 return 1;
3996 i += token_len;
3997
3998 /* token integrity tag */
3999 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4000 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4001 pkt->dcid.len, buf, i)) {
4002 return 1;
4003 }
4004
4005 i += QUIC_TLS_TAG_LEN;
4006
4007 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4008 return 1;
4009
4010 return 0;
4011}
4012
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004013/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4014 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4015 * concatenated to the <pkt> DCID field.
4016 *
4017 * Returns the instance or NULL if not found.
4018 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004019static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004020 struct listener *l,
4021 struct sockaddr_storage *saddr)
4022{
4023 struct quic_conn *qc = NULL;
4024 struct ebmb_node *node;
4025 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004026 /* set if the quic_conn is found in the second DCID tree */
4027 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004028
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004029 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4030 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4031 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4032 /* DCIDs of first packets coming from multiple clients may have
4033 * the same values. Let's distinguish them by concatenating the
4034 * socket addresses.
4035 */
4036 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004037 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004038 pkt->dcid.len + pkt->dcid.addrlen);
4039 if (node) {
4040 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4041 goto end;
4042 }
4043 }
4044
4045 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4046 * also for INITIAL/0-RTT non-first packets with the final DCID in
4047 * used.
4048 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004049 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004050 if (!node)
4051 goto end;
4052
4053 id = ebmb_entry(node, struct quic_connection_id, node);
4054 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004055 found_in_dcid = 1;
4056
4057 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004058 if (qc)
4059 quic_conn_take(qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004060
4061 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4062 * If already done, this is a noop.
4063 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004064 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004065 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004066
4067 return qc;
4068}
4069
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004070/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4071 * will extract the parameters stored in the token : <odcid>.
4072 *
4073 * Returns 0 on success else non-zero.
4074 */
4075static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4076 struct quic_cid *odcid)
4077{
4078 uint64_t odcid_len;
4079
4080 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4081 return 1;
4082
4083 memcpy(odcid->data, token, odcid_len);
4084 odcid->len = odcid_len;
4085
4086 return 0;
4087}
4088
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004089/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4090 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4091 * parameters of this session.
4092 * This is the responsibility of the caller to check the validity of all the
4093 * pointers passed as parameter to this function.
4094 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4095 * CO_ER_SSL_NO_MEM.
4096 */
4097static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4098 unsigned char *params, size_t params_len)
4099{
4100 int retry;
4101
4102 retry = 1;
4103 retry:
4104 *ssl = SSL_new(ssl_ctx);
4105 if (!*ssl) {
4106 if (!retry--)
4107 goto err;
4108
4109 pool_gc(NULL);
4110 goto retry;
4111 }
4112
4113 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4114 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4115 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004116 SSL_free(*ssl);
4117 *ssl = NULL;
4118 if (!retry--)
4119 goto err;
4120
4121 pool_gc(NULL);
4122 goto retry;
4123 }
4124
4125 return 0;
4126
4127 err:
4128 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4129 return -1;
4130}
4131
4132/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4133 * used to process <qc> received packets. The allocated context is stored in
4134 * <qc.xprt_ctx>.
4135 *
4136 * Returns 0 on success else non-zero.
4137 */
4138int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4139{
4140 struct bind_conf *bc = qc->li->bind_conf;
4141 struct ssl_sock_ctx *ctx = NULL;
4142
4143 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4144 if (!ctx)
4145 goto err;
4146
4147 ctx->wait_event.tasklet = tasklet_new();
4148 if (!ctx->wait_event.tasklet)
4149 goto err;
4150
4151 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4152 ctx->wait_event.tasklet->context = ctx;
4153 ctx->wait_event.events = 0;
4154 ctx->subs = NULL;
4155 ctx->xprt_ctx = NULL;
4156 ctx->qc = qc;
4157
4158 /* Set tasklet tid based on the SCID selected by us for this
4159 * connection. The upper layer will also be binded on the same thread.
4160 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004161 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004162
4163 if (qc_is_listener(qc)) {
4164 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4165 qc->enc_params, qc->enc_params_len) == -1) {
4166 goto err;
4167 }
4168
4169 /* Enabling 0-RTT */
4170 if (bc->ssl_conf.early_data)
4171 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4172
4173 SSL_set_accept_state(ctx->ssl);
4174 }
4175
4176 ctx->xprt = xprt_get(XPRT_QUIC);
4177
4178 /* Store the allocated context in <qc>. */
4179 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4180
4181 return 0;
4182
4183 err:
4184 if (ctx && ctx->wait_event.tasklet)
4185 tasklet_free(ctx->wait_event.tasklet);
4186 pool_free(pool_head_quic_conn_ctx, ctx);
4187
4188 return 1;
4189}
4190
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004191static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004192 struct quic_rx_packet *pkt, int first_pkt,
4193 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004195 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004196 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004198 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004199 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004200 size_t b_cspace;
4201 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004202
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004203 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004204 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004205 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004206 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004207 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4208 /* This ist only to please to traces and distinguish the
4209 * packet with parsed packet number from others.
4210 */
4211 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004212 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004213 goto err;
4214
4215 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004216 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004217 /* XXX TO BE DISCARDED */
4218 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4219 goto err;
4220 }
4221
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004222 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004223 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004224 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004226 uint64_t len;
4227
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004228 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4230 goto err;
4231 }
4232
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004233 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4234 * they must have the same DCID.
4235 */
4236 if (!first_pkt &&
4237 (pkt->dcid.len != dgram->dcid_len ||
4238 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4239 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4240 goto err;
4241 }
4242
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004243 /* Retry of Version Negotiation packets are only sent by servers */
4244 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4245 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4246 goto err;
4247 }
4248
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004249 /* RFC9000 6. Version Negotiation */
4250 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004251 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004252 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004253 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4254 goto err;
4255 }
4256
4257 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004258 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004259 }
4260
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004261 /* For Initial packets, and for servers (QUIC clients connections),
4262 * there is no Initial connection IDs storage.
4263 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004264 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004265 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004266
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004267 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4268 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004269 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4270 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004271 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004272
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004273 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4274 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004275 */
4276 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004277
4278 /* TODO Retry should be automatically activated if
4279 * suspect network usage is detected.
4280 */
4281 if (!token_len && l->bind_conf->quic_force_retry) {
4282 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004283 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004284 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4285 goto err;
4286 }
4287
4288 goto err;
4289 }
4290 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004291 pkt->token = buf;
4292 buf += pkt->token_len;
4293 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004294 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004295 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004296 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004297 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4298 goto err;
4299 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004300 }
4301
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004302 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4303 end - buf < len) {
4304 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4305 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004306 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004307
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004308 payload = buf;
4309 pkt->len = len + payload - beg;
4310
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004311 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004312 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004313 int ipv4;
4314 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004315 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004316 const unsigned char *salt = initial_salt_v1;
4317 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004318
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004319 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004320 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004321 goto err;
4322 }
4323
Frédéric Lécailledc364042022-01-27 16:51:54 +01004324 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4325 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4326 goto err;
4327 }
4328
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004329 pkt->saddr = dgram->saddr;
4330 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004331 qc = qc_new_conn(pkt->version, ipv4,
4332 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004333 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004334 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004335 goto err;
4336
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004337 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4338
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004339 odcid = &qc->rx.params.original_destination_connection_id;
4340 /* Copy the transport parameters. */
4341 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004342
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004343 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004344 if (pkt->token_len) {
4345 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4346 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4347 goto err;
4348 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004349 /* Copy retry_source_connection_id transport parameter. */
4350 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4351 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004352 }
4353 else {
4354 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4355 odcid->len = pkt->dcid.len;
4356 }
4357
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004358 /* Copy the initial source connection ID. */
4359 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4360 qc->enc_params_len =
4361 quic_transport_params_encode(qc->enc_params,
4362 qc->enc_params + sizeof qc->enc_params,
4363 &qc->rx.params, 1);
4364 if (!qc->enc_params_len)
4365 goto err;
4366
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004367 if (qc_conn_alloc_ssl_ctx(qc))
4368 goto err;
4369
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004370 if (!quic_conn_init_timer(qc))
4371 goto err;
4372
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004373 /* NOTE: the socket address has been concatenated to the destination ID
4374 * chosen by the client for Initial packets.
4375 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004376 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4377 salt = initial_salt_draft_29;
4378 salt_len = sizeof initial_salt_draft_29;
4379 }
4380 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004381 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004382 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004383 goto err;
4384 }
4385
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004386 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004387 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004388 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004389
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004390 /* If the insertion failed, it means that another
4391 * thread has already allocated a QUIC connection for
4392 * the same CID. Liberate our allocated connection.
4393 */
4394 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004395 qc_to_purge = qc;
4396
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004397 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4398 pkt->qc = qc;
4399 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004400
4401 quic_conn_take(qc);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004402
4403 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004404 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004405 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004406 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004407 else {
4408 quic_conn_drop(qc_to_purge);
4409 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410 }
4411 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004412 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004413 }
4414 }
4415 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004416 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004417 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4418 goto err;
4419 }
4420
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004421 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004422 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004423
4424 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4425 * they must have the same DCID.
4426 */
4427 if (!first_pkt &&
4428 (pkt->dcid.len != dgram->dcid_len ||
4429 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4430 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4431 goto err;
4432 }
4433
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004434 buf += QUIC_HAP_CID_LEN;
4435
4436 /* A short packet is the last one of a UDP datagram. */
4437 payload = buf;
4438 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004439
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004440 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004441 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004442 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004443 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4444 goto err;
4445 }
4446
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004447 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004448 }
4449
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004450
4451 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4452 * they must have the same DCID.
4453 *
4454 * This check must be done after the final update to pkt.len to
4455 * properly drop the packet on failure.
4456 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004457 if (first_pkt && !quic_peer_validated_addr(qc) &&
4458 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4459 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4460 QUIC_EV_CONN_LPKT, qc);
4461 /* Reset the anti-amplification bit. It will be set again
4462 * when sending the next packet if reached again.
4463 */
4464 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4465 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4466 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004468
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004469 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004470
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004471 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004472 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004473 goto out;
4474 }
4475
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004476 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004477 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004478 b_cspace = b_contig_space(&qc->rx.buf);
4479 if (b_cspace < pkt->len) {
4480 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004481 if (b_cspace) {
4482 b_putchr(&qc->rx.buf, 0x00);
4483 b_cspace--;
4484 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004485 b_add(&qc->rx.buf, b_cspace);
4486 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004487 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004488 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004489 goto err;
4490 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491 }
4492
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004493 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004494 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004495 goto err;
4496 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004497
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004498 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004499 if (pkt->aad_len)
4500 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004501 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004502 /* Wake up the connection packet handler task from here only if all
4503 * the contexts have been initialized, especially the mux context
4504 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4505 * will start it if these contexts for the connection are not already
4506 * initialized.
4507 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004508 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004509 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004511
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004512 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004513
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004514 if (qc)
4515 quic_conn_drop(qc);
4516
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004517 return pkt->len;
4518
4519 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004520 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4521 * This cannot be done by this thread.
4522 */
4523 if (io_cb_wakeup) {
4524 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4525 if (conn_ctx && conn_ctx->wait_event.tasklet)
4526 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4527 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004528 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004529 if (!pkt->len)
4530 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004531 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004532 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004533
4534 if (qc)
4535 quic_conn_drop(qc);
4536
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004537 return -1;
4538}
4539
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004540/* This function builds into <buf> buffer a QUIC long packet header.
4541 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004542 */
4543static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4544 int type, size_t pn_len, struct quic_conn *conn)
4545{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004546 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004547 return 0;
4548
4549 /* #0 byte flags */
4550 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4551 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4552 /* Version */
4553 quic_write_uint32(buf, end, conn->version);
4554 *(*buf)++ = conn->dcid.len;
4555 /* Destination connection ID */
4556 if (conn->dcid.len) {
4557 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4558 *buf += conn->dcid.len;
4559 }
4560 /* Source connection ID */
4561 *(*buf)++ = conn->scid.len;
4562 if (conn->scid.len) {
4563 memcpy(*buf, conn->scid.data, conn->scid.len);
4564 *buf += conn->scid.len;
4565 }
4566
4567 return 1;
4568}
4569
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004570/* This function builds into <buf> buffer a QUIC short packet header.
4571 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004572 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004573static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4574 size_t pn_len, struct quic_conn *conn,
4575 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004576{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004577 if (end - *buf < 1 + conn->dcid.len)
4578 return 0;
4579
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004580 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004581 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4582 ((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 +01004583 /* Destination connection ID */
4584 if (conn->dcid.len) {
4585 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4586 *buf += conn->dcid.len;
4587 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004588
4589 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004590}
4591
4592/* Apply QUIC header protection to the packet with <buf> as first byte address,
4593 * <pn> as address of the Packet number field, <pnlen> being this field length
4594 * with <aead> as AEAD cipher and <key> as secret key.
4595 * Returns 1 if succeeded or 0 if failed.
4596 */
4597static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4598 const EVP_CIPHER *aead, const unsigned char *key)
4599{
4600 int i, ret, outlen;
4601 EVP_CIPHER_CTX *ctx;
4602 /* We need an IV of at least 5 bytes: one byte for bytes #0
4603 * and at most 4 bytes for the packet number
4604 */
4605 unsigned char mask[5] = {0};
4606
4607 ret = 0;
4608 ctx = EVP_CIPHER_CTX_new();
4609 if (!ctx)
4610 return 0;
4611
4612 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4613 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4614 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4615 goto out;
4616
4617 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4618 for (i = 0; i < pnlen; i++)
4619 pn[i] ^= mask[i + 1];
4620
4621 ret = 1;
4622
4623 out:
4624 EVP_CIPHER_CTX_free(ctx);
4625
4626 return ret;
4627}
4628
4629/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4630 * ACK ranges if needed to a value below <limit> in bytes.
4631 * Return 1 if succeeded, 0 if not.
4632 */
4633static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4634{
4635 size_t room, ack_delay_sz;
4636
4637 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4638 /* A frame is made of 1 byte for the frame type. */
4639 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004640 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004641 return 0;
4642
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004643 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004644}
4645
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004646/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4647 * 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 +01004648 * and <*len> the packet Length field initialized with the number of bytes already present
4649 * 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 +02004650 * <headlen> is the number of bytes already present in this packet before building frames.
4651 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004652 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004653 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004654 * Return 1 if succeeded, 0 if not.
4655 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004656static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004657 size_t room, size_t *len, size_t headlen,
4658 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004659 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004661 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004662 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004663
Frédéric Lécailleea604992020-12-24 13:01:37 +01004664 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004665 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004666 return 0;
4667
Frédéric Lécailleea604992020-12-24 13:01:37 +01004668 /* If we are not probing we must take into an account the congestion
4669 * control window.
4670 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004671 if (!qel->pktns->tx.pto_probe) {
4672 size_t remain = quic_path_prep_data(qc->path);
4673
4674 if (headlen > remain)
4675 return 0;
4676
4677 room = QUIC_MIN(room, remain - headlen);
4678 }
4679
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004680 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004681 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004682 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004683 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004684 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004685
Frédéric Lécailleea604992020-12-24 13:01:37 +01004686 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004687 break;
4688
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004689 switch (cf->type) {
4690 case QUIC_FT_CRYPTO:
4691 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004692 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004693 /* Compute the length of this CRYPTO frame header */
4694 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4695 /* Compute the data length of this CRyPTO frame. */
4696 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4697 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004698 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004699 if (!dlen)
4700 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004701
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004702 /* CRYPTO frame length. */
4703 flen = hlen + quic_int_getsize(dlen) + dlen;
4704 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004705 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004706 /* Add the CRYPTO data length and its encoded length to the packet
4707 * length and the length of this length.
4708 */
4709 *len += flen;
4710 room -= flen;
4711 if (dlen == cf->crypto.len) {
4712 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004713 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004714 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004715 }
4716 else {
4717 struct quic_frame *new_cf;
4718
4719 new_cf = pool_alloc(pool_head_quic_frame);
4720 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004721 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004722 return 0;
4723 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004725 new_cf->type = QUIC_FT_CRYPTO;
4726 new_cf->crypto.len = dlen;
4727 new_cf->crypto.offset = cf->crypto.offset;
4728 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004729 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004730 /* Consume <dlen> bytes of the current frame. */
4731 cf->crypto.len -= dlen;
4732 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004733 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004734 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004735
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004736 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004737 /* Note that these frames are accepted in short packets only without
4738 * "Length" packet field. Here, <*len> is used only to compute the
4739 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004740 *
4741 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004742 * excepting the variable ones. Note that +1 is for the type of this frame.
4743 */
4744 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004745 ((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 +02004746 /* Compute the data length of this STREAM frame. */
4747 avail_room = room - hlen - *len;
4748 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004749 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004750
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004751 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004752 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004753 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4754 dlen = max_available_room(avail_room, &dlen_sz);
4755 if (dlen > cf->stream.len) {
4756 dlen = cf->stream.len;
4757 }
4758 dlen_sz = quic_int_getsize(dlen);
4759 flen = hlen + dlen_sz + dlen;
4760 }
4761 else {
4762 dlen = QUIC_MIN(avail_room, cf->stream.len);
4763 flen = hlen + dlen;
4764 }
4765 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004766 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004767 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004768 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004769 /* Add the STREAM data length and its encoded length to the packet
4770 * length and the length of this length.
4771 */
4772 *len += flen;
4773 room -= flen;
4774 if (dlen == cf->stream.len) {
4775 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004776 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004777 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004778 }
4779 else {
4780 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004781 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004782
4783 new_cf = pool_zalloc(pool_head_quic_frame);
4784 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004785 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004786 return 0;
4787 }
4788
4789 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004790 new_cf->stream.qcs = cf->stream.qcs;
4791 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004792 new_cf->stream.id = cf->stream.id;
4793 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4794 new_cf->stream.offset = cf->stream.offset;
4795 new_cf->stream.len = dlen;
4796 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4797 /* FIN bit reset */
4798 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4799 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004800 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004801 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4802 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004803 cf_buf = b_make(b_orig(cf->stream.buf),
4804 b_size(cf->stream.buf),
4805 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004806 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004807 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004808 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004809 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004810 break;
4811
4812 default:
4813 flen = qc_frm_len(cf);
4814 BUG_ON(!flen);
4815 if (flen > room)
4816 continue;
4817
4818 *len += flen;
4819 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004820 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004821 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004822 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004823 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004824 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004825 }
4826
Frédéric Lécailleea604992020-12-24 13:01:37 +01004827 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004828}
4829
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004830/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004831 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4832 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4833 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004834 * 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 +02004835 * 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 +02004836 * having returned from this function.
4837 * 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 +01004838 * number field in this packet. <pn_len> will also have the packet number
4839 * length as value.
4840 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004841 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004842 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004843static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4844 size_t dglen, struct quic_tx_packet *pkt,
4845 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004846 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004847 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004848{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004849 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004850 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004851 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4852 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004853 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004854 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004855 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004856 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004857 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004858
Frédéric Lécailleea604992020-12-24 13:01:37 +01004859 /* Length field value with CRYPTO frames if present. */
4860 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004861 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004862 /* When not probing and not acking, and no immediate close is required,
4863 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004864 * the congestion controller window. So, we do not limit the size of this
4865 * packet if we have an ACK frame to send because an ACK frame is not
4866 * ack-eliciting. This size will be limited if we have ack-eliciting
4867 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004868 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004869 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004870 size_t path_room;
4871
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004872 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004873 if (end - beg > path_room)
4874 end = beg + path_room;
4875 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004876
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004877 /* Ensure there is enough room for the TLS encryption tag and a zero token
4878 * length field if any.
4879 */
4880 if (end - pos < QUIC_TLS_TAG_LEN +
4881 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4882 goto no_room;
4883
4884 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004885 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004886 /* packet number length */
4887 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004888 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004889 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004890 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004891 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004892 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004893 goto no_room;
4894
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004895 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4896 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004897 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004898 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004899 /* Build an ACK frame if required. */
4900 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004901 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004902 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004903 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004904 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4905 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4906 * that from here, we do not know if we will have to send a PING frame.
4907 * This will be decided after having computed the ack-eliciting frames
4908 * to be added to this packet.
4909 */
4910 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 +01004911 if (!ack_frm_len)
4912 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004913 }
4914
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004915 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004916 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004917 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004918 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004919 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004920
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004921 /* Initialize the length of the frames built below to <len>.
4922 * If any frame could be successfully built by qc_build_frms(),
4923 * we will have len_frms > len.
4924 */
4925 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004926 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004927 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004928 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004929 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004930 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004931
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004932 /* Length (of the remaining data). Must not fail because, the buffer size
4933 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4934 * for the encryption tag. It must be taken into an account for the length
4935 * of this packet.
4936 */
4937 if (len_frms)
4938 len = len_frms + QUIC_TLS_TAG_LEN;
4939 else
4940 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004941 /* CONNECTION_CLOSE frame */
4942 if (cc) {
4943 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004944
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004945 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004946 len += qc_frm_len(&cc_frm);
4947 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004948 add_ping_frm = 0;
4949 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004950 len_sz = quic_int_getsize(len);
4951 /* Add this packet size to <dglen> */
4952 dglen += head_len + len_sz + len;
4953 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4954 /* This is a maximum padding size */
4955 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4956 /* The length field value is of this packet is <len> + <padding_len>
4957 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004958 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004959 * sizes from <padding_len>.
4960 */
4961 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4962 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004963 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004964 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004965 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004966 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004967 add_ping_frm = 1;
4968 len += 1;
4969 }
4970 /* 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 +01004971 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004972 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4973 }
4974
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004975 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4976 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004977
4978 /* Packet number field address. */
4979 *buf_pn = pos;
4980
4981 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004982 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4983 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004984
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004985 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004986 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004987
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004988 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004989 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004990 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004991
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004992 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004993 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004994 ssize_t room = end - pos;
4995 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004996 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004997 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004998 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004999 }
5000 }
5001
5002 /* Build a PING frame if needed. */
5003 if (add_ping_frm) {
5004 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005005 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005006 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005007 }
5008
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005009 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005010 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005011 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005012
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005013 /* Build a PADDING frame if needed. */
5014 if (padding_len) {
5015 frm.type = QUIC_FT_PADDING;
5016 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005017 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005018 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005019 }
5020
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005021 /* If this packet is ack-eliciting and we are probing let's
5022 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005023 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005024 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5025 qel->pktns->tx.pto_probe)
5026 qel->pktns->tx.pto_probe--;
5027
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005028 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005029 LIST_SPLICE(&pkt->frms, &frm_list);
5030 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005031
5032 return 1;
5033
5034 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005035 /* Replace the pre-built frames which could not be add to this packet */
5036 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005037 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005038 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005039}
5040
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005041static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005042{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005043 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005044 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005045 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005046 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005047 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005048 pkt->next = NULL;
5049 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005050}
5051
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005052/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005053static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5054{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005055 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005056
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005057 if (!pkt)
5058 return;
5059
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005060 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005061 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005062 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005063 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005064 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005065}
5066
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005067/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5068 * type for <qc> QUIC connection from <qel> encryption level.
5069 * Return -2 if the packet could not be allocated or encrypted for any reason,
5070 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005071 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005072static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5073 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005074 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005075 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005076 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005077{
5078 /* The pointer to the packet number field. */
5079 unsigned char *buf_pn;
5080 unsigned char *beg, *end, *payload;
5081 int64_t pn;
5082 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005083 struct quic_tls_ctx *tls_ctx;
5084 struct quic_tx_packet *pkt;
5085
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005086 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005087 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005088 pkt = pool_alloc(pool_head_quic_tx_packet);
5089 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005090 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005091 *err = -2;
5092 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005093 }
5094
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005095 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005096 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005097 pn_len = 0;
5098 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005099
5100 pn = qel->pktns->tx.next_pn + 1;
5101 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 +01005102 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005103 *err = -1;
5104 goto err;
5105 }
5106
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005107 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005108 payload = buf_pn + pn_len;
5109 payload_len = end - payload;
5110 aad_len = payload - beg;
5111
5112 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005113 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005114 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005115 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005116 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005117
5118 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005119 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005120 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5121 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005122 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005123 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005124 goto err;
5125 }
5126
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005127 /* Consume a packet number */
5128 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005129 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005130 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5131 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005132 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5133 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005135 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005137 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005138 pkt->in_flight_len = pkt->len;
5139 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005140 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005141 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005142 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005143
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005144 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145
5146 err:
5147 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005148 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005149 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005150}
5151
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005152
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005153/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5154 * event subscriber <es> is not allowed to change from a previous call as long
5155 * as at least one event is still subscribed. The <event_type> must only be a
5156 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5157 */
5158static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5159{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005160 struct qcc *qcc = conn->qc->qcc;
5161
5162 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5163 BUG_ON(qcc->subs && qcc->subs != es);
5164
5165 es->events |= event_type;
5166 qcc->subs = es;
5167
5168 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005169 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005170
5171 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005172 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005173
5174 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005175}
5176
5177/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5178 * The <es> pointer is not allowed to differ from the one passed to the
5179 * subscribe() call. It always returns zero.
5180 */
5181static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5182{
5183 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5184}
5185
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005186/* Store in <xprt_ctx> the context attached to <conn>.
5187 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005188 */
5189static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5190{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005191 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005192
5193 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5194
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005195 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005196 if (*xprt_ctx)
5197 goto out;
5198
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005199 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005200
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005201 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005202 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005203
5204 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005205}
5206
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005207/* Start the QUIC transport layer */
5208static int qc_xprt_start(struct connection *conn, void *ctx)
5209{
5210 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005211 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005212
5213 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005214 quic_mux_transport_params_update(qc->qcc);
5215 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5216 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5217 return 0;
5218 }
5219
5220 /* mux-quic can now be considered ready. */
5221 qc->mux_state = QC_MUX_READY;
5222
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005223 tasklet_wakeup(qctx->wait_event.tasklet);
5224 return 1;
5225}
5226
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005227/* transport-layer operations for QUIC connections. */
5228static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005229 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005230 .subscribe = quic_conn_subscribe,
5231 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005232 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005233 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005234 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5235 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005236 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237 .name = "QUIC",
5238};
5239
5240__attribute__((constructor))
5241static void __quic_conn_init(void)
5242{
5243 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5244 xprt_register(XPRT_QUIC, &ssl_quic);
5245}
5246
5247__attribute__((destructor))
5248static void __quic_conn_deinit(void)
5249{
5250 BIO_meth_free(ha_quic_meth);
5251}
5252
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005253/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5254 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005255 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005256 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005257struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005258{
5259 unsigned char *pos;
5260 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005261 struct quic_dghdlr *dghdlr = ctx;
5262 struct quic_dgram *dgram;
5263 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005264
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005265 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005266 pos = dgram->buf;
5267 end = pos + dgram->len;
5268 do {
5269 int ret;
5270 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005271
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005272 pkt = pool_zalloc(pool_head_quic_rx_packet);
5273 if (!pkt)
5274 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005275
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005276 quic_rx_packet_refinc(pkt);
5277 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5278 first_pkt = 0;
5279 pos += pkt->len;
5280 quic_rx_packet_refdec(pkt);
5281 if (ret == -1)
5282 /* If the packet length could not be found, we cannot continue. */
5283 break;
5284 } while (pos < end);
5285
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005286 /* Increasing the received bytes counter by the UDP datagram length
5287 * if this datagram could be associated to a connection.
5288 */
5289 if (dgram->qc)
5290 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005291
5292 /* Mark this datagram as consumed */
5293 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005294 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005295
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005296 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005297
5298 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005299 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005300}
5301
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005302/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5303 * Returns 1 if succeeded, 0 if not.
5304 */
5305static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5306 unsigned char **dcid, size_t *dcid_len)
5307{
5308 int long_header;
5309 size_t minlen, skip;
5310
5311 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5312 goto err;
5313
5314 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5315 minlen = long_header ?
5316 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5317 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005318 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005319 goto err;
5320
5321 buf += skip;
5322 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5323 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5324 goto err;
5325
5326 *dcid = buf;
5327
5328 return 1;
5329
5330 err:
5331 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5332 return 0;
5333}
5334
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005335/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5336 * correct datagram handler.
5337 * Return 1 if a correct datagram could be found, 0 if not.
5338 */
5339int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5340 struct sockaddr_storage *saddr,
5341 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005342{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005343 struct quic_dgram *dgram;
5344 unsigned char *dcid;
5345 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005346 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005347
5348 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005349 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005350
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005351 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005352 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005353 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005354
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005355 cid_tid = quic_get_cid_tid(dcid);
5356
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005357 /* All the members must be initialized! */
5358 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005359 dgram->buf = buf;
5360 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005361 dgram->dcid = dcid;
5362 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005363 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005364 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005365 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005366 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005367
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005368 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005369
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005370 return 1;
5371
5372 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005373 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005374}
5375
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005376/* Function to automatically activate QUIC traces on stdout.
5377 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5378 * Main use for now is in the docker image for QUIC interop testing.
5379 */
5380static void quic_init_stdout_traces(void)
5381{
5382#ifdef ENABLE_QUIC_STDOUT_TRACES
5383 trace_quic.sink = sink_find("stdout");
5384 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005385 trace_quic.state = TRACE_STATE_RUNNING;
5386#endif
5387}
5388INITCALL0(STG_INIT, quic_init_stdout_traces);
5389
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005390/*
5391 * Local variables:
5392 * c-indent-level: 8
5393 * c-basic-offset: 8
5394 * End:
5395 */