blob: 799621ba8d252ebeab9a10c0b381fdc713dafebe [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écaillece69cbc2022-03-22 12:45:33 +0100123 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100124 { /* end */ }
125};
126
127static const struct name_desc quic_trace_lockon_args[4] = {
128 /* arg1 */ { /* already used by the connection */ },
129 /* arg2 */ { .name="quic", .desc="QUIC transport" },
130 /* arg3 */ { },
131 /* arg4 */ { }
132};
133
134static const struct name_desc quic_trace_decoding[] = {
135#define QUIC_VERB_CLEAN 1
136 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
137 { /* end */ }
138};
139
140
141struct trace_source trace_quic = {
142 .name = IST("quic"),
143 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100144 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100145 .default_cb = quic_trace,
146 .known_events = quic_trace_events,
147 .lockon_args = quic_trace_lockon_args,
148 .decoding = quic_trace_decoding,
149 .report_events = ~0, /* report everything by default */
150};
151
152#define TRACE_SOURCE &trace_quic
153INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
154
155static BIO_METHOD *ha_quic_meth;
156
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200157DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100158DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200160 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_POOL(pool_head_quic_connection_id,
163 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100164DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100167DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100168DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100169DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200170DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100171DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100172
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200173static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +0100174 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100175 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +0100176 int padding, int ack, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100177static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100178static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100179
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100180/* Only for debug purpose */
181struct enc_debug_info {
182 unsigned char *payload;
183 size_t payload_len;
184 unsigned char *aad;
185 size_t aad_len;
186 uint64_t pn;
187};
188
189/* Initializes a enc_debug_info struct (only for debug purpose) */
190static inline void enc_debug_info_init(struct enc_debug_info *edi,
191 unsigned char *payload, size_t payload_len,
192 unsigned char *aad, size_t aad_len, uint64_t pn)
193{
194 edi->payload = payload;
195 edi->payload_len = payload_len;
196 edi->aad = aad;
197 edi->aad_len = aad_len;
198 edi->pn = pn;
199}
200
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100201/* Trace callback for QUIC.
202 * These traces always expect that arg1, if non-null, is of type connection.
203 */
204static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
205 const struct ist where, const struct ist func,
206 const void *a1, const void *a2, const void *a3, const void *a4)
207{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100208 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100210 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100211 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100212
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100213 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 if ((mask & QUIC_EV_CONN_INIT) && qc) {
215 chunk_appendf(&trace_buf, "\n odcid");
216 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100217 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100218 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100219 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100220 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100221 }
222
223 if (mask & QUIC_EV_CONN_ADDDATA) {
224 const enum ssl_encryption_level_t *level = a2;
225 const size_t *len = a3;
226
227 if (level) {
228 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
229
230 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
231 }
232 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100233 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100234 }
235 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
236 /* Initial read & write secrets. */
237 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
238 const unsigned char *rx_sec = a2;
239 const unsigned char *tx_sec = a3;
240
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100241 tls_ctx = &qc->els[level].tls_ctx;
242 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100243 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
244 if (rx_sec)
245 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100246 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100247 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
248 if (tx_sec)
249 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100250 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100251 }
252 }
253 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
254 const enum ssl_encryption_level_t *level = a2;
255 const unsigned char *secret = a3;
256 const size_t *secret_len = a4;
257
258 if (level) {
259 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
260
261 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
262 if (secret && secret_len)
263 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100264 tls_ctx = &qc->els[lvl].tls_ctx;
265 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
266 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100267 }
268 }
269
270 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
271 const enum ssl_encryption_level_t *level = a2;
272 const unsigned char *secret = a3;
273 const size_t *secret_len = a4;
274
275 if (level) {
276 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
277
278 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
279 if (secret && secret_len)
280 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100281 tls_ctx = &qc->els[lvl].tls_ctx;
282 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
283 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100284 }
285
286 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100288 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289 const struct quic_tx_packet *pkt = a2;
290 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292
293 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100294 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100295 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100296 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100297 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100298 (unsigned long long)qc->path->cwnd,
299 (unsigned long long)qc->path->prep_in_flight,
300 (unsigned long long)qc->path->in_flight,
301 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100302 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100303 }
304 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200305 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100306 if (pkt->pn_node.key != (uint64_t)-1)
307 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100308 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100309 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100310 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
311 (unsigned long long)qc->rx.bytes,
312 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100313 }
314
315 if (room) {
316 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
317 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
318 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 }
320 }
321
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100322 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100323 const enum quic_handshake_state *state = a2;
324 const int *err = a3;
325
326 if (state)
327 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
328 if (err)
329 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
330 }
331
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100332 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100333 const struct quic_rx_packet *pkt = a2;
334 const unsigned long *pktlen = a3;
335 const SSL *ssl = a4;
336
337 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100338 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100339 pkt, quic_packet_type_enc_level_char(pkt->type));
340 if (pkt->pnl)
341 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
342 (unsigned long long)pkt->pn);
343 if (pkt->token_len)
344 chunk_appendf(&trace_buf, " toklen=%llu",
345 (unsigned long long)pkt->token_len);
346 if (pkt->aad_len)
347 chunk_appendf(&trace_buf, " aadlen=%llu",
348 (unsigned long long)pkt->aad_len);
349 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
350 pkt->flags, (unsigned long long)pkt->len);
351 }
352 if (pktlen)
353 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
354 if (ssl) {
355 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
356 chunk_appendf(&trace_buf, " el=%c",
357 quic_enc_level_char(ssl_to_quic_enc_level(level)));
358 }
359 }
360
361 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
362 const struct quic_rx_packet *pkt = a2;
363 const struct quic_rx_crypto_frm *cf = a3;
364 const SSL *ssl = a4;
365
366 if (pkt)
367 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
368 quic_packet_type_enc_level_char(pkt->type),
369 (unsigned long long)pkt->pn);
370 if (cf)
371 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
372 (unsigned long long)cf->offset_node.key,
373 (unsigned long long)cf->len);
374 if (ssl) {
375 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200376 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100377 quic_enc_level_char(ssl_to_quic_enc_level(level)));
378 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100379
380 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100381 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100382 }
383
384 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
385 const struct quic_frame *frm = a2;
386
387 if (frm)
388 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
389 }
390
391 if (mask & QUIC_EV_CONN_PHPKTS) {
392 const struct quic_enc_level *qel = a2;
393
394 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100395 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100396 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100397 " 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 +0100398 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200399 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100400 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100401 (unsigned long long)qc->path->cwnd,
402 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100403 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100404 (unsigned long long)pktns->tx.in_flight,
405 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100406 }
407 }
408
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100409 if (mask & QUIC_EV_CONN_ENCPKT) {
410 const struct enc_debug_info *edi = a2;
411
412 if (edi)
413 chunk_appendf(&trace_buf,
414 " payload=@%p payload_len=%llu"
415 " aad=@%p aad_len=%llu pn=%llu",
416 edi->payload, (unsigned long long)edi->payload_len,
417 edi->aad, (unsigned long long)edi->aad_len,
418 (unsigned long long)edi->pn);
419 }
420
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100421 if (mask & QUIC_EV_CONN_RMHP) {
422 const struct quic_rx_packet *pkt = a2;
423
424 if (pkt) {
425 const int *ret = a3;
426
427 chunk_appendf(&trace_buf, " pkt@%p", pkt);
428 if (ret && *ret)
429 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
430 pkt->pnl, (unsigned long long)pkt->pn);
431 }
432 }
433
434 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200435 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100436 const unsigned long *val1 = a3;
437 const unsigned long *val2 = a4;
438
439 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100440 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100441 if (val1)
442 chunk_appendf(&trace_buf, " %lu", *val1);
443 if (val2)
444 chunk_appendf(&trace_buf, "..%lu", *val2);
445 }
446
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100447 if (mask & QUIC_EV_CONN_ACKSTRM) {
448 const struct quic_stream *s = a2;
449 const struct qcs *qcs = a3;
450
451 if (s)
452 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
453 if (qcs)
454 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)qcs->tx.ack_offset);
455 }
456
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100457 if (mask & QUIC_EV_CONN_RTTUPDT) {
458 const unsigned int *rtt_sample = a2;
459 const unsigned int *ack_delay = a3;
460 const struct quic_loss *ql = a4;
461
462 if (rtt_sample)
463 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
464 if (ack_delay)
465 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
466 if (ql)
467 chunk_appendf(&trace_buf,
468 " srtt=%ums rttvar=%ums min_rtt=%ums",
469 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
470 }
471 if (mask & QUIC_EV_CONN_CC) {
472 const struct quic_cc_event *ev = a2;
473 const struct quic_cc *cc = a3;
474
475 if (a2)
476 quic_cc_event_trace(&trace_buf, ev);
477 if (a3)
478 quic_cc_state_trace(&trace_buf, cc);
479 }
480
481 if (mask & QUIC_EV_CONN_PKTLOSS) {
482 const struct quic_pktns *pktns = a2;
483 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100484
485 if (pktns) {
486 chunk_appendf(&trace_buf, " pktns=%s",
487 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
488 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
489 if (pktns->tx.loss_time)
490 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100491 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100492 }
493 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
494 struct quic_tx_packet *pkt;
495
496 chunk_appendf(&trace_buf, " lost_pkts:");
497 list_for_each_entry(pkt, lost_pkts, list)
498 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
499 }
500 }
501
502 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 const struct quic_pktns *pktns = a2;
504 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100505 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100507 if (ifae_pkts)
508 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
509 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100510 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100511 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100513 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
514 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100515 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
516 if (pktns->tx.in_flight)
517 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100518 if (pktns->tx.loss_time)
519 chunk_appendf(&trace_buf, " loss_time=%dms",
520 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
521 }
522 if (mask & QUIC_EV_CONN_SPTO) {
523 if (pktns->tx.time_of_last_eliciting)
524 chunk_appendf(&trace_buf, " tole=%dms",
525 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
526 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100527 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 }
529 }
530
531 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
532 chunk_appendf(&trace_buf,
533 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
534 }
535 }
536
537 if (mask & QUIC_EV_CONN_SPPKTS) {
538 const struct quic_tx_packet *pkt = a2;
539
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100540 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
541 (unsigned long long)qc->path->cwnd,
542 (unsigned long long)qc->path->prep_in_flight,
543 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100544 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100545 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100546 (unsigned long)pkt->pn_node.key,
547 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
548 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100549 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100550 }
551 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100552
553 if (mask & QUIC_EV_CONN_SSLALERT) {
554 const uint8_t *alert = a2;
555 const enum ssl_encryption_level_t *level = a3;
556
557 if (alert)
558 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
559 if (level)
560 chunk_appendf(&trace_buf, " el=%c",
561 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
562 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100563
564 if (mask & QUIC_EV_CONN_BCFRMS) {
565 const size_t *sz1 = a2;
566 const size_t *sz2 = a3;
567 const size_t *sz3 = a4;
568
569 if (sz1)
570 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
571 if (sz2)
572 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
573 if (sz3)
574 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
575 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100576
577 if (mask & QUIC_EV_CONN_PSTRM) {
578 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100579
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100580 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100581 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100582 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100583 }
584 if (mask & QUIC_EV_CONN_LPKT) {
585 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200586 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100588 if (pkt) {
589 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
590 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
591 if (pkt->pn_node.key != (uint64_t)-1)
592 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
593 }
594
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200595 if (len)
596 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100597 }
598
599}
600
601/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100602static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100603{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200604 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100605
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100606 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100607 return 1;
608
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200609 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
610 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +0100611 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_PKT_RECEIVED) ||
612 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200613 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100614 return 1;
615
616 return 0;
617}
618
619/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
620 * both loss detection and PTO and schedule the task assiated to this timer if needed.
621 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100622static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100623{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100624 struct quic_pktns *pktns;
625 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200626 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100627
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100628 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100629 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100630
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100631 pktns = quic_loss_pktns(qc);
632 if (tick_isset(pktns->tx.loss_time)) {
633 qc->timer = pktns->tx.loss_time;
634 goto out;
635 }
636
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100637 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100638 * cancelled for a server which reached the anti-amplification limit.
639 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100640 if (!quic_peer_validated_addr(qc) &&
641 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100642 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100643 qc->timer = TICK_ETERNITY;
644 goto out;
645 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100647 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100648 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100649 /* Timer cancellation. */
650 qc->timer = TICK_ETERNITY;
651 goto out;
652 }
653
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200654 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
655 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100656 if (tick_isset(pto))
657 qc->timer = pto;
658 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100659 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100660 if (tick_is_expired(qc->timer, now_ms)) {
661 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100662 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100663 }
664 else {
665 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100666 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100667 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100668 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100669 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100670}
671
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100672/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
673 * connection.
674 * Return 1 if succeeded, 0 if not.
675 */
676static int quic_tls_key_update(struct quic_conn *qc)
677{
678 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
679 struct quic_tls_secrets *rx, *tx;
680 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
681 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
682
683 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
684 rx = &tls_ctx->rx;
685 tx = &tls_ctx->tx;
686 nxt_rx = &qc->ku.nxt_rx;
687 nxt_tx = &qc->ku.nxt_tx;
688
689 /* Prepare new RX secrets */
690 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
691 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100692 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100693 return 0;
694 }
695
696 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
697 nxt_rx->key, nxt_rx->keylen,
698 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
699 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100700 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100701 return 0;
702 }
703
704 /* Prepare new TX secrets */
705 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
706 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100707 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100708 return 0;
709 }
710
711 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
712 nxt_tx->key, nxt_tx->keylen,
713 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
714 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100715 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100716 return 0;
717 }
718
719 return 1;
720}
721
722/* Rotate the Key Update information for <qc> QUIC connection.
723 * Must be used after having updated them.
724 * Always succeeds.
725 */
726static void quic_tls_rotate_keys(struct quic_conn *qc)
727{
728 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
729 unsigned char *curr_secret, *curr_iv, *curr_key;
730
731 /* Rotate the RX secrets */
732 curr_secret = tls_ctx->rx.secret;
733 curr_iv = tls_ctx->rx.iv;
734 curr_key = tls_ctx->rx.key;
735
736 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
737 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
738 tls_ctx->rx.key = qc->ku.nxt_rx.key;
739
740 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
741 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
742 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
743
744 qc->ku.prv_rx.secret = curr_secret;
745 qc->ku.prv_rx.iv = curr_iv;
746 qc->ku.prv_rx.key = curr_key;
747 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
748
749 /* Update the TX secrets */
750 curr_secret = tls_ctx->tx.secret;
751 curr_iv = tls_ctx->tx.iv;
752 curr_key = tls_ctx->tx.key;
753
754 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
755 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
756 tls_ctx->tx.key = qc->ku.nxt_tx.key;
757
758 qc->ku.nxt_tx.secret = curr_secret;
759 qc->ku.nxt_tx.iv = curr_iv;
760 qc->ku.nxt_tx.key = curr_key;
761}
762
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100763#ifndef OPENSSL_IS_BORINGSSL
764int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
765 const uint8_t *read_secret,
766 const uint8_t *write_secret, size_t secret_len)
767{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100768 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
769 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 +0100770 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100771 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100772
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100773 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100774 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100775 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
776 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100777 goto out;
778 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100779
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100780 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100781 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100782 goto err;
783 }
784
785 rx = &tls_ctx->rx;
786 tx = &tls_ctx->tx;
787
788 rx->aead = tx->aead = tls_aead(cipher);
789 rx->md = tx->md = tls_md(cipher);
790 rx->hp = tx->hp = tls_hp(cipher);
791
792 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
793 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100794 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100795 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100796 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100797 }
798
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100799 /* Enqueue this connection asap if we could derive O-RTT secrets as
800 * listener. Note that a listener derives only RX secrets for this
801 * level.
802 */
803 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
804 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100805
806 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100807 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100808
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100809 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
810 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100811 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100812 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100813 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100814 }
815
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100816 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100817 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
818 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
819 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
820
821 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
822 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100823 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100824 goto err;
825 }
826
827 memcpy(rx->secret, read_secret, secret_len);
828 rx->secretlen = secret_len;
829 memcpy(tx->secret, write_secret, secret_len);
830 tx->secretlen = secret_len;
831 /* Initialize all the secret keys lengths */
832 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
833 /* Prepare the next key update */
834 if (!quic_tls_key_update(qc))
835 goto err;
836 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100837
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100838 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100839 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100840 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100841 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100842
843 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100844 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100845 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100846}
847#else
848/* ->set_read_secret callback to derive the RX secrets at <level> encryption
849 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500850 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100851 */
852int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
853 const SSL_CIPHER *cipher,
854 const uint8_t *secret, size_t secret_len)
855{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100856 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100857 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100858 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100859
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100860 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
861 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
862 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100863 goto out;
864 }
865
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100866 tls_ctx->rx.aead = tls_aead(cipher);
867 tls_ctx->rx.md = tls_md(cipher);
868 tls_ctx->rx.hp = tls_hp(cipher);
869
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100870 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
871 goto err;
872
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100873 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 +0100874 tls_ctx->rx.key, tls_ctx->rx.keylen,
875 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100876 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
877 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100878 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100879 goto err;
880 }
881
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100882 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100883 const unsigned char *buf;
884 size_t buflen;
885
886 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
887 if (!buflen)
888 goto err;
889
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100890 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891 goto err;
892 }
893
894 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100895 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100896 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897
898 return 1;
899
900 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100901 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100902 return 0;
903}
904
905/* ->set_write_secret callback to derive the TX secrets at <level>
906 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500907 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100908 */
909int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
910 const SSL_CIPHER *cipher,
911 const uint8_t *secret, size_t secret_len)
912{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100913 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
914 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 +0100915
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100916 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
917 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
918 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100919 goto out;
920 }
921
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100922 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
923 goto err;
924
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100925 tls_ctx->tx.aead = tls_aead(cipher);
926 tls_ctx->tx.md = tls_md(cipher);
927 tls_ctx->tx.hp = tls_hp(cipher);
928
929 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 +0100930 tls_ctx->tx.key, tls_ctx->tx.keylen,
931 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100932 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
933 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100934 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935 goto err;
936 }
937
938 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100939 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100940 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100941 return 1;
942
943 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100944 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100945 return 0;
946}
947#endif
948
949/* This function copies the CRYPTO data provided by the TLS stack found at <data>
950 * with <len> as size in CRYPTO buffers dedicated to store the information about
951 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
952 * It fails only if it could not managed to allocate enough CRYPTO buffers to
953 * store all the data.
954 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
955 */
956static int quic_crypto_data_cpy(struct quic_enc_level *qel,
957 const unsigned char *data, size_t len)
958{
959 struct quic_crypto_buf **qcb;
960 /* The remaining byte to store in CRYPTO buffers. */
961 size_t cf_offset, cf_len, *nb_buf;
962 unsigned char *pos;
963
964 nb_buf = &qel->tx.crypto.nb_buf;
965 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
966 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
967 cf_len = len;
968
969 while (len) {
970 size_t to_copy, room;
971
972 pos = (*qcb)->data + (*qcb)->sz;
973 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
974 to_copy = len > room ? room : len;
975 if (to_copy) {
976 memcpy(pos, data, to_copy);
977 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
978 qel->tx.crypto.sz += to_copy;
979 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100980 len -= to_copy;
981 data += to_copy;
982 }
983 else {
984 struct quic_crypto_buf **tmp;
985
986 tmp = realloc(qel->tx.crypto.bufs,
987 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
988 if (tmp) {
989 qel->tx.crypto.bufs = tmp;
990 qcb = &qel->tx.crypto.bufs[*nb_buf];
991 *qcb = pool_alloc(pool_head_quic_crypto_buf);
992 if (!*qcb)
993 return 0;
994
995 (*qcb)->sz = 0;
996 ++*nb_buf;
997 }
998 else {
999 break;
1000 }
1001 }
1002 }
1003
1004 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1005 * have been buffered.
1006 */
1007 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001008 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001009 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001010
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001011 /* There is at most one CRYPTO frame in this packet number
1012 * space. Let's look for it.
1013 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001014 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001015 if (frm->type != QUIC_FT_CRYPTO)
1016 continue;
1017
1018 /* Found */
1019 found = frm;
1020 break;
1021 }
1022
1023 if (found) {
1024 found->crypto.len += cf_len;
1025 }
1026 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001027 frm = pool_alloc(pool_head_quic_frame);
1028 if (!frm)
1029 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001030
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001031 frm->type = QUIC_FT_CRYPTO;
1032 frm->crypto.offset = cf_offset;
1033 frm->crypto.len = cf_len;
1034 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001035 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001036 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001037 }
1038
1039 return len == 0;
1040}
1041
1042
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001043/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1044void quic_set_tls_alert(struct quic_conn *qc, int alert)
1045{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001046 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001047 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001048 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001049}
1050
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001051/* Set the application for <qc> QUIC connection.
1052 * Return 1 if succeeded, 0 if not.
1053 */
1054int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1055{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001056 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1057 qc->app_ops = &h3_ops;
1058 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1059 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001060 else
1061 return 0;
1062
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001063 return 1;
1064}
1065
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001066/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1067 * wants to provide the QUIC layer with CRYPTO data.
1068 * Returns 1 if succeeded, 0 if not.
1069 */
1070int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1071 const uint8_t *data, size_t len)
1072{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001073 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001074 enum quic_tls_enc_level tel;
1075 struct quic_enc_level *qel;
1076
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001077 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1078 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
1079 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
1080 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001081 goto out;
1082 }
1083
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001084 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001085 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001086 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001087 goto err;
1088 }
1089
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001090 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001091 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001092 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093 goto err;
1094 }
1095
1096 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001097 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001098
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001099 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001100 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 return 1;
1102
1103 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001104 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105 return 0;
1106}
1107
1108int ha_quic_flush_flight(SSL *ssl)
1109{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001110 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001112 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1113 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001114
1115 return 1;
1116}
1117
1118int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1119{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001120 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001121
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001122 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1123 quic_set_tls_alert(qc, alert);
1124 HA_ATOMIC_STORE(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125 return 1;
1126}
1127
1128/* QUIC TLS methods */
1129static SSL_QUIC_METHOD ha_quic_method = {
1130#ifdef OPENSSL_IS_BORINGSSL
1131 .set_read_secret = ha_set_rsec,
1132 .set_write_secret = ha_set_wsec,
1133#else
1134 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1135#endif
1136 .add_handshake_data = ha_quic_add_handshake_data,
1137 .flush_flight = ha_quic_flush_flight,
1138 .send_alert = ha_quic_send_alert,
1139};
1140
1141/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1142 * Returns an error count.
1143 */
1144int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1145{
1146 struct proxy *curproxy = bind_conf->frontend;
1147 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1148 int cfgerr = 0;
1149
1150#if 0
1151 /* XXX Did not manage to use this. */
1152 const char *ciphers =
1153 "TLS_AES_128_GCM_SHA256:"
1154 "TLS_AES_256_GCM_SHA384:"
1155 "TLS_CHACHA20_POLY1305_SHA256:"
1156 "TLS_AES_128_CCM_SHA256";
1157#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001158 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001159 long options =
1160 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1161 SSL_OP_SINGLE_ECDH_USE |
1162 SSL_OP_CIPHER_SERVER_PREFERENCE;
1163 SSL_CTX *ctx;
1164
1165 ctx = SSL_CTX_new(TLS_server_method());
1166 bind_conf->initial_ctx = ctx;
1167
1168 SSL_CTX_set_options(ctx, options);
1169#if 0
1170 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1171 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1172 "for bind '%s' at [%s:%d].\n",
1173 curproxy->id, ciphers,
1174 bind_conf->arg, bind_conf->file, bind_conf->line);
1175 cfgerr++;
1176 }
1177#endif
1178
1179 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1180 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1181 "for bind '%s' at [%s:%d].\n",
1182 curproxy->id, groups,
1183 bind_conf->arg, bind_conf->file, bind_conf->line);
1184 cfgerr++;
1185 }
1186
1187 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1188 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1189 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1190 SSL_CTX_set_default_verify_paths(ctx);
1191
1192#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1193#ifdef OPENSSL_IS_BORINGSSL
1194 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1195 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1196#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1197 if (bind_conf->ssl_conf.early_data) {
1198 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001199 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001200 }
1201 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1202 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1203#else
1204 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1205#endif
1206 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1207#endif
1208 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1209
1210 return cfgerr;
1211}
1212
1213/* Decode an expected packet number from <truncated_on> its truncated value,
1214 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1215 * the number of bits used to encode this packet number (its length in bytes * 8).
1216 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1217 */
1218static uint64_t decode_packet_number(uint64_t largest_pn,
1219 uint32_t truncated_pn, unsigned int pn_nbits)
1220{
1221 uint64_t expected_pn = largest_pn + 1;
1222 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1223 uint64_t pn_hwin = pn_win / 2;
1224 uint64_t pn_mask = pn_win - 1;
1225 uint64_t candidate_pn;
1226
1227
1228 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1229 /* Note that <pn_win> > <pn_hwin>. */
1230 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1231 candidate_pn + pn_hwin <= expected_pn)
1232 return candidate_pn + pn_win;
1233
1234 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1235 return candidate_pn - pn_win;
1236
1237 return candidate_pn;
1238}
1239
1240/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1241 * cryptographic context.
1242 * <largest_pn> is the largest received packet number and <pn> the address of
1243 * the packet number field for this packet with <byte0> address of its first byte.
1244 * <end> points to one byte past the end of this packet.
1245 * Returns 1 if succeeded, 0 if not.
1246 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001247static int qc_do_rm_hp(struct quic_conn *qc,
1248 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001249 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001250 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251{
1252 int ret, outlen, i, pnlen;
1253 uint64_t packet_number;
1254 uint32_t truncated_pn = 0;
1255 unsigned char mask[5] = {0};
1256 unsigned char *sample;
1257 EVP_CIPHER_CTX *cctx;
1258 unsigned char *hp_key;
1259
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260 /* Check there is enough data in this packet. */
1261 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001262 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001263 return 0;
1264 }
1265
1266 cctx = EVP_CIPHER_CTX_new();
1267 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001268 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001269 return 0;
1270 }
1271
1272 ret = 0;
1273 sample = pn + QUIC_PACKET_PN_MAXLEN;
1274
1275 hp_key = tls_ctx->rx.hp_key;
1276 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1277 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1278 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001279 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001280 goto out;
1281 }
1282
1283 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1284 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1285 for (i = 0; i < pnlen; i++) {
1286 pn[i] ^= mask[i + 1];
1287 truncated_pn = (truncated_pn << 8) | pn[i];
1288 }
1289
1290 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1291 /* Store remaining information for this unprotected header */
1292 pkt->pn = packet_number;
1293 pkt->pnl = pnlen;
1294
1295 ret = 1;
1296
1297 out:
1298 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001299
1300 return ret;
1301}
1302
1303/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1304 * address, with <payload_len> as payload length, <aad> as address of
1305 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1306 * context.
1307 * Returns 1 if succeeded, 0 if not.
1308 */
1309static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1310 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001311 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312{
1313 unsigned char iv[12];
1314 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001315 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001316 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001317
1318 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001319 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001320 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001321 }
1322
1323 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1324 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001325 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001326 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001327 }
1328
1329 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001330
1331 err:
1332 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001333 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001334 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001335}
1336
1337/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1338 * Returns 1 if succeeded, 0 if not.
1339 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001340static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001341{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001342 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001343 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001344 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001346 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1347 unsigned char *rx_key = tls_ctx->rx.key;
1348
1349 kp_changed = 0;
1350 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1351 /* The two tested bits are not at the same position,
1352 * this is why they are first both inversed.
1353 */
1354 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1355 if (pkt->pn < tls_ctx->rx.pn) {
1356 /* The lowest packet number of a previous key phase
1357 * cannot be null if it really stores previous key phase
1358 * secrets.
1359 */
1360 if (!pkt->qc->ku.prv_rx.pn)
1361 return 0;
1362
1363 rx_iv = pkt->qc->ku.prv_rx.iv;
1364 rx_key = pkt->qc->ku.prv_rx.key;
1365 }
1366 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1367 /* Next key phase */
1368 kp_changed = 1;
1369 rx_iv = pkt->qc->ku.nxt_rx.iv;
1370 rx_key = pkt->qc->ku.nxt_rx.key;
1371 }
1372 }
1373 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001374
1375 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1376 return 0;
1377
1378 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1379 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001380 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001381 if (!ret)
1382 return 0;
1383
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001384 /* Update the keys only if the packet decryption succeeded. */
1385 if (kp_changed) {
1386 quic_tls_rotate_keys(pkt->qc);
1387 /* Toggle the Key Phase bit */
1388 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1389 /* Store the lowest packet number received for the current key phase */
1390 tls_ctx->rx.pn = pkt->pn;
1391 /* Prepare the next key update */
1392 if (!quic_tls_key_update(pkt->qc))
1393 return 0;
1394 }
1395
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001396 /* Update the packet length (required to parse the frames). */
1397 pkt->len = pkt->aad_len + ret;
1398
1399 return 1;
1400}
1401
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001402/* Remove from <qcs> stream the acknowledged frames.
1403 * Never fails.
1404 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001405static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001406{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001407 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001408 struct eb64_node *frm_node;
1409
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001410 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001411 frm_node = eb64_first(&qcs->tx.acked_frms);
1412 while (frm_node) {
1413 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001414 struct quic_frame *frm;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001415
1416 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001417 if (strm->offset.key > qcs->tx.ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001418 break;
1419
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001420 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1421 qcs->qcc->conn->qc, strm, qcs);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001422 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1423 const size_t diff = strm->offset.key + strm->len -
1424 qcs->tx.ack_offset;
1425 qcs->tx.ack_offset += diff;
1426 b_del(strm->buf, diff);
1427 ret = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001428
1429 if (!b_data(strm->buf)) {
1430 b_free(strm->buf);
1431 offer_buffers(NULL, 1);
1432 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001433 }
1434
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001435 frm_node = eb64_next(frm_node);
1436 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001437
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001438 frm = container_of(strm, struct quic_frame, stream);
1439 LIST_DELETE(&frm->list);
1440 quic_tx_packet_refdec(frm->pkt);
1441 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001442 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001443
1444 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001445}
1446
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001447/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001448static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1449 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001450{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001451 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001452
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001453 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001454 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001455 switch (frm->type) {
1456 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1457 {
1458 struct qcs *qcs = frm->stream.qcs;
1459 struct quic_stream *strm = &frm->stream;
1460
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001461 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm, qcs);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001462 if (strm->offset.key <= qcs->tx.ack_offset) {
1463 if (strm->offset.key + strm->len > qcs->tx.ack_offset) {
1464 const size_t diff = strm->offset.key + strm->len -
1465 qcs->tx.ack_offset;
1466 qcs->tx.ack_offset += diff;
1467 b_del(strm->buf, diff);
1468 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001469
1470 if (!b_data(strm->buf)) {
1471 b_free(strm->buf);
1472 offer_buffers(NULL, 1);
1473 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001474 }
1475
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001476 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1477 qcs->qcc->conn->qc, strm, qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001478 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001479 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001480 pool_free(pool_head_quic_frame, frm);
1481 }
1482 else {
1483 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1484 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001485
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001486 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001487 }
1488 break;
1489 default:
1490 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001491 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001492 pool_free(pool_head_quic_frame, frm);
1493 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001494
1495 if (stream_acked) {
1496 struct qcc *qcc = qc->qcc;
1497
1498 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1499 tasklet_wakeup(qcc->subs->tasklet);
1500 qcc->subs->events &= ~SUB_RETRY_SEND;
1501 if (!qcc->subs->events)
1502 qcc->subs = NULL;
1503 }
1504 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001505}
1506
1507/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1508 * deallocating them, and their TX frames.
1509 * Returns the last node reached to be used for the next range.
1510 * May be NULL if <largest> node could not be found.
1511 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001512static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1513 struct eb_root *pkts,
1514 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001515 struct list *newly_acked_pkts,
1516 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001517 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001518{
1519 struct eb64_node *node;
1520 struct quic_tx_packet *pkt;
1521
1522 if (largest_node)
1523 node = largest_node;
1524 else {
1525 node = eb64_lookup(pkts, largest);
1526 while (!node && largest > smallest) {
1527 node = eb64_lookup(pkts, --largest);
1528 }
1529 }
1530
1531 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001532 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533
1534 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1535 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001536 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001537 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001538 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001539 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001540 node = eb64_prev(node);
1541 eb64_delete(&pkt->pn_node);
1542 }
1543
1544 return node;
1545}
1546
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001547/* Remove all frames from <pkt_frm_list> and reinsert them in the
1548 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001549 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001550static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1551 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001552 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001553{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001554 struct quic_frame *frm, *frmbak;
1555 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001556
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001557 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1558 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001559 quic_tx_packet_refdec(frm->pkt);
1560 /* This frame is not freed but removed from its packet */
1561 frm->pkt = NULL;
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001562 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001563 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001564 }
1565
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001566 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001567}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001568
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001569/* Free <pkt> TX packet and its attached frames.
1570 * This is the responsability of the caller to remove this packet of
1571 * any data structure it was possibly attached to.
1572 */
1573static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1574{
1575 struct quic_frame *frm, *frmbak;
1576
1577 if (!pkt)
1578 return;
1579
1580 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1581 LIST_DELETE(&frm->list);
1582 pool_free(pool_head_quic_frame, frm);
1583 }
1584 pool_free(pool_head_quic_tx_packet, pkt);
1585}
1586
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001587/* Free the TX packets of <pkts> list */
1588static inline void free_quic_tx_pkts(struct list *pkts)
1589{
1590 struct quic_tx_packet *pkt, *tmp;
1591
1592 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001593 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001594 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001595 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001596 }
1597}
1598
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001599/* Remove already sent ranges of acknowledged packet numbers from
1600 * <pktns> packet number space tree below <largest_acked_pn> possibly
1601 * updating the range which contains <largest_acked_pn>.
1602 * Never fails.
1603 */
1604static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1605 int64_t largest_acked_pn)
1606{
1607 struct eb64_node *ar, *next_ar;
1608 struct quic_arngs *arngs = &pktns->rx.arngs;
1609
1610 ar = eb64_first(&arngs->root);
1611 while (ar) {
1612 struct quic_arng_node *ar_node;
1613
1614 next_ar = eb64_next(ar);
1615 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
1616 if ((int64_t)ar_node->first.key > largest_acked_pn)
1617 break;
1618
1619 if (largest_acked_pn < ar_node->last) {
1620 eb64_delete(ar);
1621 ar_node->first.key = largest_acked_pn + 1;
1622 eb64_insert(&arngs->root, ar);
1623 break;
1624 }
1625
1626 eb64_delete(ar);
1627 pool_free(pool_head_quic_arng, ar_node);
1628 arngs->sz--;
1629 ar = next_ar;
1630 }
1631}
1632
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001633/* Send a packet ack event nofication for each newly acked packet of
1634 * <newly_acked_pkts> list and free them.
1635 * Always succeeds.
1636 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001637static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001638 struct list *newly_acked_pkts)
1639{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001640 struct quic_tx_packet *pkt, *tmp;
1641 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1642
1643 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1644 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001645 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001646 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001647 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001648 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001649 /* If this packet contained an ACK frame, proceed to the
1650 * acknowledging of range of acks from the largest acknowledged
1651 * packet number which was sent in an ACK frame by this packet.
1652 */
1653 if (pkt->largest_acked_pn != -1)
1654 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001655 ev.ack.acked = pkt->in_flight_len;
1656 ev.ack.time_sent = pkt->time_sent;
1657 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001658 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001659 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001660 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001661 }
1662
1663}
1664
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001665/* Release all the frames attached to <pktns> packet number space */
1666static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1667{
1668 struct quic_frame *frm, *frmbak;
1669
1670 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1671 LIST_DELETE(&frm->list);
1672 pool_free(pool_head_quic_frame, frm);
1673 }
1674}
1675
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001676/* Handle <pkts> list of lost packets detected at <now_us> handling
1677 * their TX frames.
1678 * Send a packet loss event to the congestion controller if
1679 * in flight packet have been lost.
1680 * Also frees the packet in <pkts> list.
1681 * Never fails.
1682 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001683static inline void qc_release_lost_pkts(struct quic_conn *qc,
1684 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001685 struct list *pkts,
1686 uint64_t now_us)
1687{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001688 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001689 uint64_t lost_bytes;
1690
1691 lost_bytes = 0;
1692 oldest_lost = newest_lost = NULL;
1693 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001694 struct list tmp = LIST_HEAD_INIT(tmp);
1695
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001696 lost_bytes += pkt->in_flight_len;
1697 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001698 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001699 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001700 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001701 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001702 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001703 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001704 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001705 if (!oldest_lost) {
1706 oldest_lost = newest_lost = pkt;
1707 }
1708 else {
1709 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001710 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711 newest_lost = pkt;
1712 }
1713 }
1714
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001715 if (newest_lost) {
1716 /* Sent a congestion event to the controller */
1717 struct quic_cc_event ev = {
1718 .type = QUIC_CC_EVT_LOSS,
1719 .loss.time_sent = newest_lost->time_sent,
1720 };
1721
1722 quic_cc_event(&qc->path->cc, &ev);
1723 }
1724
1725 /* If an RTT have been already sampled, <rtt_min> has been set.
1726 * We must check if we are experiencing a persistent congestion.
1727 * If this is the case, the congestion controller must re-enter
1728 * slow start state.
1729 */
1730 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1731 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1732
1733 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1734 now_ms, qc->max_ack_delay))
1735 qc->path->cc.algo->slow_start(&qc->path->cc);
1736 }
1737
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001738 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001739 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001740 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001741 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001742 }
1743}
1744
1745/* Look for packet loss from sent packets for <qel> encryption level of a
1746 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1747 * their tree if deemed as lost or set the <loss_time> value the packet number
1748 * space if any not deemed lost.
1749 * Should be called after having received an ACK frame with newly acknowledged
1750 * packets or when the the loss detection timer has expired.
1751 * Always succeeds.
1752 */
1753static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1754 struct quic_conn *qc,
1755 struct list *lost_pkts)
1756{
1757 struct eb_root *pkts;
1758 struct eb64_node *node;
1759 struct quic_loss *ql;
1760 unsigned int loss_delay;
1761
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001762 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001763 pkts = &pktns->tx.pkts;
1764 pktns->tx.loss_time = TICK_ETERNITY;
1765 if (eb_is_empty(pkts))
1766 goto out;
1767
1768 ql = &qc->path->loss;
1769 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001770 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1771
1772 node = eb64_first(pkts);
1773 while (node) {
1774 struct quic_tx_packet *pkt;
1775 int64_t largest_acked_pn;
1776 unsigned int loss_time_limit, time_sent;
1777
1778 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille8f3ae022022-03-15 12:07:41 +01001779 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->rx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 node = eb64_next(node);
1781 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1782 break;
1783
1784 time_sent = pkt->time_sent;
1785 loss_time_limit = tick_add(time_sent, loss_delay);
1786 if (tick_is_le(time_sent, now_ms) ||
1787 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1788 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001789 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001790 }
1791 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001792 if (tick_isset(pktns->tx.loss_time))
1793 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1794 else
1795 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001796 }
1797 }
1798
1799 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001800 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001801}
1802
1803/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1804 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001805 * 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 +01001806 * acked ack-eliciting packet.
1807 * Return 1, if succeeded, 0 if not.
1808 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001809static inline int qc_parse_ack_frm(struct quic_conn *qc,
1810 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001811 struct quic_enc_level *qel,
1812 unsigned int *rtt_sample,
1813 const unsigned char **pos, const unsigned char *end)
1814{
1815 struct quic_ack *ack = &frm->ack;
1816 uint64_t smallest, largest;
1817 struct eb_root *pkts;
1818 struct eb64_node *largest_node;
1819 unsigned int time_sent, pkt_flags;
1820 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1821 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1822
1823 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1824 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001825 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 goto err;
1827 }
1828
1829 if (ack->first_ack_range > ack->largest_ack) {
1830 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001831 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001832 goto err;
1833 }
1834
1835 largest = ack->largest_ack;
1836 smallest = largest - ack->first_ack_range;
1837 pkts = &qel->pktns->tx.pkts;
1838 pkt_flags = 0;
1839 largest_node = NULL;
1840 time_sent = 0;
1841
Frédéric Lécaille8f3ae022022-03-15 12:07:41 +01001842 if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->rx.largest_acked_pn)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001843 largest_node = eb64_lookup(pkts, largest);
1844 if (!largest_node) {
1845 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001846 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001847 }
1848 else {
1849 time_sent = eb64_entry(&largest_node->node,
1850 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001852 }
1853
1854 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001855 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001856 do {
1857 uint64_t gap, ack_range;
1858
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001859 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1860 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861 if (!ack->ack_range_num--)
1862 break;
1863
1864 if (!quic_dec_int(&gap, pos, end))
1865 goto err;
1866
1867 if (smallest < gap + 2) {
1868 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001869 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001870 goto err;
1871 }
1872
1873 largest = smallest - gap - 2;
1874 if (!quic_dec_int(&ack_range, pos, end))
1875 goto err;
1876
1877 if (largest < ack_range) {
1878 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001879 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001880 goto err;
1881 }
1882
1883 /* Do not use this node anymore. */
1884 largest_node = NULL;
1885 /* Next range */
1886 smallest = largest - ack_range;
1887
1888 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001889 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001890 } while (1);
1891
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001892 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1893 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille8f3ae022022-03-15 12:07:41 +01001894 HA_ATOMIC_STORE(&qel->pktns->rx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001895 }
1896
1897 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1898 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001899 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001900 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001901 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001902 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001903 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1904 if (quic_peer_validated_addr(qc))
1905 qc->path->loss.pto_count = 0;
1906 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001907 }
1908
1909
1910 return 1;
1911
1912 err:
1913 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001914 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001915 return 0;
1916}
1917
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001918/* This function gives the detail of the SSL error. It is used only
1919 * if the debug mode and the verbose mode are activated. It dump all
1920 * the SSL error until the stack was empty.
1921 */
1922static forceinline void qc_ssl_dump_errors(struct connection *conn)
1923{
1924 if (unlikely(global.mode & MODE_DEBUG)) {
1925 while (1) {
1926 unsigned long ret;
1927
1928 ret = ERR_get_error();
1929 if (!ret)
1930 return;
1931
1932 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1933 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1934 }
1935 }
1936}
1937
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001938int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1939 const char **str, int *len);
1940
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001941/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1942 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001943 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 * Return 1 if succeeded, 0 if not.
1945 */
1946static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001947 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001948 const unsigned char *data, size_t len,
1949 struct quic_rx_packet *pkt,
1950 struct quic_rx_crypto_frm *cf)
1951{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001952 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001953 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001954
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001955 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001956 qc = ctx->qc;
1957
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001958 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1959
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001960 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1961 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001962 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001963 goto err;
1964 }
1965
1966 el->rx.crypto.offset += len;
1967 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001968 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001969
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001970 state = HA_ATOMIC_LOAD(&qc->state);
1971 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972 ssl_err = SSL_do_handshake(ctx->ssl);
1973 if (ssl_err != 1) {
1974 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1975 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1976 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001977 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001978 goto out;
1979 }
1980
1981 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001982 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001983 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001984 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001985 goto err;
1986 }
1987
Frédéric Lécaille00e24002022-02-18 17:13:45 +01001988 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
1989 /* I/O callback switch */
1990 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001991 if (qc_is_listener(ctx->qc)) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001992 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001993 /* The connection is ready to be accepted. */
1994 quic_accept_push_qc(qc);
1995 }
1996 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001997 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01001998 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001999 } else {
2000 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2001 if (ssl_err != 1) {
2002 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2003 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2004 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002005 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002006 goto out;
2007 }
2008
2009 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002010 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002011 goto err;
2012 }
2013
2014 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002015 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002016 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002017
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002019 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020 return 1;
2021
2022 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002023 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024 return 0;
2025}
2026
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002027/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
2028 * <pkt> RX packet.
2029 * Return it if succeeded, NULL if not.
2030 */
2031static inline
2032struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
2033 struct quic_rx_packet *pkt)
2034{
2035 struct quic_rx_strm_frm *frm;
2036
2037 frm = pool_alloc(pool_head_quic_rx_strm_frm);
2038 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002039 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002040 frm->len = stream_frm->len;
2041 frm->data = stream_frm->data;
2042 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002043 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002044 }
2045
2046 return frm;
2047}
2048
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002049/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01002050 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002051 */
2052static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2053{
2054 size_t ret;
2055
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01002056 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
2057 strm_frm->len -= ret;
2058 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002059
2060 return ret;
2061}
2062
2063/* Handle <strm_frm> bidirectional 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_bidi_strm_frm(struct quic_rx_packet *pkt,
2070 struct quic_stream *strm_frm,
2071 struct quic_conn *qc)
2072{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002073 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002074 struct eb64_node *frm_node;
2075 struct qcs *qcs = NULL;
2076 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002077
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002078 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2079 strm_frm->offset.key, strm_frm->fin,
2080 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002081
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002082 /* invalid or already received frame */
2083 if (ret == 1)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002084 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002085
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002086 if (ret == 2) {
2087 /* frame cannot be parsed at the moment and should be
2088 * buffered.
2089 */
2090 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2091 if (!frm) {
2092 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002093 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002094 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002095 }
2096
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002097 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2098 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002099
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002100 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002101 }
2102
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002103 /* Frame correctly received by the mux.
2104 * If there is buffered frame for next offset, it may be possible to
2105 * receive them now.
2106 */
2107 frm_node = eb64_first(&qcs->rx.frms);
2108 while (frm_node) {
2109 frm = eb64_entry(&frm_node->node,
2110 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002111
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002112 ret = qcc_recv(qc->qcc, qcs->by_id.key, frm->len,
2113 frm->offset_node.key, frm->fin,
2114 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002115
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002116 /* interrupt the parsing if the frame cannot be handled for the
2117 * moment only by the MUX.
2118 */
2119 if (ret == 2)
2120 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002121
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002122 /* Remove a newly received frame or an invalid one. */
2123 frm_node = eb64_next(frm_node);
2124 eb64_delete(&frm->offset_node);
2125 quic_rx_packet_refdec(frm->pkt);
2126 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002127 }
2128
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002129 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002130 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002131
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002132 return 1;
2133}
2134
2135/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2136 * streams may be open. The data are copied to the stream RX buffer if possible.
2137 * If not, the STREAM frame is stored to be treated again later.
2138 * We rely on the flow control so that not to store too much STREAM frames.
2139 * Return 1 if succeeded, 0 if not.
2140 */
2141static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2142 struct quic_stream *strm_frm,
2143 struct quic_conn *qc)
2144{
2145 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002146 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002147 struct quic_rx_strm_frm *frm;
2148 size_t strm_frm_len;
2149
2150 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2151 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002152 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002153 return 0;
2154 }
2155
2156 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002157 if (strm_frm->offset.key < strm->rx.offset) {
2158 size_t diff;
2159
2160 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2161 TRACE_PROTO("Already received STREAM data",
2162 QUIC_EV_CONN_PSTRM, qc);
2163 goto out;
2164 }
2165
2166 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2167 diff = strm->rx.offset - strm_frm->offset.key;
2168 strm_frm->offset.key = strm->rx.offset;
2169 strm_frm->len -= diff;
2170 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002171 }
2172
2173 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002174 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002175 int ret;
2176
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002177 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002178 goto store_frm;
2179
2180 /* qc_strm_cpy() will modify the offset, depending on the number
2181 * of bytes copied.
2182 */
2183 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2184 /* Inform the application of the arrival of this new stream */
2185 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002186 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002187 return 0;
2188 }
2189
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002190 if (ret)
2191 qcs_notify_recv(strm);
2192
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002193 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002194 }
2195 /* Take this frame into an account for the stream flow control */
2196 strm->rx.offset += strm_frm_len;
2197 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002198 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002199 */
2200 if (!strm_frm->len)
2201 goto out;
2202
2203 store_frm:
2204 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2205 if (!frm) {
2206 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002207 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002208 return 0;
2209 }
2210
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002211 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002212 quic_rx_packet_refinc(pkt);
2213
2214 out:
2215 return 1;
2216}
2217
2218static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2219 struct quic_stream *strm_frm,
2220 struct quic_conn *qc)
2221{
2222 if (strm_frm->id & QCS_ID_DIR_BIT)
2223 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2224 else
2225 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2226}
2227
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002228/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002229static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2230 struct quic_conn *qc)
2231{
2232 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002233 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002234 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002235
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002236 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002237 pkts = &qel->pktns->tx.pkts;
2238 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002239 /* Skip the empty packet (they have already been retransmitted) */
2240 while (node) {
2241 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2242 if (!LIST_ISEMPTY(&pkt->frms))
2243 break;
2244 node = eb64_next(node);
2245 }
2246
2247 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002248 return;
2249
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002250 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002251}
2252
2253/* Prepare a fast retransmission during handshake after a client
2254 * has resent Initial packets. According to the RFC a server may retransmit
2255 * up to two datagrams of Initial packets if did not receive all Initial packets
2256 * and resend them coalescing with others (Handshake here).
2257 * (Listener only).
2258 */
2259static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2260{
2261 struct list itmp = LIST_HEAD_INIT(itmp);
2262 struct list htmp = LIST_HEAD_INIT(htmp);
2263 struct quic_frame *frm, *frmbak;
2264
2265 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2266 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2267 struct quic_enc_level *qel = iqel;
2268 struct eb_root *pkts;
2269 struct eb64_node *node;
2270 struct quic_tx_packet *pkt;
2271 struct list *tmp = &itmp;
2272
2273 start:
2274 pkt = NULL;
2275 pkts = &qel->pktns->tx.pkts;
2276 node = eb64_first(pkts);
2277 /* Skip the empty packet (they have already been retransmitted) */
2278 while (node) {
2279 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2280 if (!LIST_ISEMPTY(&pkt->frms))
2281 break;
2282 node = eb64_next(node);
2283 }
2284
2285 if (!pkt)
2286 goto end;
2287
2288 qel->pktns->tx.pto_probe += 1;
2289 requeue:
2290 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2291 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2292 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01002293 frm->pkt = NULL;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002294 LIST_APPEND(tmp, &frm->list);
2295 }
2296
2297 if (qel == iqel) {
2298 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2299 pkt = pkt->next;
2300 tmp = &htmp;
2301 hqel->pktns->tx.pto_probe += 1;
2302 goto requeue;
2303 }
2304
2305 qel = hqel;
2306 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002307 goto start;
2308 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002309
2310 end:
2311 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2312 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002313}
2314
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002315/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2316 * as I/O handler context and <qel> as encryption level.
2317 * Returns 1 if succeeded, 0 if failed.
2318 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002319static 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 +01002320 struct quic_enc_level *qel)
2321{
2322 struct quic_frame frm;
2323 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002324 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002325 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002326
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002327 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002328 /* Skip the AAD */
2329 pos = pkt->data + pkt->aad_len;
2330 end = pkt->data + pkt->len;
2331
2332 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002333 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002334 goto err;
2335
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002336 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002338 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002339 break;
2340 case QUIC_FT_PING:
2341 break;
2342 case QUIC_FT_ACK:
2343 {
2344 unsigned int rtt_sample;
2345
2346 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002347 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002348 goto err;
2349
2350 if (rtt_sample) {
2351 unsigned int ack_delay;
2352
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002353 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002354 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2355 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2356 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002357 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002358 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002359 break;
2360 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002361 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002362 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002363 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002364 {
2365 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002366
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002367 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002368 /* XXX TO DO: <cfdebug> is used only for the traces. */
2369 struct quic_rx_crypto_frm cfdebug = { };
2370
2371 cfdebug.offset_node.key = frm.crypto.offset;
2372 cfdebug.len = frm.crypto.len;
2373 TRACE_PROTO("CRYPTO data discarded",
2374 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2375 break;
2376 }
2377
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002378 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2379 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2380 /* XXX TO DO: <cfdebug> is used only for the traces. */
2381 struct quic_rx_crypto_frm cfdebug = { };
2382
2383 cfdebug.offset_node.key = frm.crypto.offset;
2384 cfdebug.len = frm.crypto.len;
2385 /* Nothing to do */
2386 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002387 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002388 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002389 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2390 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002391 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002393 else {
2394 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2395 /* XXX TO DO: <cfdebug> is used only for the traces. */
2396 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002397
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002398 cfdebug.offset_node.key = frm.crypto.offset;
2399 cfdebug.len = frm.crypto.len;
2400 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002401 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002402 frm.crypto.len -= diff;
2403 frm.crypto.data += diff;
2404 frm.crypto.offset = qel->rx.crypto.offset;
2405 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002407
2408 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002409 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002410 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002411
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002412 cfdebug.offset_node.key = frm.crypto.offset;
2413 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002414 if (!qc_provide_cdata(qel, ctx,
2415 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002416 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002417 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002418
2419 break;
2420 }
2421
2422 /* frm.crypto.offset > qel->rx.crypto.offset */
2423 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2424 if (!cf) {
2425 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002426 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002427 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002428 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002429
2430 cf->offset_node.key = frm.crypto.offset;
2431 cf->len = frm.crypto.len;
2432 cf->data = frm.crypto.data;
2433 cf->pkt = pkt;
2434 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2435 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002436 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002437 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002438 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002439 {
2440 struct quic_stream *stream = &frm.stream;
2441
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002442 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002443 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2444 goto err;
2445 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2446 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002447
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002448 /* At the application layer the connection may have already been closed. */
2449 if (qc->mux_state != QC_MUX_READY)
2450 break;
2451
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002452 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002453 goto err;
2454
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002455 break;
2456 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002457 case QUIC_FT_MAX_DATA:
2458 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002459 if (qc->mux_state == QC_MUX_READY) {
2460 struct quic_max_stream_data *data = &frm.max_stream_data;
2461 qcc_recv_max_stream_data(qc->qcc, data->id,
2462 data->max_stream_data);
2463 }
2464 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002465 case QUIC_FT_MAX_STREAMS_BIDI:
2466 case QUIC_FT_MAX_STREAMS_UNI:
2467 case QUIC_FT_DATA_BLOCKED:
2468 case QUIC_FT_STREAM_DATA_BLOCKED:
2469 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2470 case QUIC_FT_STREAMS_BLOCKED_UNI:
2471 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002472 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002473 case QUIC_FT_RETIRE_CONNECTION_ID:
2474 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002475 break;
2476 case QUIC_FT_CONNECTION_CLOSE:
2477 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002478 /* warn the mux to close the connection */
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002479 if (qc->mux_state == QC_MUX_READY) {
Amaury Denoyelle4af65952022-02-15 11:06:15 +01002480 qc->qcc->flags |= QC_CF_CC_RECV;
Amaury Denoyelle2d0f8732022-03-03 18:04:24 +01002481 tasklet_wakeup(qc->qcc->wait_event.tasklet);
2482 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002483 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002484 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002485 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002486 goto err;
2487
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002488 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002489 break;
2490 default:
2491 goto err;
2492 }
2493 }
2494
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002495 /* Flag this packet number space as having received a packet. */
2496 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_PKT_RECEIVED);
2497
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002498 if (fast_retrans)
2499 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002500
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002501 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2502 * has successfully parse a Handshake packet. The Initial encryption must also
2503 * be discarded.
2504 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002505 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002506 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002507
2508 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002509 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002510 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002511 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002512 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002513 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002514 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002515 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002516 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002517 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002518 }
2519
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002520 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002521 return 1;
2522
2523 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002524 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525 return 0;
2526}
2527
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002528/* Must be called only by a <cbuf> writer (packet builder).
2529 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2530 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2531 * index after having marked the end of written data. This the responsability
2532 * of the caller to ensure there is enough room in <cbuf> to write the end of
2533 * data made of a uint16_t null field.
2534 *
2535 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2536 * ^ ^
2537 * r w
2538 *
2539 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2540 * ^ ^
2541 * r w
2542
2543 * +--------------------------------------+ (empty buffer, can be reused)
2544 * ^
2545 * (r = w)
2546 *
2547 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2548 * ^ ^
2549 * w r
2550 */
2551static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2552{
2553 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2554
2555 /* We can reset the writer index only if in front of the reader index and
2556 * if the reader index is not null. Resetting the writer when the reader
2557 * index is null would empty the buffer.
2558 * XXX Note than the writer index cannot reach the reader index.
2559 * Only the reader index can reach the writer index.
2560 */
2561 if (rd && rd <= cbuf->wr) {
2562 /* Mark the end of contiguous data for the reader */
2563 write_u16(cb_wr(cbuf), 0);
2564 cb_add(cbuf, sizeof(uint16_t));
2565 cb_wr_reset(cbuf);
2566 return 1;
2567 }
2568
2569 return 0;
2570}
2571
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002572/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002573 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002574 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2575 * This function must be called only after having built a correct datagram.
2576 * Always succeeds.
2577 */
2578static inline void qc_set_dg(struct cbuf *cbuf,
2579 uint16_t dglen, struct quic_tx_packet *pkt)
2580{
2581 write_u16(cb_wr(cbuf), dglen);
2582 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2583 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2584}
2585
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002586/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002587 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2588 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002589 * A header made of two fields is added to each datagram: the datagram length followed
2590 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002591 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2592 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002593 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002594static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2595 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002596{
2597 struct quic_enc_level *qel;
2598 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002599 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002600 struct quic_tx_packet *pkt;
2601 size_t total;
2602 size_t dg_headlen;
2603
2604 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2605 /* Each datagram is prepended with its length followed by the
2606 * address of the first packet in the datagram.
2607 */
2608 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2609 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2610 total = 0;
2611 start:
2612 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002613 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002614 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2615 * to ensure there is enough room to mark the end of prepared
2616 * contiguous data with a zero length.
2617 */
2618 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2619 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
2620 int err, probe, ack, cc;
2621
2622 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
2623 probe = ack = 0;
2624 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2625 if (!cc) {
2626 probe = qel->pktns->tx.pto_probe;
2627 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
2628 }
2629 /* Do not build any more packet if the TX secrets are not available or
2630 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2631 * and if there is no more packets to send upon PTO expiration
2632 * and if there is no more CRYPTO data available or in flight
2633 * congestion control limit is reached for prepared data
2634 */
2635 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2636 (!cc && !ack && !probe &&
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002637 (LIST_ISEMPTY(frms) ||
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002638 qc->path->prep_in_flight >= qc->path->cwnd))) {
2639 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2640 break;
2641 }
2642
2643 /* Leave room for the datagram header */
2644 pos += dg_headlen;
2645 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2646 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2647 }
2648 else {
2649 end = pos + qc->path->mtu;
2650 }
2651
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002652 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002653 QUIC_PACKET_TYPE_SHORT, ack, probe, cc, &err);
2654 /* Restore the PTO dgrams counter if a packet could not be built */
2655 if (err < 0) {
2656 if (ack)
2657 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
2658 }
2659 switch (err) {
2660 case -2:
2661 goto err;
2662 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002663 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2664 * MTU, we are here because of the congestion control window. There is
2665 * no need to try to reuse this buffer.
2666 */
2667 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002668 default:
2669 break;
2670 }
2671
2672 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2673 if (!pkt)
2674 goto err;
2675
2676 total += pkt->len;
2677 /* Set the current datagram as prepared into <cbuf>. */
2678 qc_set_dg(cbuf, pkt->len, pkt);
2679 }
2680
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002681 /* Reset <wr> writer index if in front of <rd> index */
2682 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002683 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002684 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002685 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002686 }
2687
2688 out:
2689 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2690 return total;
2691
2692 err:
2693 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2694 return -1;
2695}
2696
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002697/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002698 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2699 * several packets in the same datagram. A header made of two fields is added
2700 * to each datagram: the datagram length followed by the address of the first
2701 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002702 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2703 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002704 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002705static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2706 enum quic_tls_enc_level tel,
2707 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002709 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002710 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002711 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002712 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2713 /* length of datagrams */
2714 uint16_t dglen;
2715 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002716 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002717 /* Each datagram is prepended with its length followed by the
2718 * address of the first packet in the datagram.
2719 */
2720 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002721
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002722 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2723
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002724 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002725 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002726 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002727 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002728 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002729 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002730 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002731 /* Leave at least <dglen> bytes at the end of this buffer
2732 * to ensure there is enough room to mark the end of prepared
2733 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002734 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002735 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2736 first_pkt = prv_pkt = NULL;
2737 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002738 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002739 enum quic_pkt_type pkt_type;
2740
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002741 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002742 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002743 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2744 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002745 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002746 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002747 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002748 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002749 * 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 +01002750 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002751 * and if there is no more CRYPTO data available or in flight
2752 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002753 */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002754 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002755 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002756 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002757 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002758 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002759 /* Set the current datagram as prepared into <cbuf> if
2760 * the was already a correct packet which was previously written.
2761 */
2762 if (prv_pkt)
2763 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002764 /* Let's select the next encryption level */
2765 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2766 tel = next_tel;
2767 qel = &qc->els[tel];
2768 /* Build a new datagram */
2769 prv_pkt = NULL;
2770 continue;
2771 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772 break;
2773 }
2774
2775 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002776 if (!prv_pkt) {
2777 /* Leave room for the datagram header */
2778 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002779 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002780 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2781 }
2782 else {
2783 end = pos + qc->path->mtu;
2784 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002785 }
2786
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002787 cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms,
2788 qc, dglen, padding, pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002789 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002790 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002791 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002792 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002793 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002794 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002795 case -2:
2796 goto err;
2797 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002798 /* If there was already a correct packet present, set the
2799 * current datagram as prepared into <cbuf>.
2800 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002801 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002802 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002803 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002804 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002805 break;
2806 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002807
Frédéric Lécaille63556772021-12-29 17:18:21 +01002808 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2809 if (!cur_pkt)
2810 goto err;
2811
2812 total += cur_pkt->len;
2813 /* keep trace of the first packet in the datagram */
2814 if (!first_pkt)
2815 first_pkt = cur_pkt;
2816 /* Attach the current one to the previous one */
2817 if (prv_pkt)
2818 prv_pkt->next = cur_pkt;
2819 /* Let's say we have to build a new dgram */
2820 prv_pkt = NULL;
2821 dglen += cur_pkt->len;
2822 /* Client: discard the Initial encryption keys as soon as
2823 * a handshake packet could be built.
2824 */
2825 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2826 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2827 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2828 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2829 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2830 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002831 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002832 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002833 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2834 }
2835 /* If the data for the current encryption level have all been sent,
2836 * select the next level.
2837 */
2838 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002839 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002840 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2841 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2842 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2843 tel = next_tel;
2844 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002845 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002846 /* If there is data for the next level, do not
2847 * consume a datagram.
2848 */
2849 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002850 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002851 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002852 /* If we have to build a new datagram, set the current datagram as
2853 * prepared into <cbuf>.
2854 */
2855 if (!prv_pkt) {
2856 qc_set_dg(cbuf, dglen, first_pkt);
2857 first_pkt = NULL;
2858 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002859 padding = 0;
2860 }
2861 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002862 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002863 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2864 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002865 }
2866 }
2867
2868 stop_build:
2869 /* Reset <wr> writer index if in front of <rd> index */
2870 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002871 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002872 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002873 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002874 }
2875
2876 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002877 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002878 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002879
2880 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002881 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002882 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883}
2884
2885/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002886 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002887 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002888int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002889{
2890 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002891 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002892
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002893 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002894 cbuf = qr->cbuf;
2895 while (cb_contig_data(cbuf)) {
2896 unsigned char *pos;
2897 struct buffer tmpbuf = { };
2898 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2899 uint16_t dglen;
2900 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002901 unsigned int time_sent;
2902
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002903 pos = cb_rd(cbuf);
2904 dglen = read_u16(pos);
2905 /* End of prepared datagrams.
2906 * Reset the reader index only if in front of the writer index.
2907 */
2908 if (!dglen) {
2909 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2910
2911 if (wr && wr < cbuf->rd) {
2912 cb_rd_reset(cbuf);
2913 continue;
2914 }
2915 break;
2916 }
2917
2918 pos += sizeof dglen;
2919 first_pkt = read_ptr(pos);
2920 pos += sizeof first_pkt;
2921 tmpbuf.area = (char *)pos;
2922 tmpbuf.size = tmpbuf.data = dglen;
2923
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002924 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01002925 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01002926 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002927
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002928 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002929 qc->tx.bytes += tmpbuf.data;
2930 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002931
2932 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2933 pkt->time_sent = time_sent;
2934 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2935 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002936 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01002937 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
2938 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002939 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002940 qc->path->in_flight += pkt->in_flight_len;
2941 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2942 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002943 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002944 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002945 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01002946 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002947 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002948 }
2949 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002950
2951 return 1;
2952}
2953
2954/* Build all the frames which must be sent just after the handshake have succeeded.
2955 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2956 * a HANDSHAKE_DONE frame.
2957 * Return 1 if succeeded, 0 if not.
2958 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002959static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002960{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002961 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002962 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002963 struct quic_frame *frm, *frmbak;
2964 struct list frm_list = LIST_HEAD_INIT(frm_list);
2965 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002966
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002967 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002968 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002969 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002970 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002971 if (!frm)
2972 return 0;
2973
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002975 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002976 }
2977
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002978 first = 1;
2979 max = qc->tx.params.active_connection_id_limit;
2980 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002981 struct quic_connection_id *cid;
2982
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002983 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002984 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002985 goto err;
2986
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01002987 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002988 if (!cid)
2989 goto err;
2990
Frédéric Lécaille74904a42022-01-27 15:35:56 +01002991 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01002992 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002993
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002994 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002995 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01002997
2998 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002999 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003000
3001 return 1;
3002
3003 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003004 /* free the frames */
3005 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3006 pool_free(pool_head_quic_frame, frm);
3007
3008 node = eb64_first(&qc->cids);
3009 while (node) {
3010 struct quic_connection_id *cid;
3011
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003012
3013 cid = eb64_entry(&node->node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003014 if (cid->seq_num.key >= max)
3015 break;
3016
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003017 if (cid->seq_num.key < first)
3018 continue;
3019
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003020 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003021 ebmb_delete(&cid->node);
3022 eb64_delete(&cid->seq_num);
3023 pool_free(pool_head_quic_connection_id, cid);
3024 }
3025
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026 return 0;
3027}
3028
3029/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003030void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003031{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003032 struct eb64_node *n;
3033 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003034
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003035 n = eb64_first(&arngs->root);
3036 while (n) {
3037 struct eb64_node *next;
3038
3039 ar = eb64_entry(&n->node, struct quic_arng_node, first);
3040 next = eb64_next(n);
3041 eb64_delete(n);
3042 free(ar);
3043 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 }
3045}
3046
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003047/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3048 * descending order.
3049 */
3050static inline size_t sack_gap(struct quic_arng_node *p,
3051 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003052{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003053 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003054}
3055
3056
3057/* Remove the last elements of <ack_ranges> list of ack range updating its
3058 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003059 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003061static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003062{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003063 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003064
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003065 last = eb64_last(&arngs->root);
3066 while (last && arngs->enc_sz > limit) {
3067 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003068
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003069 prev = eb64_prev(last);
3070 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003071 return 0;
3072
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003073 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
3074 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
3075 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3076 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3077 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3078 --arngs->sz;
3079 eb64_delete(last);
3080 pool_free(pool_head_quic_arng, last);
3081 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003082 }
3083
3084 return 1;
3085}
3086
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003087/* Set the encoded size of <arngs> QUIC ack ranges. */
3088static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3089{
3090 struct eb64_node *node, *next;
3091 struct quic_arng_node *ar, *ar_next;
3092
3093 node = eb64_last(&arngs->root);
3094 if (!node)
3095 return;
3096
3097 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3098 arngs->enc_sz = quic_int_getsize(ar->last) +
3099 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3100
3101 while ((next = eb64_prev(node))) {
3102 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
3103 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3104 quic_int_getsize(ar_next->last - ar_next->first.key);
3105 node = next;
3106 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3107 }
3108}
3109
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003110/* Insert <ar> ack range into <argns> tree of ack ranges.
3111 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003112 */
3113static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003114struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3115 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003116{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003117 struct quic_arng_node *new_ar;
3118
3119 new_ar = pool_alloc(pool_head_quic_arng);
3120 if (new_ar) {
3121 new_ar->first.key = ar->first;
3122 new_ar->last = ar->last;
3123 eb64_insert(&arngs->root, &new_ar->first);
3124 arngs->sz++;
3125 }
3126
3127 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003128}
3129
3130/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003131 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003132 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003133 *
3134 * Descending order
3135 * ------------->
3136 * range1 range2
3137 * ..........|--------|..............|--------|
3138 * ^ ^ ^ ^
3139 * | | | |
3140 * last1 first1 last2 first2
3141 * ..........+--------+--------------+--------+......
3142 * diff1 gap12 diff2
3143 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003144 * To encode the previous list of ranges we must encode integers as follows in
3145 * descending order:
3146 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147 * with diff1 = last1 - first1
3148 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003149 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003150 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003151 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003152int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3153 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003154{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003155 struct eb64_node *le;
3156 struct quic_arng_node *new_node;
3157 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003158
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003159 new = NULL;
3160 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003161 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003162 if (!new_node)
3163 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003164
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003165 goto out;
3166 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003167
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003168 le = eb64_lookup_le(&arngs->root, ar->first);
3169 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003170 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003171 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003172 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003173
3174 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003176 else {
3177 struct quic_arng_node *le_ar =
3178 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003180 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003181 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003182 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003183
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003184 if (le_ar->last + 1 >= ar->first) {
3185 le_ar->last = ar->last;
3186 new = le;
3187 new_node = le_ar;
3188 }
3189 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003190 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003191 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003192 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003193
3194 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003195 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003196 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003197
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003198 /* Verify that the new inserted node does not overlap the nodes
3199 * which follow it.
3200 */
3201 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003202 struct eb64_node *next;
3203 struct quic_arng_node *next_node;
3204
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003205 while ((next = eb64_next(new))) {
3206 next_node =
3207 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003208 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003209 break;
3210
3211 if (next_node->last > new_node->last)
3212 new_node->last = next_node->last;
3213 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003214 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003215 /* Decrement the size of these ranges. */
3216 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003217 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003218 }
3219
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003220 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003221 quic_arngs_set_enc_sz(arngs);
3222
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003223 return 1;
3224}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225/* Remove the header protection of packets at <el> encryption level.
3226 * Always succeeds.
3227 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003228static 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 +01003229{
3230 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003231 struct quic_rx_packet *pqpkt;
3232 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233 struct quic_enc_level *app_qel;
3234
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003235 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3236 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003237 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003238 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003239 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003241 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003242 goto out;
3243 }
3244 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003245 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003246 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003247 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003248 pqpkt->data, pqpkt->data + pqpkt->len)) {
3249 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003250 /* XXX TO DO XXX */
3251 }
3252 else {
3253 /* The AAD includes the packet number field */
3254 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3255 /* Store the packet into the tree of packets to decrypt. */
3256 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003257 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003258 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3259 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003260 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003261 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003262 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003263 MT_LIST_DELETE_SAFE(pkttmp1);
3264 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003265 }
3266
3267 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003268 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003269}
3270
3271/* Process all the CRYPTO frame at <el> encryption level.
3272 * Return 1 if succeeded, 0 if not.
3273 */
3274static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003275 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276{
3277 struct eb64_node *node;
3278
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003279 node = eb64_first(&el->rx.crypto.frms);
3280 while (node) {
3281 struct quic_rx_crypto_frm *cf;
3282
3283 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3284 if (cf->offset_node.key != el->rx.crypto.offset)
3285 break;
3286
3287 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3288 goto err;
3289
3290 node = eb64_next(node);
3291 quic_rx_packet_refdec(cf->pkt);
3292 eb64_delete(&cf->offset_node);
3293 pool_free(pool_head_quic_rx_crypto_frm, cf);
3294 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003295 return 1;
3296
3297 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003298 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003299 return 0;
3300}
3301
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003302/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003303 * 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 +02003304 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003305 * Return 1 if succeeded, 0 if not.
3306 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003307int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3308 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003309{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003310 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003311 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003312 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003313 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003314
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003315 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003316 qel = cur_el;
3317 next_tel:
3318 if (!qel)
3319 goto out;
3320
3321 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3322 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003323 while (node) {
3324 struct quic_rx_packet *pkt;
3325
3326 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003327 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003328 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003329 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003330 /* Drop the packet */
3331 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003332 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003333 }
3334 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003335 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003336 /* Drop the packet */
3337 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003338 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003339 }
3340 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003341 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3342
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003343 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING) {
3344 if (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack)
3345 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
3346 qc_idle_timer_rearm(qc, 1);
3347 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003348 if (pkt->pn > largest_pn)
3349 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003351 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003352 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003353 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003354 }
3355 }
3356 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003357 eb64_delete(&pkt->pn_node);
3358 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003360 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003361
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003362 /* Update the largest packet number. */
3363 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003364 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3365 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003366 goto err;
3367
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003368 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003369 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003370 qel = next_el;
3371 goto next_tel;
3372 }
3373
3374 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003375 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003376 return 1;
3377
3378 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003379 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003380 return 0;
3381}
3382
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003383/* Check if it's possible to remove header protection for packets related to
3384 * encryption level <qel>. If <qel> is NULL, assume it's false.
3385 *
3386 * Return true if the operation is possible else false.
3387 */
3388static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3389{
3390 enum quic_tls_enc_level tel;
3391
3392 if (!qel)
3393 return 0;
3394
3395 tel = ssl_to_quic_enc_level(qel->level);
3396
3397 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003398 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003399 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003400 return 0;
3401 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003402
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003403 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003404 return 0;
3405
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003406 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003407 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003408 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003409 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003410
3411 return 1;
3412}
3413
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003414/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaillec2f561c2022-02-25 17:46:07 +01003415int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003416{
3417 int ret;
3418 struct qring *qr;
3419
3420 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3421 if (!qr)
3422 /* Never happens */
3423 return 1;
3424
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003425 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003426 if (ret == -1)
3427 goto err;
3428 else if (ret == 0)
3429 goto out;
3430
3431 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3432 goto err;
3433
3434 out:
3435 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3436 return 1;
3437
3438 err:
3439 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3440 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3441 return 0;
3442}
3443
3444/* QUIC connection packet handler task (post handshake) */
3445static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3446{
3447 struct ssl_sock_ctx *ctx;
3448 struct quic_conn *qc;
3449 struct quic_enc_level *qel;
3450 int st;
3451
3452
3453 ctx = context;
3454 qc = ctx->qc;
3455 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3456 st = HA_ATOMIC_LOAD(&qc->state);
3457
3458 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
3459
3460 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3461 qc_rm_hp_pkts(qc, qel);
3462
3463 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3464 goto err;
3465
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003466 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003467 goto err;
3468
3469 return t;
3470
3471 err:
3472 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st);
3473 return t;
3474}
3475
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003476/* QUIC connection packet handler task. */
3477struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003478{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003479 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003480 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003481 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003482 enum quic_tls_enc_level tel, next_tel;
3483 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003484 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003485 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003487 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003488 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003489 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003490 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003491 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003492 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003493 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3494 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3495 /* The I/O handler has been woken up by the dgram listener
3496 * after the anti-amplification was reached.
3497 */
3498 qc_set_timer(qc);
3499 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3500 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3501 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003502 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003503 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3504 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3505 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003506 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003507 if (st >= QUIC_HS_ST_COMPLETE &&
3508 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3509 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3510 /* There may be remaining Handshake packets to treat and acknowledge. */
3511 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3512 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3513 }
3514 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003515 goto err;
3516
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003517 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003518 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003519
3520 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003521 /* Treat packets waiting for header packet protection decryption */
3522 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003523 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003525 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3526 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3527 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003528 goto err;
3529
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003530 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003531 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003532 qel = next_qel;
3533 next_qel = NULL;
3534 goto next_level;
3535 }
3536
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003537 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003538 if (st >= QUIC_HS_ST_COMPLETE) {
3539 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3540 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003541 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003542
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003543 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003544 QUIC_FL_TLS_SECRETS_DCD)) {
3545 /* Discard the Handshake keys. */
3546 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3547 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3548 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3549 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003550 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003551 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003552 }
3553
3554 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3555 /* There may be remaining handshake to build (acks) */
3556 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3557 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003558 }
3559
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003560 if (!qr)
3561 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003562 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3563 * be considered.
3564 */
3565 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003566 goto err;
3567 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003568 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003570 else if (ret == 0)
3571 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003573 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003574 goto err;
3575
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003576 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003577 /* Check if there is something to do for the next level.
3578 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003579 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003580 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003581 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003582 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003583 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003584 goto next_level;
3585 }
3586
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003587 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003588 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003589 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590
3591 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003592 if (qr)
3593 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003594 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003595 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003596}
3597
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003598/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003599static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3600{
3601 int i;
3602
3603 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3604 if (qel->tx.crypto.bufs[i]) {
3605 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3606 qel->tx.crypto.bufs[i] = NULL;
3607 }
3608 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003609 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610}
3611
3612/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003613 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003614 * Returns 1 if succeeded, 0 if not.
3615 */
3616static int quic_conn_enc_level_init(struct quic_conn *qc,
3617 enum quic_tls_enc_level level)
3618{
3619 struct quic_enc_level *qel;
3620
3621 qel = &qc->els[level];
3622 qel->level = quic_to_ssl_enc_level(level);
3623 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3624 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3625 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003626 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627
3628 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003629 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003630 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003631 qel->rx.crypto.offset = 0;
3632 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003633
3634 /* Allocate only one buffer. */
3635 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3636 if (!qel->tx.crypto.bufs)
3637 goto err;
3638
3639 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3640 if (!qel->tx.crypto.bufs[0])
3641 goto err;
3642
3643 qel->tx.crypto.bufs[0]->sz = 0;
3644 qel->tx.crypto.nb_buf = 1;
3645
3646 qel->tx.crypto.sz = 0;
3647 qel->tx.crypto.offset = 0;
3648
3649 return 1;
3650
3651 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003652 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003653 return 0;
3654}
3655
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003656/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
3657 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003658 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003659 * This function must only be called by the thread responsible of the quic_conn
3660 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003661 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003662static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663{
3664 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003665 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003667 if (qc->idle_timer_task) {
3668 task_destroy(qc->idle_timer_task);
3669 qc->idle_timer_task = NULL;
3670 }
3671
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003672 if (qc->timer_task) {
3673 task_destroy(qc->timer_task);
3674 qc->timer_task = NULL;
3675 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003676
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003677 /* remove the connection from receiver cids trees */
3678 ebmb_delete(&qc->odcid_node);
3679 ebmb_delete(&qc->scid_node);
3680 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003681
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003682 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003683 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003684 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003685 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003686 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003687 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003688
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003690 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003691
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003692 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3693 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003694 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003695}
3696
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003697void quic_close(struct connection *conn, void *xprt_ctx)
3698{
3699 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003700 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003701
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003702 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003703
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003704 /* Next application data can be dropped. */
3705 qc->mux_state = QC_MUX_RELEASED;
3706
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003707 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003708}
3709
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003710/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003711static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003713 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003714 struct quic_conn *qc;
3715 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003716 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003717
3718 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003719 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003720 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003721 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722 task->expire = TICK_ETERNITY;
3723 pktns = quic_loss_pktns(qc);
3724 if (tick_isset(pktns->tx.loss_time)) {
3725 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3726
3727 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3728 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003729 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3730 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003731 goto out;
3732 }
3733
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003734 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003735 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003736 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003737 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3738 pktns->tx.pto_probe = 1;
3739 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3740 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3741 }
3742 else {
3743 pktns->tx.pto_probe = 2;
3744 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003745 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003746 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3748 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3749
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003750 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003752 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003753 iel->pktns->tx.pto_probe = 1;
3754 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003755
3756 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003757 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3758 continue;
3759
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003760 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003761 }
3762
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3764 qc->path->loss.pto_count++;
3765
3766 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003767 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003768
3769 return task;
3770}
3771
3772/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3773 * connections used to identify the first Initial packets of client connecting
3774 * to listeners. This parameter must be NULL for QUIC connections attached
3775 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3776 * <scid> is the source connection ID with <scid_len> as length.
3777 * Returns 1 if succeeded, 0 if not.
3778 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003779static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003780 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003781 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782{
3783 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003784 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785 /* Initial CID. */
3786 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003787 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003788 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003790 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003791 qc = pool_zalloc(pool_head_quic_conn);
3792 if (!qc) {
3793 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3794 goto err;
3795 }
3796
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003797 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3798 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003799 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003800 goto err;
3801 }
3802
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003803 qc->cids = EB_ROOT;
3804 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003805 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003806 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003807
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003808 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003809 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003810 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003811 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003812 qc->odcid.addrlen = dcid_addr_len;
3813 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003815 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816 if (scid_len)
3817 memcpy(qc->dcid.data, scid, scid_len);
3818 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003819 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003820 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003821 }
3822 /* QUIC Client (outgoing connection to servers) */
3823 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003824 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003825 if (dcid_len)
3826 memcpy(qc->dcid.data, dcid, dcid_len);
3827 qc->dcid.len = dcid_len;
3828 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003829 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003830
3831 /* Initialize the output buffer */
3832 qc->obuf.pos = qc->obuf.data;
3833
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003834 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003835 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003836 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003837 goto err;
3838 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003839
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003840 /* insert the allocated CID in the receiver datagram handler tree */
3841 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003842 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003843
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844 /* Select our SCID which is the first CID with 0 as sequence number. */
3845 qc->scid = icid->cid;
3846
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003847 /* Packet number spaces initialization. */
3848 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3849 quic_pktns_init(&qc->pktns[i]);
3850 /* QUIC encryption level context initialization. */
3851 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003852 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003853 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003854 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003855 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003856 /* Initialize the packet number space. */
3857 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3858 }
3859
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003860 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003861 qc->tps_tls_ext = qc->version & 0xff000000 ?
3862 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3863 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003864 /* TX part. */
3865 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003866 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3867 qc->tx.wbuf = qc->tx.rbuf = 0;
3868 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003869 /* RX part. */
3870 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003871 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003872 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003873 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003874 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003875 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003876 goto err;
3877 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003878
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003879 /* XXX TO DO: Only one path at this time. */
3880 qc->path = &qc->paths[0];
3881 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3882
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01003883 /* required to use MTLIST_IN_LIST */
3884 MT_LIST_INIT(&qc->accept_list);
3885
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003886 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003887
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003888 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003889
3890 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003891 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003892 pool_free(pool_head_quic_conn_rxbuf, buf_area);
3893 if (qc)
3894 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003895 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003896 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003897}
3898
3899/* Initialize the timer task of <qc> QUIC connection.
3900 * Returns 1 if succeeded, 0 if not.
3901 */
3902static int quic_conn_init_timer(struct quic_conn *qc)
3903{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003904 /* Attach this task to the same thread ID used for the connection */
3905 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003906 if (!qc->timer_task)
3907 return 0;
3908
3909 qc->timer = TICK_ETERNITY;
3910 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01003911 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003912
3913 return 1;
3914}
3915
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003916/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
3917 * which is set to 1 when receiving a packet , and 0 when sending packet
3918 */
3919static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
3920{
3921 unsigned int expire;
3922
3923 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
3924 if (read) {
3925 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
3926 }
3927 else {
3928 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
3929 }
3930 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
3931}
3932
3933/* The task handling the idle timeout */
3934static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
3935{
3936 struct quic_conn *qc = ctx;
3937
3938 quic_conn_release(qc);
3939
3940 return NULL;
3941}
3942
3943/* Initialize the idle timeout task for <qc>.
3944 * Returns 1 if succeeded, 0 if not.
3945 */
3946static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
3947{
3948 qc->idle_timer_task = task_new_here();
3949 if (!qc->idle_timer_task)
3950 return 0;
3951
3952 qc->idle_timer_task->process = qc_idle_timer_task;
3953 qc->idle_timer_task->context = qc;
3954 qc_idle_timer_rearm(qc, 1);
3955 task_queue(qc->idle_timer_task);
3956
3957 return 1;
3958}
3959
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003960/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3961 * past one byte of this buffer.
3962 */
3963static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3964 struct quic_rx_packet *pkt)
3965{
3966 unsigned char dcid_len, scid_len;
3967
3968 /* Version */
3969 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3970 return 0;
3971
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003972 /* Destination Connection ID Length */
3973 dcid_len = *(*buf)++;
3974 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3975 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3976 /* XXX MUST BE DROPPED */
3977 return 0;
3978
3979 if (dcid_len) {
3980 /* Check that the length of this received DCID matches the CID lengths
3981 * of our implementation for non Initials packets only.
3982 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003983 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3984 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003985 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986 return 0;
3987
3988 memcpy(pkt->dcid.data, *buf, dcid_len);
3989 }
3990
3991 pkt->dcid.len = dcid_len;
3992 *buf += dcid_len;
3993
3994 /* Source Connection ID Length */
3995 scid_len = *(*buf)++;
3996 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3997 /* XXX MUST BE DROPPED */
3998 return 0;
3999
4000 if (scid_len)
4001 memcpy(pkt->scid.data, *buf, scid_len);
4002 pkt->scid.len = scid_len;
4003 *buf += scid_len;
4004
4005 return 1;
4006}
4007
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004008/* Insert <pkt> RX packet in its <qel> RX packets tree */
4009static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4010{
4011 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004012 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004013 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4014 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4015 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004016}
4017
4018/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004019 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4020 * byte past the end of the buffer containing this packet and <beg> the address of
4021 * the packet first byte.
4022 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4023 * Returns 1 if succeeded, 0 if not.
4024 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004025static inline int qc_try_rm_hp(struct quic_conn *qc,
4026 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004027 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004028 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004029 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004030{
4031 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004032 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004033 struct quic_enc_level *qel;
4034 /* Only for traces. */
4035 struct quic_rx_packet *qpkt_trace;
4036
4037 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004038 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004039 /* The packet number is here. This is also the start minus
4040 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4041 * protection.
4042 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004043 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004044
4045 tel = quic_packet_type_enc_level(pkt->type);
4046 qel = &qc->els[tel];
4047
4048 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004049 /* Note that the following function enables us to unprotect the packet
4050 * number and its length subsequently used to decrypt the entire
4051 * packets.
4052 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004053 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4054 qel->pktns->rx.largest_pn, pn, beg, end)) {
4055 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004056 goto err;
4057 }
4058
4059 /* The AAD includes the packet number field found at <pn>. */
4060 pkt->aad_len = pn - beg + pkt->pnl;
4061 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004062 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004063 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004064 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004065 /* If the packet number space has been discarded, this packet
4066 * will be not parsed.
4067 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004068 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004069 goto out;
4070 }
4071
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004072 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004073 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004074 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4075 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004076 }
4077
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004078 *el = qel;
4079 /* No reference counter incrementation here!!! */
4080 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4081 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4082 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4083 b_add(&qc->rx.buf, pkt->len);
4084 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004085 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004086 return 1;
4087
4088 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004089 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004090 return 0;
4091}
4092
4093/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4094 * Also set <*long_header> to 1 if this form is long, 0 if not.
4095 */
4096static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4097 unsigned char byte0, int *long_header)
4098{
4099 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4100 pkt->type =
4101 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4102 *long_header = 1;
4103 }
4104 else {
4105 pkt->type = QUIC_PACKET_TYPE_SHORT;
4106 *long_header = 0;
4107 }
4108}
4109
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004110/*
4111 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4112 */
4113static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4114{
4115 int j = 0, version;
4116
4117 do {
4118 version = quic_supported_version[j];
4119 if (version == pkt->version)
4120 return 1;
4121
4122 version = quic_supported_version[++j];
4123 } while(version);
4124
4125 return 0;
4126}
4127
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004128/*
4129 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4130 * address <addr>.
4131 * Implementation of RFC9000 6. Version Negotiation
4132 *
4133 * TODO implement a rate-limiting sending of Version Negotiation packets
4134 *
4135 * Returns 0 on success else non-zero
4136 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004137static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4138 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004139{
4140 char buf[256];
4141 int i = 0, j, version;
4142 const socklen_t addrlen = get_addr_len(addr);
4143
4144 /*
4145 * header form
4146 * long header, fixed bit to 0 for Version Negotiation
4147 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004148 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4149 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004150
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004151 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004152 /* null version for Version Negotiation */
4153 buf[i++] = '\x00';
4154 buf[i++] = '\x00';
4155 buf[i++] = '\x00';
4156 buf[i++] = '\x00';
4157
4158 /* source connection id */
4159 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004160 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004161 i += pkt->scid.len;
4162
4163 /* destination connection id */
4164 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004165 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004166 i += pkt->dcid.len;
4167
4168 /* supported version */
4169 j = 0;
4170 do {
4171 version = htonl(quic_supported_version[j]);
4172 memcpy(&buf[i], &version, sizeof(version));
4173 i += sizeof(version);
4174
4175 version = quic_supported_version[++j];
4176 } while (version);
4177
4178 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4179 return 1;
4180
4181 return 0;
4182}
4183
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004184/* Generate the token to be used in Retry packets. The token is written to
4185 * <buf> which is expected to be <len> bytes.
4186 *
4187 * Various parameters are expected to be encoded in the token. For now, only
4188 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4189 * as this CID must be repeated by the server in the transport parameters.
4190 *
4191 * TODO add the client address to validate the token origin.
4192 *
4193 * Returns the length of the encoded token or 0 on error.
4194 */
4195static int generate_retry_token(unsigned char *buf, unsigned char len,
4196 struct quic_rx_packet *pkt)
4197{
4198 const size_t token_len = 1 + pkt->dcid.len;
4199 unsigned char i = 0;
4200
4201 if (token_len > len)
4202 return 0;
4203
4204 buf[i++] = pkt->dcid.len;
4205 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4206 i += pkt->dcid.len;
4207
4208 return i;
4209}
4210
4211/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4212 * the Initial <pkt> packet.
4213 *
4214 * Returns 0 on success else non-zero.
4215 */
4216static int send_retry(int fd, struct sockaddr_storage *addr,
4217 struct quic_rx_packet *pkt)
4218{
4219 unsigned char buf[128];
4220 int i = 0, token_len;
4221 const socklen_t addrlen = get_addr_len(addr);
4222 struct quic_cid scid;
4223
4224 /* long header + fixed bit + packet type 0x3 */
4225 buf[i++] = 0xf0;
4226 /* version */
4227 buf[i++] = 0x00;
4228 buf[i++] = 0x00;
4229 buf[i++] = 0x00;
4230 buf[i++] = 0x01;
4231
4232 /* Use the SCID from <pkt> for Retry DCID. */
4233 buf[i++] = pkt->scid.len;
4234 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4235 i += pkt->scid.len;
4236
4237 /* Generate a new CID to be used as SCID for the Retry packet. */
4238 scid.len = QUIC_HAP_CID_LEN;
4239 if (RAND_bytes(scid.data, scid.len) != 1)
4240 return 1;
4241
4242 buf[i++] = scid.len;
4243 memcpy(&buf[i], scid.data, scid.len);
4244 i += scid.len;
4245
4246 /* token */
4247 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4248 return 1;
4249 i += token_len;
4250
4251 /* token integrity tag */
4252 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4253 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4254 pkt->dcid.len, buf, i)) {
4255 return 1;
4256 }
4257
4258 i += QUIC_TLS_TAG_LEN;
4259
4260 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4261 return 1;
4262
4263 return 0;
4264}
4265
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004266/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4267 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4268 * concatenated to the <pkt> DCID field.
4269 *
4270 * Returns the instance or NULL if not found.
4271 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004272static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004273 struct listener *l,
4274 struct sockaddr_storage *saddr)
4275{
4276 struct quic_conn *qc = NULL;
4277 struct ebmb_node *node;
4278 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004279 /* set if the quic_conn is found in the second DCID tree */
4280 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004281
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004282 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4283 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4284 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4285 /* DCIDs of first packets coming from multiple clients may have
4286 * the same values. Let's distinguish them by concatenating the
4287 * socket addresses.
4288 */
4289 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004290 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004291 pkt->dcid.len + pkt->dcid.addrlen);
4292 if (node) {
4293 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4294 goto end;
4295 }
4296 }
4297
4298 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4299 * also for INITIAL/0-RTT non-first packets with the final DCID in
4300 * used.
4301 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004302 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004303 if (!node)
4304 goto end;
4305
4306 id = ebmb_entry(node, struct quic_connection_id, node);
4307 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004308 found_in_dcid = 1;
4309
4310 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004311 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4312 * If already done, this is a noop.
4313 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004314 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004315 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004316
4317 return qc;
4318}
4319
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004320/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4321 * will extract the parameters stored in the token : <odcid>.
4322 *
4323 * Returns 0 on success else non-zero.
4324 */
4325static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4326 struct quic_cid *odcid)
4327{
4328 uint64_t odcid_len;
4329
4330 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4331 return 1;
4332
Frédéric Lécaillef1f812b2022-03-17 16:22:02 +01004333 if (odcid_len > QUIC_CID_MAXLEN)
4334 return 1;
4335
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004336 memcpy(odcid->data, token, odcid_len);
4337 odcid->len = odcid_len;
4338
4339 return 0;
4340}
4341
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004342/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4343 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4344 * parameters of this session.
4345 * This is the responsibility of the caller to check the validity of all the
4346 * pointers passed as parameter to this function.
4347 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4348 * CO_ER_SSL_NO_MEM.
4349 */
4350static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4351 unsigned char *params, size_t params_len)
4352{
4353 int retry;
4354
4355 retry = 1;
4356 retry:
4357 *ssl = SSL_new(ssl_ctx);
4358 if (!*ssl) {
4359 if (!retry--)
4360 goto err;
4361
4362 pool_gc(NULL);
4363 goto retry;
4364 }
4365
4366 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4367 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4368 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004369 SSL_free(*ssl);
4370 *ssl = NULL;
4371 if (!retry--)
4372 goto err;
4373
4374 pool_gc(NULL);
4375 goto retry;
4376 }
4377
4378 return 0;
4379
4380 err:
4381 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4382 return -1;
4383}
4384
4385/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4386 * used to process <qc> received packets. The allocated context is stored in
4387 * <qc.xprt_ctx>.
4388 *
4389 * Returns 0 on success else non-zero.
4390 */
4391int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4392{
4393 struct bind_conf *bc = qc->li->bind_conf;
4394 struct ssl_sock_ctx *ctx = NULL;
4395
4396 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4397 if (!ctx)
4398 goto err;
4399
4400 ctx->wait_event.tasklet = tasklet_new();
4401 if (!ctx->wait_event.tasklet)
4402 goto err;
4403
4404 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4405 ctx->wait_event.tasklet->context = ctx;
4406 ctx->wait_event.events = 0;
4407 ctx->subs = NULL;
4408 ctx->xprt_ctx = NULL;
4409 ctx->qc = qc;
4410
4411 /* Set tasklet tid based on the SCID selected by us for this
4412 * connection. The upper layer will also be binded on the same thread.
4413 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004414 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004415
4416 if (qc_is_listener(qc)) {
4417 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4418 qc->enc_params, qc->enc_params_len) == -1) {
4419 goto err;
4420 }
4421
4422 /* Enabling 0-RTT */
4423 if (bc->ssl_conf.early_data)
4424 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4425
4426 SSL_set_accept_state(ctx->ssl);
4427 }
4428
4429 ctx->xprt = xprt_get(XPRT_QUIC);
4430
4431 /* Store the allocated context in <qc>. */
4432 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
4433
4434 return 0;
4435
4436 err:
4437 if (ctx && ctx->wait_event.tasklet)
4438 tasklet_free(ctx->wait_event.tasklet);
4439 pool_free(pool_head_quic_conn_ctx, ctx);
4440
4441 return 1;
4442}
4443
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004444static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004445 struct quic_rx_packet *pkt, int first_pkt,
4446 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004447{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004448 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004449 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004450 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004451 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004452 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004453 size_t b_cspace;
4454 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004455
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004456 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004458 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004459 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004460 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4461 /* This ist only to please to traces and distinguish the
4462 * packet with parsed packet number from others.
4463 */
4464 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004465 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004466 goto err;
4467
4468 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004469 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004470 /* XXX TO BE DISCARDED */
4471 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4472 goto err;
4473 }
4474
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004475 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004476 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004477 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004478 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004479 uint64_t len;
4480
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004481 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004482 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4483 goto err;
4484 }
4485
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004486 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4487 * they must have the same DCID.
4488 */
4489 if (!first_pkt &&
4490 (pkt->dcid.len != dgram->dcid_len ||
4491 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4492 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4493 goto err;
4494 }
4495
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004496 /* Retry of Version Negotiation packets are only sent by servers */
4497 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4498 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4499 goto err;
4500 }
4501
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004502 /* RFC9000 6. Version Negotiation */
4503 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004504 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004505 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004506 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4507 goto err;
4508 }
4509
4510 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004511 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004512 }
4513
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004514 /* For Initial packets, and for servers (QUIC clients connections),
4515 * there is no Initial connection IDs storage.
4516 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004517 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004518 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004519
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004520 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4521 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004522 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4523 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004524 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004525
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004526 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4527 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004528 */
4529 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004530
4531 /* TODO Retry should be automatically activated if
4532 * suspect network usage is detected.
4533 */
4534 if (!token_len && l->bind_conf->quic_force_retry) {
4535 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004536 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004537 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4538 goto err;
4539 }
4540
4541 goto err;
4542 }
4543 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004544 pkt->token = buf;
4545 buf += pkt->token_len;
4546 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004547 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004548 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004549 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004550 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4551 goto err;
4552 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004553 }
4554
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004555 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4556 end - buf < len) {
4557 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4558 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004559 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004560
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004561 payload = buf;
4562 pkt->len = len + payload - beg;
4563
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004564 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004565 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004566 int ipv4;
4567 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004568 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004569 const unsigned char *salt = initial_salt_v1;
4570 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004571
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004572 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004573 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004574 goto err;
4575 }
4576
Frédéric Lécailledc364042022-01-27 16:51:54 +01004577 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4578 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4579 goto err;
4580 }
4581
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004582 pkt->saddr = dgram->saddr;
4583 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004584 qc = qc_new_conn(pkt->version, ipv4,
4585 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004586 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004587 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004588 goto err;
4589
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004590 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4591
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004592 odcid = &qc->rx.params.original_destination_connection_id;
4593 /* Copy the transport parameters. */
4594 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004595
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004596 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004597 if (pkt->token_len) {
4598 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4599 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4600 goto err;
4601 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004602 /* Copy retry_source_connection_id transport parameter. */
4603 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4604 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004605 }
4606 else {
4607 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4608 odcid->len = pkt->dcid.len;
4609 }
4610
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004611 /* Copy the initial source connection ID. */
4612 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4613 qc->enc_params_len =
4614 quic_transport_params_encode(qc->enc_params,
4615 qc->enc_params + sizeof qc->enc_params,
4616 &qc->rx.params, 1);
4617 if (!qc->enc_params_len)
4618 goto err;
4619
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004620 if (qc_conn_alloc_ssl_ctx(qc))
4621 goto err;
4622
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004623 if (!quic_conn_init_timer(qc))
4624 goto err;
4625
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004626 if (!quic_conn_init_idle_timer_task(qc))
4627 goto err;
4628
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004629 /* NOTE: the socket address has been concatenated to the destination ID
4630 * chosen by the client for Initial packets.
4631 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004632 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4633 salt = initial_salt_draft_29;
4634 salt_len = sizeof initial_salt_draft_29;
4635 }
4636 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004637 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004638 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004639 goto err;
4640 }
4641
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004642 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004643 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004644 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004645
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004646 /* If the insertion failed, it means that another
4647 * thread has already allocated a QUIC connection for
4648 * the same CID. Liberate our allocated connection.
4649 */
4650 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004651 qc_to_purge = qc;
4652
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004653 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4654 pkt->qc = qc;
4655 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004656
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004657 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004658 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004659 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004660 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004661 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004662 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004663 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004664 }
4665 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004666 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004667 }
4668 }
4669 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004670 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004671 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4672 goto err;
4673 }
4674
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004675 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004676 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004677
4678 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4679 * they must have the same DCID.
4680 */
4681 if (!first_pkt &&
4682 (pkt->dcid.len != dgram->dcid_len ||
4683 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4684 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4685 goto err;
4686 }
4687
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004688 buf += QUIC_HAP_CID_LEN;
4689
4690 /* A short packet is the last one of a UDP datagram. */
4691 payload = buf;
4692 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004693
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004694 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004695 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004696 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004697 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4698 goto err;
4699 }
4700
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004701 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004702 }
4703
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004704
4705 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4706 * they must have the same DCID.
4707 *
4708 * This check must be done after the final update to pkt.len to
4709 * properly drop the packet on failure.
4710 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004711 if (first_pkt && !quic_peer_validated_addr(qc) &&
4712 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4713 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4714 QUIC_EV_CONN_LPKT, qc);
4715 /* Reset the anti-amplification bit. It will be set again
4716 * when sending the next packet if reached again.
4717 */
4718 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4719 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4720 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004722
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004723 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004725 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004726 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004727 goto out;
4728 }
4729
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004730 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004731 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004732 b_cspace = b_contig_space(&qc->rx.buf);
4733 if (b_cspace < pkt->len) {
4734 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004735 if (b_cspace) {
4736 b_putchr(&qc->rx.buf, 0x00);
4737 b_cspace--;
4738 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004739 b_add(&qc->rx.buf, b_cspace);
4740 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004741 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004742 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004743 goto err;
4744 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004745 }
4746
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004747 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004748 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004749 goto err;
4750 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004751
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004752 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004753 if (pkt->aad_len)
4754 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004755 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004756 /* Wake up the connection packet handler task from here only if all
4757 * the contexts have been initialized, especially the mux context
4758 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4759 * will start it if these contexts for the connection are not already
4760 * initialized.
4761 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004762 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004763 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004764 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004765
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004766 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004767
4768 return pkt->len;
4769
4770 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004771 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4772 * This cannot be done by this thread.
4773 */
4774 if (io_cb_wakeup) {
4775 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4776 if (conn_ctx && conn_ctx->wait_event.tasklet)
4777 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4778 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004779 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004780 if (!pkt->len)
4781 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004782 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004783 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004784
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004785 return -1;
4786}
4787
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004788/* This function builds into <buf> buffer a QUIC long packet header.
4789 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004790 */
4791static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4792 int type, size_t pn_len, struct quic_conn *conn)
4793{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004794 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004795 return 0;
4796
4797 /* #0 byte flags */
4798 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4799 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4800 /* Version */
4801 quic_write_uint32(buf, end, conn->version);
4802 *(*buf)++ = conn->dcid.len;
4803 /* Destination connection ID */
4804 if (conn->dcid.len) {
4805 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4806 *buf += conn->dcid.len;
4807 }
4808 /* Source connection ID */
4809 *(*buf)++ = conn->scid.len;
4810 if (conn->scid.len) {
4811 memcpy(*buf, conn->scid.data, conn->scid.len);
4812 *buf += conn->scid.len;
4813 }
4814
4815 return 1;
4816}
4817
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004818/* This function builds into <buf> buffer a QUIC short packet header.
4819 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004820 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004821static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4822 size_t pn_len, struct quic_conn *conn,
4823 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004824{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004825 if (end - *buf < 1 + conn->dcid.len)
4826 return 0;
4827
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004828 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004829 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4830 ((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 +01004831 /* Destination connection ID */
4832 if (conn->dcid.len) {
4833 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4834 *buf += conn->dcid.len;
4835 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004836
4837 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004838}
4839
4840/* Apply QUIC header protection to the packet with <buf> as first byte address,
4841 * <pn> as address of the Packet number field, <pnlen> being this field length
4842 * with <aead> as AEAD cipher and <key> as secret key.
4843 * Returns 1 if succeeded or 0 if failed.
4844 */
4845static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4846 const EVP_CIPHER *aead, const unsigned char *key)
4847{
4848 int i, ret, outlen;
4849 EVP_CIPHER_CTX *ctx;
4850 /* We need an IV of at least 5 bytes: one byte for bytes #0
4851 * and at most 4 bytes for the packet number
4852 */
4853 unsigned char mask[5] = {0};
4854
4855 ret = 0;
4856 ctx = EVP_CIPHER_CTX_new();
4857 if (!ctx)
4858 return 0;
4859
4860 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4861 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4862 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4863 goto out;
4864
4865 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4866 for (i = 0; i < pnlen; i++)
4867 pn[i] ^= mask[i + 1];
4868
4869 ret = 1;
4870
4871 out:
4872 EVP_CIPHER_CTX_free(ctx);
4873
4874 return ret;
4875}
4876
4877/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4878 * ACK ranges if needed to a value below <limit> in bytes.
4879 * Return 1 if succeeded, 0 if not.
4880 */
4881static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4882{
4883 size_t room, ack_delay_sz;
4884
4885 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4886 /* A frame is made of 1 byte for the frame type. */
4887 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004888 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004889 return 0;
4890
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004891 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004892}
4893
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004894/* Prepare into <outlist> as most as possible ack-eliciting frame from their
4895 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
4896 * with <room> as available room, and <*len> the packet Length field initialized
4897 * with the number of bytes already present in this buffer which must be taken
4898 * into an account for the Length packet field value. <headlen> is the number of
4899 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004900 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004901 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004902 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004903 * Return 1 if succeeded, 0 if not.
4904 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004905static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004906 size_t room, size_t *len, size_t headlen,
4907 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004908 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004909{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004910 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004911 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004912
Frédéric Lécailleea604992020-12-24 13:01:37 +01004913 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004914 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004915 return 0;
4916
Frédéric Lécailleea604992020-12-24 13:01:37 +01004917 /* If we are not probing we must take into an account the congestion
4918 * control window.
4919 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004920 if (!qel->pktns->tx.pto_probe) {
4921 size_t remain = quic_path_prep_data(qc->path);
4922
4923 if (headlen > remain)
4924 return 0;
4925
4926 room = QUIC_MIN(room, remain - headlen);
4927 }
4928
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004929 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004930 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004931 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004932 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004933 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004934
Frédéric Lécailleea604992020-12-24 13:01:37 +01004935 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004936 break;
4937
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004938 switch (cf->type) {
4939 case QUIC_FT_CRYPTO:
4940 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004941 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004942 /* Compute the length of this CRYPTO frame header */
4943 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4944 /* Compute the data length of this CRyPTO frame. */
4945 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4946 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004947 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004948 if (!dlen)
4949 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004950
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004951 /* CRYPTO frame length. */
4952 flen = hlen + quic_int_getsize(dlen) + dlen;
4953 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004954 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004955 /* Add the CRYPTO data length and its encoded length to the packet
4956 * length and the length of this length.
4957 */
4958 *len += flen;
4959 room -= flen;
4960 if (dlen == cf->crypto.len) {
4961 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004962 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004963 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004964 }
4965 else {
4966 struct quic_frame *new_cf;
4967
4968 new_cf = pool_alloc(pool_head_quic_frame);
4969 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004970 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004971 return 0;
4972 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004973
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004974 new_cf->type = QUIC_FT_CRYPTO;
4975 new_cf->crypto.len = dlen;
4976 new_cf->crypto.offset = cf->crypto.offset;
4977 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01004978 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004979 /* Consume <dlen> bytes of the current frame. */
4980 cf->crypto.len -= dlen;
4981 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004982 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004983 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004984
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004985 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004986 /* Note that these frames are accepted in short packets only without
4987 * "Length" packet field. Here, <*len> is used only to compute the
4988 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004989 *
4990 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004991 * excepting the variable ones. Note that +1 is for the type of this frame.
4992 */
4993 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004994 ((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 +02004995 /* Compute the data length of this STREAM frame. */
4996 avail_room = room - hlen - *len;
4997 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004998 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004999
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005000 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005001 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005002 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5003 dlen = max_available_room(avail_room, &dlen_sz);
5004 if (dlen > cf->stream.len) {
5005 dlen = cf->stream.len;
5006 }
5007 dlen_sz = quic_int_getsize(dlen);
5008 flen = hlen + dlen_sz + dlen;
5009 }
5010 else {
5011 dlen = QUIC_MIN(avail_room, cf->stream.len);
5012 flen = hlen + dlen;
5013 }
5014 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005015 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005016 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005017 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005018 /* Add the STREAM data length and its encoded length to the packet
5019 * length and the length of this length.
5020 */
5021 *len += flen;
5022 room -= flen;
5023 if (dlen == cf->stream.len) {
5024 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005025 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005026 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005027
5028 qcc_streams_sent_done(cf->stream.qcs,
5029 cf->stream.len,
5030 cf->stream.offset.key);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005031 }
5032 else {
5033 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005034 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005035
5036 new_cf = pool_zalloc(pool_head_quic_frame);
5037 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005038 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005039 return 0;
5040 }
5041
5042 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005043 new_cf->stream.qcs = cf->stream.qcs;
5044 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005045 new_cf->stream.id = cf->stream.id;
5046 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5047 new_cf->stream.offset = cf->stream.offset;
5048 new_cf->stream.len = dlen;
5049 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5050 /* FIN bit reset */
5051 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5052 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005053 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005054 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5055 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005056 cf_buf = b_make(b_orig(cf->stream.buf),
5057 b_size(cf->stream.buf),
5058 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005059 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005060 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005061 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005062
5063 qcc_streams_sent_done(new_cf->stream.qcs,
5064 new_cf->stream.len,
5065 new_cf->stream.offset.key);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005066 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005067
5068 /* TODO the MUX is notified about the frame sending via
5069 * previous qcc_streams_sent_done call. However, the
5070 * sending can fail later, for example if the sendto
5071 * system call returns an error. As the MUX has been
5072 * notified, the transport layer is responsible to
5073 * bufferize and resent the announced data later.
5074 */
5075
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005076 break;
5077
5078 default:
5079 flen = qc_frm_len(cf);
5080 BUG_ON(!flen);
5081 if (flen > room)
5082 continue;
5083
5084 *len += flen;
5085 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005086 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005087 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005088 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005089 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01005090 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005091 }
5092
Frédéric Lécailleea604992020-12-24 13:01:37 +01005093 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005094}
5095
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005096/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005097 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5098 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005099 * filling the buffer with as much frames as possible from <frms> list of
5100 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101 * 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 +02005102 * 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 +02005103 * having returned from this function.
5104 * 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 +01005105 * number field in this packet. <pn_len> will also have the packet number
5106 * length as value.
5107 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005108 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005109 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005110static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5111 size_t dglen, struct quic_tx_packet *pkt,
5112 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005113 int ack, int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005114 struct quic_enc_level *qel, struct quic_conn *qc,
5115 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005116{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005117 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005118 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005119 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5120 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005121 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005122 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005123 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005124 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005125 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005126 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005127
Frédéric Lécailleea604992020-12-24 13:01:37 +01005128 /* Length field value with CRYPTO frames if present. */
5129 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005130 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005131 /* When not probing and not acking, and no immediate close is required,
5132 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005133 * the congestion controller window. So, we do not limit the size of this
5134 * packet if we have an ACK frame to send because an ACK frame is not
5135 * ack-eliciting. This size will be limited if we have ack-eliciting
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005136 * frames to send from <frms>.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005137 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005138 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005139 size_t path_room;
5140
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005141 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005142 if (end - beg > path_room)
5143 end = beg + path_room;
5144 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005146 /* Ensure there is enough room for the TLS encryption tag and a zero token
5147 * length field if any.
5148 */
5149 if (end - pos < QUIC_TLS_TAG_LEN +
5150 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5151 goto no_room;
5152
5153 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005154 rx_largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->rx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005155 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005156 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005157 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005158 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005159 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005160 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005161 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005162 goto no_room;
5163
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005164 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5165 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005166 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005167 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005168 /* Build an ACK frame if required. */
5169 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005170 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005171 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005172 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005173 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5174 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5175 * that from here, we do not know if we will have to send a PING frame.
5176 * This will be decided after having computed the ack-eliciting frames
5177 * to be added to this packet.
5178 */
5179 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 +01005180 if (!ack_frm_len)
5181 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005182 }
5183
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005184 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005185 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005186 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005187 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005188 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005189
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005190 /* Initialize the length of the frames built below to <len>.
5191 * If any frame could be successfully built by qc_build_frms(),
5192 * we will have len_frms > len.
5193 */
5194 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005195 if (!qc_build_frms(&frm_list, frms,
5196 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005197 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005198 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005199 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005200 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005201
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005202 /* Length (of the remaining data). Must not fail because, the buffer size
5203 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5204 * for the encryption tag. It must be taken into an account for the length
5205 * of this packet.
5206 */
5207 if (len_frms)
5208 len = len_frms + QUIC_TLS_TAG_LEN;
5209 else
5210 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005211 /* CONNECTION_CLOSE frame */
5212 if (cc) {
5213 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005214
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005215 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005216 len += qc_frm_len(&cc_frm);
5217 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005218 add_ping_frm = 0;
5219 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005220 len_sz = quic_int_getsize(len);
5221 /* Add this packet size to <dglen> */
5222 dglen += head_len + len_sz + len;
5223 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5224 /* This is a maximum padding size */
5225 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5226 /* The length field value is of this packet is <len> + <padding_len>
5227 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005228 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005229 * sizes from <padding_len>.
5230 */
5231 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5232 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005233 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005234 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005235 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005236 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237 add_ping_frm = 1;
5238 len += 1;
5239 }
5240 /* 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 +01005241 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005242 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5243 }
5244
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005245 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5246 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005247
5248 /* Packet number field address. */
5249 *buf_pn = pos;
5250
5251 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005252 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5253 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005254
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005255 if (ack_frm_len) {
5256 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
5257 goto no_room;
5258
5259 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
5260 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005261
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005262 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005263 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005264 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005265 unsigned char *spos = pos;
5266
5267 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005268 ssize_t room = end - pos;
5269 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005270 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005271 /* TODO: this should not have happened if qc_build_frms()
5272 * had correctly computed and sized the frames to be
5273 * added to this packet. Note that <cf> was added
5274 * from <frm_list> to <frms> list by qc_build_frms().
5275 */
5276 LIST_DELETE(&cf->list);
5277 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005278 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005279 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005280
5281 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005282 quic_tx_packet_refinc(pkt);
5283 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005284 }
5285 }
5286
5287 /* Build a PING frame if needed. */
5288 if (add_ping_frm) {
5289 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005290 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005291 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005292 }
5293
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005294 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005295 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005296 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005297
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005298 /* Build a PADDING frame if needed. */
5299 if (padding_len) {
5300 frm.type = QUIC_FT_PADDING;
5301 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005302 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005303 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005304 }
5305
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005306 /* If this packet is ack-eliciting and we are probing let's
5307 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005308 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005309 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5310 qel->pktns->tx.pto_probe)
5311 qel->pktns->tx.pto_probe--;
5312
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005313 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005314 LIST_SPLICE(&pkt->frms, &frm_list);
5315 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005316
5317 return 1;
5318
5319 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005320 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005321 LIST_SPLICE(frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005322 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005323 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005324}
5325
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005326static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005327{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005328 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005329 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005330 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005331 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005332 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005333 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005334 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005335 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005336 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005337 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005338}
5339
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005340/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005341 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
5342 * of prebuilt frames.
5343 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005344 * Return -2 if the packet could not be allocated or encrypted for any reason,
5345 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01005346 * XXX NOTE XXX
5347 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
5348 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
5349 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005350 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005351static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5352 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005353 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005354 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005355 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005356{
5357 /* The pointer to the packet number field. */
5358 unsigned char *buf_pn;
5359 unsigned char *beg, *end, *payload;
5360 int64_t pn;
5361 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005362 struct quic_tls_ctx *tls_ctx;
5363 struct quic_tx_packet *pkt;
5364
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005365 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005366 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005367 pkt = pool_alloc(pool_head_quic_tx_packet);
5368 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005369 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005370 *err = -2;
5371 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005372 }
5373
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005374 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005375 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005376 pn_len = 0;
5377 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005378
5379 pn = qel->pktns->tx.next_pn + 1;
5380 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 +01005381 ack, padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005382 *err = -1;
5383 goto err;
5384 }
5385
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005386 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005387 payload = buf_pn + pn_len;
5388 payload_len = end - payload;
5389 aad_len = payload - beg;
5390
5391 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005392 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005393 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005394 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005395 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005396
5397 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005398 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005399 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5400 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005401 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005402 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005403 goto err;
5404 }
5405
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005406 /* Consume a packet number */
5407 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005408 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01005409 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005410 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005411 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5412 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005413 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005414 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005415 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005416 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005417 pkt->in_flight_len = pkt->len;
5418 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005419 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005420 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005421 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005422
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005423 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005424
5425 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005426 /* TODO: what about the frames which have been built
5427 * for this packet.
5428 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005429 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005430 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005431 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005432}
5433
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005434
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005435/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5436 * event subscriber <es> is not allowed to change from a previous call as long
5437 * as at least one event is still subscribed. The <event_type> must only be a
5438 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5439 */
5440static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5441{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005442 struct qcc *qcc = conn->qc->qcc;
5443
5444 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5445 BUG_ON(qcc->subs && qcc->subs != es);
5446
5447 es->events |= event_type;
5448 qcc->subs = es;
5449
5450 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005451 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005452
5453 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005454 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005455
5456 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005457}
5458
5459/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5460 * The <es> pointer is not allowed to differ from the one passed to the
5461 * subscribe() call. It always returns zero.
5462 */
5463static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5464{
5465 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5466}
5467
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005468/* Store in <xprt_ctx> the context attached to <conn>.
5469 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005470 */
5471static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5472{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005473 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005474
5475 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5476
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005477 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005478 if (*xprt_ctx)
5479 goto out;
5480
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005481 HA_ATOMIC_STORE(xprt_ctx, conn->qc->xprt_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005482
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005483 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005484 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005485
5486 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005487}
5488
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005489/* Start the QUIC transport layer */
5490static int qc_xprt_start(struct connection *conn, void *ctx)
5491{
5492 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005493 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005494
5495 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005496 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5497 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5498 return 0;
5499 }
5500
5501 /* mux-quic can now be considered ready. */
5502 qc->mux_state = QC_MUX_READY;
5503
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005504 tasklet_wakeup(qctx->wait_event.tasklet);
5505 return 1;
5506}
5507
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005508/* transport-layer operations for QUIC connections. */
5509static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005510 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005511 .subscribe = quic_conn_subscribe,
5512 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005513 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005514 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005515 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5516 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005517 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005518 .name = "QUIC",
5519};
5520
5521__attribute__((constructor))
5522static void __quic_conn_init(void)
5523{
5524 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5525 xprt_register(XPRT_QUIC, &ssl_quic);
5526}
5527
5528__attribute__((destructor))
5529static void __quic_conn_deinit(void)
5530{
5531 BIO_meth_free(ha_quic_meth);
5532}
5533
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005534/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5535 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005536 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005537 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005538struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005539{
5540 unsigned char *pos;
5541 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005542 struct quic_dghdlr *dghdlr = ctx;
5543 struct quic_dgram *dgram;
5544 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005545
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005546 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005547 pos = dgram->buf;
5548 end = pos + dgram->len;
5549 do {
5550 int ret;
5551 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005552
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005553 pkt = pool_zalloc(pool_head_quic_rx_packet);
5554 if (!pkt)
5555 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005556
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005557 quic_rx_packet_refinc(pkt);
5558 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5559 first_pkt = 0;
5560 pos += pkt->len;
5561 quic_rx_packet_refdec(pkt);
5562 if (ret == -1)
5563 /* If the packet length could not be found, we cannot continue. */
5564 break;
5565 } while (pos < end);
5566
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005567 /* Increasing the received bytes counter by the UDP datagram length
5568 * if this datagram could be associated to a connection.
5569 */
5570 if (dgram->qc)
5571 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005572
5573 /* Mark this datagram as consumed */
5574 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005575 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005576
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005577 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005578
5579 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005580 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005581}
5582
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005583/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5584 * Returns 1 if succeeded, 0 if not.
5585 */
5586static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5587 unsigned char **dcid, size_t *dcid_len)
5588{
5589 int long_header;
5590 size_t minlen, skip;
5591
5592 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5593 goto err;
5594
5595 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5596 minlen = long_header ?
5597 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5598 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005599 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005600 goto err;
5601
5602 buf += skip;
5603 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5604 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5605 goto err;
5606
5607 *dcid = buf;
5608
5609 return 1;
5610
5611 err:
5612 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5613 return 0;
5614}
5615
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005616/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5617 * correct datagram handler.
5618 * Return 1 if a correct datagram could be found, 0 if not.
5619 */
5620int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5621 struct sockaddr_storage *saddr,
5622 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005623{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005624 struct quic_dgram *dgram;
5625 unsigned char *dcid;
5626 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005627 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005628
5629 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005630 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005631
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005632 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005633 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005634 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005635
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005636 cid_tid = quic_get_cid_tid(dcid);
5637
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005638 /* All the members must be initialized! */
5639 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005640 dgram->buf = buf;
5641 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005642 dgram->dcid = dcid;
5643 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005644 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005645 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005646 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005647 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005648
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005649 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005650
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005651 return 1;
5652
5653 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005654 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005655}
5656
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005657/* Function to automatically activate QUIC traces on stdout.
5658 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5659 * Main use for now is in the docker image for QUIC interop testing.
5660 */
5661static void quic_init_stdout_traces(void)
5662{
5663#ifdef ENABLE_QUIC_STDOUT_TRACES
5664 trace_quic.sink = sink_find("stdout");
5665 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005666 trace_quic.state = TRACE_STATE_RUNNING;
5667#endif
5668}
5669INITCALL0(STG_INIT, quic_init_stdout_traces);
5670
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005671/*
5672 * Local variables:
5673 * c-indent-level: 8
5674 * c-basic-offset: 8
5675 * End:
5676 */