blob: d16fee548fc4e2668c48a2bb330d61ab101c73c7 [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>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010015#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <netinet/tcp.h>
23
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020024#include <import/ebmbtree.h>
25
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010026#include <haproxy/buf-t.h>
27#include <haproxy/compat.h>
28#include <haproxy/api.h>
29#include <haproxy/debug.h>
30#include <haproxy/tools.h>
31#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010032
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010038#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010039#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010040#include <haproxy/mux_quic.h>
Amaury Denoyelle3db98e92022-05-13 15:41:04 +020041#include <haproxy/ncbuf.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écaille3ccea6d2022-05-23 22:54:54 +020048#include <haproxy/quic_stats.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020049#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020050#include <haproxy/quic_tp.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020051#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010052#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010053#include <haproxy/quic_tls.h>
54#include <haproxy/ssl_sock.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010055#include <haproxy/task.h>
56#include <haproxy/trace.h>
57#include <haproxy/xprt_quic.h>
58
Amaury Denoyellea22d8602021-11-10 15:17:56 +010059/* list of supported QUIC versions by this implementation */
60static int quic_supported_version[] = {
61 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010062 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010063
64 /* placeholder, do not add entry after this */
65 0x0
66};
67
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010068/* trace source and events */
69static void quic_trace(enum trace_level level, uint64_t mask, \
70 const struct trace_source *src,
71 const struct ist where, const struct ist func,
72 const void *a1, const void *a2, const void *a3, const void *a4);
73
74static const struct trace_event quic_trace_events[] = {
75 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
76 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
77 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
78 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
79 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
80 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
81 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050082 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010083 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
84 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
85 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaille00e24002022-02-18 17:13:45 +010086 { .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 +010087 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
88 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
89 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
90 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
91 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
92 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
93 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
94 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
95 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
96 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
97 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
98 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
99 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
100 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
101 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100102 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
103 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
104 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
105 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
106 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
107 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100108 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200109 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
110 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100111 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
112 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100113 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200114 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200115 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200116 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100117 { /* end */ }
118};
119
120static const struct name_desc quic_trace_lockon_args[4] = {
121 /* arg1 */ { /* already used by the connection */ },
122 /* arg2 */ { .name="quic", .desc="QUIC transport" },
123 /* arg3 */ { },
124 /* arg4 */ { }
125};
126
127static const struct name_desc quic_trace_decoding[] = {
128#define QUIC_VERB_CLEAN 1
129 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
130 { /* end */ }
131};
132
133
134struct trace_source trace_quic = {
135 .name = IST("quic"),
136 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100137 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100138 .default_cb = quic_trace,
139 .known_events = quic_trace_events,
140 .lockon_args = quic_trace_lockon_args,
141 .decoding = quic_trace_decoding,
142 .report_events = ~0, /* report everything by default */
143};
144
145#define TRACE_SOURCE &trace_quic
146INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
147
148static BIO_METHOD *ha_quic_meth;
149
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200150DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100151DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100152DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200153 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100154DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155DECLARE_POOL(pool_head_quic_connection_id,
156 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100157DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_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 +0100159DECLARE_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 +0100160DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_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 +0200162DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100163DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200165static 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 +0100166 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100167 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200168 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100169static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille47756802022-03-25 09:12:16 +0100170static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100171static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +0200172static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
173static int quic_conn_init_timer(struct quic_conn *qc);
174static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100175
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100176/* Only for debug purpose */
177struct enc_debug_info {
178 unsigned char *payload;
179 size_t payload_len;
180 unsigned char *aad;
181 size_t aad_len;
182 uint64_t pn;
183};
184
185/* Initializes a enc_debug_info struct (only for debug purpose) */
186static inline void enc_debug_info_init(struct enc_debug_info *edi,
187 unsigned char *payload, size_t payload_len,
188 unsigned char *aad, size_t aad_len, uint64_t pn)
189{
190 edi->payload = payload;
191 edi->payload_len = payload_len;
192 edi->aad = aad;
193 edi->aad_len = aad_len;
194 edi->pn = pn;
195}
196
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100197/* Trace callback for QUIC.
198 * These traces always expect that arg1, if non-null, is of type connection.
199 */
200static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
201 const struct ist where, const struct ist func,
202 const void *a1, const void *a2, const void *a3, const void *a4)
203{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100204 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100205
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100206 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100207 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100208
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100209 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100210 if ((mask & QUIC_EV_CONN_INIT) && qc) {
211 chunk_appendf(&trace_buf, "\n odcid");
212 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100213 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100215 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100217 }
218
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200219 if (mask & QUIC_EV_TRANSP_PARAMS) {
220 const struct quic_transport_params *p = a2;
221 quic_transport_params_dump(&trace_buf, p);
222 }
223
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100224 if (mask & QUIC_EV_CONN_ADDDATA) {
225 const enum ssl_encryption_level_t *level = a2;
226 const size_t *len = a3;
227
228 if (level) {
229 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
230
231 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
232 }
233 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100234 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100235 }
236 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
237 /* Initial read & write secrets. */
238 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
239 const unsigned char *rx_sec = a2;
240 const unsigned char *tx_sec = a3;
241
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100242 tls_ctx = &qc->els[level].tls_ctx;
243 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100244 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
245 if (rx_sec)
246 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100247 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100248 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
249 if (tx_sec)
250 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100251 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100252 }
253 }
254 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
255 const enum ssl_encryption_level_t *level = a2;
256 const unsigned char *secret = a3;
257 const size_t *secret_len = a4;
258
259 if (level) {
260 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
261
262 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
263 if (secret && secret_len)
264 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100265 tls_ctx = &qc->els[lvl].tls_ctx;
266 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
267 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100268 }
269 }
270
271 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
272 const enum ssl_encryption_level_t *level = a2;
273 const unsigned char *secret = a3;
274 const size_t *secret_len = a4;
275
276 if (level) {
277 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
278
279 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
280 if (secret && secret_len)
281 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100282 tls_ctx = &qc->els[lvl].tls_ctx;
283 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
284 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285 }
286
287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200289 if (mask & QUIC_EV_CONN_FRMLIST) {
290 const struct list *l = a2;
291
292 if (l) {
293 const struct quic_frame *frm;
294 list_for_each_entry(frm, l, list)
295 chunk_frm_appendf(&trace_buf, frm);
296 }
297 }
298
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100299 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 const struct quic_tx_packet *pkt = a2;
301 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100302 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100303
304 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100305 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100306 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100307 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100308 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100309 (unsigned long long)qc->path->cwnd,
310 (unsigned long long)qc->path->prep_in_flight,
311 (unsigned long long)qc->path->in_flight,
312 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100313 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100314 }
315 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200316 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100317 if (pkt->pn_node.key != (uint64_t)-1)
318 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100320 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100321 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
322 (unsigned long long)qc->rx.bytes,
323 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100324 }
325
326 if (room) {
327 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
328 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
329 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 }
331 }
332
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100333 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100334 const enum quic_handshake_state *state = a2;
335 const int *err = a3;
336
337 if (state)
338 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
339 if (err)
340 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
341 }
342
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100343 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100344 const struct quic_rx_packet *pkt = a2;
345 const unsigned long *pktlen = a3;
346 const SSL *ssl = a4;
347
348 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200349 chunk_appendf(&trace_buf, " pkt@%p", pkt);
350 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
351 chunk_appendf(&trace_buf, " kp=%d",
352 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
353 chunk_appendf(&trace_buf, " el=%c",
354 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100355 if (pkt->pnl)
356 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
357 (unsigned long long)pkt->pn);
358 if (pkt->token_len)
359 chunk_appendf(&trace_buf, " toklen=%llu",
360 (unsigned long long)pkt->token_len);
361 if (pkt->aad_len)
362 chunk_appendf(&trace_buf, " aadlen=%llu",
363 (unsigned long long)pkt->aad_len);
364 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
365 pkt->flags, (unsigned long long)pkt->len);
366 }
367 if (pktlen)
368 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
369 if (ssl) {
370 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
371 chunk_appendf(&trace_buf, " el=%c",
372 quic_enc_level_char(ssl_to_quic_enc_level(level)));
373 }
374 }
375
376 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
377 const struct quic_rx_packet *pkt = a2;
378 const struct quic_rx_crypto_frm *cf = a3;
379 const SSL *ssl = a4;
380
381 if (pkt)
382 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
383 quic_packet_type_enc_level_char(pkt->type),
384 (unsigned long long)pkt->pn);
385 if (cf)
386 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
387 (unsigned long long)cf->offset_node.key,
388 (unsigned long long)cf->len);
389 if (ssl) {
390 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200391 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100392 quic_enc_level_char(ssl_to_quic_enc_level(level)));
393 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100394
395 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100396 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 }
398
399 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
400 const struct quic_frame *frm = a2;
401
402 if (frm)
403 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
404 }
405
406 if (mask & QUIC_EV_CONN_PHPKTS) {
407 const struct quic_enc_level *qel = a2;
408
409 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100410 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100411 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100412 " 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 +0100413 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200414 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200415 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100416 (unsigned long long)qc->path->cwnd,
417 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100418 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100419 (unsigned long long)pktns->tx.in_flight,
420 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100421 }
422 }
423
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100424 if (mask & QUIC_EV_CONN_ENCPKT) {
425 const struct enc_debug_info *edi = a2;
426
427 if (edi)
428 chunk_appendf(&trace_buf,
429 " payload=@%p payload_len=%llu"
430 " aad=@%p aad_len=%llu pn=%llu",
431 edi->payload, (unsigned long long)edi->payload_len,
432 edi->aad, (unsigned long long)edi->aad_len,
433 (unsigned long long)edi->pn);
434 }
435
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100436 if (mask & QUIC_EV_CONN_RMHP) {
437 const struct quic_rx_packet *pkt = a2;
438
439 if (pkt) {
440 const int *ret = a3;
441
442 chunk_appendf(&trace_buf, " pkt@%p", pkt);
443 if (ret && *ret)
444 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
445 pkt->pnl, (unsigned long long)pkt->pn);
446 }
447 }
448
449 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200450 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100451 const unsigned long *val1 = a3;
452 const unsigned long *val2 = a4;
453
454 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100455 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100456 if (val1)
457 chunk_appendf(&trace_buf, " %lu", *val1);
458 if (val2)
459 chunk_appendf(&trace_buf, "..%lu", *val2);
460 }
461
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100462 if (mask & QUIC_EV_CONN_ACKSTRM) {
463 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200464 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100465
466 if (s)
467 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200468 if (stream)
469 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100470 }
471
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100472 if (mask & QUIC_EV_CONN_RTTUPDT) {
473 const unsigned int *rtt_sample = a2;
474 const unsigned int *ack_delay = a3;
475 const struct quic_loss *ql = a4;
476
477 if (rtt_sample)
478 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
479 if (ack_delay)
480 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
481 if (ql)
482 chunk_appendf(&trace_buf,
483 " srtt=%ums rttvar=%ums min_rtt=%ums",
484 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
485 }
486 if (mask & QUIC_EV_CONN_CC) {
487 const struct quic_cc_event *ev = a2;
488 const struct quic_cc *cc = a3;
489
490 if (a2)
491 quic_cc_event_trace(&trace_buf, ev);
492 if (a3)
493 quic_cc_state_trace(&trace_buf, cc);
494 }
495
496 if (mask & QUIC_EV_CONN_PKTLOSS) {
497 const struct quic_pktns *pktns = a2;
498 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100499
500 if (pktns) {
501 chunk_appendf(&trace_buf, " pktns=%s",
502 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
503 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
504 if (pktns->tx.loss_time)
505 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100506 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100507 }
508 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
509 struct quic_tx_packet *pkt;
510
511 chunk_appendf(&trace_buf, " lost_pkts:");
512 list_for_each_entry(pkt, lost_pkts, list)
513 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
514 }
515 }
516
517 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100518 const struct quic_pktns *pktns = a2;
519 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100520 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100521
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100522 if (ifae_pkts)
523 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
524 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100525 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100526 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100527 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100528 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
529 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100530 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
531 if (pktns->tx.in_flight)
532 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100533 if (pktns->tx.loss_time)
534 chunk_appendf(&trace_buf, " loss_time=%dms",
535 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
536 }
537 if (mask & QUIC_EV_CONN_SPTO) {
538 if (pktns->tx.time_of_last_eliciting)
539 chunk_appendf(&trace_buf, " tole=%dms",
540 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
541 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100542 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100543 }
544 }
545
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200546 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100547 chunk_appendf(&trace_buf,
548 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
549 }
550 }
551
552 if (mask & QUIC_EV_CONN_SPPKTS) {
553 const struct quic_tx_packet *pkt = a2;
554
Frédéric Lécaille8726d632022-05-03 10:32:21 +0200555 chunk_appendf(&trace_buf, " err=%u cwnd=%llu ppif=%llu pif=%llu", qc->sendto_err,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100556 (unsigned long long)qc->path->cwnd,
557 (unsigned long long)qc->path->prep_in_flight,
558 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100559 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100560 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100561 (unsigned long)pkt->pn_node.key,
562 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
563 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100564 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100565 }
566 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100567
568 if (mask & QUIC_EV_CONN_SSLALERT) {
569 const uint8_t *alert = a2;
570 const enum ssl_encryption_level_t *level = a3;
571
572 if (alert)
573 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
574 if (level)
575 chunk_appendf(&trace_buf, " el=%c",
576 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
577 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100578
579 if (mask & QUIC_EV_CONN_BCFRMS) {
580 const size_t *sz1 = a2;
581 const size_t *sz2 = a3;
582 const size_t *sz3 = a4;
583
584 if (sz1)
585 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
586 if (sz2)
587 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
588 if (sz3)
589 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
590 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100591
592 if (mask & QUIC_EV_CONN_PSTRM) {
593 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100594
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100595 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100596 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100597 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 }
599 if (mask & QUIC_EV_CONN_LPKT) {
600 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200601 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100602
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100603 if (pkt) {
604 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
605 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
606 if (pkt->pn_node.key != (uint64_t)-1)
607 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
608 }
609
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200610 if (len)
611 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 }
613
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200614 if (mask & QUIC_EV_STATELESS_RST) {
615 const struct quic_cid *cid = a2;
616
617 if (cid)
618 quic_cid_dump(&trace_buf, cid);
619 }
620
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100621}
622
623/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100624static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100625{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200626 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100627
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100628 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100629 return 1;
630
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200631 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
632 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200633 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
634 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200635 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100636 return 1;
637
638 return 0;
639}
640
641/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
642 * both loss detection and PTO and schedule the task assiated to this timer if needed.
643 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100644static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100645{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646 struct quic_pktns *pktns;
647 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200648 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100649
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100650 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100651 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100652
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100653 pktns = quic_loss_pktns(qc);
654 if (tick_isset(pktns->tx.loss_time)) {
655 qc->timer = pktns->tx.loss_time;
656 goto out;
657 }
658
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100659 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100660 * cancelled for a server which reached the anti-amplification limit.
661 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100662 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200663 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100664 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100665 qc->timer = TICK_ETERNITY;
666 goto out;
667 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100668
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100669 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100670 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100671 /* Timer cancellation. */
672 qc->timer = TICK_ETERNITY;
673 goto out;
674 }
675
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200676 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200677 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100678 if (tick_isset(pto))
679 qc->timer = pto;
680 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100681 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100682 if (tick_is_expired(qc->timer, now_ms)) {
683 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100684 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100685 }
686 else {
687 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100688 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100689 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100690 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100691 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100692}
693
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100694/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
695 * connection.
696 * Return 1 if succeeded, 0 if not.
697 */
698static int quic_tls_key_update(struct quic_conn *qc)
699{
700 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
701 struct quic_tls_secrets *rx, *tx;
702 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
703 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
704
705 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
706 rx = &tls_ctx->rx;
707 tx = &tls_ctx->tx;
708 nxt_rx = &qc->ku.nxt_rx;
709 nxt_tx = &qc->ku.nxt_tx;
710
711 /* Prepare new RX secrets */
712 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
713 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100714 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100715 return 0;
716 }
717
718 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
719 nxt_rx->key, nxt_rx->keylen,
720 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
721 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100722 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100723 return 0;
724 }
725
726 /* Prepare new TX secrets */
727 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
728 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100729 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100730 return 0;
731 }
732
733 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
734 nxt_tx->key, nxt_tx->keylen,
735 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
736 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100737 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100738 return 0;
739 }
740
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200741 if (nxt_rx->ctx) {
742 EVP_CIPHER_CTX_free(nxt_rx->ctx);
743 nxt_rx->ctx = NULL;
744 }
745
746 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
747 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
748 return 0;
749 }
750
751 if (nxt_tx->ctx) {
752 EVP_CIPHER_CTX_free(nxt_tx->ctx);
753 nxt_tx->ctx = NULL;
754 }
755
756 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
757 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
758 return 0;
759 }
760
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100761 return 1;
762}
763
764/* Rotate the Key Update information for <qc> QUIC connection.
765 * Must be used after having updated them.
766 * Always succeeds.
767 */
768static void quic_tls_rotate_keys(struct quic_conn *qc)
769{
770 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
771 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200772 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100773
774 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200775 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100776 curr_secret = tls_ctx->rx.secret;
777 curr_iv = tls_ctx->rx.iv;
778 curr_key = tls_ctx->rx.key;
779
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200780 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100781 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
782 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
783 tls_ctx->rx.key = qc->ku.nxt_rx.key;
784
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200785 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100786 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
787 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
788 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
789
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200790 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100791 qc->ku.prv_rx.secret = curr_secret;
792 qc->ku.prv_rx.iv = curr_iv;
793 qc->ku.prv_rx.key = curr_key;
794 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
795
796 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200797 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100798 curr_secret = tls_ctx->tx.secret;
799 curr_iv = tls_ctx->tx.iv;
800 curr_key = tls_ctx->tx.key;
801
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200802 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100803 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
804 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
805 tls_ctx->tx.key = qc->ku.nxt_tx.key;
806
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200807 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100808 qc->ku.nxt_tx.secret = curr_secret;
809 qc->ku.nxt_tx.iv = curr_iv;
810 qc->ku.nxt_tx.key = curr_key;
811}
812
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100813#ifndef OPENSSL_IS_BORINGSSL
814int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
815 const uint8_t *read_secret,
816 const uint8_t *write_secret, size_t secret_len)
817{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100818 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
819 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 +0100820 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100821 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100822
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100823 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100824 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200825 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100826 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100827 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100828 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100829
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100830 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100831 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100832 goto err;
833 }
834
835 rx = &tls_ctx->rx;
836 tx = &tls_ctx->tx;
837
838 rx->aead = tx->aead = tls_aead(cipher);
839 rx->md = tx->md = tls_md(cipher);
840 rx->hp = tx->hp = tls_hp(cipher);
841
842 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
843 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100844 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100845 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100846 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100847 }
848
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200849 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
850 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
851 goto err;
852 }
853
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100854 /* Enqueue this connection asap if we could derive O-RTT secrets as
855 * listener. Note that a listener derives only RX secrets for this
856 * level.
857 */
858 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
859 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100860
861 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100862 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100863
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100864 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
865 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100866 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100867 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100868 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100869 }
870
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200871 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
872 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
873 goto err;
874 }
875
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100876 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100877 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
878 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
879 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
880
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200881 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100882 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
883 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100884 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100885 goto err;
886 }
887
888 memcpy(rx->secret, read_secret, secret_len);
889 rx->secretlen = secret_len;
890 memcpy(tx->secret, write_secret, secret_len);
891 tx->secretlen = secret_len;
892 /* Initialize all the secret keys lengths */
893 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
894 /* Prepare the next key update */
895 if (!quic_tls_key_update(qc))
896 goto err;
897 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100898
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100899 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100900 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100901 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100902 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100903 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100904
905 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100906 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100907 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100908}
909#else
910/* ->set_read_secret callback to derive the RX secrets at <level> encryption
911 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500912 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100913 */
914int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
915 const SSL_CIPHER *cipher,
916 const uint8_t *secret, size_t secret_len)
917{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100918 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100919 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100920 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100921
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100922 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200923 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100924 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100925 goto out;
926 }
927
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100928 tls_ctx->rx.aead = tls_aead(cipher);
929 tls_ctx->rx.md = tls_md(cipher);
930 tls_ctx->rx.hp = tls_hp(cipher);
931
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100932 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
933 goto err;
934
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935 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 +0100936 tls_ctx->rx.key, tls_ctx->rx.keylen,
937 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100938 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
939 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100940 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100941 goto err;
942 }
943
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100944 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100945 const unsigned char *buf;
946 size_t buflen;
947
948 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
949 if (!buflen)
950 goto err;
951
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100952 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100953 goto err;
954 }
955
956 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100957 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100958 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100959
960 return 1;
961
962 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100963 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100964 return 0;
965}
966
967/* ->set_write_secret callback to derive the TX secrets at <level>
968 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500969 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100970 */
971int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
972 const SSL_CIPHER *cipher,
973 const uint8_t *secret, size_t secret_len)
974{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100975 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
976 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 +0100977
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100978 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200979 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100980 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100981 goto out;
982 }
983
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100984 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
985 goto err;
986
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100987 tls_ctx->tx.aead = tls_aead(cipher);
988 tls_ctx->tx.md = tls_md(cipher);
989 tls_ctx->tx.hp = tls_hp(cipher);
990
991 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 +0100992 tls_ctx->tx.key, tls_ctx->tx.keylen,
993 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100994 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
995 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100996 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100997 goto err;
998 }
999
1000 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001001 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001002 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001003 return 1;
1004
1005 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001006 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001007 return 0;
1008}
1009#endif
1010
1011/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1012 * with <len> as size in CRYPTO buffers dedicated to store the information about
1013 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1014 * It fails only if it could not managed to allocate enough CRYPTO buffers to
1015 * store all the data.
1016 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1017 */
1018static int quic_crypto_data_cpy(struct quic_enc_level *qel,
1019 const unsigned char *data, size_t len)
1020{
1021 struct quic_crypto_buf **qcb;
1022 /* The remaining byte to store in CRYPTO buffers. */
1023 size_t cf_offset, cf_len, *nb_buf;
1024 unsigned char *pos;
1025
1026 nb_buf = &qel->tx.crypto.nb_buf;
1027 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1028 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1029 cf_len = len;
1030
1031 while (len) {
1032 size_t to_copy, room;
1033
1034 pos = (*qcb)->data + (*qcb)->sz;
1035 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1036 to_copy = len > room ? room : len;
1037 if (to_copy) {
1038 memcpy(pos, data, to_copy);
1039 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1040 qel->tx.crypto.sz += to_copy;
1041 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001042 len -= to_copy;
1043 data += to_copy;
1044 }
1045 else {
1046 struct quic_crypto_buf **tmp;
1047
1048 tmp = realloc(qel->tx.crypto.bufs,
1049 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1050 if (tmp) {
1051 qel->tx.crypto.bufs = tmp;
1052 qcb = &qel->tx.crypto.bufs[*nb_buf];
1053 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1054 if (!*qcb)
1055 return 0;
1056
1057 (*qcb)->sz = 0;
1058 ++*nb_buf;
1059 }
1060 else {
1061 break;
1062 }
1063 }
1064 }
1065
1066 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1067 * have been buffered.
1068 */
1069 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001070 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001071 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001072
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001073 /* There is at most one CRYPTO frame in this packet number
1074 * space. Let's look for it.
1075 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001076 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001077 if (frm->type != QUIC_FT_CRYPTO)
1078 continue;
1079
1080 /* Found */
1081 found = frm;
1082 break;
1083 }
1084
1085 if (found) {
1086 found->crypto.len += cf_len;
1087 }
1088 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001089 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001090 if (!frm)
1091 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001092
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001093 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001094 frm->type = QUIC_FT_CRYPTO;
1095 frm->crypto.offset = cf_offset;
1096 frm->crypto.len = cf_len;
1097 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001098 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001099 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001100 }
1101
1102 return len == 0;
1103}
1104
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001105/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1106 * activity for <qc> will be interrupted.
1107 */
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02001108void quic_set_connection_close(struct quic_conn *qc, int err, int app)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001109{
1110 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1111 return;
1112
1113 qc->err_code = err;
1114 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02001115
1116 if (app)
1117 qc->flags |= QUIC_FL_CONN_APP_ALERT;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001118}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001119
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001120/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1121void quic_set_tls_alert(struct quic_conn *qc, int alert)
1122{
Frédéric Lécailleeb791452022-05-24 16:01:39 +02001123 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02001124 quic_set_connection_close(qc, QC_ERR_CRYPTO_ERROR | alert, 0);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001125 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001126 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001127}
1128
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001129/* Set the application for <qc> QUIC connection.
1130 * Return 1 if succeeded, 0 if not.
1131 */
1132int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1133{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001134 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1135 qc->app_ops = &h3_ops;
1136 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1137 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001138 else
1139 return 0;
1140
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001141 return 1;
1142}
1143
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1145 * wants to provide the QUIC layer with CRYPTO data.
1146 * Returns 1 if succeeded, 0 if not.
1147 */
1148int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1149 const uint8_t *data, size_t len)
1150{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001151 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001152 enum quic_tls_enc_level tel;
1153 struct quic_enc_level *qel;
1154
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001155 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1156 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001157 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001158 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001159 goto out;
1160 }
1161
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001162 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001163 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001164 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001165 goto err;
1166 }
1167
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001168 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001169 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001170 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171 goto err;
1172 }
1173
1174 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001175 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001176
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001177 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001178 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001179 return 1;
1180
1181 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001182 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001183 return 0;
1184}
1185
1186int ha_quic_flush_flight(SSL *ssl)
1187{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001188 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001189
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001190 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1191 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001192
1193 return 1;
1194}
1195
1196int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1197{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001198 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001199
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001200 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1201 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001202 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001203 return 1;
1204}
1205
1206/* QUIC TLS methods */
1207static SSL_QUIC_METHOD ha_quic_method = {
1208#ifdef OPENSSL_IS_BORINGSSL
1209 .set_read_secret = ha_set_rsec,
1210 .set_write_secret = ha_set_wsec,
1211#else
1212 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1213#endif
1214 .add_handshake_data = ha_quic_add_handshake_data,
1215 .flush_flight = ha_quic_flush_flight,
1216 .send_alert = ha_quic_send_alert,
1217};
1218
1219/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1220 * Returns an error count.
1221 */
1222int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1223{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001224 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1225 int cfgerr = 0;
1226
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001227 long options =
1228 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1229 SSL_OP_SINGLE_ECDH_USE |
1230 SSL_OP_CIPHER_SERVER_PREFERENCE;
1231 SSL_CTX *ctx;
1232
1233 ctx = SSL_CTX_new(TLS_server_method());
1234 bind_conf->initial_ctx = ctx;
1235
1236 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001237 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1238 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1239 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001240
1241#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1242#ifdef OPENSSL_IS_BORINGSSL
1243 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1244 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1245#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1246 if (bind_conf->ssl_conf.early_data) {
1247 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001248 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001249 }
1250 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1251 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1252#else
1253 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1254#endif
1255 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1256#endif
1257 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1258
1259 return cfgerr;
1260}
1261
1262/* Decode an expected packet number from <truncated_on> its truncated value,
1263 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1264 * the number of bits used to encode this packet number (its length in bytes * 8).
1265 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1266 */
1267static uint64_t decode_packet_number(uint64_t largest_pn,
1268 uint32_t truncated_pn, unsigned int pn_nbits)
1269{
1270 uint64_t expected_pn = largest_pn + 1;
1271 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1272 uint64_t pn_hwin = pn_win / 2;
1273 uint64_t pn_mask = pn_win - 1;
1274 uint64_t candidate_pn;
1275
1276
1277 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1278 /* Note that <pn_win> > <pn_hwin>. */
1279 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1280 candidate_pn + pn_hwin <= expected_pn)
1281 return candidate_pn + pn_win;
1282
1283 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1284 return candidate_pn - pn_win;
1285
1286 return candidate_pn;
1287}
1288
1289/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1290 * cryptographic context.
1291 * <largest_pn> is the largest received packet number and <pn> the address of
1292 * the packet number field for this packet with <byte0> address of its first byte.
1293 * <end> points to one byte past the end of this packet.
1294 * Returns 1 if succeeded, 0 if not.
1295 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001296static int qc_do_rm_hp(struct quic_conn *qc,
1297 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001298 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001299 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300{
1301 int ret, outlen, i, pnlen;
1302 uint64_t packet_number;
1303 uint32_t truncated_pn = 0;
1304 unsigned char mask[5] = {0};
1305 unsigned char *sample;
1306 EVP_CIPHER_CTX *cctx;
1307 unsigned char *hp_key;
1308
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001309 /* Check there is enough data in this packet. */
1310 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001311 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312 return 0;
1313 }
1314
1315 cctx = EVP_CIPHER_CTX_new();
1316 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001317 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001318 return 0;
1319 }
1320
1321 ret = 0;
1322 sample = pn + QUIC_PACKET_PN_MAXLEN;
1323
1324 hp_key = tls_ctx->rx.hp_key;
1325 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1326 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1327 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001328 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329 goto out;
1330 }
1331
1332 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1333 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1334 for (i = 0; i < pnlen; i++) {
1335 pn[i] ^= mask[i + 1];
1336 truncated_pn = (truncated_pn << 8) | pn[i];
1337 }
1338
1339 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1340 /* Store remaining information for this unprotected header */
1341 pkt->pn = packet_number;
1342 pkt->pnl = pnlen;
1343
1344 ret = 1;
1345
1346 out:
1347 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001348
1349 return ret;
1350}
1351
1352/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1353 * address, with <payload_len> as payload length, <aad> as address of
1354 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1355 * context.
1356 * Returns 1 if succeeded, 0 if not.
1357 */
1358static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1359 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001360 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001361{
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001362 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001363 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001364 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001365 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001366
1367 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001368 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001369 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001370 }
1371
1372 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001373 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001374 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001375 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001376 }
1377
1378 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001379
1380 err:
1381 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001382 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001383 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001384}
1385
1386/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1387 * Returns 1 if succeeded, 0 if not.
1388 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001389static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001390{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001391 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001392 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001393 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001394 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001395 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001396 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1397 unsigned char *rx_key = tls_ctx->rx.key;
1398
1399 kp_changed = 0;
1400 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1401 /* The two tested bits are not at the same position,
1402 * this is why they are first both inversed.
1403 */
1404 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1405 if (pkt->pn < tls_ctx->rx.pn) {
1406 /* The lowest packet number of a previous key phase
1407 * cannot be null if it really stores previous key phase
1408 * secrets.
1409 */
1410 if (!pkt->qc->ku.prv_rx.pn)
1411 return 0;
1412
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001413 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1414 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001415 rx_key = pkt->qc->ku.prv_rx.key;
1416 }
1417 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1418 /* Next key phase */
1419 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001420 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1421 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001422 rx_key = pkt->qc->ku.nxt_rx.key;
1423 }
1424 }
1425 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001426
1427 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1428 return 0;
1429
1430 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1431 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001432 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001433 if (!ret)
1434 return 0;
1435
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001436 /* Update the keys only if the packet decryption succeeded. */
1437 if (kp_changed) {
1438 quic_tls_rotate_keys(pkt->qc);
1439 /* Toggle the Key Phase bit */
1440 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1441 /* Store the lowest packet number received for the current key phase */
1442 tls_ctx->rx.pn = pkt->pn;
1443 /* Prepare the next key update */
1444 if (!quic_tls_key_update(pkt->qc))
1445 return 0;
1446 }
1447
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001448 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001449 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001450
1451 return 1;
1452}
1453
Frédéric Lécaille96367152022-04-25 09:40:19 +02001454
1455/* Remove references to <frm> frame */
1456static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1457{
1458 uint64_t pn;
1459 struct quic_frame *f, *tmp;
1460
1461 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1462 pn = f->pkt->pn_node.key;
1463 f->origin = NULL;
1464 LIST_DELETE(&f->ref);
1465 TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1466 }
1467}
1468
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001469/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001470void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001471{
1472 uint64_t pn;
1473 struct quic_frame *origin, *f, *tmp;
1474
1475 /* Identify this frame: a frame copy or one of its copies */
1476 origin = frm->origin ? frm->origin : frm;
1477 /* Ensure the source of the copies is flagged as acked, <frm> being
1478 * possibly a copy of <origin>
1479 */
1480 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1481 /* Mark all the copy of <origin> as acknowledged. We must
1482 * not release the packets (releasing the frames) at this time as
1483 * they are possibly also to be acknowledged alongside the
1484 * the current one.
1485 */
1486 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1487 pn = f->pkt->pn_node.key;
1488 TRACE_PROTO("mark frame as acked from packet",
1489 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1490 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1491 f->origin = NULL;
1492 LIST_DELETE(&f->ref);
1493 }
1494 LIST_DELETE(&frm->list);
1495 pn = frm->pkt->pn_node.key;
1496 quic_tx_packet_refdec(frm->pkt);
1497 TRACE_PROTO("freeing frame from packet",
1498 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1499 pool_free(pool_head_quic_frame, frm);
1500}
1501
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001502/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001503 *
1504 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001505 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001506static int quic_stream_try_to_consume(struct quic_conn *qc,
1507 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001508{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001509 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001510 struct eb64_node *frm_node;
1511
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001512 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001513 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001514 while (frm_node) {
1515 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001516 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001517 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001518
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001519 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001520 offset = strm->offset.key;
1521 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001522
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001523 if (offset > stream->ack_offset)
1524 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001525
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001526 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001527 /* cf. next comment : frame may be freed at this stage. */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001528 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001529 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001530 ret = 1;
1531 }
1532
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001533 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1534 * has been freed. with the stream frames tree. Nothing to do
1535 * anymore in here.
1536 */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001537 if (!stream)
1538 return 1;
1539
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001540 frm_node = eb64_next(frm_node);
1541 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001542
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001543 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001544 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001545 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001546
1547 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001548}
1549
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001550/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001551static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1552 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001553{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001554 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001555
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001556 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001557 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001558 switch (frm->type) {
1559 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1560 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001561 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001562 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001563 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001564 const size_t offset = strm_frm->offset.key;
1565 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001566
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001567 /* do not use strm_frm->stream as the qc_stream_desc instance
1568 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001569 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001570 *
1571 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001572 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001573 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001574 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1575 if (!node) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001576 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001577 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001578 /* early return */
1579 return;
1580 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001581 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001582
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001583 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001584 if (offset <= stream->ack_offset) {
1585 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001586 stream_acked = 1;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001587 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1588 qc, strm_frm, stream);
1589 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001590
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001591 if (!stream) {
1592 /* no need to continue if stream freed. */
1593 TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001594 qc_release_frm(qc, frm);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001595 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001596 }
1597
Frédéric Lécailleda342552022-04-25 10:28:49 +02001598 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1599 qc, strm_frm, stream);
1600 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001601 }
1602 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001603 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001604 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001605
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001606 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001607 }
1608 break;
1609 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001610 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001611 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001612
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001613 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001614 struct qcc *qcc = qc->qcc;
1615
1616 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1617 tasklet_wakeup(qcc->subs->tasklet);
1618 qcc->subs->events &= ~SUB_RETRY_SEND;
1619 if (!qcc->subs->events)
1620 qcc->subs = NULL;
1621 }
1622 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001623}
1624
1625/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1626 * deallocating them, and their TX frames.
1627 * Returns the last node reached to be used for the next range.
1628 * May be NULL if <largest> node could not be found.
1629 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001630static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1631 struct eb_root *pkts,
1632 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001633 struct list *newly_acked_pkts,
1634 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001635 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001636{
1637 struct eb64_node *node;
1638 struct quic_tx_packet *pkt;
1639
1640 if (largest_node)
1641 node = largest_node;
1642 else {
1643 node = eb64_lookup(pkts, largest);
1644 while (!node && largest > smallest) {
1645 node = eb64_lookup(pkts, --largest);
1646 }
1647 }
1648
1649 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001650 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001651
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001652 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001654 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001655 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001656 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001657 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001658 node = eb64_prev(node);
1659 eb64_delete(&pkt->pn_node);
1660 }
1661
1662 return node;
1663}
1664
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001665/* Remove all frames from <pkt_frm_list> and reinsert them in the
1666 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001667 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001668static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001669 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001670 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001671{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001672 struct quic_frame *frm, *frmbak;
1673 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001674 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001675
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001676 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001677 /* Only for debug */
1678 uint64_t pn;
1679
1680 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001681 LIST_DELETE(&frm->list);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001682 pn = frm->pkt->pn_node.key;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001683 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001684 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001685 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001686 /* Remove any reference to this frame */
1687 qc_frm_unref(qc, frm);
1688 switch (frm->type) {
1689 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1690 {
1691 struct quic_stream *strm_frm = &frm->stream;
1692 struct eb64_node *node = NULL;
1693 struct qc_stream_desc *stream_desc;
1694
1695 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1696 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02001697 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001698 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1699 qc, frm, &pn);
1700 pool_free(pool_head_quic_frame, frm);
1701 continue;
1702 }
1703
1704 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1705 /* Do not resend this frame if in the "already acked range" */
1706 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1707 TRACE_PROTO("ignored frame in already acked range",
1708 QUIC_EV_CONN_PRSAFRM, qc, frm);
1709 continue;
1710 }
1711 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1712 strm_frm->offset.key = stream_desc->ack_offset;
1713 TRACE_PROTO("updated partially acked frame",
1714 QUIC_EV_CONN_PRSAFRM, qc, frm);
1715 }
1716
1717 break;
1718 }
1719
1720 default:
1721 break;
1722 }
1723
1724 /* Do not resend probing packet with old data */
1725 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1726 TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1727 qc, frm, &pn);
1728 if (frm->origin)
1729 LIST_DELETE(&frm->ref);
1730 pool_free(pool_head_quic_frame, frm);
1731 continue;
1732 }
1733
1734 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1735 TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1736 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1737 qc, frm, &pn);
1738 pool_free(pool_head_quic_frame, frm);
1739 }
1740 else {
1741 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001742 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1743 /* Mark this STREAM frame as lost. A look up their stream descriptor
1744 * will be performed to check the stream is not consumed or released.
1745 */
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001746 frm->flags = QUIC_FL_TX_FRAME_LOST;
1747 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001748 LIST_APPEND(&tmp, &frm->list);
1749 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001750 }
1751
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001752 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001753}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001754
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001755/* Free <pkt> TX packet and its attached frames.
1756 * This is the responsability of the caller to remove this packet of
1757 * any data structure it was possibly attached to.
1758 */
1759static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1760{
1761 struct quic_frame *frm, *frmbak;
1762
1763 if (!pkt)
1764 return;
1765
1766 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1767 LIST_DELETE(&frm->list);
1768 pool_free(pool_head_quic_frame, frm);
1769 }
1770 pool_free(pool_head_quic_tx_packet, pkt);
1771}
1772
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001773/* Free the TX packets of <pkts> list */
1774static inline void free_quic_tx_pkts(struct list *pkts)
1775{
1776 struct quic_tx_packet *pkt, *tmp;
1777
1778 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001779 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001781 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001782 }
1783}
1784
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001785/* Remove already sent ranges of acknowledged packet numbers from
1786 * <pktns> packet number space tree below <largest_acked_pn> possibly
1787 * updating the range which contains <largest_acked_pn>.
1788 * Never fails.
1789 */
1790static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1791 int64_t largest_acked_pn)
1792{
1793 struct eb64_node *ar, *next_ar;
1794 struct quic_arngs *arngs = &pktns->rx.arngs;
1795
1796 ar = eb64_first(&arngs->root);
1797 while (ar) {
1798 struct quic_arng_node *ar_node;
1799
1800 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001801 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001802 if ((int64_t)ar_node->first.key > largest_acked_pn)
1803 break;
1804
1805 if (largest_acked_pn < ar_node->last) {
1806 eb64_delete(ar);
1807 ar_node->first.key = largest_acked_pn + 1;
1808 eb64_insert(&arngs->root, ar);
1809 break;
1810 }
1811
1812 eb64_delete(ar);
1813 pool_free(pool_head_quic_arng, ar_node);
1814 arngs->sz--;
1815 ar = next_ar;
1816 }
1817}
1818
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001819/* Send a packet ack event nofication for each newly acked packet of
1820 * <newly_acked_pkts> list and free them.
1821 * Always succeeds.
1822 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001823static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001824 struct list *newly_acked_pkts)
1825{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 struct quic_tx_packet *pkt, *tmp;
1827 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1828
1829 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1830 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001831 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001832 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001834 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001835 /* If this packet contained an ACK frame, proceed to the
1836 * acknowledging of range of acks from the largest acknowledged
1837 * packet number which was sent in an ACK frame by this packet.
1838 */
1839 if (pkt->largest_acked_pn != -1)
1840 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001841 ev.ack.acked = pkt->in_flight_len;
1842 ev.ack.time_sent = pkt->time_sent;
1843 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001844 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001845 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001846 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001847 }
1848
1849}
1850
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001851/* Release all the frames attached to <pktns> packet number space */
1852static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1853{
1854 struct quic_frame *frm, *frmbak;
1855
1856 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1857 LIST_DELETE(&frm->list);
1858 pool_free(pool_head_quic_frame, frm);
1859 }
1860}
1861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862/* Handle <pkts> list of lost packets detected at <now_us> handling
1863 * their TX frames.
1864 * Send a packet loss event to the congestion controller if
1865 * in flight packet have been lost.
1866 * Also frees the packet in <pkts> list.
1867 * Never fails.
1868 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001869static inline void qc_release_lost_pkts(struct quic_conn *qc,
1870 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871 struct list *pkts,
1872 uint64_t now_us)
1873{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001875 uint64_t lost_bytes;
1876
1877 lost_bytes = 0;
1878 oldest_lost = newest_lost = NULL;
1879 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001880 struct list tmp = LIST_HEAD_INIT(tmp);
1881
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001882 lost_bytes += pkt->in_flight_len;
1883 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001884 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001885 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001887 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001888 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001889 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001890 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001891 if (!oldest_lost) {
1892 oldest_lost = newest_lost = pkt;
1893 }
1894 else {
1895 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001896 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001897 newest_lost = pkt;
1898 }
1899 }
1900
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001901 if (newest_lost) {
1902 /* Sent a congestion event to the controller */
1903 struct quic_cc_event ev = {
1904 .type = QUIC_CC_EVT_LOSS,
1905 .loss.time_sent = newest_lost->time_sent,
1906 };
1907
1908 quic_cc_event(&qc->path->cc, &ev);
1909 }
1910
1911 /* If an RTT have been already sampled, <rtt_min> has been set.
1912 * We must check if we are experiencing a persistent congestion.
1913 * If this is the case, the congestion controller must re-enter
1914 * slow start state.
1915 */
1916 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1917 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1918
1919 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1920 now_ms, qc->max_ack_delay))
1921 qc->path->cc.algo->slow_start(&qc->path->cc);
1922 }
1923
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001924 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001925 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001927 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001928 }
1929}
1930
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001931/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1932 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001933 * 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 +01001934 * acked ack-eliciting packet.
1935 * Return 1, if succeeded, 0 if not.
1936 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001937static inline int qc_parse_ack_frm(struct quic_conn *qc,
1938 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001939 struct quic_enc_level *qel,
1940 unsigned int *rtt_sample,
1941 const unsigned char **pos, const unsigned char *end)
1942{
1943 struct quic_ack *ack = &frm->ack;
1944 uint64_t smallest, largest;
1945 struct eb_root *pkts;
1946 struct eb64_node *largest_node;
1947 unsigned int time_sent, pkt_flags;
1948 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1949 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1950
1951 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1952 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001953 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001954 goto err;
1955 }
1956
1957 if (ack->first_ack_range > ack->largest_ack) {
1958 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001959 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001960 goto err;
1961 }
1962
1963 largest = ack->largest_ack;
1964 smallest = largest - ack->first_ack_range;
1965 pkts = &qel->pktns->tx.pkts;
1966 pkt_flags = 0;
1967 largest_node = NULL;
1968 time_sent = 0;
1969
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001970 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001971 largest_node = eb64_lookup(pkts, largest);
1972 if (!largest_node) {
1973 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001974 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001975 }
1976 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001977 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001978 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001979 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001980 }
1981
1982 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001983 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001984 do {
1985 uint64_t gap, ack_range;
1986
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001987 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1988 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001989 if (!ack->ack_range_num--)
1990 break;
1991
1992 if (!quic_dec_int(&gap, pos, end))
1993 goto err;
1994
1995 if (smallest < gap + 2) {
1996 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001997 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001998 goto err;
1999 }
2000
2001 largest = smallest - gap - 2;
2002 if (!quic_dec_int(&ack_range, pos, end))
2003 goto err;
2004
2005 if (largest < ack_range) {
2006 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002007 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002008 goto err;
2009 }
2010
2011 /* Do not use this node anymore. */
2012 largest_node = NULL;
2013 /* Next range */
2014 smallest = largest - ack_range;
2015
2016 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002017 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018 } while (1);
2019
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2021 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002022 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002023 }
2024
2025 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2026 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002027 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002028 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002029 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002030 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002031 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2032 if (quic_peer_validated_addr(qc))
2033 qc->path->loss.pto_count = 0;
2034 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002035 }
2036
2037
2038 return 1;
2039
2040 err:
2041 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002042 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002043 return 0;
2044}
2045
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002046/* This function gives the detail of the SSL error. It is used only
2047 * if the debug mode and the verbose mode are activated. It dump all
2048 * the SSL error until the stack was empty.
2049 */
2050static forceinline void qc_ssl_dump_errors(struct connection *conn)
2051{
2052 if (unlikely(global.mode & MODE_DEBUG)) {
2053 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002054 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002055 unsigned long ret;
2056
Willy Tarreau325fc632022-04-11 18:47:38 +02002057 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002058 ret = ERR_get_error();
2059 if (!ret)
2060 return;
2061
2062 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002063 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002064 }
2065 }
2066}
2067
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002068int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2069 const char **str, int *len);
2070
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002071/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2072 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002073 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002074 * Return 1 if succeeded, 0 if not.
2075 */
2076static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002077 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002078 const unsigned char *data, size_t len,
2079 struct quic_rx_packet *pkt,
2080 struct quic_rx_crypto_frm *cf)
2081{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002082 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002083 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002084
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002085 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002086 qc = ctx->qc;
2087
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002088 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2089
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002090 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2091 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002092 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 goto err;
2094 }
2095
2096 el->rx.crypto.offset += len;
2097 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002098 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002099
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002100 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002101 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002102 ssl_err = SSL_do_handshake(ctx->ssl);
2103 if (ssl_err != 1) {
2104 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2105 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2106 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002107 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002108 goto out;
2109 }
2110
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002111 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002112 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002113 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002114 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002115 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002116 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002117 goto err;
2118 }
2119
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002120 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002121
2122 /* Check the alpn could be negotiated */
2123 if (!qc->app_ops) {
2124 TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2125 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2126 goto err;
2127 }
2128
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002129 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002130 /* I/O callback switch */
2131 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002132 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002133 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002134 /* The connection is ready to be accepted. */
2135 quic_accept_push_qc(qc);
2136 }
2137 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002138 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002139 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002140 } else {
2141 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2142 if (ssl_err != 1) {
2143 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2144 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2145 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002146 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002147 goto out;
2148 }
2149
2150 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002151 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002152 goto err;
2153 }
2154
2155 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002156 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002157 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002158
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002159 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002160 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002161 return 1;
2162
2163 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002164 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002165 return 0;
2166}
2167
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002168/* Parse a STREAM frame <strm_frm>
2169 *
2170 * Return 1 on success. On error, 0 is returned. In this case, the packet
2171 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002172 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002173static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2174 struct quic_stream *strm_frm,
2175 struct quic_conn *qc)
2176{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002177 int ret;
2178
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002179 /* RFC9000 13.1. Packet Processing
2180 *
2181 * A packet MUST NOT be acknowledged until packet protection has been
2182 * successfully removed and all frames contained in the packet have
2183 * been processed. For STREAM frames, this means the data has been
2184 * enqueued in preparation to be received by the application protocol,
2185 * but it does not require that data be delivered and consumed.
2186 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002187 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2188 strm_frm->offset.key, strm_frm->fin,
2189 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002190
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002191 /* frame rejected - packet must not be acknowledeged */
2192 if (ret)
2193 return 0;
2194
2195 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002196}
2197
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002198/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2199 * for <qc> QUIC connection.
2200 * This is a best effort function which never fails even if no memory could be
2201 * allocated to duplicate these frames.
2202 */
2203static void qc_dup_pkt_frms(struct quic_conn *qc,
2204 struct list *pkt_frm_list, struct list *out_frm_list)
2205{
2206 struct quic_frame *frm, *frmbak;
2207 struct list tmp = LIST_HEAD_INIT(tmp);
2208
2209 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2210 struct quic_frame *dup_frm, *origin;
2211
2212 switch (frm->type) {
2213 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2214 {
2215 struct quic_stream *strm_frm = &frm->stream;
2216 struct eb64_node *node = NULL;
2217 struct qc_stream_desc *stream_desc;
2218
2219 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2220 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02002221 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002222 continue;
2223 }
2224
2225 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2226 /* Do not resend this frame if in the "already acked range" */
2227 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2228 TRACE_PROTO("ignored frame frame in already acked range",
2229 QUIC_EV_CONN_PRSAFRM, qc, frm);
2230 continue;
2231 }
2232 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2233 strm_frm->offset.key = stream_desc->ack_offset;
2234 TRACE_PROTO("updated partially acked frame",
2235 QUIC_EV_CONN_PRSAFRM, qc, frm);
2236 }
2237
2238 break;
2239 }
2240
2241 default:
2242 break;
2243 }
2244
2245 dup_frm = pool_zalloc(pool_head_quic_frame);
2246 if (!dup_frm) {
2247 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2248 break;
2249 }
2250
2251 /* If <frm> is already a copy of another frame, we must take
2252 * its original frame as source for the copy.
2253 */
2254 origin = frm->origin ? frm->origin : frm;
2255 TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
2256 *dup_frm = *origin;
2257 LIST_INIT(&dup_frm->reflist);
2258 TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM,
2259 qc, NULL, &origin->pkt->pn_node.key);
2260 dup_frm->origin = origin;
2261 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2262 LIST_APPEND(&tmp, &dup_frm->list);
2263 }
2264
2265 LIST_SPLICE(out_frm_list, &tmp);
2266}
2267
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002268/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002269static void qc_prep_fast_retrans(struct quic_conn *qc,
2270 struct quic_enc_level *qel,
2271 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002272{
2273 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002274 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002275 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002276 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002277
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002278 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002279 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002280 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002281 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002282 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2283 node = eb64_next(node);
2284 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002285 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002286 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002287 }
2288
2289 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002290 return;
2291
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002292 /* When building a packet from another one, the field which may increase the
2293 * packet size is the packet number. And the maximum increase is 4 bytes.
2294 */
2295 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2296 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2297 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2298 return;
2299 }
2300
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002301 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2302 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2303 if (frms == frms1 && frms2) {
2304 frms = frms2;
2305 goto start;
2306 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002307}
2308
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002309/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002310 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002311 * Initial packets send them coalescing with others (Handshake here).
2312 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002313 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002314static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2315 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002316{
2317 struct list itmp = LIST_HEAD_INIT(itmp);
2318 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002319
2320 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2321 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2322 struct quic_enc_level *qel = iqel;
2323 struct eb_root *pkts;
2324 struct eb64_node *node;
2325 struct quic_tx_packet *pkt;
2326 struct list *tmp = &itmp;
2327
2328 start:
2329 pkt = NULL;
2330 pkts = &qel->pktns->tx.pkts;
2331 node = eb64_first(pkts);
2332 /* Skip the empty packet (they have already been retransmitted) */
2333 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002334 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002335 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002336 break;
2337 node = eb64_next(node);
2338 }
2339
2340 if (!pkt)
2341 goto end;
2342
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002343 /* When building a packet from another one, the field which may increase the
2344 * packet size is the packet number. And the maximum increase is 4 bytes.
2345 */
2346 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2347 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2348 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2349 goto end;
2350 }
2351
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002352 qel->pktns->tx.pto_probe += 1;
2353 requeue:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002354 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2355 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002356 if (qel == iqel) {
2357 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2358 pkt = pkt->next;
2359 tmp = &htmp;
2360 hqel->pktns->tx.pto_probe += 1;
2361 goto requeue;
2362 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002363 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002364
2365 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002366 LIST_SPLICE(ifrms, &itmp);
2367 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002368}
2369
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002370static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002371{
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002372 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2373 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2374 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2375 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2376 return;
2377
2378 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2379 }
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002380}
2381
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002382/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2383 * frame list to send.
2384 * Return 1 if succeeded, 0 if not.
2385 */
2386static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2387{
2388 struct quic_frame *frm;
2389 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2390 uint64_t app_error_code;
2391
2392 /* TODO: the mux may be released, we cannot have more
2393 * information about the application error code to send
2394 * at this time.
2395 */
2396 app_error_code = H3_REQUEST_REJECTED;
2397
2398 frm = pool_zalloc(pool_head_quic_frame);
2399 if (!frm)
2400 return 0;
2401
2402 frm->type = QUIC_FT_STOP_SENDING;
2403 frm->stop_sending.id = id;
2404 frm->stop_sending.app_error_code = app_error_code;
2405 LIST_INIT(&frm->reflist);
2406 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
2407
2408 return 1;
2409}
2410
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002411/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2412 * as I/O handler context and <qel> as encryption level.
2413 * Returns 1 if succeeded, 0 if failed.
2414 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002415static 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 +01002416 struct quic_enc_level *qel)
2417{
2418 struct quic_frame frm;
2419 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002420 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002421 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002422
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002423 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002424 /* Skip the AAD */
2425 pos = pkt->data + pkt->aad_len;
2426 end = pkt->data + pkt->len;
2427
2428 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002429 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002430 goto err;
2431
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002432 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002433 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002434 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002435 break;
2436 case QUIC_FT_PING:
2437 break;
2438 case QUIC_FT_ACK:
2439 {
2440 unsigned int rtt_sample;
2441
2442 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002443 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002444 goto err;
2445
2446 if (rtt_sample) {
2447 unsigned int ack_delay;
2448
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002449 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002450 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002451 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2452 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002453 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002454 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002455 break;
2456 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002457 case QUIC_FT_RESET_STREAM:
2458 /* TODO: handle this frame at STREAM level */
2459 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002460 case QUIC_FT_STOP_SENDING:
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002461 /* TODO: handle this frame at STREAM level */
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002462 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002464 {
2465 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002466
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002467 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002468 /* XXX TO DO: <cfdebug> is used only for the traces. */
2469 struct quic_rx_crypto_frm cfdebug = { };
2470
2471 cfdebug.offset_node.key = frm.crypto.offset;
2472 cfdebug.len = frm.crypto.len;
2473 TRACE_PROTO("CRYPTO data discarded",
2474 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2475 break;
2476 }
2477
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002478 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2479 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2480 /* XXX TO DO: <cfdebug> is used only for the traces. */
2481 struct quic_rx_crypto_frm cfdebug = { };
2482
2483 cfdebug.offset_node.key = frm.crypto.offset;
2484 cfdebug.len = frm.crypto.len;
2485 /* Nothing to do */
2486 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002487 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002488 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002489 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2490 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002491 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002493 else {
2494 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2495 /* XXX TO DO: <cfdebug> is used only for the traces. */
2496 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002497
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002498 cfdebug.offset_node.key = frm.crypto.offset;
2499 cfdebug.len = frm.crypto.len;
2500 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002501 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002502 frm.crypto.len -= diff;
2503 frm.crypto.data += diff;
2504 frm.crypto.offset = qel->rx.crypto.offset;
2505 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002506 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002507
2508 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002509 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002510 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002511
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002512 cfdebug.offset_node.key = frm.crypto.offset;
2513 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 if (!qc_provide_cdata(qel, ctx,
2515 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002516 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002517 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002518
2519 break;
2520 }
2521
2522 /* frm.crypto.offset > qel->rx.crypto.offset */
2523 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2524 if (!cf) {
2525 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002526 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002527 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002528 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002529
2530 cf->offset_node.key = frm.crypto.offset;
2531 cf->len = frm.crypto.len;
2532 cf->data = frm.crypto.data;
2533 cf->pkt = pkt;
2534 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2535 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002536 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002537 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002538 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002539 {
2540 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002541 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002542
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002543 /* The upper layer may not be allocated. */
2544 if (qc->mux_state != QC_MUX_READY) {
2545 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
2546 TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
2547 break;
2548 }
2549 else {
2550 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002551 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
2552 TRACE_PROTO("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2553
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002554 goto err;
2555 }
2556 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002557
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002558 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002559 goto err;
2560
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002561 break;
2562 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002563 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002564 if (qc->mux_state == QC_MUX_READY) {
2565 struct quic_max_data *data = &frm.max_data;
2566 qcc_recv_max_data(qc->qcc, data->max_data);
2567 }
2568 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002569 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002570 if (qc->mux_state == QC_MUX_READY) {
2571 struct quic_max_stream_data *data = &frm.max_stream_data;
2572 qcc_recv_max_stream_data(qc->qcc, data->id,
2573 data->max_stream_data);
2574 }
2575 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002576 case QUIC_FT_MAX_STREAMS_BIDI:
2577 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002578 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002579 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002580 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2581 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002582 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002583 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2584 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002585 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002586 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2587 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002588 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002589 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002590 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002591 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002592 case QUIC_FT_RETIRE_CONNECTION_ID:
2593 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002594 break;
2595 case QUIC_FT_CONNECTION_CLOSE:
2596 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002597 /* Increment the error counters */
2598 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002599 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille07968dc2022-05-20 20:53:20 +02002600 /* If the connection did not reached the handshake complete state,
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002601 * the <half_open_conn> counter was not decremented. Note that if
Frédéric Lécaille07968dc2022-05-20 20:53:20 +02002602 * a TLS alert was received from the TLS stack, this counter
2603 * has already been decremented.
2604 */
2605 if (qc->state < QUIC_HS_ST_COMPLETE && !(qc->flags & QUIC_FL_CONN_TLS_ALERT))
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002606 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002607 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2608 /* RFC 9000 10.2. Immediate Close:
2609 * The closing and draining connection states exist to ensure
2610 * that connections close cleanly and that delayed or reordered
2611 * packets are properly discarded. These states SHOULD persist
2612 * for at least three times the current PTO interval...
2613 *
2614 * Rearm the idle timeout only one time when entering draining
2615 * state.
2616 */
2617 qc_idle_timer_do_rearm(qc);
2618 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002619 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002620 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002621 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002622 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002623 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002624 goto err;
2625
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002626 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002627 break;
2628 default:
2629 goto err;
2630 }
2631 }
2632
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002633 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002634 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002635
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002636 if (fast_retrans) {
2637 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2638 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2639
2640 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2641 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002642
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002643 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2644 * has successfully parse a Handshake packet. The Initial encryption must also
2645 * be discarded.
2646 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002647 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002648 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002649 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2650 QUIC_FL_TLS_SECRETS_DCD)) {
2651 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2652 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2653 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2654 qc_set_timer(ctx->qc);
2655 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2656 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2657 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002658 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2659 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002660 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002661 }
2662
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002663 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002664 return 1;
2665
2666 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002667 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 return 0;
2669}
2670
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002671/* Must be called only by a <cbuf> writer (packet builder).
2672 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2673 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2674 * index after having marked the end of written data. This the responsability
2675 * of the caller to ensure there is enough room in <cbuf> to write the end of
2676 * data made of a uint16_t null field.
2677 *
2678 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2679 * ^ ^
2680 * r w
2681 *
2682 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2683 * ^ ^
2684 * r w
2685
2686 * +--------------------------------------+ (empty buffer, can be reused)
2687 * ^
2688 * (r = w)
2689 *
2690 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2691 * ^ ^
2692 * w r
2693 */
2694static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2695{
2696 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2697
2698 /* We can reset the writer index only if in front of the reader index and
2699 * if the reader index is not null. Resetting the writer when the reader
2700 * index is null would empty the buffer.
2701 * XXX Note than the writer index cannot reach the reader index.
2702 * Only the reader index can reach the writer index.
2703 */
2704 if (rd && rd <= cbuf->wr) {
2705 /* Mark the end of contiguous data for the reader */
2706 write_u16(cb_wr(cbuf), 0);
2707 cb_add(cbuf, sizeof(uint16_t));
2708 cb_wr_reset(cbuf);
2709 return 1;
2710 }
2711
2712 return 0;
2713}
2714
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002715/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002716 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002717 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2718 * This function must be called only after having built a correct datagram.
2719 * Always succeeds.
2720 */
2721static inline void qc_set_dg(struct cbuf *cbuf,
2722 uint16_t dglen, struct quic_tx_packet *pkt)
2723{
2724 write_u16(cb_wr(cbuf), dglen);
2725 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2726 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2727}
2728
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002729/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2730 * with <frms> as ack-eliciting frame list to send, 0 if not.
2731 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2732 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2733 */
2734static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2735 struct quic_enc_level *qel, int cc, int probe)
2736{
2737 unsigned int must_ack =
2738 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2739
2740 /* Do not build any more packet if the TX secrets are not available or
2741 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2742 * and if there is no more packets to send upon PTO expiration
2743 * and if there is no more ack-eliciting frames to send or in flight
2744 * congestion control limit is reached for prepared data
2745 */
2746 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2747 (!cc && !probe && !must_ack &&
2748 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2749 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2750 return 0;
2751 }
2752
2753 return 1;
2754}
2755
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002756/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002757 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2758 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002759 * A header made of two fields is added to each datagram: the datagram length followed
2760 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002761 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2762 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002763 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002764static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2765 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002766{
2767 struct quic_enc_level *qel;
2768 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002769 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002770 struct quic_tx_packet *pkt;
2771 size_t total;
2772 size_t dg_headlen;
2773
2774 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2775 /* Each datagram is prepended with its length followed by the
2776 * address of the first packet in the datagram.
2777 */
2778 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2779 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2780 total = 0;
2781 start:
2782 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002783 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002784 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2785 * to ensure there is enough room to mark the end of prepared
2786 * contiguous data with a zero length.
2787 */
2788 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2789 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002790 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002791
2792 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002793 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002794 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002795 /* We do not probe if an immediate close was asked */
2796 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002797 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002798
2799 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002800 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002801
2802 /* Leave room for the datagram header */
2803 pos += dg_headlen;
2804 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2805 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2806 }
2807 else {
2808 end = pos + qc->path->mtu;
2809 }
2810
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002811 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002812 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002813 switch (err) {
2814 case -2:
2815 goto err;
2816 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002817 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2818 * MTU, we are here because of the congestion control window. There is
2819 * no need to try to reuse this buffer.
2820 */
2821 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002822 default:
2823 break;
2824 }
2825
2826 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2827 if (!pkt)
2828 goto err;
2829
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002830 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2831 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2832
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002833 total += pkt->len;
2834 /* Set the current datagram as prepared into <cbuf>. */
2835 qc_set_dg(cbuf, pkt->len, pkt);
2836 }
2837
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002838 /* Reset <wr> writer index if in front of <rd> index */
2839 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002840 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002841 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002842 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002843 }
2844
2845 out:
2846 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2847 return total;
2848
2849 err:
2850 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2851 return -1;
2852}
2853
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002854/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002855 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2856 * several packets in the same datagram. A header made of two fields is added
2857 * to each datagram: the datagram length followed by the address of the first
2858 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002859 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2860 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002862static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002863 enum quic_tls_enc_level tel, struct list *tel_frms,
2864 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002867 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002868 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002869 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2870 /* length of datagrams */
2871 uint16_t dglen;
2872 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002873 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002874 /* Each datagram is prepended with its length followed by the
2875 * address of the first packet in the datagram.
2876 */
2877 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002878 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002879
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002880 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2881
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002882 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002883 qel = &qc->els[tel];
2884 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002885 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002886 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002887 padding = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002888 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002889 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002890 /* Leave at least <dglen> bytes at the end of this buffer
2891 * to ensure there is enough room to mark the end of prepared
2892 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002893 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002894 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2895 first_pkt = prv_pkt = NULL;
2896 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002897 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002898 enum quic_pkt_type pkt_type;
2899
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002900 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002901 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002902 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002903 /* We do not probe if an immediate close was asked */
2904 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002905 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002906
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002907 if (!qc_may_build_pkt(qc, frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002908 if (prv_pkt)
2909 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002910 /* Let's select the next encryption level */
2911 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2912 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002913 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002914 qel = &qc->els[tel];
2915 /* Build a new datagram */
2916 prv_pkt = NULL;
2917 continue;
2918 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002919 break;
2920 }
2921
2922 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002923 if (!prv_pkt) {
2924 /* Leave room for the datagram header */
2925 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002926 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002927 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2928 }
2929 else {
2930 end = pos + qc->path->mtu;
2931 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002932 }
2933
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002934 cur_pkt = qc_build_pkt(&pos, end, qel, frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002935 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002936 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002937 case -2:
2938 goto err;
2939 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002940 /* If there was already a correct packet present, set the
2941 * current datagram as prepared into <cbuf>.
2942 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002943 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002944 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002945 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002946 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002947 break;
2948 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002949
Frédéric Lécaille63556772021-12-29 17:18:21 +01002950 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2951 if (!cur_pkt)
2952 goto err;
2953
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002954 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2955 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2956
Frédéric Lécaille63556772021-12-29 17:18:21 +01002957 total += cur_pkt->len;
2958 /* keep trace of the first packet in the datagram */
2959 if (!first_pkt)
2960 first_pkt = cur_pkt;
2961 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002962 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002963 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002964 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
2965 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01002966 /* Let's say we have to build a new dgram */
2967 prv_pkt = NULL;
2968 dglen += cur_pkt->len;
2969 /* Client: discard the Initial encryption keys as soon as
2970 * a handshake packet could be built.
2971 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002972 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01002973 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2974 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2975 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2976 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2977 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002978 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002979 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002980 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002981 }
2982 /* If the data for the current encryption level have all been sent,
2983 * select the next level.
2984 */
2985 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02002986 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002987 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2988 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2989 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2990 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002991 if (tel == QUIC_TLS_ENC_LEVEL_APP)
2992 frms = &qc->els[tel].pktns->tx.frms;
2993 else
2994 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002995 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002996 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002997 /* If there is data for the next level, do not
2998 * consume a datagram.
2999 */
3000 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003002 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003003 /* If we have to build a new datagram, set the current datagram as
3004 * prepared into <cbuf>.
3005 */
3006 if (!prv_pkt) {
3007 qc_set_dg(cbuf, dglen, first_pkt);
3008 first_pkt = NULL;
3009 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003010 padding = 0;
3011 }
3012 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003013 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003014 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3015 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003016 }
3017 }
3018
3019 stop_build:
3020 /* Reset <wr> writer index if in front of <rd> index */
3021 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003022 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003023 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003024 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025 }
3026
3027 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003028 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003029 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030
3031 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003032 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003033 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003034}
3035
3036/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003037 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003038 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003039int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040{
3041 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003042 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003044 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003045 cbuf = qr->cbuf;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003046 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003047 while (cb_contig_data(cbuf)) {
3048 unsigned char *pos;
3049 struct buffer tmpbuf = { };
3050 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3051 uint16_t dglen;
3052 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003053 unsigned int time_sent;
3054
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003055 pos = cb_rd(cbuf);
3056 dglen = read_u16(pos);
3057 /* End of prepared datagrams.
3058 * Reset the reader index only if in front of the writer index.
3059 */
3060 if (!dglen) {
3061 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3062
3063 if (wr && wr < cbuf->rd) {
3064 cb_rd_reset(cbuf);
3065 continue;
3066 }
3067 break;
3068 }
3069
3070 pos += sizeof dglen;
3071 first_pkt = read_ptr(pos);
3072 pos += sizeof first_pkt;
3073 tmpbuf.area = (char *)pos;
3074 tmpbuf.size = tmpbuf.data = dglen;
3075
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003076 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003077 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003078 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003079
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003080 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003081 qc->tx.bytes += tmpbuf.data;
3082 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003083
3084 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3085 pkt->time_sent = time_sent;
3086 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3087 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003088 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003089 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3090 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003092 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3093 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3094 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003095 qc_notify_close(qc);
3096
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003097 /* RFC 9000 10.2. Immediate Close:
3098 * The closing and draining connection states exist to ensure
3099 * that connections close cleanly and that delayed or reordered
3100 * packets are properly discarded. These states SHOULD persist
3101 * for at least three times the current PTO interval...
3102 *
3103 * Rearm the idle timeout only one time when entering closing
3104 * state.
3105 */
3106 qc_idle_timer_do_rearm(qc);
3107 if (qc->timer_task) {
3108 task_destroy(qc->timer_task);
3109 qc->timer_task = NULL;
3110 }
3111 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003112 qc->path->in_flight += pkt->in_flight_len;
3113 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3114 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003115 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003116 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003117 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003118 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003119 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003120 }
3121 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003122
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003123 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3124
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003125 return 1;
3126}
3127
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003128/* Copy into <buf> buffer a stateless reset token depending on the
3129 * <salt> salt input. This is the cluster secret which will be derived
3130 * as HKDF input secret to generate this token.
3131 * Return 1 if succeeded, 0 if not.
3132 */
3133static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len,
3134 const unsigned char *salt, size_t saltlen)
3135{
3136 /* Input secret */
3137 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3138 size_t keylen = strlen(global.cluster_secret);
3139 /* Info */
3140 const unsigned char label[] = "stateless token";
3141 size_t labellen = sizeof label - 1;
3142
3143 return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3144 key, keylen, salt, saltlen, label, labellen);
3145}
3146
3147/* Initialize the stateless reset token attached to <cid> connection ID.
3148 * Returns 1 if succeeded, 0 if not.
3149 */
3150static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid)
3151{
3152 if (global.cluster_secret) {
3153 /* Output secret */
3154 unsigned char *token = quic_cid->stateless_reset_token;
3155 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3156 /* Salt */
3157 const unsigned char *cid = quic_cid->cid.data;
3158 size_t cidlen = quic_cid->cid.len;
3159
3160 return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
3161 }
3162 else {
3163 return RAND_bytes(quic_cid->stateless_reset_token,
3164 sizeof quic_cid->stateless_reset_token) == 1;
3165 }
3166}
3167
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003168/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3169 * ebtree.
3170 *
3171 * The CID is randomly generated in part with the result altered to be
3172 * associated with the current thread ID. This means this function must only
3173 * be called by the quic_conn thread.
3174 *
3175 * Returns the new CID if succeeded, NULL if not.
3176 */
3177static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3178 struct quic_conn *qc,
3179 int seq_num)
3180{
3181 struct quic_connection_id *cid;
3182
3183 cid = pool_alloc(pool_head_quic_connection_id);
3184 if (!cid)
3185 return NULL;
3186
3187 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003188 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1)
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003189 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003190
3191 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003192 if (quic_stateless_reset_token_init(cid) != 1)
3193 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003194
3195 cid->qc = qc;
3196
3197 cid->seq_num.key = seq_num;
3198 cid->retire_prior_to = 0;
3199 /* insert the allocated CID in the quic_conn tree */
3200 eb64_insert(root, &cid->seq_num);
3201
3202 return cid;
3203
3204 err:
3205 pool_free(pool_head_quic_connection_id, cid);
3206 return NULL;
3207}
3208
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003209/* Build all the frames which must be sent just after the handshake have succeeded.
3210 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3211 * a HANDSHAKE_DONE frame.
3212 * Return 1 if succeeded, 0 if not.
3213 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003214static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003216 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003217 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003218 struct quic_frame *frm, *frmbak;
3219 struct list frm_list = LIST_HEAD_INIT(frm_list);
3220 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003221
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003222 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003223 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003224 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003225 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003226 if (!frm)
3227 return 0;
3228
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003229 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003230 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003231 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003232 }
3233
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003234 first = 1;
3235 max = qc->tx.params.active_connection_id_limit;
3236 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003237 struct quic_connection_id *cid;
3238
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003239 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003240 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241 goto err;
3242
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003243 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003244 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003245 if (!cid)
3246 goto err;
3247
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003248 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003249 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003250
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003251 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003252 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003253 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003254
3255 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003256 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003257
3258 return 1;
3259
3260 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003261 /* free the frames */
3262 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3263 pool_free(pool_head_quic_frame, frm);
3264
3265 node = eb64_first(&qc->cids);
3266 while (node) {
3267 struct quic_connection_id *cid;
3268
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003269
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003270 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003271 if (cid->seq_num.key >= max)
3272 break;
3273
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003274 if (cid->seq_num.key < first)
3275 continue;
3276
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003277 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003278 ebmb_delete(&cid->node);
3279 eb64_delete(&cid->seq_num);
3280 pool_free(pool_head_quic_connection_id, cid);
3281 }
3282
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003283 return 0;
3284}
3285
3286/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003287void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003288{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003289 struct eb64_node *n;
3290 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003291
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003292 n = eb64_first(&arngs->root);
3293 while (n) {
3294 struct eb64_node *next;
3295
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003296 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003297 next = eb64_next(n);
3298 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003299 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003300 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003301 }
3302}
3303
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003304/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3305 * descending order.
3306 */
3307static inline size_t sack_gap(struct quic_arng_node *p,
3308 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003309{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003310 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003311}
3312
3313
3314/* Remove the last elements of <ack_ranges> list of ack range updating its
3315 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003316 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003317 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003318static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003319{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003320 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003322 last = eb64_last(&arngs->root);
3323 while (last && arngs->enc_sz > limit) {
3324 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003325
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003326 prev = eb64_prev(last);
3327 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003328 return 0;
3329
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003330 last_node = eb64_entry(last, struct quic_arng_node, first);
3331 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003332 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3333 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3334 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3335 --arngs->sz;
3336 eb64_delete(last);
3337 pool_free(pool_head_quic_arng, last);
3338 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003339 }
3340
3341 return 1;
3342}
3343
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003344/* Set the encoded size of <arngs> QUIC ack ranges. */
3345static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3346{
3347 struct eb64_node *node, *next;
3348 struct quic_arng_node *ar, *ar_next;
3349
3350 node = eb64_last(&arngs->root);
3351 if (!node)
3352 return;
3353
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003354 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003355 arngs->enc_sz = quic_int_getsize(ar->last) +
3356 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3357
3358 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003359 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003360 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3361 quic_int_getsize(ar_next->last - ar_next->first.key);
3362 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003363 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003364 }
3365}
3366
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003367/* Insert <ar> ack range into <argns> tree of ack ranges.
3368 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003369 */
3370static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003371struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3372 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003373{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003374 struct quic_arng_node *new_ar;
3375
3376 new_ar = pool_alloc(pool_head_quic_arng);
3377 if (new_ar) {
3378 new_ar->first.key = ar->first;
3379 new_ar->last = ar->last;
3380 eb64_insert(&arngs->root, &new_ar->first);
3381 arngs->sz++;
3382 }
3383
3384 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003385}
3386
3387/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003388 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003389 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003390 *
3391 * Descending order
3392 * ------------->
3393 * range1 range2
3394 * ..........|--------|..............|--------|
3395 * ^ ^ ^ ^
3396 * | | | |
3397 * last1 first1 last2 first2
3398 * ..........+--------+--------------+--------+......
3399 * diff1 gap12 diff2
3400 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003401 * To encode the previous list of ranges we must encode integers as follows in
3402 * descending order:
3403 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003404 * with diff1 = last1 - first1
3405 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003406 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003408 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003409int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3410 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003411{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003412 struct eb64_node *le;
3413 struct quic_arng_node *new_node;
3414 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003415
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003416 new = NULL;
3417 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003418 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003419 if (!new_node)
3420 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003422 goto out;
3423 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003424
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003425 le = eb64_lookup_le(&arngs->root, ar->first);
3426 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003427 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003428 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003429 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003430
3431 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003432 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003433 else {
3434 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003435 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003437 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003438 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003439 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003440
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003441 if (le_ar->last + 1 >= ar->first) {
3442 le_ar->last = ar->last;
3443 new = le;
3444 new_node = le_ar;
3445 }
3446 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003447 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003448 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003450
3451 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003452 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003453 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003454
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003455 /* Verify that the new inserted node does not overlap the nodes
3456 * which follow it.
3457 */
3458 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003459 struct eb64_node *next;
3460 struct quic_arng_node *next_node;
3461
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003462 while ((next = eb64_next(new))) {
3463 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003464 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003465 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003466 break;
3467
3468 if (next_node->last > new_node->last)
3469 new_node->last = next_node->last;
3470 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003471 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003472 /* Decrement the size of these ranges. */
3473 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003474 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003475 }
3476
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003477 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003478 quic_arngs_set_enc_sz(arngs);
3479
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003480 return 1;
3481}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003482/* Remove the header protection of packets at <el> encryption level.
3483 * Always succeeds.
3484 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003485static 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 +01003486{
3487 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003488 struct quic_rx_packet *pqpkt;
3489 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490 struct quic_enc_level *app_qel;
3491
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003492 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3493 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003495 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003496 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003497 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003498 goto out;
3499 }
3500 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003501 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003502 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003503 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003504 pqpkt->data, pqpkt->data + pqpkt->len)) {
3505 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003506 /* XXX TO DO XXX */
3507 }
3508 else {
3509 /* The AAD includes the packet number field */
3510 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3511 /* Store the packet into the tree of packets to decrypt. */
3512 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003513 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003514 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3515 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003516 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003517 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003518 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003519 MT_LIST_DELETE_SAFE(pkttmp1);
3520 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 }
3522
3523 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003524 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003525}
3526
3527/* Process all the CRYPTO frame at <el> encryption level.
3528 * Return 1 if succeeded, 0 if not.
3529 */
3530static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003531 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003532{
3533 struct eb64_node *node;
3534
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003535 node = eb64_first(&el->rx.crypto.frms);
3536 while (node) {
3537 struct quic_rx_crypto_frm *cf;
3538
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003539 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003540 if (cf->offset_node.key != el->rx.crypto.offset)
3541 break;
3542
3543 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3544 goto err;
3545
3546 node = eb64_next(node);
3547 quic_rx_packet_refdec(cf->pkt);
3548 eb64_delete(&cf->offset_node);
3549 pool_free(pool_head_quic_rx_crypto_frm, cf);
3550 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003551 return 1;
3552
3553 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003554 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003555 return 0;
3556}
3557
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003558/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003559 * 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 +02003560 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561 * Return 1 if succeeded, 0 if not.
3562 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003563int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3564 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003565{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003566 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003567 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003568 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003569 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003570 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003571
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003572 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003573 qel = cur_el;
3574 next_tel:
3575 if (!qel)
3576 goto out;
3577
3578 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3579 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003580 while (node) {
3581 struct quic_rx_packet *pkt;
3582
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003583 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003584 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003585 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003586 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003587 /* Drop the packet */
3588 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003589 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 }
3591 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003592 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593 /* Drop the packet */
3594 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003595 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003596 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597 }
3598 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003599 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3600
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003601 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3602 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3603 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003604 qc_idle_timer_rearm(qc, 1);
3605 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003606 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003607 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003608 largest_pn_time_received = pkt->time_received;
3609 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003611 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003613 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003614 }
3615 }
3616 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003617 eb64_delete(&pkt->pn_node);
3618 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003619 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003620 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003621
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003622 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3623 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003624 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003625 /* Update the largest acknowledged packet timestamps */
3626 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3627 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3628 }
3629
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003630 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631 goto err;
3632
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003633 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003634 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003635 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003636 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003637 goto next_tel;
3638 }
3639
3640 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003641 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003642 return 1;
3643
3644 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003645 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003646 return 0;
3647}
3648
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003649/* Check if it's possible to remove header protection for packets related to
3650 * encryption level <qel>. If <qel> is NULL, assume it's false.
3651 *
3652 * Return true if the operation is possible else false.
3653 */
3654static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3655{
3656 enum quic_tls_enc_level tel;
3657
3658 if (!qel)
3659 return 0;
3660
3661 tel = ssl_to_quic_enc_level(qel->level);
3662
3663 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003664 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003665 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003666 return 0;
3667 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003668
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003669 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003670 return 0;
3671
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003672 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003673 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003674 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003675 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003676
3677 return 1;
3678}
3679
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003680/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003681int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003682{
3683 int ret;
3684 struct qring *qr;
3685
3686 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3687 if (!qr)
3688 /* Never happens */
3689 return 1;
3690
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003691 if (old_data)
3692 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003693 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003694 if (ret == -1)
3695 goto err;
3696 else if (ret == 0)
3697 goto out;
3698
3699 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3700 goto err;
3701
3702 out:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003703 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003704 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3705 return 1;
3706
3707 err:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003708 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003709 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3710 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3711 return 0;
3712}
3713
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003714/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3715 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
3716 * QUIC connection
3717 * Returns 1 if succeeded, 0 if not.
3718 */
3719int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
3720 enum quic_tls_enc_level tel, struct list *tel_frms,
3721 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3722{
3723 int ret;
3724 struct qring *qr;
3725
3726 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3727 if (!qr)
3728 /* Never happens */
3729 return 1;
3730
3731 if (old_data)
3732 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
3733 ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms);
3734 if (ret == -1)
3735 goto err;
3736 else if (ret == 0)
3737 goto out;
3738
3739 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3740 goto err;
3741
3742 out:
3743 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3744 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3745 return 1;
3746
3747 err:
3748 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3749 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3750 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3751 return 0;
3752}
3753
3754/* Retransmit up to two datagrams depending on packet number space */
3755static void qc_dgrams_retransmit(struct quic_conn *qc)
3756{
3757 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3758 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3759 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3760
3761 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3762 struct list ifrms = LIST_HEAD_INIT(ifrms);
3763 struct list hfrms = LIST_HEAD_INIT(hfrms);
3764
3765 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
3766 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
3767 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3768 if (!LIST_ISEMPTY(&ifrms)) {
3769 iqel->pktns->tx.pto_probe = 1;
3770 if (!LIST_ISEMPTY(&hfrms)) {
3771 hqel->pktns->tx.pto_probe = 1;
3772 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
3773 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
3774 }
3775 }
3776 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3777 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
3778 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3779 if (!LIST_ISEMPTY(&hfrms)) {
3780 hqel->pktns->tx.pto_probe = 1;
3781 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
3782 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3783 }
3784 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3785 }
3786 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3787 }
3788 else {
3789 int i;
3790 struct list frms1 = LIST_HEAD_INIT(frms1);
3791 struct list frms2 = LIST_HEAD_INIT(frms2);
3792
3793 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3794 hqel->pktns->tx.pto_probe = 0;
3795 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
3796 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
3797 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3798 if (!LIST_ISEMPTY(&frms1)) {
3799 hqel->pktns->tx.pto_probe = 1;
3800 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
3801 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3802 }
3803 }
3804 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3805 }
3806 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3807 aqel->pktns->tx.pto_probe = 0;
3808 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
3809 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3810 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
3811 if (!LIST_ISEMPTY(&frms1)) {
3812 aqel->pktns->tx.pto_probe = 1;
3813 qc_send_app_pkts(qc, 1, &frms1);
3814 }
3815 if (!LIST_ISEMPTY(&frms2)) {
3816 aqel->pktns->tx.pto_probe = 1;
3817 qc_send_app_pkts(qc, 1, &frms2);
3818 }
3819 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3820 }
3821 }
3822}
3823
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003824/* QUIC connection packet handler task (post handshake) */
3825static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3826{
3827 struct ssl_sock_ctx *ctx;
3828 struct quic_conn *qc;
3829 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003830
3831
3832 ctx = context;
3833 qc = ctx->qc;
3834 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003835
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003836 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003837
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003838 /* Retranmissions */
3839 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3840 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
3841 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3842 qc_dgrams_retransmit(qc);
3843 }
3844
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003845 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3846 qc_rm_hp_pkts(qc, qel);
3847
3848 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3849 goto err;
3850
Frédéric Lécaille47756802022-03-25 09:12:16 +01003851 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3852 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3853 goto out;
3854
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003855 if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003856 goto err;
3857
Frédéric Lécaille47756802022-03-25 09:12:16 +01003858out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003859 return t;
3860
3861 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003862 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003863 return t;
3864}
3865
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003866/* QUIC connection packet handler task. */
3867struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003868{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003869 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003870 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003871 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003872 enum quic_tls_enc_level tel, next_tel;
3873 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003874 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003875 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003876
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003877 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003878 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003879 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003880 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003881 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003882 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003883
3884 /* Retranmissions */
3885 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3886 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
3887 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3888 qc_dgrams_retransmit(qc);
3889 }
3890
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003891 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3892 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003893 /* The I/O handler has been woken up by the dgram listener
3894 * after the anti-amplification was reached.
3895 */
3896 qc_set_timer(qc);
3897 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3898 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3899 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003900 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003901 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3902 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3903 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003904 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003905 if (st >= QUIC_HS_ST_COMPLETE &&
3906 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3907 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3908 /* There may be remaining Handshake packets to treat and acknowledge. */
3909 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3910 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3911 }
3912 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003913 goto err;
3914
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003915 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003916 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003917
3918 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003919 /* Treat packets waiting for header packet protection decryption */
3920 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003921 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003922
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003923 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3924 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3925 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003926 goto err;
3927
Frédéric Lécaille47756802022-03-25 09:12:16 +01003928 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3929 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3930 goto out;
3931
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02003932 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
3933 !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
3934 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3935 qel = next_qel;
3936 next_qel = NULL;
3937 goto next_level;
3938 }
3939 else {
3940 struct quic_rx_packet *pkt;
3941 struct mt_list *elt1, elt2;
3942 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
3943
3944 /* Drop these 0-RTT packets */
3945 TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
3946 mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) {
3947 MT_LIST_DELETE_SAFE(elt1);
3948 quic_rx_packet_refdec(pkt);
3949 }
3950 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003951 }
3952
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003953 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003954 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003955 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003956 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003957 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003958
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003959 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003960 QUIC_FL_TLS_SECRETS_DCD)) {
3961 /* Discard the Handshake keys. */
3962 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3963 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3964 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3965 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003966 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003967 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003968 }
3969
3970 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3971 /* There may be remaining handshake to build (acks) */
3972 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3973 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003974 }
3975
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003976 if (!qr)
3977 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003978 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3979 * be considered.
3980 */
3981 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003982 goto err;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003983 ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms,
3984 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003985 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003987 else if (ret == 0)
3988 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003989
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003990 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003991 goto err;
3992
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003993 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003994 /* Check if there is something to do for the next level.
3995 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003996 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003997 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003998 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003999 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004000 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004001 goto next_level;
4002 }
4003
Frédéric Lécaille47756802022-03-25 09:12:16 +01004004 out:
4005 if (qr)
4006 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004007 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004008 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004009
4010 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004011 if (qr)
4012 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004013 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004014 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004015}
4016
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004017/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004018static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
4019{
4020 int i;
4021
4022 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4023 if (qel->tx.crypto.bufs[i]) {
4024 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4025 qel->tx.crypto.bufs[i] = NULL;
4026 }
4027 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004028 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004029}
4030
4031/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004032 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004033 * Returns 1 if succeeded, 0 if not.
4034 */
4035static int quic_conn_enc_level_init(struct quic_conn *qc,
4036 enum quic_tls_enc_level level)
4037{
4038 struct quic_enc_level *qel;
4039
4040 qel = &qc->els[level];
4041 qel->level = quic_to_ssl_enc_level(level);
4042 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4043 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4044 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004045 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004046
4047 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02004048 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004049 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004050 qel->rx.crypto.offset = 0;
4051 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004052
4053 /* Allocate only one buffer. */
4054 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4055 if (!qel->tx.crypto.bufs)
4056 goto err;
4057
4058 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4059 if (!qel->tx.crypto.bufs[0])
4060 goto err;
4061
4062 qel->tx.crypto.bufs[0]->sz = 0;
4063 qel->tx.crypto.nb_buf = 1;
4064
4065 qel->tx.crypto.sz = 0;
4066 qel->tx.crypto.offset = 0;
4067
4068 return 1;
4069
4070 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004071 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004072 return 0;
4073}
4074
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004075/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4076 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004077 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004078 * This function must only be called by the thread responsible of the quic_conn
4079 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004080 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004081static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082{
4083 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004084 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004085 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004086 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004087
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004088 /* We must not free the quic-conn if the MUX is still allocated. */
4089 BUG_ON(qc->mux_state == QC_MUX_READY);
4090
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004091 /* free remaining stream descriptors */
4092 node = eb64_first(&qc->streams_by_id);
4093 while (node) {
4094 struct qc_stream_desc *stream;
4095
4096 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4097 node = eb64_next(node);
4098
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004099 /* all streams attached to the quic-conn are released, so
4100 * qc_stream_desc_free will liberate the stream instance.
4101 */
4102 BUG_ON(!stream->release);
4103 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004104 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004105
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004106 if (qc->idle_timer_task) {
4107 task_destroy(qc->idle_timer_task);
4108 qc->idle_timer_task = NULL;
4109 }
4110
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004111 if (qc->timer_task) {
4112 task_destroy(qc->timer_task);
4113 qc->timer_task = NULL;
4114 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004115
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004116 /* remove the connection from receiver cids trees */
4117 ebmb_delete(&qc->odcid_node);
4118 ebmb_delete(&qc->scid_node);
4119 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004120
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004121 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004122 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004123 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004124 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004125 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004126 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004127
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004128 quic_tls_ku_free(qc);
4129 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4130 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004131 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004132 }
4133
4134 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4135 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4136 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004137
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004138 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
4139 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02004140 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004141 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004142
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004143 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4144 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004145 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004146}
4147
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004148static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004149{
4150 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004151 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004152
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004153 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004154
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004155 /* Next application data can be dropped. */
4156 qc->mux_state = QC_MUX_RELEASED;
4157
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004158 /* If the quic-conn timer has already expired free the quic-conn. */
4159 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4160 quic_conn_release(qc);
4161 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
4162 return;
4163 }
4164
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004165 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004166}
4167
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004169static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004170{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004171 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004172 struct quic_conn *qc;
4173 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004174
4175 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004176 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004177 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004178 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004179 task->expire = TICK_ETERNITY;
4180 pktns = quic_loss_pktns(qc);
4181 if (tick_isset(pktns->tx.loss_time)) {
4182 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4183
4184 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4185 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004186 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4187 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188 goto out;
4189 }
4190
4191 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004192 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004193 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4194 qc->qcc->subs->events & SUB_RETRY_SEND) {
4195 struct qcc *qcc = qc->qcc;
4196
4197 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4198 tasklet_wakeup(qcc->subs->tasklet);
4199 qcc->subs->events &= ~SUB_RETRY_SEND;
4200 if (!qcc->subs->events)
4201 qcc->subs = NULL;
4202 }
4203 else {
4204 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4205 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4206 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
4207 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
4208 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4209 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004210 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004211 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004212 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004213 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4214 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4215
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004216 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004217 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004218 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004219 iel->pktns->tx.pto_probe = 1;
4220 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004221
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004222 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4223 qc->path->loss.pto_count++;
4224
4225 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004226 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004227
4228 return task;
4229}
4230
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004231/* Parse the Retry token from buffer <token> with <end> a pointer to
4232 * one byte past the end of this buffer. This will extract the ODCID
4233 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004234 *
4235 * Returns 0 on success else non-zero.
4236 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004237static int parse_retry_token(const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004238 struct quic_cid *odcid)
4239{
4240 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004241 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004242
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004243 if (!quic_dec_int(&odcid_len, &token, end))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004244 return 1;
4245
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004246 /* RFC 9000 7.2. Negotiating Connection IDs:
4247 * When an Initial packet is sent by a client that has not previously
4248 * received an Initial or Retry packet from the server, the client
4249 * populates the Destination Connection ID field with an unpredictable
4250 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4251 */
4252 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN)
4253 return 1;
4254
4255 if (end - token < odcid_len + sizeof timestamp)
4256 return 1;
4257
4258 timestamp = ntohl(read_u32(token + odcid_len));
4259 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms)
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004260 return 1;
4261
4262 memcpy(odcid->data, token, odcid_len);
4263 odcid->len = odcid_len;
4264
4265 return 0;
4266}
4267
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004268/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4269 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4270 * for QUIC servers (or haproxy listeners).
4271 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4272 * <token> the token found to be used for this connection with <token_len> as
4273 * length. <saddr> is the source address.
4274 * Returns the connection if succeeded, NULL if not.
4275 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004276static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004277 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004278 const struct quic_cid *odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004279 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004280 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004281{
4282 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004283 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004284 /* Initial CID. */
4285 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004286 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004287 struct listener *l = NULL;
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004288 const unsigned char *salt = initial_salt_v1;
4289 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004290
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004291 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004292 qc = pool_zalloc(pool_head_quic_conn);
4293 if (!qc) {
4294 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
4295 goto err;
4296 }
4297
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004298 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4299 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004300 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004301 goto err;
4302 }
4303
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004304 qc->cids = EB_ROOT;
4305 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004306 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004307 struct proxy *prx;
4308
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004309 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004310 prx = l->bind_conf->frontend;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004311
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004312 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4313 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004314 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004315 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004316 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004317 qc->odcid.len = dcid->len;
4318 qc->odcid.addrlen = dcid->addrlen;
4319 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004320
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004321 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004322 if (scid->len)
4323 memcpy(qc->dcid.data, scid->data, scid->len);
4324 qc->dcid.len = scid->len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02004325 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004326 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004327 }
4328 /* QUIC Client (outgoing connection to servers) */
4329 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004330 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004331 if (dcid->len)
4332 memcpy(qc->dcid.data, dcid->data, dcid->len);
4333 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004334 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004335 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004336
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004337 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004338 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004339 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004340 goto err;
4341 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004342
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004343 /* insert the allocated CID in the receiver datagram handler tree */
4344 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004345 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004346
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004347 /* Select our SCID which is the first CID with 0 as sequence number. */
4348 qc->scid = icid->cid;
4349
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004350 /* Packet number spaces initialization. */
4351 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4352 quic_pktns_init(&qc->pktns[i]);
4353 /* QUIC encryption level context initialization. */
4354 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004355 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004356 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004357 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004358 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004359 /* Initialize the packet number space. */
4360 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4361 }
4362
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02004363 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004364 qc->tps_tls_ext = qc->version & 0xff000000 ?
4365 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4366 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004367 /* TX part. */
4368 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004369 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4370 qc->tx.wbuf = qc->tx.rbuf = 0;
4371 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004372 /* RX part. */
4373 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004374 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004375 for (i = 0; i < QCS_MAX_TYPES; i++)
4376 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004377
4378 qc->nb_pkt_for_cc = 1;
4379 qc->nb_pkt_since_cc = 0;
4380
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004381 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004382 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004383 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004384 goto err;
4385 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004386
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004387 /* XXX TO DO: Only one path at this time. */
4388 qc->path = &qc->paths[0];
4389 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4390
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004391 /* required to use MTLIST_IN_LIST */
4392 MT_LIST_INIT(&qc->accept_list);
4393
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004394 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004395 qc->stream_buf_count = 0;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02004396 qc->sendto_err = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004397 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4398
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004399 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4400 icid->stateless_reset_token,
4401 dcid->data, dcid->len,
4402 qc->scid.data, qc->scid.len,
4403 odcid->data, odcid->len, token))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004404 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004405
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004406 qc->enc_params_len =
4407 quic_transport_params_encode(qc->enc_params,
4408 qc->enc_params + sizeof qc->enc_params,
4409 &qc->rx.params, 1);
4410 if (!qc->enc_params_len)
4411 goto err;
4412
4413 if (qc_conn_alloc_ssl_ctx(qc) ||
4414 !quic_conn_init_timer(qc) ||
4415 !quic_conn_init_idle_timer_task(qc))
4416 goto err;
4417
4418 if (version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4419 salt = initial_salt_draft_29;
4420 salt_len = sizeof initial_salt_draft_29;
4421 }
4422
4423 if (!qc_new_isecs(qc, salt, salt_len, dcid->data, dcid->len, 1))
4424 goto err;
4425
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004426 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004427
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004428 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004429
4430 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004431 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004432 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4433 if (qc)
4434 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004435 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004436 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004437}
4438
4439/* Initialize the timer task of <qc> QUIC connection.
4440 * Returns 1 if succeeded, 0 if not.
4441 */
4442static int quic_conn_init_timer(struct quic_conn *qc)
4443{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004444 /* Attach this task to the same thread ID used for the connection */
4445 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004446 if (!qc->timer_task)
4447 return 0;
4448
4449 qc->timer = TICK_ETERNITY;
4450 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004451 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004452
4453 return 1;
4454}
4455
Frédéric Lécaille47756802022-03-25 09:12:16 +01004456/* Rearm the idle timer for <qc> QUIC connection. */
4457static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4458{
4459 unsigned int expire;
4460
4461 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4462 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4463}
4464
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004465/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4466 * which is set to 1 when receiving a packet , and 0 when sending packet
4467 */
4468static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4469{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004470 if (read) {
4471 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4472 }
4473 else {
4474 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4475 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004476 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004477}
4478
4479/* The task handling the idle timeout */
4480static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4481{
4482 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004483 struct quic_counters *prx_counters = qc->prx_counters;
4484 int qc_state = qc->state;
4485 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004486
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004487 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4488 * might free the quic-conn too early via quic_close().
4489 */
4490 qc_notify_close(qc);
4491
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004492 /* If the MUX is still alive, keep the quic-conn. The MUX is
4493 * responsible to call quic_close to release it.
4494 */
4495 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4496 if (qc->mux_state != QC_MUX_READY)
4497 quic_conn_release(qc);
4498
4499 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4500 * least clean some parts of it such as the tasklet.
4501 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004502
4503 /* If the connection did not reached the handshake complete state,
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004504 * the <half_open_conn> counter was not decremented. Note that if
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004505 * a TLS alert was received from the TLS stack, this counter
4506 * has already been decremented.
4507 */
Frédéric Lécaillead9895d2022-05-20 20:50:59 +02004508 if (qc_state < QUIC_HS_ST_COMPLETE && !(qc_flags & QUIC_FL_CONN_TLS_ALERT))
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004509 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004510
4511 return NULL;
4512}
4513
4514/* Initialize the idle timeout task for <qc>.
4515 * Returns 1 if succeeded, 0 if not.
4516 */
4517static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4518{
4519 qc->idle_timer_task = task_new_here();
4520 if (!qc->idle_timer_task)
4521 return 0;
4522
4523 qc->idle_timer_task->process = qc_idle_timer_task;
4524 qc->idle_timer_task->context = qc;
4525 qc_idle_timer_rearm(qc, 1);
4526 task_queue(qc->idle_timer_task);
4527
4528 return 1;
4529}
4530
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004531/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4532 * past one byte of this buffer.
4533 */
4534static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4535 struct quic_rx_packet *pkt)
4536{
4537 unsigned char dcid_len, scid_len;
4538
4539 /* Version */
4540 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4541 return 0;
4542
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004543 /* Destination Connection ID Length */
4544 dcid_len = *(*buf)++;
4545 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4546 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4547 /* XXX MUST BE DROPPED */
4548 return 0;
4549
4550 if (dcid_len) {
4551 /* Check that the length of this received DCID matches the CID lengths
4552 * of our implementation for non Initials packets only.
4553 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004554 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4555 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004556 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004557 return 0;
4558
4559 memcpy(pkt->dcid.data, *buf, dcid_len);
4560 }
4561
4562 pkt->dcid.len = dcid_len;
4563 *buf += dcid_len;
4564
4565 /* Source Connection ID Length */
4566 scid_len = *(*buf)++;
4567 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4568 /* XXX MUST BE DROPPED */
4569 return 0;
4570
4571 if (scid_len)
4572 memcpy(pkt->scid.data, *buf, scid_len);
4573 pkt->scid.len = scid_len;
4574 *buf += scid_len;
4575
4576 return 1;
4577}
4578
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004579/* Insert <pkt> RX packet in its <qel> RX packets tree */
4580static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4581{
4582 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004583 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004584 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4585 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4586 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004587}
4588
4589/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004590 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4591 * byte past the end of the buffer containing this packet and <beg> the address of
4592 * the packet first byte.
4593 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4594 * Returns 1 if succeeded, 0 if not.
4595 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004596static inline int qc_try_rm_hp(struct quic_conn *qc,
4597 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004598 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004599 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004600 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004601{
4602 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004603 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004604 struct quic_enc_level *qel;
4605 /* Only for traces. */
4606 struct quic_rx_packet *qpkt_trace;
4607
4608 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004609 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004610 /* The packet number is here. This is also the start minus
4611 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4612 * protection.
4613 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004614 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004615
4616 tel = quic_packet_type_enc_level(pkt->type);
4617 qel = &qc->els[tel];
4618
4619 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004620 /* Note that the following function enables us to unprotect the packet
4621 * number and its length subsequently used to decrypt the entire
4622 * packets.
4623 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004624 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4625 qel->pktns->rx.largest_pn, pn, beg, end)) {
4626 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004627 goto err;
4628 }
4629
4630 /* The AAD includes the packet number field found at <pn>. */
4631 pkt->aad_len = pn - beg + pkt->pnl;
4632 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004633 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004634 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004635 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004636 /* If the packet number space has been discarded, this packet
4637 * will be not parsed.
4638 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004639 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004640 goto out;
4641 }
4642
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004643 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004644 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004645 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4646 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004647 }
4648
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004649 *el = qel;
4650 /* No reference counter incrementation here!!! */
4651 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4652 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4653 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4654 b_add(&qc->rx.buf, pkt->len);
4655 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004656 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004657 return 1;
4658
4659 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004660 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004661 return 0;
4662}
4663
4664/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4665 * Also set <*long_header> to 1 if this form is long, 0 if not.
4666 */
4667static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4668 unsigned char byte0, int *long_header)
4669{
4670 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4671 pkt->type =
4672 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4673 *long_header = 1;
4674 }
4675 else {
4676 pkt->type = QUIC_PACKET_TYPE_SHORT;
4677 *long_header = 0;
4678 }
4679}
4680
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004681/*
4682 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4683 */
4684static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4685{
4686 int j = 0, version;
4687
4688 do {
4689 version = quic_supported_version[j];
4690 if (version == pkt->version)
4691 return 1;
4692
4693 version = quic_supported_version[++j];
4694 } while(version);
4695
4696 return 0;
4697}
4698
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004699/*
4700 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4701 * address <addr>.
4702 * Implementation of RFC9000 6. Version Negotiation
4703 *
4704 * TODO implement a rate-limiting sending of Version Negotiation packets
4705 *
4706 * Returns 0 on success else non-zero
4707 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004708static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4709 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004710{
4711 char buf[256];
4712 int i = 0, j, version;
4713 const socklen_t addrlen = get_addr_len(addr);
4714
4715 /*
4716 * header form
4717 * long header, fixed bit to 0 for Version Negotiation
4718 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004719 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4720 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004721
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004722 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004723 /* null version for Version Negotiation */
4724 buf[i++] = '\x00';
4725 buf[i++] = '\x00';
4726 buf[i++] = '\x00';
4727 buf[i++] = '\x00';
4728
4729 /* source connection id */
4730 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004731 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004732 i += pkt->scid.len;
4733
4734 /* destination connection id */
4735 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004736 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004737 i += pkt->dcid.len;
4738
4739 /* supported version */
4740 j = 0;
4741 do {
4742 version = htonl(quic_supported_version[j]);
4743 memcpy(&buf[i], &version, sizeof(version));
4744 i += sizeof(version);
4745
4746 version = quic_supported_version[++j];
4747 } while (version);
4748
4749 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4750 return 1;
4751
4752 return 0;
4753}
4754
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004755/* Send a stateless reset packet depending on <pkt> RX packet information
4756 * from <fd> UDP socket to <dst>
4757 * Return 1 if succeeded, 0 if not.
4758 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004759static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004760 struct quic_rx_packet *rxpkt)
4761{
4762 int pktlen, rndlen;
4763 unsigned char pkt[64];
4764 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004765 struct proxy *prx;
4766 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004767
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004768 prx = l->bind_conf->frontend;
4769 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004770 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
4771 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
4772 * Reset is difficult to distinguish from other packets if the recipient requires
4773 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
4774 * that all packets it sends are at least 22 bytes longer than the minimum
4775 * connection ID length that it requests the peer to include in its packets,
4776 * adding PADDING frames as necessary. This ensures that any Stateless Reset
4777 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
4778 * An endpoint that sends a Stateless Reset in response to a packet that is
4779 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
4780 * than the packet it responds to.
4781 */
4782
4783 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
4784 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
4785 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
4786 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
4787 /* Put a header of random bytes */
4788 if (RAND_bytes(pkt, rndlen) != 1)
4789 return 0;
4790
4791 /* Clear the most significant bit, and set the second one */
4792 *pkt = (*pkt & ~0x80) | 0x40;
4793 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
4794 rxpkt->dcid.data, rxpkt->dcid.len))
4795 return 0;
4796
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004797 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004798 return 0;
4799
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004800 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004801 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
4802 return 1;
4803}
4804
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004805/* QUIC server only function.
4806 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
4807 * This is the responsability of the caller to check <aad> size is big enough
4808 * to contain these data.
4809 * Return the number of bytes copied to <aad>.
4810 */
4811static int quic_generate_retry_token_aad(unsigned char *aad,
4812 uint32_t version,
4813 const struct quic_cid *cid,
4814 const struct sockaddr_storage *addr)
4815{
4816 unsigned char *p;
4817
4818 p = aad;
4819 memcpy(p, &version, sizeof version);
4820 p += sizeof version;
4821 p += quic_saddr_cpy(p, addr);
4822 memcpy(p, cid->data, cid->len);
4823 p += cid->len;
4824
4825 return p - aad;
4826}
4827
4828/* QUIC server only function.
4829 * Generate the token to be used in Retry packets. The token is written to
4830 * <buf> whith <len> as length. <odcid> is the original destination connection
4831 * ID and <dcid> is our side destination connection ID (or client source
4832 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004833 * Returns the length of the encoded token or 0 on error.
4834 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004835static int quic_generate_retry_token(unsigned char *buf, size_t len,
4836 const uint32_t version,
4837 const struct quic_cid *odcid,
4838 const struct quic_cid *dcid,
4839 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004840{
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004841 unsigned char *p;
4842 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4843 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4844 size_t aadlen;
4845 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
4846 unsigned char key[QUIC_TLS_KEY_LEN];
4847 unsigned char iv[QUIC_TLS_IV_LEN];
4848 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4849 size_t seclen = strlen(global.cluster_secret);
4850 EVP_CIPHER_CTX *ctx = NULL;
4851 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4852 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004853
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004854 /* We copy the odcid into the token, prefixed by its one byte
4855 * length, the format token byte. It is followed by an AEAD TAG, and finally
4856 * the random bytes used to derive the secret to encrypt the token.
4857 */
4858 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004859 return 0;
4860
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004861 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
4862 if (RAND_bytes(salt, sizeof salt) != 1)
4863 goto err;
4864
4865 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
4866 salt, sizeof salt, sec, seclen))
4867 goto err;
4868
4869 if (!quic_tls_tx_ctx_init(&ctx, aead, key))
4870 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004871
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004872 /* Token build */
4873 p = buf;
4874 *p++ = QUIC_TOKEN_FMT_RETRY,
4875 *p++ = odcid->len;
4876 memcpy(p, odcid->data, odcid->len);
4877 p += odcid->len;
4878 write_u32(p, htonl(timestamp));
4879 p += sizeof timestamp;
4880
4881 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
4882 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv))
4883 goto err;
4884
4885 p += QUIC_TLS_TAG_LEN;
4886 memcpy(p, salt, sizeof salt);
4887 p += sizeof salt;
4888 EVP_CIPHER_CTX_free(ctx);
4889
4890 return p - buf;
4891
4892 err:
4893 if (ctx)
4894 EVP_CIPHER_CTX_free(ctx);
4895 return 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004896}
4897
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004898/* QUIC server only function.
4899 * Check the validity of the Retry token from <token> buffer with <tokenlen>
4900 * as length. If valid, the ODCID of <qc> QUIC connection will be put
4901 * into <odcid> connection ID. <dcid> is our side destination connection ID
4902 * of client source connection ID.
4903 * Return 1 if succeeded, 0 if not.
4904 */
4905static int quic_retry_token_check(unsigned char *token, size_t tokenlen,
4906 const uint32_t version,
4907 struct quic_cid *odcid,
4908 const struct quic_cid *dcid,
4909 struct quic_conn *qc,
4910 struct sockaddr_storage *addr)
4911{
4912 unsigned char buf[128];
4913 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4914 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4915 size_t aadlen;
4916 unsigned char *salt;
4917 unsigned char key[QUIC_TLS_KEY_LEN];
4918 unsigned char iv[QUIC_TLS_IV_LEN];
4919 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4920 size_t seclen = strlen(global.cluster_secret);
4921 EVP_CIPHER_CTX *ctx = NULL;
4922 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4923
4924 if (sizeof buf < tokenlen)
4925 return 0;
4926
4927 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
4928 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
4929 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
4930 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen))
4931 return 0;
4932
4933 if (!quic_tls_rx_ctx_init(&ctx, aead, key))
4934 goto err;
4935
4936 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
4937 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
4938 ctx, aead, key, iv))
4939 goto err;
4940
4941 if (parse_retry_token(buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
4942 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4943 goto err;
4944 }
4945
4946 EVP_CIPHER_CTX_free(ctx);
4947 return 1;
4948
4949 err:
4950 if (ctx)
4951 EVP_CIPHER_CTX_free(ctx);
4952 return 0;
4953}
4954
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004955/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4956 * the Initial <pkt> packet.
4957 *
4958 * Returns 0 on success else non-zero.
4959 */
4960static int send_retry(int fd, struct sockaddr_storage *addr,
4961 struct quic_rx_packet *pkt)
4962{
4963 unsigned char buf[128];
4964 int i = 0, token_len;
4965 const socklen_t addrlen = get_addr_len(addr);
4966 struct quic_cid scid;
4967
4968 /* long header + fixed bit + packet type 0x3 */
4969 buf[i++] = 0xf0;
4970 /* version */
4971 buf[i++] = 0x00;
4972 buf[i++] = 0x00;
4973 buf[i++] = 0x00;
4974 buf[i++] = 0x01;
4975
4976 /* Use the SCID from <pkt> for Retry DCID. */
4977 buf[i++] = pkt->scid.len;
4978 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4979 i += pkt->scid.len;
4980
4981 /* Generate a new CID to be used as SCID for the Retry packet. */
4982 scid.len = QUIC_HAP_CID_LEN;
4983 if (RAND_bytes(scid.data, scid.len) != 1)
4984 return 1;
4985
4986 buf[i++] = scid.len;
4987 memcpy(&buf[i], scid.data, scid.len);
4988 i += scid.len;
4989
4990 /* token */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004991 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, pkt->version,
4992 &pkt->dcid, &pkt->scid, addr)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004993 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004994
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004995 i += token_len;
4996
4997 /* token integrity tag */
4998 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4999 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
5000 pkt->dcid.len, buf, i)) {
5001 return 1;
5002 }
5003
5004 i += QUIC_TLS_TAG_LEN;
5005
5006 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
5007 return 1;
5008
5009 return 0;
5010}
5011
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005012/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5013 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5014 * concatenated to the <pkt> DCID field.
5015 *
5016 * Returns the instance or NULL if not found.
5017 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005018static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005019 struct listener *l,
5020 struct sockaddr_storage *saddr)
5021{
5022 struct quic_conn *qc = NULL;
5023 struct ebmb_node *node;
5024 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005025 /* set if the quic_conn is found in the second DCID tree */
5026 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005027
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005028 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5029 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5030 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5031 /* DCIDs of first packets coming from multiple clients may have
5032 * the same values. Let's distinguish them by concatenating the
5033 * socket addresses.
5034 */
5035 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005036 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005037 pkt->dcid.len + pkt->dcid.addrlen);
5038 if (node) {
5039 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5040 goto end;
5041 }
5042 }
5043
5044 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5045 * also for INITIAL/0-RTT non-first packets with the final DCID in
5046 * used.
5047 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005048 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005049 if (!node)
5050 goto end;
5051
5052 id = ebmb_entry(node, struct quic_connection_id, node);
5053 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005054 found_in_dcid = 1;
5055
5056 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005057 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5058 * If already done, this is a noop.
5059 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01005060 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005061 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005062
5063 return qc;
5064}
5065
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005066/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5067 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5068 * parameters of this session.
5069 * This is the responsibility of the caller to check the validity of all the
5070 * pointers passed as parameter to this function.
5071 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5072 * CO_ER_SSL_NO_MEM.
5073 */
5074static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5075 unsigned char *params, size_t params_len)
5076{
5077 int retry;
5078
5079 retry = 1;
5080 retry:
5081 *ssl = SSL_new(ssl_ctx);
5082 if (!*ssl) {
5083 if (!retry--)
5084 goto err;
5085
5086 pool_gc(NULL);
5087 goto retry;
5088 }
5089
5090 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5091 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
5092 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005093 SSL_free(*ssl);
5094 *ssl = NULL;
5095 if (!retry--)
5096 goto err;
5097
5098 pool_gc(NULL);
5099 goto retry;
5100 }
5101
5102 return 0;
5103
5104 err:
5105 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5106 return -1;
5107}
5108
5109/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5110 * used to process <qc> received packets. The allocated context is stored in
5111 * <qc.xprt_ctx>.
5112 *
5113 * Returns 0 on success else non-zero.
5114 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005115static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005116{
5117 struct bind_conf *bc = qc->li->bind_conf;
5118 struct ssl_sock_ctx *ctx = NULL;
5119
5120 ctx = pool_zalloc(pool_head_quic_conn_ctx);
5121 if (!ctx)
5122 goto err;
5123
5124 ctx->wait_event.tasklet = tasklet_new();
5125 if (!ctx->wait_event.tasklet)
5126 goto err;
5127
5128 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5129 ctx->wait_event.tasklet->context = ctx;
5130 ctx->wait_event.events = 0;
5131 ctx->subs = NULL;
5132 ctx->xprt_ctx = NULL;
5133 ctx->qc = qc;
5134
5135 /* Set tasklet tid based on the SCID selected by us for this
5136 * connection. The upper layer will also be binded on the same thread.
5137 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005138 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005139
5140 if (qc_is_listener(qc)) {
5141 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5142 qc->enc_params, qc->enc_params_len) == -1) {
5143 goto err;
5144 }
5145
5146 /* Enabling 0-RTT */
5147 if (bc->ssl_conf.early_data)
5148 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5149
5150 SSL_set_accept_state(ctx->ssl);
5151 }
5152
5153 ctx->xprt = xprt_get(XPRT_QUIC);
5154
5155 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005156 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005157
5158 return 0;
5159
5160 err:
5161 if (ctx && ctx->wait_event.tasklet)
5162 tasklet_free(ctx->wait_event.tasklet);
5163 pool_free(pool_head_quic_conn_ctx, ctx);
5164
5165 return 1;
5166}
5167
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005168/* Check that all the bytes between <buf> included and <end> address
5169 * excluded are null. This is the responsability of the caller to
5170 * check that there is at least one byte between <buf> end <end>.
5171 * Return 1 if this all the bytes are null, 0 if not.
5172 */
5173static inline int quic_padding_check(const unsigned char *buf,
5174 const unsigned char *end)
5175{
5176 while (buf < end && !*buf)
5177 buf++;
5178
5179 return buf == end;
5180}
5181
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005182/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5183 * end of this buffer past one byte and populate <pkt> RX packet structure
5184 * with the information collected from the packet.
5185 * This function sets ->len <pkt> field value to the end of the packet past one
5186 * byte to enable the caller to run this function again to continue to parse
5187 * the remaing QUIC packets carried by the datagram.
5188 * Note that this function always sets this ->len value. If a paquet could
5189 * not be correctly found, ->len value will be set to the remaining number
5190 * bytes in the datagram to entirely consume this latter.
5191 */
5192static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5193 struct quic_rx_packet *pkt, int first_pkt,
5194 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005195{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005196 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005197 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005198 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005199 struct proxy *prx;
5200 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005201 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005202 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005203 size_t b_cspace;
5204 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005205
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005206 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005207 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005208 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005209 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005210 TRACE_ENTER(QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005211 l = dgram->owner;
5212 prx = l->bind_conf->frontend;
5213 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005214 /* This ist only to please to traces and distinguish the
5215 * packet with parsed packet number from others.
5216 */
5217 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005218 if (end <= buf) {
5219 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5220 goto drop;
5221 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005222
5223 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005224 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005225 if (!first_pkt && quic_padding_check(buf, end)) {
5226 /* Some browsers may pad the remaining datagram space with null bytes.
5227 * That is what we called add padding out of QUIC packets. Such
5228 * datagrams must be considered as valid. But we can only consume
5229 * the remaining space.
5230 */
5231 pkt->len = end - buf;
5232 goto drop_no_conn;
5233 }
5234
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005235 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005236 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237 }
5238
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005239 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005240 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005241 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005242 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005243 struct quic_cid odcid;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005244
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005245 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005246 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005247 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005248 }
5249
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005250 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5251 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005252 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005253 }
5254 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5255 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005256 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005257 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005258 }
5259
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005260 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5261 * they must have the same DCID.
5262 */
5263 if (!first_pkt &&
5264 (pkt->dcid.len != dgram->dcid_len ||
5265 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5266 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005267 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005268 }
5269
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005270 /* Retry of Version Negotiation packets are only sent by servers */
5271 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
5272 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005273 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005274 }
5275
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005276 /* RFC9000 6. Version Negotiation */
5277 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005278 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005279 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005280 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
5281 goto err;
5282 }
5283
5284 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005285 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005286 }
5287
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005288 /* For Initial packets, and for servers (QUIC clients connections),
5289 * there is no Initial connection IDs storage.
5290 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005291 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005292 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005293
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005294 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5295 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005296 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005297 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005298 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005299
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005300 /* TODO Retry should be automatically activated if
5301 * suspect network usage is detected.
5302 */
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005303 if (global.cluster_secret) {
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005304 if (!token_len) {
Willy Tarreau787e92a2022-05-20 16:06:01 +02005305 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005306 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
5307 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
5308 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
5309 goto err;
5310 }
5311
5312 HA_ATOMIC_INC(&prx_counters->retry_sent);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005313 goto err;
5314 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005315 }
5316 else {
5317 if (*buf == QUIC_TOKEN_FMT_RETRY) {
5318 if (!quic_retry_token_check(buf, token_len, pkt->version, &odcid,
5319 &pkt->scid, qc, &dgram->saddr)) {
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005320 HA_ATOMIC_INC(&prx_counters->retry_error);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005321 TRACE_PROTO("Wrong retry token", QUIC_EV_CONN_LPKT);
5322 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5323 * with an INVALID_TOKEN error.
5324 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005325 goto drop;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005326 }
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005327
5328 HA_ATOMIC_INC(&prx_counters->retry_validated);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005329 }
5330 else {
5331 /* TODO: New token check */
5332 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005333 goto drop;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005334 }
5335 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005336 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005337
5338 pkt->token = buf;
5339 pkt->token_len = token_len;
5340 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005341 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005342 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005343 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005344 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005345 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005346 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005347 }
5348
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005349 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5350 end - buf < len) {
5351 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005352 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005353 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005354
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005355 payload = buf;
5356 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005357 if (drop_no_conn)
5358 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005359
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005360 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005361 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005362 int ipv4;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005363 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005364
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005365 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01005366 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005367 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005368 }
5369
Willy Tarreau787e92a2022-05-20 16:06:01 +02005370 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005371 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005372 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
5373 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
5374 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
5375 goto err;
5376 }
5377
5378 HA_ATOMIC_INC(&prx_counters->retry_sent);
5379 goto err;
5380 }
5381
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005382 /* RFC 9000 7.2. Negotiating Connection IDs:
5383 * When an Initial packet is sent by a client that has not previously
5384 * received an Initial or Retry packet from the server, the client
5385 * populates the Destination Connection ID field with an unpredictable
5386 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5387 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01005388 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
5389 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005390 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01005391 }
5392
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005393 pkt->saddr = dgram->saddr;
5394 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaille748ece62022-05-21 23:58:40 +02005395 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &odcid,
5396 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005397 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005398 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005399
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005400 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005401 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005402 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005403 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005404
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005405 /* If the insertion failed, it means that another
5406 * thread has already allocated a QUIC connection for
5407 * the same CID. Liberate our allocated connection.
5408 */
5409 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005410 qc_to_purge = qc;
5411
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005412 qc = ebmb_entry(n, struct quic_conn, odcid_node);
5413 pkt->qc = qc;
5414 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005415
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005416 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005417 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01005418 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005419 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005420 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01005421 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005422 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005423 }
5424 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005425 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005426 }
5427 }
5428 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005429 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005430 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005431 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005432 }
5433
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005434 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005435 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005436
5437 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5438 * they must have the same DCID.
5439 */
5440 if (!first_pkt &&
5441 (pkt->dcid.len != dgram->dcid_len ||
5442 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5443 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005444 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005445 }
5446
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005447 buf += QUIC_HAP_CID_LEN;
5448
5449 /* A short packet is the last one of a UDP datagram. */
5450 payload = buf;
5451 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005452
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005453 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005454 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005455 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005456 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005457 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005458 TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005459 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005460 }
5461
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005462 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005463 }
5464
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005465 if (qc->flags & QUIC_FL_CONN_CLOSING) {
5466 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
5467 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
5468 qc->nb_pkt_for_cc++;
5469 qc->nb_pkt_since_cc = 0;
5470 }
5471 /* Skip the entire datagram */
5472 pkt->len = end - beg;
5473 TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005474 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005475 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005476
5477 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5478 * they must have the same DCID.
5479 *
5480 * This check must be done after the final update to pkt.len to
5481 * properly drop the packet on failure.
5482 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005483 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005484 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005485 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
5486 QUIC_EV_CONN_LPKT, qc);
5487 /* Reset the anti-amplification bit. It will be set again
5488 * when sending the next packet if reached again.
5489 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005490 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
5491 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005492 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005493 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005494
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005495 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005496
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005497 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005498 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005499 goto out;
5500 }
5501
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005502 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005503 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005504 b_cspace = b_contig_space(&qc->rx.buf);
5505 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02005506 /* Do not consume buf if space not at the end. */
5507 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
5508 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
5509 goto err;
5510 }
5511
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005512 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005513 if (b_cspace) {
5514 b_putchr(&qc->rx.buf, 0x00);
5515 b_cspace--;
5516 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005517 b_add(&qc->rx.buf, b_cspace);
5518 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005519 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01005520 qc_list_all_rx_pkts(qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005521 goto drop;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005522 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005523 }
5524
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005525 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005526 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005527 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02005528 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005529
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005530 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005531 if (pkt->aad_len)
5532 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005533 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02005534 /* Wake up the connection packet handler task from here only if all
5535 * the contexts have been initialized, especially the mux context
5536 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
5537 * will start it if these contexts for the connection are not already
5538 * initialized.
5539 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005540 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005541 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005542 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005543
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005544 drop_no_conn:
5545 if (drop_no_conn)
5546 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005547 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005548
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005549 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005550
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005551 drop:
5552 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005553 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005554 /* Wakeup the I/O handler callback if the PTO timer must be armed.
5555 * This cannot be done by this thread.
5556 */
5557 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005558 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005559 if (conn_ctx && conn_ctx->wait_event.tasklet)
5560 tasklet_wakeup(conn_ctx->wait_event.tasklet);
5561 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01005562 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005563 if (!pkt->len)
5564 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005565 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005566 qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005567}
5568
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005569/* This function builds into <buf> buffer a QUIC long packet header.
5570 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005571 */
5572static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
5573 int type, size_t pn_len, struct quic_conn *conn)
5574{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005575 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005576 return 0;
5577
5578 /* #0 byte flags */
5579 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5580 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5581 /* Version */
5582 quic_write_uint32(buf, end, conn->version);
5583 *(*buf)++ = conn->dcid.len;
5584 /* Destination connection ID */
5585 if (conn->dcid.len) {
5586 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5587 *buf += conn->dcid.len;
5588 }
5589 /* Source connection ID */
5590 *(*buf)++ = conn->scid.len;
5591 if (conn->scid.len) {
5592 memcpy(*buf, conn->scid.data, conn->scid.len);
5593 *buf += conn->scid.len;
5594 }
5595
5596 return 1;
5597}
5598
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005599/* This function builds into <buf> buffer a QUIC short packet header.
5600 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005601 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005602static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5603 size_t pn_len, struct quic_conn *conn,
5604 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005605{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005606 if (end - *buf < 1 + conn->dcid.len)
5607 return 0;
5608
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005609 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005610 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5611 ((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 +01005612 /* Destination connection ID */
5613 if (conn->dcid.len) {
5614 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5615 *buf += conn->dcid.len;
5616 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005617
5618 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005619}
5620
5621/* Apply QUIC header protection to the packet with <buf> as first byte address,
5622 * <pn> as address of the Packet number field, <pnlen> being this field length
5623 * with <aead> as AEAD cipher and <key> as secret key.
5624 * Returns 1 if succeeded or 0 if failed.
5625 */
5626static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5627 const EVP_CIPHER *aead, const unsigned char *key)
5628{
5629 int i, ret, outlen;
5630 EVP_CIPHER_CTX *ctx;
5631 /* We need an IV of at least 5 bytes: one byte for bytes #0
5632 * and at most 4 bytes for the packet number
5633 */
5634 unsigned char mask[5] = {0};
5635
5636 ret = 0;
5637 ctx = EVP_CIPHER_CTX_new();
5638 if (!ctx)
5639 return 0;
5640
5641 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5642 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5643 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5644 goto out;
5645
5646 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5647 for (i = 0; i < pnlen; i++)
5648 pn[i] ^= mask[i + 1];
5649
5650 ret = 1;
5651
5652 out:
5653 EVP_CIPHER_CTX_free(ctx);
5654
5655 return ret;
5656}
5657
5658/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5659 * ACK ranges if needed to a value below <limit> in bytes.
5660 * Return 1 if succeeded, 0 if not.
5661 */
5662static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5663{
5664 size_t room, ack_delay_sz;
5665
5666 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5667 /* A frame is made of 1 byte for the frame type. */
5668 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005669 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005670 return 0;
5671
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005672 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005673}
5674
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005675/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5676 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5677 * with <room> as available room, and <*len> the packet Length field initialized
5678 * with the number of bytes already present in this buffer which must be taken
5679 * into an account for the Length packet field value. <headlen> is the number of
5680 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005681 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005682 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005683 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005684 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005685 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005686static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005687 size_t room, size_t *len, size_t headlen,
5688 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005689 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005690{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005691 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005692 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005693
Frédéric Lécailleea604992020-12-24 13:01:37 +01005694 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005695 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005696 return 0;
5697
Frédéric Lécailleea604992020-12-24 13:01:37 +01005698 /* If we are not probing we must take into an account the congestion
5699 * control window.
5700 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005701 if (!qel->pktns->tx.pto_probe) {
5702 size_t remain = quic_path_prep_data(qc->path);
5703
5704 if (headlen > remain)
5705 return 0;
5706
5707 room = QUIC_MIN(room, remain - headlen);
5708 }
5709
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005710 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005711 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005712
5713 /* NOTE: switch/case block inside a loop, a successful status must be
5714 * returned by this function only if at least one frame could be built
5715 * in the switch/case block.
5716 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005717 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005718 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005719 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005720
Frédéric Lécailleea604992020-12-24 13:01:37 +01005721 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005722 break;
5723
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005724 switch (cf->type) {
5725 case QUIC_FT_CRYPTO:
5726 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005727 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005728 /* Compute the length of this CRYPTO frame header */
5729 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5730 /* Compute the data length of this CRyPTO frame. */
5731 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5732 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005733 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005734 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005735 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005736
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005737 /* CRYPTO frame length. */
5738 flen = hlen + quic_int_getsize(dlen) + dlen;
5739 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005740 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005741 /* Add the CRYPTO data length and its encoded length to the packet
5742 * length and the length of this length.
5743 */
5744 *len += flen;
5745 room -= flen;
5746 if (dlen == cf->crypto.len) {
5747 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005748 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005749 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005750 }
5751 else {
5752 struct quic_frame *new_cf;
5753
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005754 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005755 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005756 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005757 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005758 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005759
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005760 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005761 new_cf->type = QUIC_FT_CRYPTO;
5762 new_cf->crypto.len = dlen;
5763 new_cf->crypto.offset = cf->crypto.offset;
5764 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005765 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005766 /* Consume <dlen> bytes of the current frame. */
5767 cf->crypto.len -= dlen;
5768 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005769 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005770 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005771
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005772 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005773 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
5774 struct eb64_node *node = NULL;
5775 struct qc_stream_desc *stream_desc = NULL;
5776 struct quic_stream *strm = &cf->stream;
5777
5778 /* As this frame has been already lost, ensure the stream is always
5779 * available or the range of this frame is not consumed before
5780 * resending it.
5781 */
5782 node = eb64_lookup(&qc->streams_by_id, strm->id);
5783 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005784 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005785 LIST_DELETE(&cf->list);
5786 pool_free(pool_head_quic_frame, cf);
5787 continue;
5788 }
5789
5790 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
5791 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
5792 TRACE_PROTO("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005793 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005794 LIST_DELETE(&cf->list);
5795 pool_free(pool_head_quic_frame, cf);
5796 continue;
5797 }
5798 else if (strm->offset.key < stream_desc->ack_offset) {
5799 strm->offset.key = stream_desc->ack_offset;
5800 TRACE_PROTO("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005801 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005802 }
5803 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005804 /* Note that these frames are accepted in short packets only without
5805 * "Length" packet field. Here, <*len> is used only to compute the
5806 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005807 *
5808 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005809 * excepting the variable ones. Note that +1 is for the type of this frame.
5810 */
5811 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005812 ((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 +02005813 /* Compute the data length of this STREAM frame. */
5814 avail_room = room - hlen - *len;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005815 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005816 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005817 if ((ssize_t)avail_room <= 0)
5818 continue;
5819
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005820 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5821 dlen = max_available_room(avail_room, &dlen_sz);
5822 if (dlen > cf->stream.len) {
5823 dlen = cf->stream.len;
5824 }
5825 dlen_sz = quic_int_getsize(dlen);
5826 flen = hlen + dlen_sz + dlen;
5827 }
5828 else {
5829 dlen = QUIC_MIN(avail_room, cf->stream.len);
5830 flen = hlen + dlen;
5831 }
5832 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005833 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005834 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005835 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005836 /* Add the STREAM data length and its encoded length to the packet
5837 * length and the length of this length.
5838 */
5839 *len += flen;
5840 room -= flen;
5841 if (dlen == cf->stream.len) {
5842 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005843 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005844 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005845
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005846 /* The MUX stream might be released at this
5847 * stage. This can most notably happen on
5848 * retransmission.
5849 */
5850 if (qc->mux_state == QC_MUX_READY &&
5851 !cf->stream.stream->release) {
5852 qcc_streams_sent_done(cf->stream.stream->ctx,
5853 cf->stream.len,
5854 cf->stream.offset.key);
5855 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005856 }
5857 else {
5858 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005859 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005860
5861 new_cf = pool_zalloc(pool_head_quic_frame);
5862 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005863 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005864 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005865 }
5866
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005867 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005868 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005869 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005870 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005871 new_cf->stream.id = cf->stream.id;
5872 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5873 new_cf->stream.offset = cf->stream.offset;
5874 new_cf->stream.len = dlen;
5875 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5876 /* FIN bit reset */
5877 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5878 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005879 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005880 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5881 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005882 cf_buf = b_make(b_orig(cf->stream.buf),
5883 b_size(cf->stream.buf),
5884 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005885 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005886 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005887 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005888
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005889 /* The MUX stream might be released at this
5890 * stage. This can most notably happen on
5891 * retransmission.
5892 */
5893 if (qc->mux_state == QC_MUX_READY &&
5894 !cf->stream.stream->release) {
5895 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5896 new_cf->stream.len,
5897 new_cf->stream.offset.key);
5898 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005899 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005900
5901 /* TODO the MUX is notified about the frame sending via
5902 * previous qcc_streams_sent_done call. However, the
5903 * sending can fail later, for example if the sendto
5904 * system call returns an error. As the MUX has been
5905 * notified, the transport layer is responsible to
5906 * bufferize and resent the announced data later.
5907 */
5908
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005909 break;
5910
5911 default:
5912 flen = qc_frm_len(cf);
5913 BUG_ON(!flen);
5914 if (flen > room)
5915 continue;
5916
5917 *len += flen;
5918 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005919 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005920 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005921 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005922 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005923
5924 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01005925 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005926 }
5927
Frédéric Lécailleea604992020-12-24 13:01:37 +01005928 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005929}
5930
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005931/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005932 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5933 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005934 * filling the buffer with as much frames as possible from <frms> list of
5935 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005936 * 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 +02005937 * 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 +02005938 * having returned from this function.
5939 * 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 +01005940 * number field in this packet. <pn_len> will also have the packet number
5941 * length as value.
5942 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005943 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005944 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005945static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5946 size_t dglen, struct quic_tx_packet *pkt,
5947 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005948 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005949 struct quic_enc_level *qel, struct quic_conn *qc,
5950 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005951{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005952 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005953 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005954 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5955 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02005956 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005957 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005958 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005959 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005960 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005961 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005962
Frédéric Lécailleea604992020-12-24 13:01:37 +01005963 /* Length field value with CRYPTO frames if present. */
5964 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005965 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005966 /* When not probing, and no immediate close is required, reduce the size of this
5967 * buffer to respect the congestion controller window.
5968 * This size will be limited if we have ack-eliciting frames to send from <frms>.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005969 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005970 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005971 size_t path_room;
5972
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005973 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005974 if (end - beg > path_room)
5975 end = beg + path_room;
5976 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005977
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005978 /* Ensure there is enough room for the TLS encryption tag and a zero token
5979 * length field if any.
5980 */
5981 if (end - pos < QUIC_TLS_TAG_LEN +
5982 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5983 goto no_room;
5984
5985 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005986 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005987 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005988 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005989 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005990 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005991 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005992 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005993 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005994 goto no_room;
5995
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005996 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5997 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005998 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005999 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006000 /* Build an ACK frame if required. */
6001 ack_frm_len = 0;
Frédéric Lécaille337108e2022-04-25 10:38:27 +02006002 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006003 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006004 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006005 ack_frm.tx_ack.arngs = arngs;
6006 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6007 qel->pktns->tx.ack_delay =
6008 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6009 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6010 }
6011 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006012 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6013 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6014 * that from here, we do not know if we will have to send a PING frame.
6015 * This will be decided after having computed the ack-eliciting frames
6016 * to be added to this packet.
6017 */
6018 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 +01006019 if (!ack_frm_len)
6020 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006021 }
6022
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006023 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006024 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006025 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006026 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006027 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006028
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006029 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006030 /* Initialize the length of the frames built below to <len>.
6031 * If any frame could be successfully built by qc_build_frms(),
6032 * we will have len_frms > len.
6033 */
6034 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006035 if (!qc_build_frms(&frm_list, frms,
6036 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01006037 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006038 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006039 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6040 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006041 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006042 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006043
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006044 /* Length (of the remaining data). Must not fail because, the buffer size
6045 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6046 * for the encryption tag. It must be taken into an account for the length
6047 * of this packet.
6048 */
6049 if (len_frms)
6050 len = len_frms + QUIC_TLS_TAG_LEN;
6051 else
6052 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006053 /* CONNECTION_CLOSE frame */
6054 if (cc) {
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006055 cc_frm.type = qc->flags & QUIC_FL_CONN_APP_ALERT ?
6056 QUIC_FT_CONNECTION_CLOSE_APP : QUIC_FT_CONNECTION_CLOSE;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006057
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006058 cc_frm.connection_close.error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006059 len += qc_frm_len(&cc_frm);
6060 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006061 add_ping_frm = 0;
6062 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006063 len_sz = quic_int_getsize(len);
6064 /* Add this packet size to <dglen> */
6065 dglen += head_len + len_sz + len;
6066 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6067 /* This is a maximum padding size */
6068 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6069 /* The length field value is of this packet is <len> + <padding_len>
6070 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006071 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006072 * sizes from <padding_len>.
6073 */
6074 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6075 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006076 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006077 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006078 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006079 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006080 add_ping_frm = 1;
6081 len += 1;
6082 }
6083 /* 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 +01006084 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006085 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6086 }
6087
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006088 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6089 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006090
6091 /* Packet number field address. */
6092 *buf_pn = pos;
6093
6094 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006095 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6096 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006097
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006098 if (ack_frm_len) {
6099 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6100 goto no_room;
6101
6102 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006103 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006104 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006105
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006106 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006107 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006108 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006109 unsigned char *spos = pos;
6110
6111 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006112 ssize_t room = end - pos;
6113 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006114 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006115 /* TODO: this should not have happened if qc_build_frms()
6116 * had correctly computed and sized the frames to be
6117 * added to this packet. Note that <cf> was added
6118 * from <frm_list> to <frms> list by qc_build_frms().
6119 */
6120 LIST_DELETE(&cf->list);
6121 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006122 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006123 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006124
6125 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006126 quic_tx_packet_refinc(pkt);
6127 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006128 }
6129 }
6130
6131 /* Build a PING frame if needed. */
6132 if (add_ping_frm) {
6133 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006134 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006135 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006136 }
6137
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006138 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006139 if (cc) {
6140 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6141 goto no_room;
6142
6143 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6144 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006145
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006146 /* Build a PADDING frame if needed. */
6147 if (padding_len) {
6148 frm.type = QUIC_FT_PADDING;
6149 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006150 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006151 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006152 }
6153
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006154 /* If this packet is ack-eliciting and we are probing let's
6155 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006156 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006157 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6158 qel->pktns->tx.pto_probe)
6159 qel->pktns->tx.pto_probe--;
6160
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006161 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006162 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006163 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006164
6165 return 1;
6166
6167 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006168 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006169 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailled8b798d2022-04-25 17:48:40 +02006170 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006171
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006172 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006173}
6174
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006175static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006176{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006177 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006178 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006179 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006180 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006181 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006182 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006183 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006184 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006185 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006186 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006187}
6188
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006189/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006190 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6191 * of prebuilt frames.
6192 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006193 * Return -2 if the packet could not be allocated or encrypted for any reason,
6194 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006195 * XXX NOTE XXX
6196 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6197 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6198 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006199 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006200static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6201 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006202 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006203 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006204 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006205{
6206 /* The pointer to the packet number field. */
6207 unsigned char *buf_pn;
6208 unsigned char *beg, *end, *payload;
6209 int64_t pn;
6210 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006211 struct quic_tls_ctx *tls_ctx;
6212 struct quic_tx_packet *pkt;
6213
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006214 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006215 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006216 pkt = pool_alloc(pool_head_quic_tx_packet);
6217 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006218 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006219 *err = -2;
6220 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006221 }
6222
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006223 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006224 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006225 pn_len = 0;
6226 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006227
6228 pn = qel->pktns->tx.next_pn + 1;
6229 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006230 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006231 *err = -1;
6232 goto err;
6233 }
6234
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006235 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006236 payload = buf_pn + pn_len;
6237 payload_len = end - payload;
6238 aad_len = payload - beg;
6239
6240 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006241 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006242 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006243 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006244 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006245
6246 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006247 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006248 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
6249 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006250 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006251 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006252 goto err;
6253 }
6254
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006255 /* Consume a packet number */
6256 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006257 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01006258 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006259 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006260 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6261 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006262 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006263 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006264 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006265 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006266 pkt->in_flight_len = pkt->len;
6267 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006268 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006269 /* Always reset this flags */
6270 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006271 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6272 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6273 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6274 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006275
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006276 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006277 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006278
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006279 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006280
6281 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006282 /* TODO: what about the frames which have been built
6283 * for this packet.
6284 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006285 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006286 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006287 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006288}
6289
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006290
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006291/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6292 * event subscriber <es> is not allowed to change from a previous call as long
6293 * as at least one event is still subscribed. The <event_type> must only be a
6294 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6295 */
6296static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6297{
Willy Tarreau784b8682022-04-11 14:18:10 +02006298 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006299
6300 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6301 BUG_ON(qcc->subs && qcc->subs != es);
6302
6303 es->events |= event_type;
6304 qcc->subs = es;
6305
6306 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02006307 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006308
6309 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02006310 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006311
6312 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006313}
6314
6315/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6316 * The <es> pointer is not allowed to differ from the one passed to the
6317 * subscribe() call. It always returns zero.
6318 */
6319static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6320{
6321 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
6322}
6323
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006324/* Store in <xprt_ctx> the context attached to <conn>.
6325 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006326 */
6327static int qc_conn_init(struct connection *conn, void **xprt_ctx)
6328{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01006329 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006330
6331 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
6332
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006333 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006334 if (*xprt_ctx)
6335 goto out;
6336
Willy Tarreau784b8682022-04-11 14:18:10 +02006337 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006338
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006339 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01006340 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006341
6342 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006343}
6344
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006345/* Start the QUIC transport layer */
6346static int qc_xprt_start(struct connection *conn, void *ctx)
6347{
6348 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02006349 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006350
Willy Tarreau784b8682022-04-11 14:18:10 +02006351 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006352 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
6353 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02006354 /* prepare a CONNECTION_CLOSE frame */
6355 qc->err_code = QC_ERR_APPLICATION_ERROR;
6356 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle05d4ae62022-04-13 17:40:26 +02006357 return -1;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006358 }
6359
6360 /* mux-quic can now be considered ready. */
6361 qc->mux_state = QC_MUX_READY;
6362
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006363 tasklet_wakeup(qctx->wait_event.tasklet);
6364 return 1;
6365}
6366
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006367static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
6368{
Willy Tarreau784b8682022-04-11 14:18:10 +02006369 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006370 return NULL;
6371
Willy Tarreau784b8682022-04-11 14:18:10 +02006372 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006373}
6374
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006375/* transport-layer operations for QUIC connections. */
6376static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02006377 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006378 .subscribe = quic_conn_subscribe,
6379 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006380 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006381 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006382 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
6383 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01006384 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006385 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006386 .name = "QUIC",
6387};
6388
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006389static void __quic_conn_init(void)
6390{
6391 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
6392 xprt_register(XPRT_QUIC, &ssl_quic);
6393}
Willy Tarreau79367f92022-04-25 19:18:24 +02006394INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006395
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006396static void __quic_conn_deinit(void)
6397{
6398 BIO_meth_free(ha_quic_meth);
6399}
Willy Tarreau79367f92022-04-25 19:18:24 +02006400REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006401
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006402/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
6403 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05006404 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006405 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006406struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006407{
6408 unsigned char *pos;
6409 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006410 struct quic_dghdlr *dghdlr = ctx;
6411 struct quic_dgram *dgram;
6412 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006413
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006414 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006415 pos = dgram->buf;
6416 end = pos + dgram->len;
6417 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006418 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006419
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006420 pkt = pool_zalloc(pool_head_quic_rx_packet);
6421 if (!pkt)
6422 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006423
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006424 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006425 quic_rx_packet_refinc(pkt);
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006426 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006427 first_pkt = 0;
6428 pos += pkt->len;
6429 quic_rx_packet_refdec(pkt);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006430 } while (pos < end);
6431
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006432 /* Increasing the received bytes counter by the UDP datagram length
6433 * if this datagram could be associated to a connection.
6434 */
6435 if (dgram->qc)
6436 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01006437
6438 /* Mark this datagram as consumed */
6439 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006440 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006441
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006442 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006443
6444 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006445 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006446}
6447
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006448/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
6449 * Returns 1 if succeeded, 0 if not.
6450 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02006451int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
6452 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006453{
6454 int long_header;
6455 size_t minlen, skip;
6456
6457 if (!(*buf & QUIC_PACKET_FIXED_BIT))
6458 goto err;
6459
6460 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02006461 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
6462 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006463 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01006464 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006465 goto err;
6466
6467 buf += skip;
6468 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
6469 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
6470 goto err;
6471
6472 *dcid = buf;
6473
6474 return 1;
6475
6476 err:
6477 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
6478 return 0;
6479}
6480
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02006481/* Notify the MUX layer if alive about an imminent close of <qc>. */
6482void qc_notify_close(struct quic_conn *qc)
6483{
6484 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
6485 return;
6486
6487 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
6488
6489 /* wake up the MUX */
6490 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
6491 qc->conn->mux->wake(qc->conn);
6492}
6493
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006494/*
6495 * Local variables:
6496 * c-indent-level: 8
6497 * c-basic-offset: 8
6498 * End:
6499 */