blob: f76634f5b1a422a0385e7b901cab786846f83c04 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005 *
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écaille28c7ea32022-02-25 17:41:39 +0100173 struct quic_enc_level *qel, struct list *frms,
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) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100209 const struct quic_tls_ctx *tls_ctx;
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
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100239 tls_ctx = &qc->els[level].tls_ctx;
240 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100241 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);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100244 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
246 if (tx_sec)
247 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100248 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100249 }
250 }
251 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
252 const enum ssl_encryption_level_t *level = a2;
253 const unsigned char *secret = a3;
254 const size_t *secret_len = a4;
255
256 if (level) {
257 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
258
259 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
260 if (secret && secret_len)
261 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100262 tls_ctx = &qc->els[lvl].tls_ctx;
263 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
264 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100265 }
266 }
267
268 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
269 const enum ssl_encryption_level_t *level = a2;
270 const unsigned char *secret = a3;
271 const size_t *secret_len = a4;
272
273 if (level) {
274 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
275
276 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
277 if (secret && secret_len)
278 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100279 tls_ctx = &qc->els[lvl].tls_ctx;
280 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
281 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100282 }
283
284 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100286 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287 const struct quic_tx_packet *pkt = a2;
288 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100289 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290
291 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100292 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100293 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100294 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100295 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100296 (unsigned long long)qc->path->cwnd,
297 (unsigned long long)qc->path->prep_in_flight,
298 (unsigned long long)qc->path->in_flight,
299 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100300 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100301 }
302 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200303 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100304 if (pkt->pn_node.key != (uint64_t)-1)
305 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100306 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100307 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100308 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
309 (unsigned long long)qc->rx.bytes,
310 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 }
312
313 if (room) {
314 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
315 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
316 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100317 }
318 }
319
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100320 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100321 const enum quic_handshake_state *state = a2;
322 const int *err = a3;
323
324 if (state)
325 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
326 if (err)
327 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
328 }
329
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100330 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 const struct quic_rx_packet *pkt = a2;
332 const unsigned long *pktlen = a3;
333 const SSL *ssl = a4;
334
335 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100336 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100337 pkt, quic_packet_type_enc_level_char(pkt->type));
338 if (pkt->pnl)
339 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
340 (unsigned long long)pkt->pn);
341 if (pkt->token_len)
342 chunk_appendf(&trace_buf, " toklen=%llu",
343 (unsigned long long)pkt->token_len);
344 if (pkt->aad_len)
345 chunk_appendf(&trace_buf, " aadlen=%llu",
346 (unsigned long long)pkt->aad_len);
347 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
348 pkt->flags, (unsigned long long)pkt->len);
349 }
350 if (pktlen)
351 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
352 if (ssl) {
353 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
354 chunk_appendf(&trace_buf, " el=%c",
355 quic_enc_level_char(ssl_to_quic_enc_level(level)));
356 }
357 }
358
359 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
360 const struct quic_rx_packet *pkt = a2;
361 const struct quic_rx_crypto_frm *cf = a3;
362 const SSL *ssl = a4;
363
364 if (pkt)
365 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
366 quic_packet_type_enc_level_char(pkt->type),
367 (unsigned long long)pkt->pn);
368 if (cf)
369 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
370 (unsigned long long)cf->offset_node.key,
371 (unsigned long long)cf->len);
372 if (ssl) {
373 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200374 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100375 quic_enc_level_char(ssl_to_quic_enc_level(level)));
376 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100377
378 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100379 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100380 }
381
382 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
383 const struct quic_frame *frm = a2;
384
385 if (frm)
386 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
387 }
388
389 if (mask & QUIC_EV_CONN_PHPKTS) {
390 const struct quic_enc_level *qel = a2;
391
392 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100393 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100394 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100395 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100396 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200397 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100398 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100399 (unsigned long long)qc->path->cwnd,
400 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100402 (unsigned long long)pktns->tx.in_flight,
403 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 }
405 }
406
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100407 if (mask & QUIC_EV_CONN_ENCPKT) {
408 const struct enc_debug_info *edi = a2;
409
410 if (edi)
411 chunk_appendf(&trace_buf,
412 " payload=@%p payload_len=%llu"
413 " aad=@%p aad_len=%llu pn=%llu",
414 edi->payload, (unsigned long long)edi->payload_len,
415 edi->aad, (unsigned long long)edi->aad_len,
416 (unsigned long long)edi->pn);
417 }
418
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100419 if (mask & QUIC_EV_CONN_RMHP) {
420 const struct quic_rx_packet *pkt = a2;
421
422 if (pkt) {
423 const int *ret = a3;
424
425 chunk_appendf(&trace_buf, " pkt@%p", pkt);
426 if (ret && *ret)
427 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
428 pkt->pnl, (unsigned long long)pkt->pn);
429 }
430 }
431
432 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200433 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100434 const unsigned long *val1 = a3;
435 const unsigned long *val2 = a4;
436
437 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100438 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100439 if (val1)
440 chunk_appendf(&trace_buf, " %lu", *val1);
441 if (val2)
442 chunk_appendf(&trace_buf, "..%lu", *val2);
443 }
444
445 if (mask & QUIC_EV_CONN_RTTUPDT) {
446 const unsigned int *rtt_sample = a2;
447 const unsigned int *ack_delay = a3;
448 const struct quic_loss *ql = a4;
449
450 if (rtt_sample)
451 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
452 if (ack_delay)
453 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
454 if (ql)
455 chunk_appendf(&trace_buf,
456 " srtt=%ums rttvar=%ums min_rtt=%ums",
457 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
458 }
459 if (mask & QUIC_EV_CONN_CC) {
460 const struct quic_cc_event *ev = a2;
461 const struct quic_cc *cc = a3;
462
463 if (a2)
464 quic_cc_event_trace(&trace_buf, ev);
465 if (a3)
466 quic_cc_state_trace(&trace_buf, cc);
467 }
468
469 if (mask & QUIC_EV_CONN_PKTLOSS) {
470 const struct quic_pktns *pktns = a2;
471 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100472
473 if (pktns) {
474 chunk_appendf(&trace_buf, " pktns=%s",
475 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
476 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
477 if (pktns->tx.loss_time)
478 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100479 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100480 }
481 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
482 struct quic_tx_packet *pkt;
483
484 chunk_appendf(&trace_buf, " lost_pkts:");
485 list_for_each_entry(pkt, lost_pkts, list)
486 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
487 }
488 }
489
490 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100491 const struct quic_pktns *pktns = a2;
492 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100493 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100495 if (ifae_pkts)
496 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
497 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100498 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100499 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100501 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
502 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100503 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
504 if (pktns->tx.in_flight)
505 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506 if (pktns->tx.loss_time)
507 chunk_appendf(&trace_buf, " loss_time=%dms",
508 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
509 }
510 if (mask & QUIC_EV_CONN_SPTO) {
511 if (pktns->tx.time_of_last_eliciting)
512 chunk_appendf(&trace_buf, " tole=%dms",
513 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
514 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100515 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100516 }
517 }
518
519 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
520 chunk_appendf(&trace_buf,
521 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
522 }
523 }
524
525 if (mask & QUIC_EV_CONN_SPPKTS) {
526 const struct quic_tx_packet *pkt = a2;
527
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100528 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
529 (unsigned long long)qc->path->cwnd,
530 (unsigned long long)qc->path->prep_in_flight,
531 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100533 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 (unsigned long)pkt->pn_node.key,
535 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
536 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100537 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100538 }
539 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100540
541 if (mask & QUIC_EV_CONN_SSLALERT) {
542 const uint8_t *alert = a2;
543 const enum ssl_encryption_level_t *level = a3;
544
545 if (alert)
546 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
547 if (level)
548 chunk_appendf(&trace_buf, " el=%c",
549 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
550 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100551
552 if (mask & QUIC_EV_CONN_BCFRMS) {
553 const size_t *sz1 = a2;
554 const size_t *sz2 = a3;
555 const size_t *sz3 = a4;
556
557 if (sz1)
558 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
559 if (sz2)
560 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
561 if (sz3)
562 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
563 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100564
565 if (mask & QUIC_EV_CONN_PSTRM) {
566 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100567
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100568 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100569 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100570 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100571 }
572 if (mask & QUIC_EV_CONN_LPKT) {
573 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200574 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100575
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100576 if (pkt) {
577 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
578 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
579 if (pkt->pn_node.key != (uint64_t)-1)
580 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
581 }
582
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200583 if (len)
584 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100585 }
586
587}
588
589/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100590static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100591{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200592 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100594 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100595 return 1;
596
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200597 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
598 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
599 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
600 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200601 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100602 return 1;
603
604 return 0;
605}
606
607/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
608 * both loss detection and PTO and schedule the task assiated to this timer if needed.
609 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100610static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 struct quic_pktns *pktns;
613 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200614 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100616 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100617 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100618
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100619 pktns = quic_loss_pktns(qc);
620 if (tick_isset(pktns->tx.loss_time)) {
621 qc->timer = pktns->tx.loss_time;
622 goto out;
623 }
624
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100625 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626 * cancelled for a server which reached the anti-amplification limit.
627 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100628 if (!quic_peer_validated_addr(qc) &&
629 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100630 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100631 qc->timer = TICK_ETERNITY;
632 goto out;
633 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100634
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100635 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100636 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637 /* Timer cancellation. */
638 qc->timer = TICK_ETERNITY;
639 goto out;
640 }
641
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200642 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
643 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100644 if (tick_isset(pto))
645 qc->timer = pto;
646 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100647 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
648 if (tick_is_expired(qc->timer, now_ms))
649 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
650 else
651 task_schedule(qc->timer_task, qc->timer);
652 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100653 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100654}
655
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100656/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
657 * connection.
658 * Return 1 if succeeded, 0 if not.
659 */
660static int quic_tls_key_update(struct quic_conn *qc)
661{
662 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
663 struct quic_tls_secrets *rx, *tx;
664 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
665 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
666
667 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
668 rx = &tls_ctx->rx;
669 tx = &tls_ctx->tx;
670 nxt_rx = &qc->ku.nxt_rx;
671 nxt_tx = &qc->ku.nxt_tx;
672
673 /* Prepare new RX secrets */
674 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
675 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100676 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100677 return 0;
678 }
679
680 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
681 nxt_rx->key, nxt_rx->keylen,
682 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
683 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100684 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100685 return 0;
686 }
687
688 /* Prepare new TX secrets */
689 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
690 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100691 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100692 return 0;
693 }
694
695 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
696 nxt_tx->key, nxt_tx->keylen,
697 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
698 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100699 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100700 return 0;
701 }
702
703 return 1;
704}
705
706/* Rotate the Key Update information for <qc> QUIC connection.
707 * Must be used after having updated them.
708 * Always succeeds.
709 */
710static void quic_tls_rotate_keys(struct quic_conn *qc)
711{
712 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
713 unsigned char *curr_secret, *curr_iv, *curr_key;
714
715 /* Rotate the RX secrets */
716 curr_secret = tls_ctx->rx.secret;
717 curr_iv = tls_ctx->rx.iv;
718 curr_key = tls_ctx->rx.key;
719
720 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
721 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
722 tls_ctx->rx.key = qc->ku.nxt_rx.key;
723
724 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
725 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
726 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
727
728 qc->ku.prv_rx.secret = curr_secret;
729 qc->ku.prv_rx.iv = curr_iv;
730 qc->ku.prv_rx.key = curr_key;
731 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
732
733 /* Update the TX secrets */
734 curr_secret = tls_ctx->tx.secret;
735 curr_iv = tls_ctx->tx.iv;
736 curr_key = tls_ctx->tx.key;
737
738 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
739 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
740 tls_ctx->tx.key = qc->ku.nxt_tx.key;
741
742 qc->ku.nxt_tx.secret = curr_secret;
743 qc->ku.nxt_tx.iv = curr_iv;
744 qc->ku.nxt_tx.key = curr_key;
745}
746
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100747#ifndef OPENSSL_IS_BORINGSSL
748int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
749 const uint8_t *read_secret,
750 const uint8_t *write_secret, size_t secret_len)
751{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100752 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
753 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 +0100754 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100755 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100756
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100757 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100758 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100759 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
760 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100761 goto out;
762 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100763
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100764 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100765 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100766 goto err;
767 }
768
769 rx = &tls_ctx->rx;
770 tx = &tls_ctx->tx;
771
772 rx->aead = tx->aead = tls_aead(cipher);
773 rx->md = tx->md = tls_md(cipher);
774 rx->hp = tx->hp = tls_hp(cipher);
775
776 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
777 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100778 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100779 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100780 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100781 }
782
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100783 /* Enqueue this connection asap if we could derive O-RTT secrets as
784 * listener. Note that a listener derives only RX secrets for this
785 * level.
786 */
787 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
788 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100789
790 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100791 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100792
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100793 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
794 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100795 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100796 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100797 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100798 }
799
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100800 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100801 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
802 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
803 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
804
805 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
806 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100807 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100808 goto err;
809 }
810
811 memcpy(rx->secret, read_secret, secret_len);
812 rx->secretlen = secret_len;
813 memcpy(tx->secret, write_secret, secret_len);
814 tx->secretlen = secret_len;
815 /* Initialize all the secret keys lengths */
816 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
817 /* Prepare the next key update */
818 if (!quic_tls_key_update(qc))
819 goto err;
820 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100821
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100822 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100823 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100824 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100825 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100826
827 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100828 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100829 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100830}
831#else
832/* ->set_read_secret callback to derive the RX secrets at <level> encryption
833 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500834 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100835 */
836int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
837 const SSL_CIPHER *cipher,
838 const uint8_t *secret, size_t secret_len)
839{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100840 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100841 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100842 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100843
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100844 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
845 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
846 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100847 goto out;
848 }
849
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100850 tls_ctx->rx.aead = tls_aead(cipher);
851 tls_ctx->rx.md = tls_md(cipher);
852 tls_ctx->rx.hp = tls_hp(cipher);
853
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100854 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
855 goto err;
856
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100857 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 +0100858 tls_ctx->rx.key, tls_ctx->rx.keylen,
859 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100860 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
861 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100862 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100863 goto err;
864 }
865
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100866 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100867 const unsigned char *buf;
868 size_t buflen;
869
870 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
871 if (!buflen)
872 goto err;
873
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100874 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100875 goto err;
876 }
877
878 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100879 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100880 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100881
882 return 1;
883
884 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100885 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100886 return 0;
887}
888
889/* ->set_write_secret callback to derive the TX secrets at <level>
890 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500891 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100892 */
893int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
894 const SSL_CIPHER *cipher,
895 const uint8_t *secret, size_t secret_len)
896{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100897 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
898 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 +0100899
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100900 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
901 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
902 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100903 goto out;
904 }
905
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100906 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
907 goto err;
908
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100909 tls_ctx->tx.aead = tls_aead(cipher);
910 tls_ctx->tx.md = tls_md(cipher);
911 tls_ctx->tx.hp = tls_hp(cipher);
912
913 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 +0100914 tls_ctx->tx.key, tls_ctx->tx.keylen,
915 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100916 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
917 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100918 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100919 goto err;
920 }
921
922 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100923 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100924 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100925 return 1;
926
927 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100928 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100929 return 0;
930}
931#endif
932
933/* This function copies the CRYPTO data provided by the TLS stack found at <data>
934 * with <len> as size in CRYPTO buffers dedicated to store the information about
935 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
936 * It fails only if it could not managed to allocate enough CRYPTO buffers to
937 * store all the data.
938 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
939 */
940static int quic_crypto_data_cpy(struct quic_enc_level *qel,
941 const unsigned char *data, size_t len)
942{
943 struct quic_crypto_buf **qcb;
944 /* The remaining byte to store in CRYPTO buffers. */
945 size_t cf_offset, cf_len, *nb_buf;
946 unsigned char *pos;
947
948 nb_buf = &qel->tx.crypto.nb_buf;
949 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
950 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
951 cf_len = len;
952
953 while (len) {
954 size_t to_copy, room;
955
956 pos = (*qcb)->data + (*qcb)->sz;
957 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
958 to_copy = len > room ? room : len;
959 if (to_copy) {
960 memcpy(pos, data, to_copy);
961 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
962 qel->tx.crypto.sz += to_copy;
963 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100964 len -= to_copy;
965 data += to_copy;
966 }
967 else {
968 struct quic_crypto_buf **tmp;
969
970 tmp = realloc(qel->tx.crypto.bufs,
971 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
972 if (tmp) {
973 qel->tx.crypto.bufs = tmp;
974 qcb = &qel->tx.crypto.bufs[*nb_buf];
975 *qcb = pool_alloc(pool_head_quic_crypto_buf);
976 if (!*qcb)
977 return 0;
978
979 (*qcb)->sz = 0;
980 ++*nb_buf;
981 }
982 else {
983 break;
984 }
985 }
986 }
987
988 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
989 * have been buffered.
990 */
991 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200992 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100993 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100994
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100995 /* There is at most one CRYPTO frame in this packet number
996 * space. Let's look for it.
997 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +0100998 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +0100999 if (frm->type != QUIC_FT_CRYPTO)
1000 continue;
1001
1002 /* Found */
1003 found = frm;
1004 break;
1005 }
1006
1007 if (found) {
1008 found->crypto.len += cf_len;
1009 }
1010 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001011 frm = pool_alloc(pool_head_quic_frame);
1012 if (!frm)
1013 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001014
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001015 frm->type = QUIC_FT_CRYPTO;
1016 frm->crypto.offset = cf_offset;
1017 frm->crypto.len = cf_len;
1018 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001019 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001020 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001021 }
1022
1023 return len == 0;
1024}
1025
1026
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001027/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1028void quic_set_tls_alert(struct quic_conn *qc, int alert)
1029{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001030 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001031 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001032 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001033}
1034
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001035/* Set the application for <qc> QUIC connection.
1036 * Return 1 if succeeded, 0 if not.
1037 */
1038int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1039{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001040 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1041 qc->app_ops = &h3_ops;
1042 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1043 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001044 else
1045 return 0;
1046
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001047 return 1;
1048}
1049
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001050/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1051 * wants to provide the QUIC layer with CRYPTO data.
1052 * Returns 1 if succeeded, 0 if not.
1053 */
1054int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1055 const uint8_t *data, size_t len)
1056{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001057 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001058 enum quic_tls_enc_level tel;
1059 struct quic_enc_level *qel;
1060
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001061 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1062 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1063 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1064 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001065 goto out;
1066 }
1067
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001068 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001069 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001070 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001071 goto err;
1072 }
1073
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001074 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001075 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001076 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001077 goto err;
1078 }
1079
1080 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001081 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001082
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001083 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001084 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001085 return 1;
1086
1087 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001088 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089 return 0;
1090}
1091
1092int ha_quic_flush_flight(SSL *ssl)
1093{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001094 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001096 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1097 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001098
1099 return 1;
1100}
1101
1102int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1103{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001104 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001106 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1107 quic_set_tls_alert(qc, alert);
1108 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001109 return 1;
1110}
1111
1112/* QUIC TLS methods */
1113static SSL_QUIC_METHOD ha_quic_method = {
1114#ifdef OPENSSL_IS_BORINGSSL
1115 .set_read_secret = ha_set_rsec,
1116 .set_write_secret = ha_set_wsec,
1117#else
1118 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1119#endif
1120 .add_handshake_data = ha_quic_add_handshake_data,
1121 .flush_flight = ha_quic_flush_flight,
1122 .send_alert = ha_quic_send_alert,
1123};
1124
1125/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1126 * Returns an error count.
1127 */
1128int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1129{
1130 struct proxy *curproxy = bind_conf->frontend;
1131 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1132 int cfgerr = 0;
1133
1134#if 0
1135 /* XXX Did not manage to use this. */
1136 const char *ciphers =
1137 "TLS_AES_128_GCM_SHA256:"
1138 "TLS_AES_256_GCM_SHA384:"
1139 "TLS_CHACHA20_POLY1305_SHA256:"
1140 "TLS_AES_128_CCM_SHA256";
1141#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001142 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143 long options =
1144 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1145 SSL_OP_SINGLE_ECDH_USE |
1146 SSL_OP_CIPHER_SERVER_PREFERENCE;
1147 SSL_CTX *ctx;
1148
1149 ctx = SSL_CTX_new(TLS_server_method());
1150 bind_conf->initial_ctx = ctx;
1151
1152 SSL_CTX_set_options(ctx, options);
1153#if 0
1154 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1155 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1156 "for bind '%s' at [%s:%d].\n",
1157 curproxy->id, ciphers,
1158 bind_conf->arg, bind_conf->file, bind_conf->line);
1159 cfgerr++;
1160 }
1161#endif
1162
1163 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1164 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1165 "for bind '%s' at [%s:%d].\n",
1166 curproxy->id, groups,
1167 bind_conf->arg, bind_conf->file, bind_conf->line);
1168 cfgerr++;
1169 }
1170
1171 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1172 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1173 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1174 SSL_CTX_set_default_verify_paths(ctx);
1175
1176#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1177#ifdef OPENSSL_IS_BORINGSSL
1178 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1179 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1180#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1181 if (bind_conf->ssl_conf.early_data) {
1182 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001183 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001184 }
1185 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1186 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1187#else
1188 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1189#endif
1190 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1191#endif
1192 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1193
1194 return cfgerr;
1195}
1196
1197/* Decode an expected packet number from <truncated_on> its truncated value,
1198 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1199 * the number of bits used to encode this packet number (its length in bytes * 8).
1200 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1201 */
1202static uint64_t decode_packet_number(uint64_t largest_pn,
1203 uint32_t truncated_pn, unsigned int pn_nbits)
1204{
1205 uint64_t expected_pn = largest_pn + 1;
1206 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1207 uint64_t pn_hwin = pn_win / 2;
1208 uint64_t pn_mask = pn_win - 1;
1209 uint64_t candidate_pn;
1210
1211
1212 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1213 /* Note that <pn_win> > <pn_hwin>. */
1214 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1215 candidate_pn + pn_hwin <= expected_pn)
1216 return candidate_pn + pn_win;
1217
1218 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1219 return candidate_pn - pn_win;
1220
1221 return candidate_pn;
1222}
1223
1224/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1225 * cryptographic context.
1226 * <largest_pn> is the largest received packet number and <pn> the address of
1227 * the packet number field for this packet with <byte0> address of its first byte.
1228 * <end> points to one byte past the end of this packet.
1229 * Returns 1 if succeeded, 0 if not.
1230 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001231static int qc_do_rm_hp(struct quic_conn *qc,
1232 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001233 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001234 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001235{
1236 int ret, outlen, i, pnlen;
1237 uint64_t packet_number;
1238 uint32_t truncated_pn = 0;
1239 unsigned char mask[5] = {0};
1240 unsigned char *sample;
1241 EVP_CIPHER_CTX *cctx;
1242 unsigned char *hp_key;
1243
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001244 /* Check there is enough data in this packet. */
1245 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001246 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001247 return 0;
1248 }
1249
1250 cctx = EVP_CIPHER_CTX_new();
1251 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001252 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253 return 0;
1254 }
1255
1256 ret = 0;
1257 sample = pn + QUIC_PACKET_PN_MAXLEN;
1258
1259 hp_key = tls_ctx->rx.hp_key;
1260 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1261 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1262 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001263 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001264 goto out;
1265 }
1266
1267 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1268 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1269 for (i = 0; i < pnlen; i++) {
1270 pn[i] ^= mask[i + 1];
1271 truncated_pn = (truncated_pn << 8) | pn[i];
1272 }
1273
1274 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1275 /* Store remaining information for this unprotected header */
1276 pkt->pn = packet_number;
1277 pkt->pnl = pnlen;
1278
1279 ret = 1;
1280
1281 out:
1282 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001283
1284 return ret;
1285}
1286
1287/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1288 * address, with <payload_len> as payload length, <aad> as address of
1289 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1290 * context.
1291 * Returns 1 if succeeded, 0 if not.
1292 */
1293static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1294 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001295 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001296{
1297 unsigned char iv[12];
1298 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001299 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001300 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001301
1302 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001303 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001304 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001305 }
1306
1307 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1308 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001309 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001310 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001311 }
1312
1313 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001314
1315 err:
1316 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001317 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001318 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001319}
1320
1321/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1322 * Returns 1 if succeeded, 0 if not.
1323 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001324static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001325{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001326 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001327 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001328 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001330 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1331 unsigned char *rx_key = tls_ctx->rx.key;
1332
1333 kp_changed = 0;
1334 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1335 /* The two tested bits are not at the same position,
1336 * this is why they are first both inversed.
1337 */
1338 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1339 if (pkt->pn < tls_ctx->rx.pn) {
1340 /* The lowest packet number of a previous key phase
1341 * cannot be null if it really stores previous key phase
1342 * secrets.
1343 */
1344 if (!pkt->qc->ku.prv_rx.pn)
1345 return 0;
1346
1347 rx_iv = pkt->qc->ku.prv_rx.iv;
1348 rx_key = pkt->qc->ku.prv_rx.key;
1349 }
1350 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1351 /* Next key phase */
1352 kp_changed = 1;
1353 rx_iv = pkt->qc->ku.nxt_rx.iv;
1354 rx_key = pkt->qc->ku.nxt_rx.key;
1355 }
1356 }
1357 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001358
1359 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1360 return 0;
1361
1362 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1363 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001364 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001365 if (!ret)
1366 return 0;
1367
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001368 /* Update the keys only if the packet decryption succeeded. */
1369 if (kp_changed) {
1370 quic_tls_rotate_keys(pkt->qc);
1371 /* Toggle the Key Phase bit */
1372 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1373 /* Store the lowest packet number received for the current key phase */
1374 tls_ctx->rx.pn = pkt->pn;
1375 /* Prepare the next key update */
1376 if (!quic_tls_key_update(pkt->qc))
1377 return 0;
1378 }
1379
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001380 /* Update the packet length (required to parse the frames). */
1381 pkt->len = pkt->aad_len + ret;
1382
1383 return 1;
1384}
1385
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001386/* Remove from <qcs> stream the acknowledged frames.
1387 * Never fails.
1388 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001389static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001390{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001391 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001392 struct eb64_node *frm_node;
1393
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001394 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001395 frm_node = eb64_first(&qcs->tx.acked_frms);
1396 while (frm_node) {
1397 struct quic_stream *strm;
1398
1399 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001400 if (strm->offset.key > qcs->tx.ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001401 break;
1402
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001403 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1404 const size_t diff = strm->offset.key + strm->len -
1405 qcs->tx.ack_offset;
1406 qcs->tx.ack_offset += diff;
1407 b_del(strm->buf, diff);
1408 ret = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001409
1410 if (!b_data(strm->buf)) {
1411 b_free(strm->buf);
1412 offer_buffers(NULL, 1);
1413 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001414 }
1415
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001416 frm_node = eb64_next(frm_node);
1417 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001418
1419 /* TODO
1420 *
1421 * memleak: The quic_frame container of the quic_stream should
1422 * be liberated here, as in qc_treat_acked_tx_frm. However this
1423 * code seems to cause a bug which can lead to interrupted
1424 * transfers.
1425 *
1426 * struct quic_frame frm = container_of(strm, struct quic_frame, stream);
1427 * LIST_DELETE(&frm->list);
1428 * pool_free(pool_head_quic_frame, frm);
1429 */
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001430 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001431
1432 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001433}
1434
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001435/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001436static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1437 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001438{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001439 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001440
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001441 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001442 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001443 switch (frm->type) {
1444 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1445 {
1446 struct qcs *qcs = frm->stream.qcs;
1447 struct quic_stream *strm = &frm->stream;
1448
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001449 if (strm->offset.key <= qcs->tx.ack_offset) {
1450 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1451 const size_t diff = strm->offset.key + strm->len -
1452 qcs->tx.ack_offset;
1453 qcs->tx.ack_offset += diff;
1454 b_del(strm->buf, diff);
1455 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001456
1457 if (!b_data(strm->buf)) {
1458 b_free(strm->buf);
1459 offer_buffers(NULL, 1);
1460 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001461 }
1462
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001463 LIST_DELETE(&frm->list);
1464 pool_free(pool_head_quic_frame, frm);
1465 }
1466 else {
1467 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1468 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001469
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001470 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001471 }
1472 break;
1473 default:
1474 LIST_DELETE(&frm->list);
1475 pool_free(pool_head_quic_frame, frm);
1476 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001477
1478 if (stream_acked) {
1479 struct qcc *qcc = qc->qcc;
1480
1481 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1482 tasklet_wakeup(qcc->subs->tasklet);
1483 qcc->subs->events &= ~SUB_RETRY_SEND;
1484 if (!qcc->subs->events)
1485 qcc->subs = NULL;
1486 }
1487 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001488}
1489
1490/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1491 * deallocating them, and their TX frames.
1492 * Returns the last node reached to be used for the next range.
1493 * May be NULL if <largest> node could not be found.
1494 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001495static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1496 struct eb_root *pkts,
1497 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001498 struct list *newly_acked_pkts,
1499 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001500 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001501{
1502 struct eb64_node *node;
1503 struct quic_tx_packet *pkt;
1504
1505 if (largest_node)
1506 node = largest_node;
1507 else {
1508 node = eb64_lookup(pkts, largest);
1509 while (!node && largest > smallest) {
1510 node = eb64_lookup(pkts, --largest);
1511 }
1512 }
1513
1514 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001515 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001516
1517 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1518 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001519 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001520 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001521 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001522 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001523 node = eb64_prev(node);
1524 eb64_delete(&pkt->pn_node);
1525 }
1526
1527 return node;
1528}
1529
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001530/* Remove all frames from <pkt_frm_list> and reinsert them in the
1531 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001532 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001533static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1534 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001535 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001536{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001537 struct quic_frame *frm, *frmbak;
1538 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001539
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001540 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1541 LIST_DELETE(&frm->list);
1542 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001543 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001544 }
1545
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001546 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001547}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001548
1549/* Free the TX packets of <pkts> list */
1550static inline void free_quic_tx_pkts(struct list *pkts)
1551{
1552 struct quic_tx_packet *pkt, *tmp;
1553
1554 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001555 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001556 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001557 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001558 }
1559}
1560
1561/* Send a packet loss event nofification to the congestion controller
1562 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1563 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1564 * at <now_us> current time.
1565 * Always succeeds.
1566 */
1567static inline void qc_cc_loss_event(struct quic_conn *qc,
1568 unsigned int lost_bytes,
1569 unsigned int newest_time_sent,
1570 unsigned int period,
1571 unsigned int now_us)
1572{
1573 struct quic_cc_event ev = {
1574 .type = QUIC_CC_EVT_LOSS,
1575 .loss.now_ms = now_ms,
1576 .loss.max_ack_delay = qc->max_ack_delay,
1577 .loss.lost_bytes = lost_bytes,
1578 .loss.newest_time_sent = newest_time_sent,
1579 .loss.period = period,
1580 };
1581
1582 quic_cc_event(&qc->path->cc, &ev);
1583}
1584
1585/* Send a packet ack event nofication for each newly acked packet of
1586 * <newly_acked_pkts> list and free them.
1587 * Always succeeds.
1588 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001589static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001590 struct list *newly_acked_pkts)
1591{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001592 struct quic_tx_packet *pkt, *tmp;
1593 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1594
1595 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1596 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001597 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001598 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001599 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600 ev.ack.acked = pkt->in_flight_len;
1601 ev.ack.time_sent = pkt->time_sent;
1602 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001603 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001604 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001605 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001606 }
1607
1608}
1609
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001610/* Release all the frames attached to <pktns> packet number space */
1611static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1612{
1613 struct quic_frame *frm, *frmbak;
1614
1615 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1616 LIST_DELETE(&frm->list);
1617 pool_free(pool_head_quic_frame, frm);
1618 }
1619}
1620
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001621/* Handle <pkts> list of lost packets detected at <now_us> handling
1622 * their TX frames.
1623 * Send a packet loss event to the congestion controller if
1624 * in flight packet have been lost.
1625 * Also frees the packet in <pkts> list.
1626 * Never fails.
1627 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001628static inline void qc_release_lost_pkts(struct quic_conn *qc,
1629 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001630 struct list *pkts,
1631 uint64_t now_us)
1632{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001633 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 uint64_t lost_bytes;
1635
1636 lost_bytes = 0;
1637 oldest_lost = newest_lost = NULL;
1638 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001639 struct list tmp = LIST_HEAD_INIT(tmp);
1640
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001641 lost_bytes += pkt->in_flight_len;
1642 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001643 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001644 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001645 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001646 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001647 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001648 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001649 if (!oldest_lost) {
1650 oldest_lost = newest_lost = pkt;
1651 }
1652 else {
1653 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001654 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001655 newest_lost = pkt;
1656 }
1657 }
1658
1659 if (lost_bytes) {
1660 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001661 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001662 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001663 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001664 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001665 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001666 }
1667}
1668
1669/* Look for packet loss from sent packets for <qel> encryption level of a
1670 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1671 * their tree if deemed as lost or set the <loss_time> value the packet number
1672 * space if any not deemed lost.
1673 * Should be called after having received an ACK frame with newly acknowledged
1674 * packets or when the the loss detection timer has expired.
1675 * Always succeeds.
1676 */
1677static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1678 struct quic_conn *qc,
1679 struct list *lost_pkts)
1680{
1681 struct eb_root *pkts;
1682 struct eb64_node *node;
1683 struct quic_loss *ql;
1684 unsigned int loss_delay;
1685
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001686 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001687 pkts = &pktns->tx.pkts;
1688 pktns->tx.loss_time = TICK_ETERNITY;
1689 if (eb_is_empty(pkts))
1690 goto out;
1691
1692 ql = &qc->path->loss;
1693 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001694 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1695
1696 node = eb64_first(pkts);
1697 while (node) {
1698 struct quic_tx_packet *pkt;
1699 int64_t largest_acked_pn;
1700 unsigned int loss_time_limit, time_sent;
1701
1702 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001703 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001704 node = eb64_next(node);
1705 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1706 break;
1707
1708 time_sent = pkt->time_sent;
1709 loss_time_limit = tick_add(time_sent, loss_delay);
1710 if (tick_is_le(time_sent, now_ms) ||
1711 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1712 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001713 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001714 }
1715 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001716 if (tick_isset(pktns->tx.loss_time))
1717 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1718 else
1719 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001720 }
1721 }
1722
1723 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001724 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001725}
1726
1727/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1728 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001729 * 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 +01001730 * acked ack-eliciting packet.
1731 * Return 1, if succeeded, 0 if not.
1732 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001733static inline int qc_parse_ack_frm(struct quic_conn *qc,
1734 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001735 struct quic_enc_level *qel,
1736 unsigned int *rtt_sample,
1737 const unsigned char **pos, const unsigned char *end)
1738{
1739 struct quic_ack *ack = &frm->ack;
1740 uint64_t smallest, largest;
1741 struct eb_root *pkts;
1742 struct eb64_node *largest_node;
1743 unsigned int time_sent, pkt_flags;
1744 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1745 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1746
1747 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1748 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001749 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001750 goto err;
1751 }
1752
1753 if (ack->first_ack_range > ack->largest_ack) {
1754 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001755 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001756 goto err;
1757 }
1758
1759 largest = ack->largest_ack;
1760 smallest = largest - ack->first_ack_range;
1761 pkts = &qel->pktns->tx.pkts;
1762 pkt_flags = 0;
1763 largest_node = NULL;
1764 time_sent = 0;
1765
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001766 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 +01001767 largest_node = eb64_lookup(pkts, largest);
1768 if (!largest_node) {
1769 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001770 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001771 }
1772 else {
1773 time_sent = eb64_entry(&largest_node->node,
1774 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001776 }
1777
1778 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001779 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 do {
1781 uint64_t gap, ack_range;
1782
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001783 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1784 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001785 if (!ack->ack_range_num--)
1786 break;
1787
1788 if (!quic_dec_int(&gap, pos, end))
1789 goto err;
1790
1791 if (smallest < gap + 2) {
1792 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001793 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001794 goto err;
1795 }
1796
1797 largest = smallest - gap - 2;
1798 if (!quic_dec_int(&ack_range, pos, end))
1799 goto err;
1800
1801 if (largest < ack_range) {
1802 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001803 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001804 goto err;
1805 }
1806
1807 /* Do not use this node anymore. */
1808 largest_node = NULL;
1809 /* Next range */
1810 smallest = largest - ack_range;
1811
1812 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001813 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001814 } while (1);
1815
1816 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001817 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001818
1819 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1820 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001821 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001822 }
1823
1824 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1825 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001826 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001827 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001828 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001829 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001830 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1831 if (quic_peer_validated_addr(qc))
1832 qc->path->loss.pto_count = 0;
1833 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001834 }
1835
1836
1837 return 1;
1838
1839 err:
1840 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001841 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001842 return 0;
1843}
1844
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001845/* This function gives the detail of the SSL error. It is used only
1846 * if the debug mode and the verbose mode are activated. It dump all
1847 * the SSL error until the stack was empty.
1848 */
1849static forceinline void qc_ssl_dump_errors(struct connection *conn)
1850{
1851 if (unlikely(global.mode & MODE_DEBUG)) {
1852 while (1) {
1853 unsigned long ret;
1854
1855 ret = ERR_get_error();
1856 if (!ret)
1857 return;
1858
1859 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1860 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1861 }
1862 }
1863}
1864
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001865int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1866 const char **str, int *len);
1867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1869 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001870 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871 * Return 1 if succeeded, 0 if not.
1872 */
1873static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001874 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001875 const unsigned char *data, size_t len,
1876 struct quic_rx_packet *pkt,
1877 struct quic_rx_crypto_frm *cf)
1878{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001879 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001880 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001881
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001882 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001883 qc = ctx->qc;
1884
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001885 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1886
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1888 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001889 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001890 goto err;
1891 }
1892
1893 el->rx.crypto.offset += len;
1894 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001895 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001896
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001897 state = HA_ATOMIC_LOAD(&qc->state);
1898 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001899 ssl_err = SSL_do_handshake(ctx->ssl);
1900 if (ssl_err != 1) {
1901 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1902 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1903 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001904 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001905 goto out;
1906 }
1907
1908 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001909 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001910 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001911 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001912 goto err;
1913 }
1914
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001915 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
1916 /* I/O callback switch */
1917 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001918 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001919 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001920 /* The connection is ready to be accepted. */
1921 quic_accept_push_qc(qc);
1922 }
1923 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001924 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001925 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 } else {
1927 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1928 if (ssl_err != 1) {
1929 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1930 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1931 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001932 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001933 goto out;
1934 }
1935
1936 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001937 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001938 goto err;
1939 }
1940
1941 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001942 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001943 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001944
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001945 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001946 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001947 return 1;
1948
1949 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001950 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001951 return 0;
1952}
1953
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001954/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1955 * <pkt> RX packet.
1956 * Return it if succeeded, NULL if not.
1957 */
1958static inline
1959struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1960 struct quic_rx_packet *pkt)
1961{
1962 struct quic_rx_strm_frm *frm;
1963
1964 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1965 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001966 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001967 frm->len = stream_frm->len;
1968 frm->data = stream_frm->data;
1969 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01001970 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001971 }
1972
1973 return frm;
1974}
1975
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001976/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001977 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001978 */
1979static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1980{
1981 size_t ret;
1982
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01001983 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
1984 strm_frm->len -= ret;
1985 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001986
1987 return ret;
1988}
1989
1990/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1991 * streams may be open. The data are copied to the stream RX buffer if possible.
1992 * If not, the STREAM frame is stored to be treated again later.
1993 * We rely on the flow control so that not to store too much STREAM frames.
1994 * Return 1 if succeeded, 0 if not.
1995 */
1996static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1997 struct quic_stream *strm_frm,
1998 struct quic_conn *qc)
1999{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002000 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002001 struct eb64_node *frm_node;
2002 struct qcs *qcs = NULL;
2003 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002004
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002005 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2006 strm_frm->offset.key, strm_frm->fin,
2007 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002008
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002009 /* invalid or already received frame */
2010 if (ret == 1)
2011 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002012
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002013 if (ret == 2) {
2014 /* frame cannot be parsed at the moment and should be
2015 * buffered.
2016 */
2017 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2018 if (!frm) {
2019 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002020 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002021 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002022 }
2023
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002024 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2025 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002026
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002027 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002028 }
2029
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002030 /* Frame correctly received by the mux.
2031 * If there is buffered frame for next offset, it may be possible to
2032 * receive them now.
2033 */
2034 frm_node = eb64_first(&qcs->rx.frms);
2035 while (frm_node) {
2036 frm = eb64_entry(&frm_node->node,
2037 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002038
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002039 ret = qcc_recv(qc->qcc, qcs->by_id.key, frm->len,
2040 frm->offset_node.key, frm->fin,
2041 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002042
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002043 /* interrupt the parsing if the frame cannot be handled for the
2044 * moment only by the MUX.
2045 */
2046 if (ret == 2)
2047 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002048
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002049 /* Remove a newly received frame or an invalid one. */
2050 frm_node = eb64_next(frm_node);
2051 eb64_delete(&frm->offset_node);
2052 quic_rx_packet_refdec(frm->pkt);
2053 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002054 }
2055
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002056 /* Decode the received data. */
2057 if (qcc_decode_qcs(qc->qcc, qcs))
2058 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002059
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002060 return 1;
2061}
2062
2063/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2064 * streams may be open. The data are copied to the stream RX buffer if possible.
2065 * If not, the STREAM frame is stored to be treated again later.
2066 * We rely on the flow control so that not to store too much STREAM frames.
2067 * Return 1 if succeeded, 0 if not.
2068 */
2069static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2070 struct quic_stream *strm_frm,
2071 struct quic_conn *qc)
2072{
2073 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002074 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002075 struct quic_rx_strm_frm *frm;
2076 size_t strm_frm_len;
2077
2078 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2079 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002080 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002081 return 0;
2082 }
2083
2084 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002085 if (strm_frm->offset.key < strm->rx.offset) {
2086 size_t diff;
2087
2088 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2089 TRACE_PROTO("Already received STREAM data",
2090 QUIC_EV_CONN_PSTRM, qc);
2091 goto out;
2092 }
2093
2094 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2095 diff = strm->rx.offset - strm_frm->offset.key;
2096 strm_frm->offset.key = strm->rx.offset;
2097 strm_frm->len -= diff;
2098 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002099 }
2100
2101 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002102 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002103 int ret;
2104
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002105 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002106 goto store_frm;
2107
2108 /* qc_strm_cpy() will modify the offset, depending on the number
2109 * of bytes copied.
2110 */
2111 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2112 /* Inform the application of the arrival of this new stream */
2113 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002114 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002115 return 0;
2116 }
2117
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002118 if (ret)
2119 qcs_notify_recv(strm);
2120
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002121 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002122 }
2123 /* Take this frame into an account for the stream flow control */
2124 strm->rx.offset += strm_frm_len;
2125 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002126 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002127 */
2128 if (!strm_frm->len)
2129 goto out;
2130
2131 store_frm:
2132 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2133 if (!frm) {
2134 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002135 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002136 return 0;
2137 }
2138
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002139 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002140 quic_rx_packet_refinc(pkt);
2141
2142 out:
2143 return 1;
2144}
2145
2146static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2147 struct quic_stream *strm_frm,
2148 struct quic_conn *qc)
2149{
2150 if (strm_frm->id & QCS_ID_DIR_BIT)
2151 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2152 else
2153 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2154}
2155
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002156/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002157static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2158 struct quic_conn *qc)
2159{
2160 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002161 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002162 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002163
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002164 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002165 pkts = &qel->pktns->tx.pkts;
2166 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002167 /* Skip the empty packet (they have already been retransmitted) */
2168 while (node) {
2169 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2170 if (!LIST_ISEMPTY(&pkt->frms))
2171 break;
2172 node = eb64_next(node);
2173 }
2174
2175 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002176 return;
2177
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002178 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002179}
2180
2181/* Prepare a fast retransmission during handshake after a client
2182 * has resent Initial packets. According to the RFC a server may retransmit
2183 * up to two datagrams of Initial packets if did not receive all Initial packets
2184 * and resend them coalescing with others (Handshake here).
2185 * (Listener only).
2186 */
2187static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2188{
2189 struct list itmp = LIST_HEAD_INIT(itmp);
2190 struct list htmp = LIST_HEAD_INIT(htmp);
2191 struct quic_frame *frm, *frmbak;
2192
2193 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2194 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2195 struct quic_enc_level *qel = iqel;
2196 struct eb_root *pkts;
2197 struct eb64_node *node;
2198 struct quic_tx_packet *pkt;
2199 struct list *tmp = &itmp;
2200
2201 start:
2202 pkt = NULL;
2203 pkts = &qel->pktns->tx.pkts;
2204 node = eb64_first(pkts);
2205 /* Skip the empty packet (they have already been retransmitted) */
2206 while (node) {
2207 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2208 if (!LIST_ISEMPTY(&pkt->frms))
2209 break;
2210 node = eb64_next(node);
2211 }
2212
2213 if (!pkt)
2214 goto end;
2215
2216 qel->pktns->tx.pto_probe += 1;
2217 requeue:
2218 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2219 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2220 LIST_DELETE(&frm->list);
2221 LIST_APPEND(tmp, &frm->list);
2222 }
2223
2224 if (qel == iqel) {
2225 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2226 pkt = pkt->next;
2227 tmp = &htmp;
2228 hqel->pktns->tx.pto_probe += 1;
2229 goto requeue;
2230 }
2231
2232 qel = hqel;
2233 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002234 goto start;
2235 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002236
2237 end:
2238 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2239 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002240}
2241
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002242/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2243 * as I/O handler context and <qel> as encryption level.
2244 * Returns 1 if succeeded, 0 if failed.
2245 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002246static 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 +01002247 struct quic_enc_level *qel)
2248{
2249 struct quic_frame frm;
2250 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002251 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002252 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002254 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002255 /* Skip the AAD */
2256 pos = pkt->data + pkt->aad_len;
2257 end = pkt->data + pkt->len;
2258
2259 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002260 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002261 goto err;
2262
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002263 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002264 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002265 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002266 break;
2267 case QUIC_FT_PING:
2268 break;
2269 case QUIC_FT_ACK:
2270 {
2271 unsigned int rtt_sample;
2272
2273 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002274 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002275 goto err;
2276
2277 if (rtt_sample) {
2278 unsigned int ack_delay;
2279
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002280 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002281 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2282 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2283 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002284 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002285 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002286 break;
2287 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002288 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002289 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002290 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002291 {
2292 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002293
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002294 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002295 /* XXX TO DO: <cfdebug> is used only for the traces. */
2296 struct quic_rx_crypto_frm cfdebug = { };
2297
2298 cfdebug.offset_node.key = frm.crypto.offset;
2299 cfdebug.len = frm.crypto.len;
2300 TRACE_PROTO("CRYPTO data discarded",
2301 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2302 break;
2303 }
2304
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002305 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2306 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2307 /* XXX TO DO: <cfdebug> is used only for the traces. */
2308 struct quic_rx_crypto_frm cfdebug = { };
2309
2310 cfdebug.offset_node.key = frm.crypto.offset;
2311 cfdebug.len = frm.crypto.len;
2312 /* Nothing to do */
2313 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002314 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002315 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002316 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2317 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002318 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002319 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002320 else {
2321 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2322 /* XXX TO DO: <cfdebug> is used only for the traces. */
2323 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002324
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002325 cfdebug.offset_node.key = frm.crypto.offset;
2326 cfdebug.len = frm.crypto.len;
2327 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002328 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002329 frm.crypto.len -= diff;
2330 frm.crypto.data += diff;
2331 frm.crypto.offset = qel->rx.crypto.offset;
2332 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002333 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002334
2335 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002336 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002337 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002338
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002339 cfdebug.offset_node.key = frm.crypto.offset;
2340 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002341 if (!qc_provide_cdata(qel, ctx,
2342 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002343 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002344 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002345
2346 break;
2347 }
2348
2349 /* frm.crypto.offset > qel->rx.crypto.offset */
2350 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2351 if (!cf) {
2352 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002353 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002354 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002355 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002356
2357 cf->offset_node.key = frm.crypto.offset;
2358 cf->len = frm.crypto.len;
2359 cf->data = frm.crypto.data;
2360 cf->pkt = pkt;
2361 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2362 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002363 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002364 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002365 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002366 {
2367 struct quic_stream *stream = &frm.stream;
2368
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002369 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002370 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2371 goto err;
2372 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2373 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002374
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002375 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002376 goto err;
2377
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002378 break;
2379 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002380 case QUIC_FT_MAX_DATA:
2381 case QUIC_FT_MAX_STREAM_DATA:
2382 case QUIC_FT_MAX_STREAMS_BIDI:
2383 case QUIC_FT_MAX_STREAMS_UNI:
2384 case QUIC_FT_DATA_BLOCKED:
2385 case QUIC_FT_STREAM_DATA_BLOCKED:
2386 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2387 case QUIC_FT_STREAMS_BLOCKED_UNI:
2388 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002389 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002390 case QUIC_FT_RETIRE_CONNECTION_ID:
2391 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392 break;
2393 case QUIC_FT_CONNECTION_CLOSE:
2394 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002395 /* warn the mux to close the connection */
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002396 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelle4af65952022-02-15 11:06:15 +01002397 qc->qcc->flags |= QC_CF_CC_RECV;
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002398 tasklet_wakeup(qc->qcc->wait_event.tasklet);
2399 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002400 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002402 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002403 goto err;
2404
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002405 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406 break;
2407 default:
2408 goto err;
2409 }
2410 }
2411
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002412 if (fast_retrans)
2413 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002414
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2416 * has successfully parse a Handshake packet. The Initial encryption must also
2417 * be discarded.
2418 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002419 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002420 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002421
2422 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002423 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002424 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002425 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002426 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002427 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002428 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002429 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002430 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002431 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002432 }
2433
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002434 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002435 return 1;
2436
2437 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002438 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002439 return 0;
2440}
2441
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002442/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002443 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002444 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2445 * This function must be called only after having built a correct datagram.
2446 * Always succeeds.
2447 */
2448static inline void qc_set_dg(struct cbuf *cbuf,
2449 uint16_t dglen, struct quic_tx_packet *pkt)
2450{
2451 write_u16(cb_wr(cbuf), dglen);
2452 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2453 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2454}
2455
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002456/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002457 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2458 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002459 * A header made of two fields is added to each datagram: the datagram length followed
2460 * by the address of the first packet in this datagram.
2461 * Returns 1 if succeeded, or 0 if something wrong happened.
2462 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002463static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2464 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002465{
2466 struct quic_enc_level *qel;
2467 struct cbuf *cbuf;
2468 unsigned char *end_buf, *end, *pos, *spos;
2469 struct quic_tx_packet *pkt;
2470 size_t total;
2471 size_t dg_headlen;
2472
2473 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2474 /* Each datagram is prepended with its length followed by the
2475 * address of the first packet in the datagram.
2476 */
2477 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2478 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2479 total = 0;
2480 start:
2481 cbuf = qr->cbuf;
2482 spos = pos = cb_wr(cbuf);
2483 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2484 * to ensure there is enough room to mark the end of prepared
2485 * contiguous data with a zero length.
2486 */
2487 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2488 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
2489 int err, probe, ack, cc;
2490
2491 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
2492 probe = ack = 0;
2493 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2494 if (!cc) {
2495 probe = qel->pktns->tx.pto_probe;
2496 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
2497 }
2498 /* Do not build any more packet if the TX secrets are not available or
2499 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2500 * and if there is no more packets to send upon PTO expiration
2501 * and if there is no more CRYPTO data available or in flight
2502 * congestion control limit is reached for prepared data
2503 */
2504 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2505 (!cc && !ack && !probe &&
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002506 (LIST_ISEMPTY(frms) ||
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002507 qc->path->prep_in_flight >= qc->path->cwnd))) {
2508 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2509 break;
2510 }
2511
2512 /* Leave room for the datagram header */
2513 pos += dg_headlen;
2514 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2515 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2516 }
2517 else {
2518 end = pos + qc->path->mtu;
2519 }
2520
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002521 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002522 QUIC_PACKET_TYPE_SHORT, ack, probe, cc, &err);
2523 /* Restore the PTO dgrams counter if a packet could not be built */
2524 if (err < 0) {
2525 if (ack)
2526 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
2527 }
2528 switch (err) {
2529 case -2:
2530 goto err;
2531 case -1:
2532 goto out;
2533 default:
2534 break;
2535 }
2536
2537 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2538 if (!pkt)
2539 goto err;
2540
2541 total += pkt->len;
2542 /* Set the current datagram as prepared into <cbuf>. */
2543 qc_set_dg(cbuf, pkt->len, pkt);
2544 }
2545
2546 stop_build:
2547 /* Reset <wr> writer index if in front of <rd> index */
2548 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2549 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2550
2551 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
2552 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2553 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2554 /* Mark the end of contiguous data for the reader */
2555 write_u16(cb_wr(cbuf), 0);
2556 cb_add(cbuf, sizeof(uint16_t));
2557 }
2558 }
2559
2560 if (rd && rd <= cbuf->wr) {
2561 cb_wr_reset(cbuf);
2562 /* Let's try to reuse this buffer */
2563 goto start;
2564 }
2565 }
2566
2567 out:
2568 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2569 return total;
2570
2571 err:
2572 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2573 return -1;
2574}
2575
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002576/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002577 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2578 * several packets in the same datagram. A header made of two fields is added
2579 * to each datagram: the datagram length followed by the address of the first
2580 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002581 * Returns 1 if succeeded, or 0 if something wrong happened.
2582 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002583static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2584 enum quic_tls_enc_level tel,
2585 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002586{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002588 struct cbuf *cbuf;
2589 unsigned char *end_buf, *end, *pos, *spos;
2590 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2591 /* length of datagrams */
2592 uint16_t dglen;
2593 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002594 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002595 /* Each datagram is prepended with its length followed by the
2596 * address of the first packet in the datagram.
2597 */
2598 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002599
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002600 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2601
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002602 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002603 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002604 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002605 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002606 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002607 cbuf = qr->cbuf;
2608 spos = pos = cb_wr(cbuf);
2609 /* Leave at least <dglen> bytes at the end of this buffer
2610 * to ensure there is enough room to mark the end of prepared
2611 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002613 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2614 first_pkt = prv_pkt = NULL;
2615 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002616 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002617 enum quic_pkt_type pkt_type;
2618
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002619 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002620 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002621 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2622 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002623 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002624 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002625 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002626 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002627 * 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 +01002628 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002629 * and if there is no more CRYPTO data available or in flight
2630 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002632 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002633 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002634 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002635 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002636 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002637 /* Set the current datagram as prepared into <cbuf> if
2638 * the was already a correct packet which was previously written.
2639 */
2640 if (prv_pkt)
2641 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002642 /* Let's select the next encryption level */
2643 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2644 tel = next_tel;
2645 qel = &qc->els[tel];
2646 /* Build a new datagram */
2647 prv_pkt = NULL;
2648 continue;
2649 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 break;
2651 }
2652
2653 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002654 if (!prv_pkt) {
2655 /* Leave room for the datagram header */
2656 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002657 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002658 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2659 }
2660 else {
2661 end = pos + qc->path->mtu;
2662 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002663 }
2664
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002665 cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms,
2666 qc, dglen, padding, pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002667 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002668 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002669 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002670 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002671 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002672 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 case -2:
2674 goto err;
2675 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002676 /* If there was already a correct packet present, set the
2677 * current datagram as prepared into <cbuf>.
2678 */
2679 if (prv_pkt) {
2680 qc_set_dg(cbuf, dglen, first_pkt);
2681 goto stop_build;
2682 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002683 goto out;
2684 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002685 break;
2686 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002687
Frédéric Lécaille63556772021-12-29 17:18:21 +01002688 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2689 if (!cur_pkt)
2690 goto err;
2691
2692 total += cur_pkt->len;
2693 /* keep trace of the first packet in the datagram */
2694 if (!first_pkt)
2695 first_pkt = cur_pkt;
2696 /* Attach the current one to the previous one */
2697 if (prv_pkt)
2698 prv_pkt->next = cur_pkt;
2699 /* Let's say we have to build a new dgram */
2700 prv_pkt = NULL;
2701 dglen += cur_pkt->len;
2702 /* Client: discard the Initial encryption keys as soon as
2703 * a handshake packet could be built.
2704 */
2705 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2706 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2707 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2708 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2709 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2710 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002711 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002712 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002713 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2714 }
2715 /* If the data for the current encryption level have all been sent,
2716 * select the next level.
2717 */
2718 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002719 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002720 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2721 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2722 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2723 tel = next_tel;
2724 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002725 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002726 /* If there is data for the next level, do not
2727 * consume a datagram.
2728 */
2729 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002730 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002731 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002732 /* If we have to build a new datagram, set the current datagram as
2733 * prepared into <cbuf>.
2734 */
2735 if (!prv_pkt) {
2736 qc_set_dg(cbuf, dglen, first_pkt);
2737 first_pkt = NULL;
2738 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002739 padding = 0;
2740 }
2741 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002742 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002743 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2744 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002745 }
2746 }
2747
2748 stop_build:
2749 /* Reset <wr> writer index if in front of <rd> index */
2750 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2751 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2752
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002753 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002754 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2755 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2756 /* Mark the end of contiguous data for the reader */
2757 write_u16(cb_wr(cbuf), 0);
2758 cb_add(cbuf, sizeof(uint16_t));
2759 }
2760 }
2761
Frédéric Lécaille9355d502022-02-23 09:38:01 +01002762 if (rd && rd <= cbuf->wr) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002763 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002764 /* Let's try to reuse this buffer */
2765 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002766 }
2767 }
2768
2769 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002770 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002771 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772
2773 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002774 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002775 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002776}
2777
2778/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002779 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002781int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002782{
2783 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002784 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002786 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002787 cbuf = qr->cbuf;
2788 while (cb_contig_data(cbuf)) {
2789 unsigned char *pos;
2790 struct buffer tmpbuf = { };
2791 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2792 uint16_t dglen;
2793 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794 unsigned int time_sent;
2795
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002796 pos = cb_rd(cbuf);
2797 dglen = read_u16(pos);
2798 /* End of prepared datagrams.
2799 * Reset the reader index only if in front of the writer index.
2800 */
2801 if (!dglen) {
2802 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2803
2804 if (wr && wr < cbuf->rd) {
2805 cb_rd_reset(cbuf);
2806 continue;
2807 }
2808 break;
2809 }
2810
2811 pos += sizeof dglen;
2812 first_pkt = read_ptr(pos);
2813 pos += sizeof first_pkt;
2814 tmpbuf.area = (char *)pos;
2815 tmpbuf.size = tmpbuf.data = dglen;
2816
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002817 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002818 for (pkt = first_pkt; pkt; pkt = pkt->next)
2819 quic_tx_packet_refinc(pkt);
Amaury Denoyelle58a77042022-02-09 15:43:07 +01002820 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002821 for (pkt = first_pkt; pkt; pkt = pkt->next)
2822 quic_tx_packet_refdec(pkt);
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002823 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002824 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002825
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002826 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 qc->tx.bytes += tmpbuf.data;
2828 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002829
2830 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2831 pkt->time_sent = time_sent;
2832 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2833 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002834 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002835 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002836 qc->path->in_flight += pkt->in_flight_len;
2837 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2838 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002839 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002840 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002841 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002842 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002843 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002844 }
2845 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002846
2847 return 1;
2848}
2849
2850/* Build all the frames which must be sent just after the handshake have succeeded.
2851 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2852 * a HANDSHAKE_DONE frame.
2853 * Return 1 if succeeded, 0 if not.
2854 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002855static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856{
2857 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002858 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859 struct quic_frame *frm;
2860
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002861 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002862 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002863 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002865 if (!frm)
2866 return 0;
2867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002868 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002869 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870 }
2871
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002872 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873 struct quic_connection_id *cid;
2874
2875 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002876 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877 goto err;
2878
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01002879 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002880 if (!cid)
2881 goto err;
2882
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002883 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01002884 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002885
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002887 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002889 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890
2891 return 1;
2892
2893 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894 return 0;
2895}
2896
2897/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002898void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002900 struct eb64_node *n;
2901 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002902
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002903 n = eb64_first(&arngs->root);
2904 while (n) {
2905 struct eb64_node *next;
2906
2907 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2908 next = eb64_next(n);
2909 eb64_delete(n);
2910 free(ar);
2911 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002912 }
2913}
2914
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002915/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2916 * descending order.
2917 */
2918static inline size_t sack_gap(struct quic_arng_node *p,
2919 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002920{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002921 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922}
2923
2924
2925/* Remove the last elements of <ack_ranges> list of ack range updating its
2926 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002927 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002928 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002929static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002930{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002931 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002932
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002933 last = eb64_last(&arngs->root);
2934 while (last && arngs->enc_sz > limit) {
2935 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002936
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002937 prev = eb64_prev(last);
2938 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002939 return 0;
2940
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002941 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2942 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2943 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2944 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2945 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2946 --arngs->sz;
2947 eb64_delete(last);
2948 pool_free(pool_head_quic_arng, last);
2949 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002950 }
2951
2952 return 1;
2953}
2954
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002955/* Set the encoded size of <arngs> QUIC ack ranges. */
2956static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2957{
2958 struct eb64_node *node, *next;
2959 struct quic_arng_node *ar, *ar_next;
2960
2961 node = eb64_last(&arngs->root);
2962 if (!node)
2963 return;
2964
2965 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2966 arngs->enc_sz = quic_int_getsize(ar->last) +
2967 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2968
2969 while ((next = eb64_prev(node))) {
2970 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2971 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2972 quic_int_getsize(ar_next->last - ar_next->first.key);
2973 node = next;
2974 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2975 }
2976}
2977
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002978/* Insert <ar> ack range into <argns> tree of ack ranges.
2979 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002980 */
2981static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002982struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2983 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002984{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002985 struct quic_arng_node *new_ar;
2986
2987 new_ar = pool_alloc(pool_head_quic_arng);
2988 if (new_ar) {
2989 new_ar->first.key = ar->first;
2990 new_ar->last = ar->last;
2991 eb64_insert(&arngs->root, &new_ar->first);
2992 arngs->sz++;
2993 }
2994
2995 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002996}
2997
2998/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003000 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001 *
3002 * Descending order
3003 * ------------->
3004 * range1 range2
3005 * ..........|--------|..............|--------|
3006 * ^ ^ ^ ^
3007 * | | | |
3008 * last1 first1 last2 first2
3009 * ..........+--------+--------------+--------+......
3010 * diff1 gap12 diff2
3011 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003012 * To encode the previous list of ranges we must encode integers as follows in
3013 * descending order:
3014 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 * with diff1 = last1 - first1
3016 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003017 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003018 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003019 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003020int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3021 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003023 struct eb64_node *le;
3024 struct quic_arng_node *new_node;
3025 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003027 new = NULL;
3028 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003029 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003030 if (!new_node)
3031 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003032
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003033 goto out;
3034 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003035
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003036 le = eb64_lookup_le(&arngs->root, ar->first);
3037 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003038 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003039 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003041
3042 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003044 else {
3045 struct quic_arng_node *le_ar =
3046 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003047
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003048 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003049 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003050 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003051
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003052 if (le_ar->last + 1 >= ar->first) {
3053 le_ar->last = ar->last;
3054 new = le;
3055 new_node = le_ar;
3056 }
3057 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003058 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003059 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003061
3062 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003063 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003064 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003066 /* Verify that the new inserted node does not overlap the nodes
3067 * which follow it.
3068 */
3069 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003070 struct eb64_node *next;
3071 struct quic_arng_node *next_node;
3072
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003073 while ((next = eb64_next(new))) {
3074 next_node =
3075 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003076 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003077 break;
3078
3079 if (next_node->last > new_node->last)
3080 new_node->last = next_node->last;
3081 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003082 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003083 /* Decrement the size of these ranges. */
3084 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003085 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086 }
3087
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003088 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003089 quic_arngs_set_enc_sz(arngs);
3090
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091 return 1;
3092}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003093/* Remove the header protection of packets at <el> encryption level.
3094 * Always succeeds.
3095 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003096static 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 +01003097{
3098 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003099 struct quic_rx_packet *pqpkt;
3100 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003101 struct quic_enc_level *app_qel;
3102
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003103 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3104 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003106 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003107 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003109 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110 goto out;
3111 }
3112 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003113 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003114 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003115 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003116 pqpkt->data, pqpkt->data + pqpkt->len)) {
3117 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003118 /* XXX TO DO XXX */
3119 }
3120 else {
3121 /* The AAD includes the packet number field */
3122 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3123 /* Store the packet into the tree of packets to decrypt. */
3124 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003125 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003126 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3127 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003128 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003129 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003131 MT_LIST_DELETE_SAFE(pkttmp1);
3132 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003133 }
3134
3135 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003136 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003137}
3138
3139/* Process all the CRYPTO frame at <el> encryption level.
3140 * Return 1 if succeeded, 0 if not.
3141 */
3142static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003143 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003144{
3145 struct eb64_node *node;
3146
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147 node = eb64_first(&el->rx.crypto.frms);
3148 while (node) {
3149 struct quic_rx_crypto_frm *cf;
3150
3151 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3152 if (cf->offset_node.key != el->rx.crypto.offset)
3153 break;
3154
3155 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3156 goto err;
3157
3158 node = eb64_next(node);
3159 quic_rx_packet_refdec(cf->pkt);
3160 eb64_delete(&cf->offset_node);
3161 pool_free(pool_head_quic_rx_crypto_frm, cf);
3162 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003163 return 1;
3164
3165 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003166 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003167 return 0;
3168}
3169
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003170/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003171 * 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 +02003172 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003173 * Return 1 if succeeded, 0 if not.
3174 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003175int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3176 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003178 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003179 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003180 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003181 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003182
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003183 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003184 qel = cur_el;
3185 next_tel:
3186 if (!qel)
3187 goto out;
3188
3189 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3190 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003191 while (node) {
3192 struct quic_rx_packet *pkt;
3193
3194 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003195 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003196 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003197 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003198 /* Drop the packet */
3199 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003200 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003201 }
3202 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003203 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003204 /* Drop the packet */
3205 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003206 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003207 }
3208 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003209 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3210
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003211 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003212 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003213 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003214 if (pkt->pn > largest_pn)
3215 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003216 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003217 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003218 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003219 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003220 }
3221 }
3222 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003223 eb64_delete(&pkt->pn_node);
3224 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003226 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003227
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003228 /* Update the largest packet number. */
3229 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003230 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3231 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003232 goto err;
3233
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003234 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003235 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003236 qel = next_el;
3237 goto next_tel;
3238 }
3239
3240 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003241 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003242 return 1;
3243
3244 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003245 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003246 return 0;
3247}
3248
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003249/* Check if it's possible to remove header protection for packets related to
3250 * encryption level <qel>. If <qel> is NULL, assume it's false.
3251 *
3252 * Return true if the operation is possible else false.
3253 */
3254static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3255{
3256 enum quic_tls_enc_level tel;
3257
3258 if (!qel)
3259 return 0;
3260
3261 tel = ssl_to_quic_enc_level(qel->level);
3262
3263 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003264 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003265 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003266 return 0;
3267 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003268
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003269 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003270 return 0;
3271
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003272 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003273 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
3274 qc->mux_state != QC_MUX_READY)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003275 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003276
3277 return 1;
3278}
3279
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003280/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003281static int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003282{
3283 int ret;
3284 struct qring *qr;
3285
3286 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3287 if (!qr)
3288 /* Never happens */
3289 return 1;
3290
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003291 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003292 if (ret == -1)
3293 goto err;
3294 else if (ret == 0)
3295 goto out;
3296
3297 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3298 goto err;
3299
3300 out:
3301 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3302 return 1;
3303
3304 err:
3305 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3306 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3307 return 0;
3308}
3309
3310/* QUIC connection packet handler task (post handshake) */
3311static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3312{
3313 struct ssl_sock_ctx *ctx;
3314 struct quic_conn *qc;
3315 struct quic_enc_level *qel;
3316 int st;
3317
3318
3319 ctx = context;
3320 qc = ctx->qc;
3321 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3322 st = HA_ATOMIC_LOAD(&qc->state);
3323
3324 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
3325
3326 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3327 qc_rm_hp_pkts(qc, qel);
3328
3329 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3330 goto err;
3331
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003332 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003333 goto err;
3334
3335 return t;
3336
3337 err:
3338 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st);
3339 return t;
3340}
3341
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003342/* QUIC connection packet handler task. */
3343struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003344{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003345 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003346 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003347 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348 enum quic_tls_enc_level tel, next_tel;
3349 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003350 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003351 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003352
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003353 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003354 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003355 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003356 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003357 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003358 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003359 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3360 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3361 /* The I/O handler has been woken up by the dgram listener
3362 * after the anti-amplification was reached.
3363 */
3364 qc_set_timer(qc);
3365 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3366 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3367 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003369 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3370 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3371 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003372 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003373 if (st >= QUIC_HS_ST_COMPLETE &&
3374 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3375 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3376 /* There may be remaining Handshake packets to treat and acknowledge. */
3377 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3378 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3379 }
3380 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003381 goto err;
3382
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003383 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003384 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003385
3386 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003387 /* Treat packets waiting for header packet protection decryption */
3388 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003389 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003390
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003391 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3392 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3393 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003394 goto err;
3395
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003396 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003397 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003398 qel = next_qel;
3399 next_qel = NULL;
3400 goto next_level;
3401 }
3402
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003403 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003404 if (st >= QUIC_HS_ST_COMPLETE) {
3405 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3406 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003407 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003408
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003409 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003410 QUIC_FL_TLS_SECRETS_DCD)) {
3411 /* Discard the Handshake keys. */
3412 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3413 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3414 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3415 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003416 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003417 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003418 }
3419
3420 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3421 /* There may be remaining handshake to build (acks) */
3422 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3423 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003424 }
3425
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003426 if (!qr)
3427 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003428 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3429 * be considered.
3430 */
3431 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003432 goto err;
3433 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003434 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003435 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003436 else if (ret == 0)
3437 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003438
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003439 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003440 goto err;
3441
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003442 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003443 /* Check if there is something to do for the next level.
3444 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003445 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003446 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003447 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003448 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003449 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003450 goto next_level;
3451 }
3452
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003453 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003454 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003455 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003456
3457 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003458 if (qr)
3459 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003460 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003461 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003462}
3463
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003464/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003465static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3466{
3467 int i;
3468
3469 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3470 if (qel->tx.crypto.bufs[i]) {
3471 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3472 qel->tx.crypto.bufs[i] = NULL;
3473 }
3474 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003475 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476}
3477
3478/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003479 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003480 * Returns 1 if succeeded, 0 if not.
3481 */
3482static int quic_conn_enc_level_init(struct quic_conn *qc,
3483 enum quic_tls_enc_level level)
3484{
3485 struct quic_enc_level *qel;
3486
3487 qel = &qc->els[level];
3488 qel->level = quic_to_ssl_enc_level(level);
3489 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3490 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3491 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003492 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003493
3494 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003495 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003496 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003497 qel->rx.crypto.offset = 0;
3498 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003499
3500 /* Allocate only one buffer. */
3501 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3502 if (!qel->tx.crypto.bufs)
3503 goto err;
3504
3505 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3506 if (!qel->tx.crypto.bufs[0])
3507 goto err;
3508
3509 qel->tx.crypto.bufs[0]->sz = 0;
3510 qel->tx.crypto.nb_buf = 1;
3511
3512 qel->tx.crypto.sz = 0;
3513 qel->tx.crypto.offset = 0;
3514
3515 return 1;
3516
3517 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003518 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003519 return 0;
3520}
3521
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003522/* Increment the <qc> refcount.
3523 *
3524 * This operation must be conducted when manipulating the quic_conn outside of
3525 * the connection pinned thread. These threads can only retrieve the connection
3526 * in the CID tree, so this function must be conducted under the CID lock.
3527 */
3528static inline void quic_conn_take(struct quic_conn *qc)
3529{
3530 HA_ATOMIC_INC(&qc->refcount);
3531}
3532
3533/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003534 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003535 */
3536static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003537{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003538 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003539 int i;
3540
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003541 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003542 return;
3543
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003544 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003545 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003546
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003547 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003548 if (conn_ctx) {
3549 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003550 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003551 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003552
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003553 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003554 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003555
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003556 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3557 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003558 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003559}
3560
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003561/* Release the quic_conn <qc>. It will decrement its refcount so that the
3562 * connection will be freed once all threads have finished to work with it. The
3563 * connection is removed from the CIDs tree and thus cannot be found by other
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003564 * threads after it. The connection tasklet is killed.
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003565 *
3566 * Do not use <qc> after it as it may be freed. This function must only be
3567 * called by the thread responsible of the quic_conn tasklet.
3568 */
3569static void quic_conn_release(struct quic_conn *qc)
3570{
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003571 struct ssl_sock_ctx *conn_ctx;
3572
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003573 /* remove the connection from receiver cids trees */
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003574 ebmb_delete(&qc->odcid_node);
3575 ebmb_delete(&qc->scid_node);
3576 free_quic_conn_cids(qc);
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003577
Amaury Denoyelle760da3b2022-01-20 17:43:02 +01003578 /* Kill the tasklet. Do not use tasklet_free as this is not thread safe
3579 * as other threads may call tasklet_wakeup after this.
3580 */
3581 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3582 if (conn_ctx)
3583 tasklet_kill(conn_ctx->wait_event.tasklet);
3584
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003585 quic_conn_drop(qc);
3586}
3587
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003588void quic_close(struct connection *conn, void *xprt_ctx)
3589{
3590 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003591 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003592
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003593 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003594 /* This task must be deleted by the connection-pinned thread. */
3595 if (qc->timer_task) {
3596 task_destroy(qc->timer_task);
3597 qc->timer_task = NULL;
3598 }
3599
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003600 /* Next application data can be dropped. */
3601 qc->mux_state = QC_MUX_RELEASED;
3602
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003603 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003604
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003605 /* TODO for now release the quic_conn on notification by the upper
3606 * layer. It could be useful to delay it if there is remaining data to
3607 * send or data to be acked.
3608 */
3609 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003610}
3611
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003613static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003614{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003615 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003616 struct quic_conn *qc;
3617 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003618 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003619
3620 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003621 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003622 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003623 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003624 task->expire = TICK_ETERNITY;
3625 pktns = quic_loss_pktns(qc);
3626 if (tick_isset(pktns->tx.loss_time)) {
3627 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3628
3629 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3630 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003631 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3632 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003633 goto out;
3634 }
3635
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003636 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003637 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003638 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003639 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3640 pktns->tx.pto_probe = 1;
3641 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3642 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3643 }
3644 else {
3645 pktns->tx.pto_probe = 2;
3646 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003648 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003649 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3650 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3651
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003652 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003654 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003655 iel->pktns->tx.pto_probe = 1;
3656 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003657
3658 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003659 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3660 continue;
3661
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003662 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003663 }
3664
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003665 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3666 qc->path->loss.pto_count++;
3667
3668 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003669 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003670
3671 return task;
3672}
3673
3674/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3675 * connections used to identify the first Initial packets of client connecting
3676 * to listeners. This parameter must be NULL for QUIC connections attached
3677 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3678 * <scid> is the source connection ID with <scid_len> as length.
3679 * Returns 1 if succeeded, 0 if not.
3680 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003681static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003682 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003683 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003684{
3685 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003686 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003687 /* Initial CID. */
3688 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003689 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003690 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003691
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003692 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003693 qc = pool_zalloc(pool_head_quic_conn);
3694 if (!qc) {
3695 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3696 goto err;
3697 }
3698
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003699 HA_ATOMIC_STORE(&qc->refcount, 0);
3700
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003701 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3702 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003703 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003704 goto err;
3705 }
3706
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707 qc->cids = EB_ROOT;
3708 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003709 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003710 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003711
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003712 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003713 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003714 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003715 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003716 qc->odcid.addrlen = dcid_addr_len;
3717 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003718
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003719 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003720 if (scid_len)
3721 memcpy(qc->dcid.data, scid, scid_len);
3722 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003723 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003724 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725 }
3726 /* QUIC Client (outgoing connection to servers) */
3727 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003728 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003729 if (dcid_len)
3730 memcpy(qc->dcid.data, dcid, dcid_len);
3731 qc->dcid.len = dcid_len;
3732 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003733 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003734
3735 /* Initialize the output buffer */
3736 qc->obuf.pos = qc->obuf.data;
3737
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003738 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003739 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003740 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003741 goto err;
3742 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003743
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003744 /* insert the allocated CID in the receiver datagram handler tree */
3745 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003746 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003747
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748 /* Select our SCID which is the first CID with 0 as sequence number. */
3749 qc->scid = icid->cid;
3750
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751 /* Packet number spaces initialization. */
3752 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3753 quic_pktns_init(&qc->pktns[i]);
3754 /* QUIC encryption level context initialization. */
3755 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003756 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003757 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003758 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003759 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003760 /* Initialize the packet number space. */
3761 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3762 }
3763
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003764 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003765 qc->tps_tls_ext = qc->version & 0xff000000 ?
3766 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3767 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003768 /* TX part. */
3769 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003770 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3771 qc->tx.wbuf = qc->tx.rbuf = 0;
3772 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003773 /* RX part. */
3774 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003775 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003776 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003777 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003778 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003779 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003780 goto err;
3781 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003783 /* XXX TO DO: Only one path at this time. */
3784 qc->path = &qc->paths[0];
3785 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3786
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003787 /* required to use MTLIST_IN_LIST */
3788 MT_LIST_INIT(&qc->accept_list);
3789
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003790 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003791
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003792 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003793
3794 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003795 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003796 pool_free(pool_head_quic_conn_rxbuf, buf_area);
3797 if (qc)
3798 qc->rx.buf.area = NULL;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003799 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003800 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003801}
3802
3803/* Initialize the timer task of <qc> QUIC connection.
3804 * Returns 1 if succeeded, 0 if not.
3805 */
3806static int quic_conn_init_timer(struct quic_conn *qc)
3807{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003808 /* Attach this task to the same thread ID used for the connection */
3809 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003810 if (!qc->timer_task)
3811 return 0;
3812
3813 qc->timer = TICK_ETERNITY;
3814 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01003815 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816
3817 return 1;
3818}
3819
3820/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3821 * past one byte of this buffer.
3822 */
3823static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3824 struct quic_rx_packet *pkt)
3825{
3826 unsigned char dcid_len, scid_len;
3827
3828 /* Version */
3829 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3830 return 0;
3831
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003832 /* Destination Connection ID Length */
3833 dcid_len = *(*buf)++;
3834 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3835 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3836 /* XXX MUST BE DROPPED */
3837 return 0;
3838
3839 if (dcid_len) {
3840 /* Check that the length of this received DCID matches the CID lengths
3841 * of our implementation for non Initials packets only.
3842 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003843 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3844 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003845 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003846 return 0;
3847
3848 memcpy(pkt->dcid.data, *buf, dcid_len);
3849 }
3850
3851 pkt->dcid.len = dcid_len;
3852 *buf += dcid_len;
3853
3854 /* Source Connection ID Length */
3855 scid_len = *(*buf)++;
3856 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3857 /* XXX MUST BE DROPPED */
3858 return 0;
3859
3860 if (scid_len)
3861 memcpy(pkt->scid.data, *buf, scid_len);
3862 pkt->scid.len = scid_len;
3863 *buf += scid_len;
3864
3865 return 1;
3866}
3867
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003868/* Insert <pkt> RX packet in its <qel> RX packets tree */
3869static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3870{
3871 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003872 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003873 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3874 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3875 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003876}
3877
3878/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003879 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3880 * byte past the end of the buffer containing this packet and <beg> the address of
3881 * the packet first byte.
3882 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3883 * Returns 1 if succeeded, 0 if not.
3884 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003885static inline int qc_try_rm_hp(struct quic_conn *qc,
3886 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003887 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003888 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003889 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003890{
3891 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003892 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003893 struct quic_enc_level *qel;
3894 /* Only for traces. */
3895 struct quic_rx_packet *qpkt_trace;
3896
3897 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003898 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003899 /* The packet number is here. This is also the start minus
3900 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3901 * protection.
3902 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003903 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003904
3905 tel = quic_packet_type_enc_level(pkt->type);
3906 qel = &qc->els[tel];
3907
3908 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003909 /* Note that the following function enables us to unprotect the packet
3910 * number and its length subsequently used to decrypt the entire
3911 * packets.
3912 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003913 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3914 qel->pktns->rx.largest_pn, pn, beg, end)) {
3915 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003916 goto err;
3917 }
3918
3919 /* The AAD includes the packet number field found at <pn>. */
3920 pkt->aad_len = pn - beg + pkt->pnl;
3921 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003922 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01003923 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003924 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003925 /* If the packet number space has been discarded, this packet
3926 * will be not parsed.
3927 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003928 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003929 goto out;
3930 }
3931
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003932 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003933 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003934 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3935 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003936 }
3937
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003938 *el = qel;
3939 /* No reference counter incrementation here!!! */
3940 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3941 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3942 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3943 b_add(&qc->rx.buf, pkt->len);
3944 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003945 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003946 return 1;
3947
3948 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003949 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003950 return 0;
3951}
3952
3953/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3954 * Also set <*long_header> to 1 if this form is long, 0 if not.
3955 */
3956static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3957 unsigned char byte0, int *long_header)
3958{
3959 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3960 pkt->type =
3961 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3962 *long_header = 1;
3963 }
3964 else {
3965 pkt->type = QUIC_PACKET_TYPE_SHORT;
3966 *long_header = 0;
3967 }
3968}
3969
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003970/*
3971 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3972 */
3973static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3974{
3975 int j = 0, version;
3976
3977 do {
3978 version = quic_supported_version[j];
3979 if (version == pkt->version)
3980 return 1;
3981
3982 version = quic_supported_version[++j];
3983 } while(version);
3984
3985 return 0;
3986}
3987
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003988/*
3989 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3990 * address <addr>.
3991 * Implementation of RFC9000 6. Version Negotiation
3992 *
3993 * TODO implement a rate-limiting sending of Version Negotiation packets
3994 *
3995 * Returns 0 on success else non-zero
3996 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003997static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3998 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003999{
4000 char buf[256];
4001 int i = 0, j, version;
4002 const socklen_t addrlen = get_addr_len(addr);
4003
4004 /*
4005 * header form
4006 * long header, fixed bit to 0 for Version Negotiation
4007 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004008 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4009 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004010
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004011 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004012 /* null version for Version Negotiation */
4013 buf[i++] = '\x00';
4014 buf[i++] = '\x00';
4015 buf[i++] = '\x00';
4016 buf[i++] = '\x00';
4017
4018 /* source connection id */
4019 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004020 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004021 i += pkt->scid.len;
4022
4023 /* destination connection id */
4024 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004025 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004026 i += pkt->dcid.len;
4027
4028 /* supported version */
4029 j = 0;
4030 do {
4031 version = htonl(quic_supported_version[j]);
4032 memcpy(&buf[i], &version, sizeof(version));
4033 i += sizeof(version);
4034
4035 version = quic_supported_version[++j];
4036 } while (version);
4037
4038 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4039 return 1;
4040
4041 return 0;
4042}
4043
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004044/* Generate the token to be used in Retry packets. The token is written to
4045 * <buf> which is expected to be <len> bytes.
4046 *
4047 * Various parameters are expected to be encoded in the token. For now, only
4048 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4049 * as this CID must be repeated by the server in the transport parameters.
4050 *
4051 * TODO add the client address to validate the token origin.
4052 *
4053 * Returns the length of the encoded token or 0 on error.
4054 */
4055static int generate_retry_token(unsigned char *buf, unsigned char len,
4056 struct quic_rx_packet *pkt)
4057{
4058 const size_t token_len = 1 + pkt->dcid.len;
4059 unsigned char i = 0;
4060
4061 if (token_len > len)
4062 return 0;
4063
4064 buf[i++] = pkt->dcid.len;
4065 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4066 i += pkt->dcid.len;
4067
4068 return i;
4069}
4070
4071/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4072 * the Initial <pkt> packet.
4073 *
4074 * Returns 0 on success else non-zero.
4075 */
4076static int send_retry(int fd, struct sockaddr_storage *addr,
4077 struct quic_rx_packet *pkt)
4078{
4079 unsigned char buf[128];
4080 int i = 0, token_len;
4081 const socklen_t addrlen = get_addr_len(addr);
4082 struct quic_cid scid;
4083
4084 /* long header + fixed bit + packet type 0x3 */
4085 buf[i++] = 0xf0;
4086 /* version */
4087 buf[i++] = 0x00;
4088 buf[i++] = 0x00;
4089 buf[i++] = 0x00;
4090 buf[i++] = 0x01;
4091
4092 /* Use the SCID from <pkt> for Retry DCID. */
4093 buf[i++] = pkt->scid.len;
4094 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4095 i += pkt->scid.len;
4096
4097 /* Generate a new CID to be used as SCID for the Retry packet. */
4098 scid.len = QUIC_HAP_CID_LEN;
4099 if (RAND_bytes(scid.data, scid.len) != 1)
4100 return 1;
4101
4102 buf[i++] = scid.len;
4103 memcpy(&buf[i], scid.data, scid.len);
4104 i += scid.len;
4105
4106 /* token */
4107 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4108 return 1;
4109 i += token_len;
4110
4111 /* token integrity tag */
4112 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4113 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4114 pkt->dcid.len, buf, i)) {
4115 return 1;
4116 }
4117
4118 i += QUIC_TLS_TAG_LEN;
4119
4120 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4121 return 1;
4122
4123 return 0;
4124}
4125
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004126/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4127 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4128 * concatenated to the <pkt> DCID field.
4129 *
4130 * Returns the instance or NULL if not found.
4131 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004132static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004133 struct listener *l,
4134 struct sockaddr_storage *saddr)
4135{
4136 struct quic_conn *qc = NULL;
4137 struct ebmb_node *node;
4138 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004139 /* set if the quic_conn is found in the second DCID tree */
4140 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004141
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004142 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4143 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4144 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4145 /* DCIDs of first packets coming from multiple clients may have
4146 * the same values. Let's distinguish them by concatenating the
4147 * socket addresses.
4148 */
4149 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004150 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004151 pkt->dcid.len + pkt->dcid.addrlen);
4152 if (node) {
4153 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4154 goto end;
4155 }
4156 }
4157
4158 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4159 * also for INITIAL/0-RTT non-first packets with the final DCID in
4160 * used.
4161 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004162 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004163 if (!node)
4164 goto end;
4165
4166 id = ebmb_entry(node, struct quic_connection_id, node);
4167 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004168 found_in_dcid = 1;
4169
4170 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004171 if (qc)
4172 quic_conn_take(qc);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004173
4174 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4175 * If already done, this is a noop.
4176 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004177 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004178 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004179
4180 return qc;
4181}
4182
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004183/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4184 * will extract the parameters stored in the token : <odcid>.
4185 *
4186 * Returns 0 on success else non-zero.
4187 */
4188static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4189 struct quic_cid *odcid)
4190{
4191 uint64_t odcid_len;
4192
4193 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4194 return 1;
4195
4196 memcpy(odcid->data, token, odcid_len);
4197 odcid->len = odcid_len;
4198
4199 return 0;
4200}
4201
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004202/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4203 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4204 * parameters of this session.
4205 * This is the responsibility of the caller to check the validity of all the
4206 * pointers passed as parameter to this function.
4207 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4208 * CO_ER_SSL_NO_MEM.
4209 */
4210static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4211 unsigned char *params, size_t params_len)
4212{
4213 int retry;
4214
4215 retry = 1;
4216 retry:
4217 *ssl = SSL_new(ssl_ctx);
4218 if (!*ssl) {
4219 if (!retry--)
4220 goto err;
4221
4222 pool_gc(NULL);
4223 goto retry;
4224 }
4225
4226 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4227 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4228 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004229 SSL_free(*ssl);
4230 *ssl = NULL;
4231 if (!retry--)
4232 goto err;
4233
4234 pool_gc(NULL);
4235 goto retry;
4236 }
4237
4238 return 0;
4239
4240 err:
4241 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4242 return -1;
4243}
4244
4245/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4246 * used to process <qc> received packets. The allocated context is stored in
4247 * <qc.xprt_ctx>.
4248 *
4249 * Returns 0 on success else non-zero.
4250 */
4251int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4252{
4253 struct bind_conf *bc = qc->li->bind_conf;
4254 struct ssl_sock_ctx *ctx = NULL;
4255
4256 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4257 if (!ctx)
4258 goto err;
4259
4260 ctx->wait_event.tasklet = tasklet_new();
4261 if (!ctx->wait_event.tasklet)
4262 goto err;
4263
4264 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4265 ctx->wait_event.tasklet->context = ctx;
4266 ctx->wait_event.events = 0;
4267 ctx->subs = NULL;
4268 ctx->xprt_ctx = NULL;
4269 ctx->qc = qc;
4270
4271 /* Set tasklet tid based on the SCID selected by us for this
4272 * connection. The upper layer will also be binded on the same thread.
4273 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004274 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004275
4276 if (qc_is_listener(qc)) {
4277 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4278 qc->enc_params, qc->enc_params_len) == -1) {
4279 goto err;
4280 }
4281
4282 /* Enabling 0-RTT */
4283 if (bc->ssl_conf.early_data)
4284 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4285
4286 SSL_set_accept_state(ctx->ssl);
4287 }
4288
4289 ctx->xprt = xprt_get(XPRT_QUIC);
4290
4291 /* Store the allocated context in <qc>. */
4292 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4293
4294 return 0;
4295
4296 err:
4297 if (ctx && ctx->wait_event.tasklet)
4298 tasklet_free(ctx->wait_event.tasklet);
4299 pool_free(pool_head_quic_conn_ctx, ctx);
4300
4301 return 1;
4302}
4303
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004304static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004305 struct quic_rx_packet *pkt, int first_pkt,
4306 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004307{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004308 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004309 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004310 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004311 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004312 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004313 size_t b_cspace;
4314 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004315
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004316 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004318 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004319 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004320 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4321 /* This ist only to please to traces and distinguish the
4322 * packet with parsed packet number from others.
4323 */
4324 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004325 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004326 goto err;
4327
4328 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004329 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004330 /* XXX TO BE DISCARDED */
4331 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4332 goto err;
4333 }
4334
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004335 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004336 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004337 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004338 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004339 uint64_t len;
4340
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004341 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004342 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4343 goto err;
4344 }
4345
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004346 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4347 * they must have the same DCID.
4348 */
4349 if (!first_pkt &&
4350 (pkt->dcid.len != dgram->dcid_len ||
4351 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4352 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4353 goto err;
4354 }
4355
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004356 /* Retry of Version Negotiation packets are only sent by servers */
4357 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4358 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4359 goto err;
4360 }
4361
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004362 /* RFC9000 6. Version Negotiation */
4363 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004364 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004365 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004366 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4367 goto err;
4368 }
4369
4370 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004371 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004372 }
4373
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004374 /* For Initial packets, and for servers (QUIC clients connections),
4375 * there is no Initial connection IDs storage.
4376 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004377 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004378 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004379
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004380 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4381 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004382 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4383 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004384 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004385
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004386 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4387 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004388 */
4389 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004390
4391 /* TODO Retry should be automatically activated if
4392 * suspect network usage is detected.
4393 */
4394 if (!token_len && l->bind_conf->quic_force_retry) {
4395 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004396 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004397 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4398 goto err;
4399 }
4400
4401 goto err;
4402 }
4403 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004404 pkt->token = buf;
4405 buf += pkt->token_len;
4406 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004407 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004408 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004409 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4411 goto err;
4412 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004413 }
4414
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004415 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4416 end - buf < len) {
4417 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4418 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004419 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004420
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004421 payload = buf;
4422 pkt->len = len + payload - beg;
4423
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004424 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004425 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004426 int ipv4;
4427 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004428 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004429 const unsigned char *salt = initial_salt_v1;
4430 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004431
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004432 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004433 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004434 goto err;
4435 }
4436
Frédéric Lécailledc364042022-01-27 16:51:54 +01004437 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4438 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4439 goto err;
4440 }
4441
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004442 pkt->saddr = dgram->saddr;
4443 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004444 qc = qc_new_conn(pkt->version, ipv4,
4445 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004446 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004447 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004448 goto err;
4449
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004450 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4451
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004452 odcid = &qc->rx.params.original_destination_connection_id;
4453 /* Copy the transport parameters. */
4454 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004455
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004456 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004457 if (pkt->token_len) {
4458 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4459 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4460 goto err;
4461 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004462 /* Copy retry_source_connection_id transport parameter. */
4463 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4464 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004465 }
4466 else {
4467 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4468 odcid->len = pkt->dcid.len;
4469 }
4470
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004471 /* Copy the initial source connection ID. */
4472 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4473 qc->enc_params_len =
4474 quic_transport_params_encode(qc->enc_params,
4475 qc->enc_params + sizeof qc->enc_params,
4476 &qc->rx.params, 1);
4477 if (!qc->enc_params_len)
4478 goto err;
4479
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004480 if (qc_conn_alloc_ssl_ctx(qc))
4481 goto err;
4482
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004483 if (!quic_conn_init_timer(qc))
4484 goto err;
4485
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004486 /* NOTE: the socket address has been concatenated to the destination ID
4487 * chosen by the client for Initial packets.
4488 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004489 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4490 salt = initial_salt_draft_29;
4491 salt_len = sizeof initial_salt_draft_29;
4492 }
4493 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004494 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004495 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004496 goto err;
4497 }
4498
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004499 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004500 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004501 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004502
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004503 /* If the insertion failed, it means that another
4504 * thread has already allocated a QUIC connection for
4505 * the same CID. Liberate our allocated connection.
4506 */
4507 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004508 qc_to_purge = qc;
4509
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004510 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4511 pkt->qc = qc;
4512 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004513
4514 quic_conn_take(qc);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004515
4516 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004517 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004518 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004519 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004520 else {
4521 quic_conn_drop(qc_to_purge);
4522 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523 }
4524 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004525 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004526 }
4527 }
4528 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004529 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004530 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4531 goto err;
4532 }
4533
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004534 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004535 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004536
4537 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4538 * they must have the same DCID.
4539 */
4540 if (!first_pkt &&
4541 (pkt->dcid.len != dgram->dcid_len ||
4542 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4543 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4544 goto err;
4545 }
4546
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004547 buf += QUIC_HAP_CID_LEN;
4548
4549 /* A short packet is the last one of a UDP datagram. */
4550 payload = buf;
4551 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004552
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004553 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004554 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004555 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004556 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4557 goto err;
4558 }
4559
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004560 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004561 }
4562
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004563
4564 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4565 * they must have the same DCID.
4566 *
4567 * This check must be done after the final update to pkt.len to
4568 * properly drop the packet on failure.
4569 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004570 if (first_pkt && !quic_peer_validated_addr(qc) &&
4571 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4572 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4573 QUIC_EV_CONN_LPKT, qc);
4574 /* Reset the anti-amplification bit. It will be set again
4575 * when sending the next packet if reached again.
4576 */
4577 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4578 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4579 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004580 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004581
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004582 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004583
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004584 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004585 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004586 goto out;
4587 }
4588
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004589 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004590 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004591 b_cspace = b_contig_space(&qc->rx.buf);
4592 if (b_cspace < pkt->len) {
4593 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004594 if (b_cspace) {
4595 b_putchr(&qc->rx.buf, 0x00);
4596 b_cspace--;
4597 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004598 b_add(&qc->rx.buf, b_cspace);
4599 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004600 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004601 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004602 goto err;
4603 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004604 }
4605
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004606 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004607 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004608 goto err;
4609 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004610
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004611 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004612 if (pkt->aad_len)
4613 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004614 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004615 /* Wake up the connection packet handler task from here only if all
4616 * the contexts have been initialized, especially the mux context
4617 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4618 * will start it if these contexts for the connection are not already
4619 * initialized.
4620 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004621 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004622 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004623 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004624
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004625 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004626
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004627 if (qc)
4628 quic_conn_drop(qc);
4629
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004630 return pkt->len;
4631
4632 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004633 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4634 * This cannot be done by this thread.
4635 */
4636 if (io_cb_wakeup) {
4637 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4638 if (conn_ctx && conn_ctx->wait_event.tasklet)
4639 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4640 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004641 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004642 if (!pkt->len)
4643 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004644 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004645 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004646
4647 if (qc)
4648 quic_conn_drop(qc);
4649
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004650 return -1;
4651}
4652
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004653/* This function builds into <buf> buffer a QUIC long packet header.
4654 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004655 */
4656static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4657 int type, size_t pn_len, struct quic_conn *conn)
4658{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004659 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660 return 0;
4661
4662 /* #0 byte flags */
4663 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4664 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4665 /* Version */
4666 quic_write_uint32(buf, end, conn->version);
4667 *(*buf)++ = conn->dcid.len;
4668 /* Destination connection ID */
4669 if (conn->dcid.len) {
4670 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4671 *buf += conn->dcid.len;
4672 }
4673 /* Source connection ID */
4674 *(*buf)++ = conn->scid.len;
4675 if (conn->scid.len) {
4676 memcpy(*buf, conn->scid.data, conn->scid.len);
4677 *buf += conn->scid.len;
4678 }
4679
4680 return 1;
4681}
4682
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004683/* This function builds into <buf> buffer a QUIC short packet header.
4684 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004685 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004686static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4687 size_t pn_len, struct quic_conn *conn,
4688 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004689{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004690 if (end - *buf < 1 + conn->dcid.len)
4691 return 0;
4692
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004693 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004694 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4695 ((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 +01004696 /* Destination connection ID */
4697 if (conn->dcid.len) {
4698 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4699 *buf += conn->dcid.len;
4700 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004701
4702 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004703}
4704
4705/* Apply QUIC header protection to the packet with <buf> as first byte address,
4706 * <pn> as address of the Packet number field, <pnlen> being this field length
4707 * with <aead> as AEAD cipher and <key> as secret key.
4708 * Returns 1 if succeeded or 0 if failed.
4709 */
4710static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4711 const EVP_CIPHER *aead, const unsigned char *key)
4712{
4713 int i, ret, outlen;
4714 EVP_CIPHER_CTX *ctx;
4715 /* We need an IV of at least 5 bytes: one byte for bytes #0
4716 * and at most 4 bytes for the packet number
4717 */
4718 unsigned char mask[5] = {0};
4719
4720 ret = 0;
4721 ctx = EVP_CIPHER_CTX_new();
4722 if (!ctx)
4723 return 0;
4724
4725 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4726 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4727 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4728 goto out;
4729
4730 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4731 for (i = 0; i < pnlen; i++)
4732 pn[i] ^= mask[i + 1];
4733
4734 ret = 1;
4735
4736 out:
4737 EVP_CIPHER_CTX_free(ctx);
4738
4739 return ret;
4740}
4741
4742/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4743 * ACK ranges if needed to a value below <limit> in bytes.
4744 * Return 1 if succeeded, 0 if not.
4745 */
4746static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4747{
4748 size_t room, ack_delay_sz;
4749
4750 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4751 /* A frame is made of 1 byte for the frame type. */
4752 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004753 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004754 return 0;
4755
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004756 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004757}
4758
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004759/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4760 * 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 +01004761 * and <*len> the packet Length field initialized with the number of bytes already present
4762 * 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 +02004763 * <headlen> is the number of bytes already present in this packet before building frames.
4764 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004765 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004766 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004767 * Return 1 if succeeded, 0 if not.
4768 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004769static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004770 size_t room, size_t *len, size_t headlen,
4771 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004772 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004773{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004774 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004775 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004776
Frédéric Lécailleea604992020-12-24 13:01:37 +01004777 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004778 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004779 return 0;
4780
Frédéric Lécailleea604992020-12-24 13:01:37 +01004781 /* If we are not probing we must take into an account the congestion
4782 * control window.
4783 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004784 if (!qel->pktns->tx.pto_probe) {
4785 size_t remain = quic_path_prep_data(qc->path);
4786
4787 if (headlen > remain)
4788 return 0;
4789
4790 room = QUIC_MIN(room, remain - headlen);
4791 }
4792
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004793 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004794 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004795 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004796 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004797 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004798
Frédéric Lécailleea604992020-12-24 13:01:37 +01004799 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800 break;
4801
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004802 switch (cf->type) {
4803 case QUIC_FT_CRYPTO:
4804 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004805 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004806 /* Compute the length of this CRYPTO frame header */
4807 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4808 /* Compute the data length of this CRyPTO frame. */
4809 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4810 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004811 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004812 if (!dlen)
4813 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004814
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004815 /* CRYPTO frame length. */
4816 flen = hlen + quic_int_getsize(dlen) + dlen;
4817 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004818 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004819 /* Add the CRYPTO data length and its encoded length to the packet
4820 * length and the length of this length.
4821 */
4822 *len += flen;
4823 room -= flen;
4824 if (dlen == cf->crypto.len) {
4825 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004826 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004827 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004828 }
4829 else {
4830 struct quic_frame *new_cf;
4831
4832 new_cf = pool_alloc(pool_head_quic_frame);
4833 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004834 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004835 return 0;
4836 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004837
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004838 new_cf->type = QUIC_FT_CRYPTO;
4839 new_cf->crypto.len = dlen;
4840 new_cf->crypto.offset = cf->crypto.offset;
4841 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004842 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004843 /* Consume <dlen> bytes of the current frame. */
4844 cf->crypto.len -= dlen;
4845 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004846 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004847 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004848
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004849 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004850 /* Note that these frames are accepted in short packets only without
4851 * "Length" packet field. Here, <*len> is used only to compute the
4852 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004853 *
4854 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004855 * excepting the variable ones. Note that +1 is for the type of this frame.
4856 */
4857 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004858 ((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 +02004859 /* Compute the data length of this STREAM frame. */
4860 avail_room = room - hlen - *len;
4861 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004862 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004863
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004864 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004865 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004866 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4867 dlen = max_available_room(avail_room, &dlen_sz);
4868 if (dlen > cf->stream.len) {
4869 dlen = cf->stream.len;
4870 }
4871 dlen_sz = quic_int_getsize(dlen);
4872 flen = hlen + dlen_sz + dlen;
4873 }
4874 else {
4875 dlen = QUIC_MIN(avail_room, cf->stream.len);
4876 flen = hlen + dlen;
4877 }
4878 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004879 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004880 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004881 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004882 /* Add the STREAM data length and its encoded length to the packet
4883 * length and the length of this length.
4884 */
4885 *len += flen;
4886 room -= flen;
4887 if (dlen == cf->stream.len) {
4888 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004889 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004890 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004891 }
4892 else {
4893 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004894 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004895
4896 new_cf = pool_zalloc(pool_head_quic_frame);
4897 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004898 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004899 return 0;
4900 }
4901
4902 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004903 new_cf->stream.qcs = cf->stream.qcs;
4904 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004905 new_cf->stream.id = cf->stream.id;
4906 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4907 new_cf->stream.offset = cf->stream.offset;
4908 new_cf->stream.len = dlen;
4909 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4910 /* FIN bit reset */
4911 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4912 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004913 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004914 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4915 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004916 cf_buf = b_make(b_orig(cf->stream.buf),
4917 b_size(cf->stream.buf),
4918 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004919 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004920 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01004921 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004922 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004923 break;
4924
4925 default:
4926 flen = qc_frm_len(cf);
4927 BUG_ON(!flen);
4928 if (flen > room)
4929 continue;
4930
4931 *len += flen;
4932 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004933 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004934 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004935 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004936 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004937 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004938 }
4939
Frédéric Lécailleea604992020-12-24 13:01:37 +01004940 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004941}
4942
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004943/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004944 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4945 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01004946 * filling the buffer with as much frames as possible from <frms> list of
4947 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004948 * 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 +02004949 * 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 +02004950 * having returned from this function.
4951 * 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 +01004952 * number field in this packet. <pn_len> will also have the packet number
4953 * length as value.
4954 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004955 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004956 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004957static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4958 size_t dglen, struct quic_tx_packet *pkt,
4959 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004960 int ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01004961 struct quic_enc_level *qel, struct quic_conn *qc,
4962 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004963{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004964 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004965 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004966 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4967 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004968 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004969 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004970 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004971 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004972 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004973
Frédéric Lécailleea604992020-12-24 13:01:37 +01004974 /* Length field value with CRYPTO frames if present. */
4975 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004976 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004977 /* When not probing and not acking, and no immediate close is required,
4978 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004979 * the congestion controller window. So, we do not limit the size of this
4980 * packet if we have an ACK frame to send because an ACK frame is not
4981 * ack-eliciting. This size will be limited if we have ack-eliciting
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01004982 * frames to send from <frms>.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004983 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004984 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004985 size_t path_room;
4986
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004987 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004988 if (end - beg > path_room)
4989 end = beg + path_room;
4990 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004991
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004992 /* Ensure there is enough room for the TLS encryption tag and a zero token
4993 * length field if any.
4994 */
4995 if (end - pos < QUIC_TLS_TAG_LEN +
4996 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4997 goto no_room;
4998
4999 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02005000 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005001 /* packet number length */
5002 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005003 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005004 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005005 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005006 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005007 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005008 goto no_room;
5009
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005010 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5011 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005012 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005013 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005014 /* Build an ACK frame if required. */
5015 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005016 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005017 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005018 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005019 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5020 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5021 * that from here, we do not know if we will have to send a PING frame.
5022 * This will be decided after having computed the ack-eliciting frames
5023 * to be added to this packet.
5024 */
5025 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 +01005026 if (!ack_frm_len)
5027 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005028 }
5029
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005030 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005031 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005032 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005033 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005034 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005035
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005036 /* Initialize the length of the frames built below to <len>.
5037 * If any frame could be successfully built by qc_build_frms(),
5038 * we will have len_frms > len.
5039 */
5040 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005041 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005042 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005043 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005044 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005045 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005046
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005047 /* Length (of the remaining data). Must not fail because, the buffer size
5048 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5049 * for the encryption tag. It must be taken into an account for the length
5050 * of this packet.
5051 */
5052 if (len_frms)
5053 len = len_frms + QUIC_TLS_TAG_LEN;
5054 else
5055 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005056 /* CONNECTION_CLOSE frame */
5057 if (cc) {
5058 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005059
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005060 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005061 len += qc_frm_len(&cc_frm);
5062 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005063 add_ping_frm = 0;
5064 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005065 len_sz = quic_int_getsize(len);
5066 /* Add this packet size to <dglen> */
5067 dglen += head_len + len_sz + len;
5068 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5069 /* This is a maximum padding size */
5070 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5071 /* The length field value is of this packet is <len> + <padding_len>
5072 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005073 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005074 * sizes from <padding_len>.
5075 */
5076 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5077 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005078 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005079 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005080 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005081 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082 add_ping_frm = 1;
5083 len += 1;
5084 }
5085 /* 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 +01005086 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005087 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5088 }
5089
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005090 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5091 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005092
5093 /* Packet number field address. */
5094 *buf_pn = pos;
5095
5096 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005097 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5098 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005099
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005100 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005101 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005102
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005103 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005104 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005105 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005106
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005107 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005108 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005109 ssize_t room = end - pos;
5110 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005111 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005112 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005113 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005114 }
5115 }
5116
5117 /* Build a PING frame if needed. */
5118 if (add_ping_frm) {
5119 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005120 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005121 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005122 }
5123
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005124 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005125 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005126 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005127
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005128 /* Build a PADDING frame if needed. */
5129 if (padding_len) {
5130 frm.type = QUIC_FT_PADDING;
5131 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005132 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005133 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005134 }
5135
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005136 /* If this packet is ack-eliciting and we are probing let's
5137 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005138 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005139 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5140 qel->pktns->tx.pto_probe)
5141 qel->pktns->tx.pto_probe--;
5142
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005143 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005144 LIST_SPLICE(&pkt->frms, &frm_list);
5145 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005146
5147 return 1;
5148
5149 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005150 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005151 LIST_SPLICE(frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005152 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005153 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005154}
5155
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005156static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005157{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005158 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005159 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005160 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005161 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005163 pkt->next = NULL;
5164 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005165}
5166
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005167/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005168static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5169{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005170 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005171
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005172 if (!pkt)
5173 return;
5174
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005175 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005176 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005177 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005178 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005179 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005180}
5181
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005182/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005183 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
5184 * of prebuilt frames.
5185 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005186 * Return -2 if the packet could not be allocated or encrypted for any reason,
5187 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005188 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005189static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5190 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005191 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005192 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005193 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005194{
5195 /* The pointer to the packet number field. */
5196 unsigned char *buf_pn;
5197 unsigned char *beg, *end, *payload;
5198 int64_t pn;
5199 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005200 struct quic_tls_ctx *tls_ctx;
5201 struct quic_tx_packet *pkt;
5202
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005203 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005204 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005205 pkt = pool_alloc(pool_head_quic_tx_packet);
5206 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005207 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005208 *err = -2;
5209 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005210 }
5211
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005212 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005213 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005214 pn_len = 0;
5215 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005216
5217 pn = qel->pktns->tx.next_pn + 1;
5218 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005219 ack, padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005220 *err = -1;
5221 goto err;
5222 }
5223
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005224 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005225 payload = buf_pn + pn_len;
5226 payload_len = end - payload;
5227 aad_len = payload - beg;
5228
5229 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005230 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005231 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005232 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005233 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005234
5235 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005236 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5238 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005239 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005240 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005241 goto err;
5242 }
5243
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005244 /* Consume a packet number */
5245 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005246 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005247 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5248 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005249 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5250 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005251 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005252 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005253 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005254 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005255 pkt->in_flight_len = pkt->len;
5256 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005257 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005258 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005259 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005260
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005261 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005262
5263 err:
5264 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005265 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005266 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005267}
5268
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005269
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005270/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5271 * event subscriber <es> is not allowed to change from a previous call as long
5272 * as at least one event is still subscribed. The <event_type> must only be a
5273 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5274 */
5275static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5276{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005277 struct qcc *qcc = conn->qc->qcc;
5278
5279 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5280 BUG_ON(qcc->subs && qcc->subs != es);
5281
5282 es->events |= event_type;
5283 qcc->subs = es;
5284
5285 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005286 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005287
5288 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005289 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005290
5291 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005292}
5293
5294/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5295 * The <es> pointer is not allowed to differ from the one passed to the
5296 * subscribe() call. It always returns zero.
5297 */
5298static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5299{
5300 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5301}
5302
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005303/* Store in <xprt_ctx> the context attached to <conn>.
5304 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005305 */
5306static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5307{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005308 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005309
5310 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5311
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005312 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005313 if (*xprt_ctx)
5314 goto out;
5315
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005316 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005317
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005318 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005319 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005320
5321 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005322}
5323
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005324/* Start the QUIC transport layer */
5325static int qc_xprt_start(struct connection *conn, void *ctx)
5326{
5327 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005328 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005329
5330 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005331 quic_mux_transport_params_update(qc->qcc);
5332 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5333 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5334 return 0;
5335 }
5336
5337 /* mux-quic can now be considered ready. */
5338 qc->mux_state = QC_MUX_READY;
5339
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005340 tasklet_wakeup(qctx->wait_event.tasklet);
5341 return 1;
5342}
5343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005344/* transport-layer operations for QUIC connections. */
5345static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005346 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005347 .subscribe = quic_conn_subscribe,
5348 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005349 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005350 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005351 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5352 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005353 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005354 .name = "QUIC",
5355};
5356
5357__attribute__((constructor))
5358static void __quic_conn_init(void)
5359{
5360 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5361 xprt_register(XPRT_QUIC, &ssl_quic);
5362}
5363
5364__attribute__((destructor))
5365static void __quic_conn_deinit(void)
5366{
5367 BIO_meth_free(ha_quic_meth);
5368}
5369
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005370/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5371 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005372 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005373 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005374struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005375{
5376 unsigned char *pos;
5377 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005378 struct quic_dghdlr *dghdlr = ctx;
5379 struct quic_dgram *dgram;
5380 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005381
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005382 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005383 pos = dgram->buf;
5384 end = pos + dgram->len;
5385 do {
5386 int ret;
5387 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005388
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005389 pkt = pool_zalloc(pool_head_quic_rx_packet);
5390 if (!pkt)
5391 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005392
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005393 quic_rx_packet_refinc(pkt);
5394 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5395 first_pkt = 0;
5396 pos += pkt->len;
5397 quic_rx_packet_refdec(pkt);
5398 if (ret == -1)
5399 /* If the packet length could not be found, we cannot continue. */
5400 break;
5401 } while (pos < end);
5402
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005403 /* Increasing the received bytes counter by the UDP datagram length
5404 * if this datagram could be associated to a connection.
5405 */
5406 if (dgram->qc)
5407 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005408
5409 /* Mark this datagram as consumed */
5410 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005411 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005412
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005413 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005414
5415 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005416 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005417}
5418
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005419/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5420 * Returns 1 if succeeded, 0 if not.
5421 */
5422static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5423 unsigned char **dcid, size_t *dcid_len)
5424{
5425 int long_header;
5426 size_t minlen, skip;
5427
5428 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5429 goto err;
5430
5431 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5432 minlen = long_header ?
5433 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5434 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005435 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005436 goto err;
5437
5438 buf += skip;
5439 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5440 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5441 goto err;
5442
5443 *dcid = buf;
5444
5445 return 1;
5446
5447 err:
5448 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5449 return 0;
5450}
5451
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005452/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5453 * correct datagram handler.
5454 * Return 1 if a correct datagram could be found, 0 if not.
5455 */
5456int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5457 struct sockaddr_storage *saddr,
5458 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005459{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005460 struct quic_dgram *dgram;
5461 unsigned char *dcid;
5462 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005463 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005464
5465 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005466 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005467
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005468 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005469 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005470 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005471
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005472 cid_tid = quic_get_cid_tid(dcid);
5473
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005474 /* All the members must be initialized! */
5475 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005476 dgram->buf = buf;
5477 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005478 dgram->dcid = dcid;
5479 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005480 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005481 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005482 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005483 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005484
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005485 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005486
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005487 return 1;
5488
5489 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005490 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005491}
5492
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005493/* Function to automatically activate QUIC traces on stdout.
5494 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5495 * Main use for now is in the docker image for QUIC interop testing.
5496 */
5497static void quic_init_stdout_traces(void)
5498{
5499#ifdef ENABLE_QUIC_STDOUT_TRACES
5500 trace_quic.sink = sink_find("stdout");
5501 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005502 trace_quic.state = TRACE_STATE_RUNNING;
5503#endif
5504}
5505INITCALL0(STG_INIT, quic_init_stdout_traces);
5506
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005507/*
5508 * Local variables:
5509 * c-indent-level: 8
5510 * c-basic-offset: 8
5511 * End:
5512 */