blob: 1341dc91f58c20025e478cb91759f6ae9905dc40 [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écaillea89659a2022-05-19 11:58:53 +020048#include <haproxy/quic_stats-t.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020049#include <haproxy/quic_stream.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020050#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010051#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010052#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010053#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010054#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écaille48fc74a2021-09-03 16:42:19 +020068/* This is the values of some QUIC transport parameters when absent.
69 * Should be used to initialize any transport parameters (local or remote)
70 * before updating them with customized values.
71 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010072struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020073 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020074 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
75 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020076 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010077};
78
79/* trace source and events */
80static void quic_trace(enum trace_level level, uint64_t mask, \
81 const struct trace_source *src,
82 const struct ist where, const struct ist func,
83 const void *a1, const void *a2, const void *a3, const void *a4);
84
85static const struct trace_event quic_trace_events[] = {
86 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
87 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
88 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
89 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
90 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
91 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
92 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050093 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010094 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
95 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
96 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaille00e24002022-02-18 17:13:45 +010097 { .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 +010098 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
99 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
100 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
101 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
102 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
103 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
104 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
105 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
106 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
107 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
108 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
109 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
110 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
111 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
112 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100113 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
114 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
115 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
116 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
117 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
118 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100119 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200120 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
121 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100122 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
123 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100124 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200125 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200126 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100127 { /* end */ }
128};
129
130static const struct name_desc quic_trace_lockon_args[4] = {
131 /* arg1 */ { /* already used by the connection */ },
132 /* arg2 */ { .name="quic", .desc="QUIC transport" },
133 /* arg3 */ { },
134 /* arg4 */ { }
135};
136
137static const struct name_desc quic_trace_decoding[] = {
138#define QUIC_VERB_CLEAN 1
139 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
140 { /* end */ }
141};
142
143
144struct trace_source trace_quic = {
145 .name = IST("quic"),
146 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100147 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100148 .default_cb = quic_trace,
149 .known_events = quic_trace_events,
150 .lockon_args = quic_trace_lockon_args,
151 .decoding = quic_trace_decoding,
152 .report_events = ~0, /* report everything by default */
153};
154
155#define TRACE_SOURCE &trace_quic
156INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
157
158static BIO_METHOD *ha_quic_meth;
159
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200160DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100161DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200163 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165DECLARE_POOL(pool_head_quic_connection_id,
166 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100167DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100168DECLARE_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 +0100169DECLARE_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 +0100170DECLARE_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 +0100171DECLARE_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 +0200172DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100173DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100174
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200175static 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 +0100176 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100177 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200178 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100179static 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 +0100180static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100181static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +0200182static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
183static int quic_conn_init_timer(struct quic_conn *qc);
184static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100185
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100186/* Only for debug purpose */
187struct enc_debug_info {
188 unsigned char *payload;
189 size_t payload_len;
190 unsigned char *aad;
191 size_t aad_len;
192 uint64_t pn;
193};
194
195/* Initializes a enc_debug_info struct (only for debug purpose) */
196static inline void enc_debug_info_init(struct enc_debug_info *edi,
197 unsigned char *payload, size_t payload_len,
198 unsigned char *aad, size_t aad_len, uint64_t pn)
199{
200 edi->payload = payload;
201 edi->payload_len = payload_len;
202 edi->aad = aad;
203 edi->aad_len = aad_len;
204 edi->pn = pn;
205}
206
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207/* Trace callback for QUIC.
208 * These traces always expect that arg1, if non-null, is of type connection.
209 */
210static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
211 const struct ist where, const struct ist func,
212 const void *a1, const void *a2, const void *a3, const void *a4)
213{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100214 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100216 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100217 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100218
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100219 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100220 if ((mask & QUIC_EV_CONN_INIT) && qc) {
221 chunk_appendf(&trace_buf, "\n odcid");
222 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100223 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100224 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100225 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100226 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100227 }
228
229 if (mask & QUIC_EV_CONN_ADDDATA) {
230 const enum ssl_encryption_level_t *level = a2;
231 const size_t *len = a3;
232
233 if (level) {
234 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
235
236 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
237 }
238 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100239 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100240 }
241 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
242 /* Initial read & write secrets. */
243 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
244 const unsigned char *rx_sec = a2;
245 const unsigned char *tx_sec = a3;
246
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100247 tls_ctx = &qc->els[level].tls_ctx;
248 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100249 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
250 if (rx_sec)
251 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100252 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100253 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
254 if (tx_sec)
255 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100256 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100257 }
258 }
259 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
260 const enum ssl_encryption_level_t *level = a2;
261 const unsigned char *secret = a3;
262 const size_t *secret_len = a4;
263
264 if (level) {
265 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
266
267 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
268 if (secret && secret_len)
269 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100270 tls_ctx = &qc->els[lvl].tls_ctx;
271 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
272 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100273 }
274 }
275
276 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
277 const enum ssl_encryption_level_t *level = a2;
278 const unsigned char *secret = a3;
279 const size_t *secret_len = a4;
280
281 if (level) {
282 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
283
284 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
285 if (secret && secret_len)
286 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100287 tls_ctx = &qc->els[lvl].tls_ctx;
288 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
289 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 }
291
292 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100293
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200294 if (mask & QUIC_EV_CONN_FRMLIST) {
295 const struct list *l = a2;
296
297 if (l) {
298 const struct quic_frame *frm;
299 list_for_each_entry(frm, l, list)
300 chunk_frm_appendf(&trace_buf, frm);
301 }
302 }
303
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100304 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100305 const struct quic_tx_packet *pkt = a2;
306 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100307 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100308
309 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100310 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100312 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100313 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100314 (unsigned long long)qc->path->cwnd,
315 (unsigned long long)qc->path->prep_in_flight,
316 (unsigned long long)qc->path->in_flight,
317 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100318 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 }
320 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200321 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100322 if (pkt->pn_node.key != (uint64_t)-1)
323 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100324 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100325 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100326 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
327 (unsigned long long)qc->rx.bytes,
328 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100329 }
330
331 if (room) {
332 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
333 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
334 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100335 }
336 }
337
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100338 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100339 const enum quic_handshake_state *state = a2;
340 const int *err = a3;
341
342 if (state)
343 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
344 if (err)
345 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
346 }
347
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100348 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100349 const struct quic_rx_packet *pkt = a2;
350 const unsigned long *pktlen = a3;
351 const SSL *ssl = a4;
352
353 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200354 chunk_appendf(&trace_buf, " pkt@%p", pkt);
355 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
356 chunk_appendf(&trace_buf, " kp=%d",
357 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
358 chunk_appendf(&trace_buf, " el=%c",
359 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100360 if (pkt->pnl)
361 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
362 (unsigned long long)pkt->pn);
363 if (pkt->token_len)
364 chunk_appendf(&trace_buf, " toklen=%llu",
365 (unsigned long long)pkt->token_len);
366 if (pkt->aad_len)
367 chunk_appendf(&trace_buf, " aadlen=%llu",
368 (unsigned long long)pkt->aad_len);
369 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
370 pkt->flags, (unsigned long long)pkt->len);
371 }
372 if (pktlen)
373 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
374 if (ssl) {
375 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
376 chunk_appendf(&trace_buf, " el=%c",
377 quic_enc_level_char(ssl_to_quic_enc_level(level)));
378 }
379 }
380
381 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
382 const struct quic_rx_packet *pkt = a2;
383 const struct quic_rx_crypto_frm *cf = a3;
384 const SSL *ssl = a4;
385
386 if (pkt)
387 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
388 quic_packet_type_enc_level_char(pkt->type),
389 (unsigned long long)pkt->pn);
390 if (cf)
391 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
392 (unsigned long long)cf->offset_node.key,
393 (unsigned long long)cf->len);
394 if (ssl) {
395 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200396 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 quic_enc_level_char(ssl_to_quic_enc_level(level)));
398 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100399
400 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100401 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100402 }
403
404 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
405 const struct quic_frame *frm = a2;
406
407 if (frm)
408 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
409 }
410
411 if (mask & QUIC_EV_CONN_PHPKTS) {
412 const struct quic_enc_level *qel = a2;
413
414 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100415 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100416 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100417 " 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 +0100418 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200419 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200420 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100421 (unsigned long long)qc->path->cwnd,
422 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100423 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100424 (unsigned long long)pktns->tx.in_flight,
425 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100426 }
427 }
428
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100429 if (mask & QUIC_EV_CONN_ENCPKT) {
430 const struct enc_debug_info *edi = a2;
431
432 if (edi)
433 chunk_appendf(&trace_buf,
434 " payload=@%p payload_len=%llu"
435 " aad=@%p aad_len=%llu pn=%llu",
436 edi->payload, (unsigned long long)edi->payload_len,
437 edi->aad, (unsigned long long)edi->aad_len,
438 (unsigned long long)edi->pn);
439 }
440
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100441 if (mask & QUIC_EV_CONN_RMHP) {
442 const struct quic_rx_packet *pkt = a2;
443
444 if (pkt) {
445 const int *ret = a3;
446
447 chunk_appendf(&trace_buf, " pkt@%p", pkt);
448 if (ret && *ret)
449 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
450 pkt->pnl, (unsigned long long)pkt->pn);
451 }
452 }
453
454 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200455 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100456 const unsigned long *val1 = a3;
457 const unsigned long *val2 = a4;
458
459 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100460 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100461 if (val1)
462 chunk_appendf(&trace_buf, " %lu", *val1);
463 if (val2)
464 chunk_appendf(&trace_buf, "..%lu", *val2);
465 }
466
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100467 if (mask & QUIC_EV_CONN_ACKSTRM) {
468 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200469 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100470
471 if (s)
472 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200473 if (stream)
474 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100475 }
476
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100477 if (mask & QUIC_EV_CONN_RTTUPDT) {
478 const unsigned int *rtt_sample = a2;
479 const unsigned int *ack_delay = a3;
480 const struct quic_loss *ql = a4;
481
482 if (rtt_sample)
483 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
484 if (ack_delay)
485 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
486 if (ql)
487 chunk_appendf(&trace_buf,
488 " srtt=%ums rttvar=%ums min_rtt=%ums",
489 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
490 }
491 if (mask & QUIC_EV_CONN_CC) {
492 const struct quic_cc_event *ev = a2;
493 const struct quic_cc *cc = a3;
494
495 if (a2)
496 quic_cc_event_trace(&trace_buf, ev);
497 if (a3)
498 quic_cc_state_trace(&trace_buf, cc);
499 }
500
501 if (mask & QUIC_EV_CONN_PKTLOSS) {
502 const struct quic_pktns *pktns = a2;
503 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100504
505 if (pktns) {
506 chunk_appendf(&trace_buf, " pktns=%s",
507 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
508 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
509 if (pktns->tx.loss_time)
510 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100511 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512 }
513 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
514 struct quic_tx_packet *pkt;
515
516 chunk_appendf(&trace_buf, " lost_pkts:");
517 list_for_each_entry(pkt, lost_pkts, list)
518 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
519 }
520 }
521
522 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100523 const struct quic_pktns *pktns = a2;
524 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100525 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100527 if (ifae_pkts)
528 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
529 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100531 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100533 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
534 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100535 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
536 if (pktns->tx.in_flight)
537 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100538 if (pktns->tx.loss_time)
539 chunk_appendf(&trace_buf, " loss_time=%dms",
540 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
541 }
542 if (mask & QUIC_EV_CONN_SPTO) {
543 if (pktns->tx.time_of_last_eliciting)
544 chunk_appendf(&trace_buf, " tole=%dms",
545 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
546 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100547 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100548 }
549 }
550
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200551 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100552 chunk_appendf(&trace_buf,
553 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
554 }
555 }
556
557 if (mask & QUIC_EV_CONN_SPPKTS) {
558 const struct quic_tx_packet *pkt = a2;
559
Frédéric Lécaille8726d632022-05-03 10:32:21 +0200560 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 +0100561 (unsigned long long)qc->path->cwnd,
562 (unsigned long long)qc->path->prep_in_flight,
563 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100564 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100565 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100566 (unsigned long)pkt->pn_node.key,
567 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
568 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100569 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100570 }
571 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100572
573 if (mask & QUIC_EV_CONN_SSLALERT) {
574 const uint8_t *alert = a2;
575 const enum ssl_encryption_level_t *level = a3;
576
577 if (alert)
578 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
579 if (level)
580 chunk_appendf(&trace_buf, " el=%c",
581 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
582 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100583
584 if (mask & QUIC_EV_CONN_BCFRMS) {
585 const size_t *sz1 = a2;
586 const size_t *sz2 = a3;
587 const size_t *sz3 = a4;
588
589 if (sz1)
590 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
591 if (sz2)
592 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
593 if (sz3)
594 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
595 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100596
597 if (mask & QUIC_EV_CONN_PSTRM) {
598 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100599
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100600 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100601 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100602 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100603 }
604 if (mask & QUIC_EV_CONN_LPKT) {
605 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200606 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100607
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100608 if (pkt) {
609 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
610 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
611 if (pkt->pn_node.key != (uint64_t)-1)
612 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
613 }
614
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200615 if (len)
616 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100617 }
618
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200619 if (mask & QUIC_EV_STATELESS_RST) {
620 const struct quic_cid *cid = a2;
621
622 if (cid)
623 quic_cid_dump(&trace_buf, cid);
624 }
625
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626}
627
628/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100629static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100630{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200631 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100633 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100634 return 1;
635
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200636 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
637 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200638 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
639 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200640 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100641 return 1;
642
643 return 0;
644}
645
646/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
647 * both loss detection and PTO and schedule the task assiated to this timer if needed.
648 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100649static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100650{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100651 struct quic_pktns *pktns;
652 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200653 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100654
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100655 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100656 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100657
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100658 pktns = quic_loss_pktns(qc);
659 if (tick_isset(pktns->tx.loss_time)) {
660 qc->timer = pktns->tx.loss_time;
661 goto out;
662 }
663
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100664 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100665 * cancelled for a server which reached the anti-amplification limit.
666 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100667 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200668 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100669 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100670 qc->timer = TICK_ETERNITY;
671 goto out;
672 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100673
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100674 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100675 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100676 /* Timer cancellation. */
677 qc->timer = TICK_ETERNITY;
678 goto out;
679 }
680
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200681 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200682 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100683 if (tick_isset(pto))
684 qc->timer = pto;
685 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100686 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100687 if (tick_is_expired(qc->timer, now_ms)) {
688 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100689 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100690 }
691 else {
692 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100693 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100694 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100695 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100696 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100697}
698
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100699/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
700 * connection.
701 * Return 1 if succeeded, 0 if not.
702 */
703static int quic_tls_key_update(struct quic_conn *qc)
704{
705 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
706 struct quic_tls_secrets *rx, *tx;
707 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
708 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
709
710 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
711 rx = &tls_ctx->rx;
712 tx = &tls_ctx->tx;
713 nxt_rx = &qc->ku.nxt_rx;
714 nxt_tx = &qc->ku.nxt_tx;
715
716 /* Prepare new RX secrets */
717 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
718 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100719 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100720 return 0;
721 }
722
723 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
724 nxt_rx->key, nxt_rx->keylen,
725 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
726 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100727 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100728 return 0;
729 }
730
731 /* Prepare new TX secrets */
732 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
733 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100734 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100735 return 0;
736 }
737
738 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
739 nxt_tx->key, nxt_tx->keylen,
740 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
741 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100742 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100743 return 0;
744 }
745
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200746 if (nxt_rx->ctx) {
747 EVP_CIPHER_CTX_free(nxt_rx->ctx);
748 nxt_rx->ctx = NULL;
749 }
750
751 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
752 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
753 return 0;
754 }
755
756 if (nxt_tx->ctx) {
757 EVP_CIPHER_CTX_free(nxt_tx->ctx);
758 nxt_tx->ctx = NULL;
759 }
760
761 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
762 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
763 return 0;
764 }
765
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100766 return 1;
767}
768
769/* Rotate the Key Update information for <qc> QUIC connection.
770 * Must be used after having updated them.
771 * Always succeeds.
772 */
773static void quic_tls_rotate_keys(struct quic_conn *qc)
774{
775 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
776 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200777 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100778
779 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200780 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100781 curr_secret = tls_ctx->rx.secret;
782 curr_iv = tls_ctx->rx.iv;
783 curr_key = tls_ctx->rx.key;
784
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200785 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100786 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
787 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
788 tls_ctx->rx.key = qc->ku.nxt_rx.key;
789
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200790 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100791 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
792 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
793 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
794
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200795 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100796 qc->ku.prv_rx.secret = curr_secret;
797 qc->ku.prv_rx.iv = curr_iv;
798 qc->ku.prv_rx.key = curr_key;
799 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
800
801 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200802 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100803 curr_secret = tls_ctx->tx.secret;
804 curr_iv = tls_ctx->tx.iv;
805 curr_key = tls_ctx->tx.key;
806
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200807 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100808 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
809 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
810 tls_ctx->tx.key = qc->ku.nxt_tx.key;
811
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200812 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100813 qc->ku.nxt_tx.secret = curr_secret;
814 qc->ku.nxt_tx.iv = curr_iv;
815 qc->ku.nxt_tx.key = curr_key;
816}
817
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100818#ifndef OPENSSL_IS_BORINGSSL
819int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
820 const uint8_t *read_secret,
821 const uint8_t *write_secret, size_t secret_len)
822{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100823 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
824 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 +0100825 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100826 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100827
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100828 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100829 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200830 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100831 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100832 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100833 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100834
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100835 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100836 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100837 goto err;
838 }
839
840 rx = &tls_ctx->rx;
841 tx = &tls_ctx->tx;
842
843 rx->aead = tx->aead = tls_aead(cipher);
844 rx->md = tx->md = tls_md(cipher);
845 rx->hp = tx->hp = tls_hp(cipher);
846
847 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
848 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100849 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100850 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100851 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100852 }
853
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200854 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
855 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
856 goto err;
857 }
858
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100859 /* Enqueue this connection asap if we could derive O-RTT secrets as
860 * listener. Note that a listener derives only RX secrets for this
861 * level.
862 */
863 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
864 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100865
866 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100867 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100868
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100869 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
870 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100871 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100872 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100873 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100874 }
875
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200876 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
877 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
878 goto err;
879 }
880
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100881 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100882 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
883 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
884 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
885
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200886 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100887 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
888 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100889 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100890 goto err;
891 }
892
893 memcpy(rx->secret, read_secret, secret_len);
894 rx->secretlen = secret_len;
895 memcpy(tx->secret, write_secret, secret_len);
896 tx->secretlen = secret_len;
897 /* Initialize all the secret keys lengths */
898 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
899 /* Prepare the next key update */
900 if (!quic_tls_key_update(qc))
901 goto err;
902 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100903
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100904 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100905 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100906 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100907 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100908 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100909
910 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100911 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100912 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100913}
914#else
915/* ->set_read_secret callback to derive the RX secrets at <level> encryption
916 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500917 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100918 */
919int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
920 const SSL_CIPHER *cipher,
921 const uint8_t *secret, size_t secret_len)
922{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100923 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100924 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100925 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100926
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100927 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200928 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100929 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100930 goto out;
931 }
932
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100933 tls_ctx->rx.aead = tls_aead(cipher);
934 tls_ctx->rx.md = tls_md(cipher);
935 tls_ctx->rx.hp = tls_hp(cipher);
936
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100937 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
938 goto err;
939
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100940 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 +0100941 tls_ctx->rx.key, tls_ctx->rx.keylen,
942 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100943 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
944 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100945 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100946 goto err;
947 }
948
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100949 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100950 const unsigned char *buf;
951 size_t buflen;
952
953 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
954 if (!buflen)
955 goto err;
956
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100957 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100958 goto err;
959 }
960
961 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100962 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100963 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100964
965 return 1;
966
967 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100968 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100969 return 0;
970}
971
972/* ->set_write_secret callback to derive the TX secrets at <level>
973 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500974 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100975 */
976int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
977 const SSL_CIPHER *cipher,
978 const uint8_t *secret, size_t secret_len)
979{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100980 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
981 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 +0100982
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100983 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200984 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100985 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100986 goto out;
987 }
988
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100989 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
990 goto err;
991
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100992 tls_ctx->tx.aead = tls_aead(cipher);
993 tls_ctx->tx.md = tls_md(cipher);
994 tls_ctx->tx.hp = tls_hp(cipher);
995
996 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 +0100997 tls_ctx->tx.key, tls_ctx->tx.keylen,
998 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100999 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
1000 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001001 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001002 goto err;
1003 }
1004
1005 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001006 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001007 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001008 return 1;
1009
1010 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001011 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001012 return 0;
1013}
1014#endif
1015
1016/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1017 * with <len> as size in CRYPTO buffers dedicated to store the information about
1018 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1019 * It fails only if it could not managed to allocate enough CRYPTO buffers to
1020 * store all the data.
1021 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1022 */
1023static int quic_crypto_data_cpy(struct quic_enc_level *qel,
1024 const unsigned char *data, size_t len)
1025{
1026 struct quic_crypto_buf **qcb;
1027 /* The remaining byte to store in CRYPTO buffers. */
1028 size_t cf_offset, cf_len, *nb_buf;
1029 unsigned char *pos;
1030
1031 nb_buf = &qel->tx.crypto.nb_buf;
1032 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1033 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1034 cf_len = len;
1035
1036 while (len) {
1037 size_t to_copy, room;
1038
1039 pos = (*qcb)->data + (*qcb)->sz;
1040 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1041 to_copy = len > room ? room : len;
1042 if (to_copy) {
1043 memcpy(pos, data, to_copy);
1044 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1045 qel->tx.crypto.sz += to_copy;
1046 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001047 len -= to_copy;
1048 data += to_copy;
1049 }
1050 else {
1051 struct quic_crypto_buf **tmp;
1052
1053 tmp = realloc(qel->tx.crypto.bufs,
1054 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1055 if (tmp) {
1056 qel->tx.crypto.bufs = tmp;
1057 qcb = &qel->tx.crypto.bufs[*nb_buf];
1058 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1059 if (!*qcb)
1060 return 0;
1061
1062 (*qcb)->sz = 0;
1063 ++*nb_buf;
1064 }
1065 else {
1066 break;
1067 }
1068 }
1069 }
1070
1071 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1072 * have been buffered.
1073 */
1074 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001075 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001076 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001077
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001078 /* There is at most one CRYPTO frame in this packet number
1079 * space. Let's look for it.
1080 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001081 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001082 if (frm->type != QUIC_FT_CRYPTO)
1083 continue;
1084
1085 /* Found */
1086 found = frm;
1087 break;
1088 }
1089
1090 if (found) {
1091 found->crypto.len += cf_len;
1092 }
1093 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001094 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001095 if (!frm)
1096 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001097
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001098 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001099 frm->type = QUIC_FT_CRYPTO;
1100 frm->crypto.offset = cf_offset;
1101 frm->crypto.len = cf_len;
1102 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001103 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001104 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001105 }
1106
1107 return len == 0;
1108}
1109
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001110/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1111 * activity for <qc> will be interrupted.
1112 */
1113void quic_set_connection_close(struct quic_conn *qc, int err)
1114{
1115 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1116 return;
1117
1118 qc->err_code = err;
1119 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
1120}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001121
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001122/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1123void quic_set_tls_alert(struct quic_conn *qc, int alert)
1124{
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02001125 HA_ATOMIC_DEC(&qc->prx_counters->conn_opening);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001126 quic_set_connection_close(qc, QC_ERR_CRYPTO_ERROR | alert);
1127 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001128 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001129}
1130
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001131/* Set the application for <qc> QUIC connection.
1132 * Return 1 if succeeded, 0 if not.
1133 */
1134int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1135{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001136 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1137 qc->app_ops = &h3_ops;
1138 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1139 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001140 else
1141 return 0;
1142
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001143 return 1;
1144}
1145
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001146/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1147 * wants to provide the QUIC layer with CRYPTO data.
1148 * Returns 1 if succeeded, 0 if not.
1149 */
1150int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1151 const uint8_t *data, size_t len)
1152{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001153 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001154 enum quic_tls_enc_level tel;
1155 struct quic_enc_level *qel;
1156
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001157 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1158 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001159 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001160 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001161 goto out;
1162 }
1163
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001164 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001165 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001166 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001167 goto err;
1168 }
1169
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001170 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001172 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001173 goto err;
1174 }
1175
1176 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001177 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001178
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001179 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001180 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001181 return 1;
1182
1183 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001184 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001185 return 0;
1186}
1187
1188int ha_quic_flush_flight(SSL *ssl)
1189{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001190 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001191
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001192 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1193 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001194
1195 return 1;
1196}
1197
1198int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1199{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001200 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001201
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001202 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1203 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001204 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001205 return 1;
1206}
1207
1208/* QUIC TLS methods */
1209static SSL_QUIC_METHOD ha_quic_method = {
1210#ifdef OPENSSL_IS_BORINGSSL
1211 .set_read_secret = ha_set_rsec,
1212 .set_write_secret = ha_set_wsec,
1213#else
1214 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1215#endif
1216 .add_handshake_data = ha_quic_add_handshake_data,
1217 .flush_flight = ha_quic_flush_flight,
1218 .send_alert = ha_quic_send_alert,
1219};
1220
1221/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1222 * Returns an error count.
1223 */
1224int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1225{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001226 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1227 int cfgerr = 0;
1228
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001229 long options =
1230 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1231 SSL_OP_SINGLE_ECDH_USE |
1232 SSL_OP_CIPHER_SERVER_PREFERENCE;
1233 SSL_CTX *ctx;
1234
1235 ctx = SSL_CTX_new(TLS_server_method());
1236 bind_conf->initial_ctx = ctx;
1237
1238 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001239 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1240 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1241 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001242
1243#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1244#ifdef OPENSSL_IS_BORINGSSL
1245 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1246 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1247#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1248 if (bind_conf->ssl_conf.early_data) {
1249 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001250 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 }
1252 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1253 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1254#else
1255 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1256#endif
1257 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1258#endif
1259 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1260
1261 return cfgerr;
1262}
1263
1264/* Decode an expected packet number from <truncated_on> its truncated value,
1265 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1266 * the number of bits used to encode this packet number (its length in bytes * 8).
1267 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1268 */
1269static uint64_t decode_packet_number(uint64_t largest_pn,
1270 uint32_t truncated_pn, unsigned int pn_nbits)
1271{
1272 uint64_t expected_pn = largest_pn + 1;
1273 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1274 uint64_t pn_hwin = pn_win / 2;
1275 uint64_t pn_mask = pn_win - 1;
1276 uint64_t candidate_pn;
1277
1278
1279 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1280 /* Note that <pn_win> > <pn_hwin>. */
1281 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1282 candidate_pn + pn_hwin <= expected_pn)
1283 return candidate_pn + pn_win;
1284
1285 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1286 return candidate_pn - pn_win;
1287
1288 return candidate_pn;
1289}
1290
1291/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1292 * cryptographic context.
1293 * <largest_pn> is the largest received packet number and <pn> the address of
1294 * the packet number field for this packet with <byte0> address of its first byte.
1295 * <end> points to one byte past the end of this packet.
1296 * Returns 1 if succeeded, 0 if not.
1297 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001298static int qc_do_rm_hp(struct quic_conn *qc,
1299 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001301 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001302{
1303 int ret, outlen, i, pnlen;
1304 uint64_t packet_number;
1305 uint32_t truncated_pn = 0;
1306 unsigned char mask[5] = {0};
1307 unsigned char *sample;
1308 EVP_CIPHER_CTX *cctx;
1309 unsigned char *hp_key;
1310
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001311 /* Check there is enough data in this packet. */
1312 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001313 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314 return 0;
1315 }
1316
1317 cctx = EVP_CIPHER_CTX_new();
1318 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001319 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001320 return 0;
1321 }
1322
1323 ret = 0;
1324 sample = pn + QUIC_PACKET_PN_MAXLEN;
1325
1326 hp_key = tls_ctx->rx.hp_key;
1327 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1328 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1329 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001330 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001331 goto out;
1332 }
1333
1334 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1335 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1336 for (i = 0; i < pnlen; i++) {
1337 pn[i] ^= mask[i + 1];
1338 truncated_pn = (truncated_pn << 8) | pn[i];
1339 }
1340
1341 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1342 /* Store remaining information for this unprotected header */
1343 pkt->pn = packet_number;
1344 pkt->pnl = pnlen;
1345
1346 ret = 1;
1347
1348 out:
1349 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001350
1351 return ret;
1352}
1353
1354/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1355 * address, with <payload_len> as payload length, <aad> as address of
1356 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1357 * context.
1358 * Returns 1 if succeeded, 0 if not.
1359 */
1360static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1361 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001362 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001363{
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001364 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001365 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001366 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001367 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001368
1369 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001370 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001371 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001372 }
1373
1374 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001375 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001376 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001377 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001378 }
1379
1380 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001381
1382 err:
1383 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001384 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001385 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001386}
1387
1388/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1389 * Returns 1 if succeeded, 0 if not.
1390 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001391static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001392{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001393 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001394 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001395 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001396 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001397 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001398 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1399 unsigned char *rx_key = tls_ctx->rx.key;
1400
1401 kp_changed = 0;
1402 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1403 /* The two tested bits are not at the same position,
1404 * this is why they are first both inversed.
1405 */
1406 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1407 if (pkt->pn < tls_ctx->rx.pn) {
1408 /* The lowest packet number of a previous key phase
1409 * cannot be null if it really stores previous key phase
1410 * secrets.
1411 */
1412 if (!pkt->qc->ku.prv_rx.pn)
1413 return 0;
1414
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001415 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1416 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001417 rx_key = pkt->qc->ku.prv_rx.key;
1418 }
1419 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1420 /* Next key phase */
1421 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001422 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1423 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001424 rx_key = pkt->qc->ku.nxt_rx.key;
1425 }
1426 }
1427 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001428
1429 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1430 return 0;
1431
1432 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1433 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001434 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001435 if (!ret)
1436 return 0;
1437
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001438 /* Update the keys only if the packet decryption succeeded. */
1439 if (kp_changed) {
1440 quic_tls_rotate_keys(pkt->qc);
1441 /* Toggle the Key Phase bit */
1442 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1443 /* Store the lowest packet number received for the current key phase */
1444 tls_ctx->rx.pn = pkt->pn;
1445 /* Prepare the next key update */
1446 if (!quic_tls_key_update(pkt->qc))
1447 return 0;
1448 }
1449
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001450 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001451 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001452
1453 return 1;
1454}
1455
Frédéric Lécaille96367152022-04-25 09:40:19 +02001456
1457/* Remove references to <frm> frame */
1458static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1459{
1460 uint64_t pn;
1461 struct quic_frame *f, *tmp;
1462
1463 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1464 pn = f->pkt->pn_node.key;
1465 f->origin = NULL;
1466 LIST_DELETE(&f->ref);
1467 TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1468 }
1469}
1470
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001471/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001472void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001473{
1474 uint64_t pn;
1475 struct quic_frame *origin, *f, *tmp;
1476
1477 /* Identify this frame: a frame copy or one of its copies */
1478 origin = frm->origin ? frm->origin : frm;
1479 /* Ensure the source of the copies is flagged as acked, <frm> being
1480 * possibly a copy of <origin>
1481 */
1482 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1483 /* Mark all the copy of <origin> as acknowledged. We must
1484 * not release the packets (releasing the frames) at this time as
1485 * they are possibly also to be acknowledged alongside the
1486 * the current one.
1487 */
1488 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1489 pn = f->pkt->pn_node.key;
1490 TRACE_PROTO("mark frame as acked from packet",
1491 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1492 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1493 f->origin = NULL;
1494 LIST_DELETE(&f->ref);
1495 }
1496 LIST_DELETE(&frm->list);
1497 pn = frm->pkt->pn_node.key;
1498 quic_tx_packet_refdec(frm->pkt);
1499 TRACE_PROTO("freeing frame from packet",
1500 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1501 pool_free(pool_head_quic_frame, frm);
1502}
1503
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001504/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001505 *
1506 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001507 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001508static int quic_stream_try_to_consume(struct quic_conn *qc,
1509 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001510{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001511 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001512 struct eb64_node *frm_node;
1513
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001514 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001515 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001516 while (frm_node) {
1517 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001518 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001519 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001520
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001521 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001522 offset = strm->offset.key;
1523 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001524
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001525 if (offset > stream->ack_offset)
1526 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001527
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001528 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001529 /* cf. next comment : frame may be freed at this stage. */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001530 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001531 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001532 ret = 1;
1533 }
1534
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001535 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1536 * has been freed. with the stream frames tree. Nothing to do
1537 * anymore in here.
1538 */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001539 if (!stream)
1540 return 1;
1541
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001542 frm_node = eb64_next(frm_node);
1543 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001544
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001545 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001546 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001547 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001548
1549 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001550}
1551
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001552/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001553static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1554 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001555{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001556 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001557
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001558 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001559 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001560 switch (frm->type) {
1561 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1562 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001563 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001564 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001565 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001566 const size_t offset = strm_frm->offset.key;
1567 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001568
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001569 /* do not use strm_frm->stream as the qc_stream_desc instance
1570 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001571 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001572 *
1573 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001574 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001575 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001576 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1577 if (!node) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001578 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001579 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001580 /* early return */
1581 return;
1582 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001583 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001584
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001585 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001586 if (offset <= stream->ack_offset) {
1587 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001588 stream_acked = 1;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001589 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1590 qc, strm_frm, stream);
1591 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001592
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001593 if (!stream) {
1594 /* no need to continue if stream freed. */
1595 TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001596 qc_release_frm(qc, frm);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001597 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001598 }
1599
Frédéric Lécailleda342552022-04-25 10:28:49 +02001600 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1601 qc, strm_frm, stream);
1602 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001603 }
1604 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001605 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001606 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001607
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001608 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001609 }
1610 break;
1611 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001612 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001613 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001614
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001615 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001616 struct qcc *qcc = qc->qcc;
1617
1618 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1619 tasklet_wakeup(qcc->subs->tasklet);
1620 qcc->subs->events &= ~SUB_RETRY_SEND;
1621 if (!qcc->subs->events)
1622 qcc->subs = NULL;
1623 }
1624 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001625}
1626
1627/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1628 * deallocating them, and their TX frames.
1629 * Returns the last node reached to be used for the next range.
1630 * May be NULL if <largest> node could not be found.
1631 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001632static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1633 struct eb_root *pkts,
1634 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001635 struct list *newly_acked_pkts,
1636 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001637 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001638{
1639 struct eb64_node *node;
1640 struct quic_tx_packet *pkt;
1641
1642 if (largest_node)
1643 node = largest_node;
1644 else {
1645 node = eb64_lookup(pkts, largest);
1646 while (!node && largest > smallest) {
1647 node = eb64_lookup(pkts, --largest);
1648 }
1649 }
1650
1651 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001652 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001654 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001655 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001656 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001657 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001658 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001659 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001660 node = eb64_prev(node);
1661 eb64_delete(&pkt->pn_node);
1662 }
1663
1664 return node;
1665}
1666
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001667/* Remove all frames from <pkt_frm_list> and reinsert them in the
1668 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001669 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001670static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001671 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001672 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001673{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001674 struct quic_frame *frm, *frmbak;
1675 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001676 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001677
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001678 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001679 /* Only for debug */
1680 uint64_t pn;
1681
1682 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001683 LIST_DELETE(&frm->list);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001684 pn = frm->pkt->pn_node.key;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001685 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001686 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001687 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001688 /* Remove any reference to this frame */
1689 qc_frm_unref(qc, frm);
1690 switch (frm->type) {
1691 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1692 {
1693 struct quic_stream *strm_frm = &frm->stream;
1694 struct eb64_node *node = NULL;
1695 struct qc_stream_desc *stream_desc;
1696
1697 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1698 if (!node) {
1699 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm);
1700 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1701 qc, frm, &pn);
1702 pool_free(pool_head_quic_frame, frm);
1703 continue;
1704 }
1705
1706 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1707 /* Do not resend this frame if in the "already acked range" */
1708 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1709 TRACE_PROTO("ignored frame in already acked range",
1710 QUIC_EV_CONN_PRSAFRM, qc, frm);
1711 continue;
1712 }
1713 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1714 strm_frm->offset.key = stream_desc->ack_offset;
1715 TRACE_PROTO("updated partially acked frame",
1716 QUIC_EV_CONN_PRSAFRM, qc, frm);
1717 }
1718
1719 break;
1720 }
1721
1722 default:
1723 break;
1724 }
1725
1726 /* Do not resend probing packet with old data */
1727 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1728 TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1729 qc, frm, &pn);
1730 if (frm->origin)
1731 LIST_DELETE(&frm->ref);
1732 pool_free(pool_head_quic_frame, frm);
1733 continue;
1734 }
1735
1736 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1737 TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1738 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1739 qc, frm, &pn);
1740 pool_free(pool_head_quic_frame, frm);
1741 }
1742 else {
1743 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001744 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1745 /* Mark this STREAM frame as lost. A look up their stream descriptor
1746 * will be performed to check the stream is not consumed or released.
1747 */
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001748 frm->flags = QUIC_FL_TX_FRAME_LOST;
1749 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001750 LIST_APPEND(&tmp, &frm->list);
1751 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001752 }
1753
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001754 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001755}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001756
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001757/* Free <pkt> TX packet and its attached frames.
1758 * This is the responsability of the caller to remove this packet of
1759 * any data structure it was possibly attached to.
1760 */
1761static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1762{
1763 struct quic_frame *frm, *frmbak;
1764
1765 if (!pkt)
1766 return;
1767
1768 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1769 LIST_DELETE(&frm->list);
1770 pool_free(pool_head_quic_frame, frm);
1771 }
1772 pool_free(pool_head_quic_tx_packet, pkt);
1773}
1774
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775/* Free the TX packets of <pkts> list */
1776static inline void free_quic_tx_pkts(struct list *pkts)
1777{
1778 struct quic_tx_packet *pkt, *tmp;
1779
1780 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001781 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001782 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001783 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001784 }
1785}
1786
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001787/* Remove already sent ranges of acknowledged packet numbers from
1788 * <pktns> packet number space tree below <largest_acked_pn> possibly
1789 * updating the range which contains <largest_acked_pn>.
1790 * Never fails.
1791 */
1792static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1793 int64_t largest_acked_pn)
1794{
1795 struct eb64_node *ar, *next_ar;
1796 struct quic_arngs *arngs = &pktns->rx.arngs;
1797
1798 ar = eb64_first(&arngs->root);
1799 while (ar) {
1800 struct quic_arng_node *ar_node;
1801
1802 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001803 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001804 if ((int64_t)ar_node->first.key > largest_acked_pn)
1805 break;
1806
1807 if (largest_acked_pn < ar_node->last) {
1808 eb64_delete(ar);
1809 ar_node->first.key = largest_acked_pn + 1;
1810 eb64_insert(&arngs->root, ar);
1811 break;
1812 }
1813
1814 eb64_delete(ar);
1815 pool_free(pool_head_quic_arng, ar_node);
1816 arngs->sz--;
1817 ar = next_ar;
1818 }
1819}
1820
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001821/* Send a packet ack event nofication for each newly acked packet of
1822 * <newly_acked_pkts> list and free them.
1823 * Always succeeds.
1824 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001825static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 struct list *newly_acked_pkts)
1827{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828 struct quic_tx_packet *pkt, *tmp;
1829 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1830
1831 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1832 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001833 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001834 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001835 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001836 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001837 /* If this packet contained an ACK frame, proceed to the
1838 * acknowledging of range of acks from the largest acknowledged
1839 * packet number which was sent in an ACK frame by this packet.
1840 */
1841 if (pkt->largest_acked_pn != -1)
1842 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001843 ev.ack.acked = pkt->in_flight_len;
1844 ev.ack.time_sent = pkt->time_sent;
1845 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001846 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001847 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001848 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001849 }
1850
1851}
1852
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001853/* Release all the frames attached to <pktns> packet number space */
1854static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1855{
1856 struct quic_frame *frm, *frmbak;
1857
1858 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1859 LIST_DELETE(&frm->list);
1860 pool_free(pool_head_quic_frame, frm);
1861 }
1862}
1863
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001864/* Handle <pkts> list of lost packets detected at <now_us> handling
1865 * their TX frames.
1866 * Send a packet loss event to the congestion controller if
1867 * in flight packet have been lost.
1868 * Also frees the packet in <pkts> list.
1869 * Never fails.
1870 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001871static inline void qc_release_lost_pkts(struct quic_conn *qc,
1872 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001873 struct list *pkts,
1874 uint64_t now_us)
1875{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001876 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001877 uint64_t lost_bytes;
1878
1879 lost_bytes = 0;
1880 oldest_lost = newest_lost = NULL;
1881 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001882 struct list tmp = LIST_HEAD_INIT(tmp);
1883
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001884 lost_bytes += pkt->in_flight_len;
1885 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001886 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001887 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001888 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001889 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001890 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001891 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001892 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001893 if (!oldest_lost) {
1894 oldest_lost = newest_lost = pkt;
1895 }
1896 else {
1897 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001898 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001899 newest_lost = pkt;
1900 }
1901 }
1902
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001903 if (newest_lost) {
1904 /* Sent a congestion event to the controller */
1905 struct quic_cc_event ev = {
1906 .type = QUIC_CC_EVT_LOSS,
1907 .loss.time_sent = newest_lost->time_sent,
1908 };
1909
1910 quic_cc_event(&qc->path->cc, &ev);
1911 }
1912
1913 /* If an RTT have been already sampled, <rtt_min> has been set.
1914 * We must check if we are experiencing a persistent congestion.
1915 * If this is the case, the congestion controller must re-enter
1916 * slow start state.
1917 */
1918 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1919 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1920
1921 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1922 now_ms, qc->max_ack_delay))
1923 qc->path->cc.algo->slow_start(&qc->path->cc);
1924 }
1925
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001927 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001928 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001929 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001930 }
1931}
1932
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001933/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1934 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001935 * 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 +01001936 * acked ack-eliciting packet.
1937 * Return 1, if succeeded, 0 if not.
1938 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001939static inline int qc_parse_ack_frm(struct quic_conn *qc,
1940 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001941 struct quic_enc_level *qel,
1942 unsigned int *rtt_sample,
1943 const unsigned char **pos, const unsigned char *end)
1944{
1945 struct quic_ack *ack = &frm->ack;
1946 uint64_t smallest, largest;
1947 struct eb_root *pkts;
1948 struct eb64_node *largest_node;
1949 unsigned int time_sent, pkt_flags;
1950 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1951 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1952
1953 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1954 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001955 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 goto err;
1957 }
1958
1959 if (ack->first_ack_range > ack->largest_ack) {
1960 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001961 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001962 goto err;
1963 }
1964
1965 largest = ack->largest_ack;
1966 smallest = largest - ack->first_ack_range;
1967 pkts = &qel->pktns->tx.pkts;
1968 pkt_flags = 0;
1969 largest_node = NULL;
1970 time_sent = 0;
1971
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001972 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001973 largest_node = eb64_lookup(pkts, largest);
1974 if (!largest_node) {
1975 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001976 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001977 }
1978 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001979 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001980 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001981 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001982 }
1983
1984 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001985 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001986 do {
1987 uint64_t gap, ack_range;
1988
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001989 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1990 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001991 if (!ack->ack_range_num--)
1992 break;
1993
1994 if (!quic_dec_int(&gap, pos, end))
1995 goto err;
1996
1997 if (smallest < gap + 2) {
1998 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001999 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002000 goto err;
2001 }
2002
2003 largest = smallest - gap - 2;
2004 if (!quic_dec_int(&ack_range, pos, end))
2005 goto err;
2006
2007 if (largest < ack_range) {
2008 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002009 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002010 goto err;
2011 }
2012
2013 /* Do not use this node anymore. */
2014 largest_node = NULL;
2015 /* Next range */
2016 smallest = largest - ack_range;
2017
2018 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002019 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020 } while (1);
2021
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002022 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2023 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002024 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002025 }
2026
2027 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2028 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002029 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002030 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002031 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002032 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002033 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2034 if (quic_peer_validated_addr(qc))
2035 qc->path->loss.pto_count = 0;
2036 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 }
2038
2039
2040 return 1;
2041
2042 err:
2043 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002044 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002045 return 0;
2046}
2047
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002048/* This function gives the detail of the SSL error. It is used only
2049 * if the debug mode and the verbose mode are activated. It dump all
2050 * the SSL error until the stack was empty.
2051 */
2052static forceinline void qc_ssl_dump_errors(struct connection *conn)
2053{
2054 if (unlikely(global.mode & MODE_DEBUG)) {
2055 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002056 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002057 unsigned long ret;
2058
Willy Tarreau325fc632022-04-11 18:47:38 +02002059 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002060 ret = ERR_get_error();
2061 if (!ret)
2062 return;
2063
2064 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002065 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002066 }
2067 }
2068}
2069
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002070int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2071 const char **str, int *len);
2072
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002073/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2074 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002075 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002076 * Return 1 if succeeded, 0 if not.
2077 */
2078static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002079 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002080 const unsigned char *data, size_t len,
2081 struct quic_rx_packet *pkt,
2082 struct quic_rx_crypto_frm *cf)
2083{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002084 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002085 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002086
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002087 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002088 qc = ctx->qc;
2089
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002090 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2091
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002092 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2093 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002094 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002095 goto err;
2096 }
2097
2098 el->rx.crypto.offset += len;
2099 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002100 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002102 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002103 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002104 ssl_err = SSL_do_handshake(ctx->ssl);
2105 if (ssl_err != 1) {
2106 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2107 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2108 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002109 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002110 goto out;
2111 }
2112
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002113 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2114 HA_ATOMIC_DEC(&qc->prx_counters->conn_opening);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002115 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002116 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002117 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002118 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002119 goto err;
2120 }
2121
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002122 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002123
2124 /* Check the alpn could be negotiated */
2125 if (!qc->app_ops) {
2126 TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2127 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2128 goto err;
2129 }
2130
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002131 HA_ATOMIC_DEC(&qc->prx_counters->conn_opening);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002132 /* I/O callback switch */
2133 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002134 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002135 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002136 /* The connection is ready to be accepted. */
2137 quic_accept_push_qc(qc);
2138 }
2139 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002140 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002141 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002142 } else {
2143 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2144 if (ssl_err != 1) {
2145 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2146 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2147 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002148 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002149 goto out;
2150 }
2151
2152 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002153 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002154 goto err;
2155 }
2156
2157 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002158 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002159 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002160
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002161 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002162 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002163 return 1;
2164
2165 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002166 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002167 return 0;
2168}
2169
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002170/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2171 * streams may be open. The data are copied to the stream RX buffer if possible.
2172 * If not, the STREAM frame is stored to be treated again later.
2173 * We rely on the flow control so that not to store too much STREAM frames.
2174 * Return 1 if succeeded, 0 if not.
2175 */
2176static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2177 struct quic_stream *strm_frm,
2178 struct quic_conn *qc)
2179{
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002180 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002181
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002182 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2183 strm_frm->offset.key, strm_frm->fin,
Amaury Denoyelle3a086402022-05-18 11:38:22 +02002184 (char *)strm_frm->data);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002185
Amaury Denoyelle1290f1e2022-05-13 14:49:05 +02002186 /* frame rejected - packet must not be acknowledeged */
2187 if (ret)
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002188 return 0;
2189
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002190 return 1;
2191}
2192
2193/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2194 * streams may be open. The data are copied to the stream RX buffer if possible.
2195 * If not, the STREAM frame is stored to be treated again later.
2196 * We rely on the flow control so that not to store too much STREAM frames.
2197 * Return 1 if succeeded, 0 if not.
2198 */
2199static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2200 struct quic_stream *strm_frm,
2201 struct quic_conn *qc)
2202{
2203 struct qcs *strm;
Amaury Denoyelle3db98e92022-05-13 15:41:04 +02002204 enum ncb_ret ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002205
Amaury Denoyelle50742292022-03-29 14:57:19 +02002206 strm = qcc_get_qcs(qc->qcc, strm_frm->id);
2207 if (!strm) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002208 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002209 return 0;
2210 }
2211
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002212 if (strm_frm->offset.key < strm->rx.offset) {
2213 size_t diff;
2214
2215 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2216 TRACE_PROTO("Already received STREAM data",
2217 QUIC_EV_CONN_PSTRM, qc);
2218 goto out;
2219 }
2220
2221 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2222 diff = strm->rx.offset - strm_frm->offset.key;
2223 strm_frm->offset.key = strm->rx.offset;
2224 strm_frm->len -= diff;
2225 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002226 }
2227
Amaury Denoyelle3db98e92022-05-13 15:41:04 +02002228 qc_get_ncbuf(strm, &strm->rx.ncbuf);
2229 if (ncb_is_null(&strm->rx.ncbuf))
2230 return 0;
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002231
Amaury Denoyelle3db98e92022-05-13 15:41:04 +02002232 ret = ncb_add(&strm->rx.ncbuf, strm_frm->offset.key - strm->rx.offset,
2233 (char *)strm_frm->data, strm_frm->len, NCB_ADD_COMPARE);
2234 if (ret != NCB_RET_OK)
2235 return 0;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002236
Amaury Denoyelle3db98e92022-05-13 15:41:04 +02002237 /* Inform the application of the arrival of this new stream */
2238 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
2239 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002240 return 0;
2241 }
2242
Amaury Denoyelle3db98e92022-05-13 15:41:04 +02002243 qcs_notify_recv(strm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002244
2245 out:
2246 return 1;
2247}
2248
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002249/* Returns 1 on success or 0 on error. On error, the packet containing the
2250 * frame must not be acknowledged.
2251 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002252static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2253 struct quic_stream *strm_frm,
2254 struct quic_conn *qc)
2255{
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002256 /* RFC9000 13.1. Packet Processing
2257 *
2258 * A packet MUST NOT be acknowledged until packet protection has been
2259 * successfully removed and all frames contained in the packet have
2260 * been processed. For STREAM frames, this means the data has been
2261 * enqueued in preparation to be received by the application protocol,
2262 * but it does not require that data be delivered and consumed.
2263 */
2264
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002265 if (strm_frm->id & QCS_ID_DIR_BIT)
2266 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2267 else
2268 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2269}
2270
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002271/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2272 * for <qc> QUIC connection.
2273 * This is a best effort function which never fails even if no memory could be
2274 * allocated to duplicate these frames.
2275 */
2276static void qc_dup_pkt_frms(struct quic_conn *qc,
2277 struct list *pkt_frm_list, struct list *out_frm_list)
2278{
2279 struct quic_frame *frm, *frmbak;
2280 struct list tmp = LIST_HEAD_INIT(tmp);
2281
2282 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2283 struct quic_frame *dup_frm, *origin;
2284
2285 switch (frm->type) {
2286 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2287 {
2288 struct quic_stream *strm_frm = &frm->stream;
2289 struct eb64_node *node = NULL;
2290 struct qc_stream_desc *stream_desc;
2291
2292 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2293 if (!node) {
2294 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm);
2295 continue;
2296 }
2297
2298 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2299 /* Do not resend this frame if in the "already acked range" */
2300 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2301 TRACE_PROTO("ignored frame frame in already acked range",
2302 QUIC_EV_CONN_PRSAFRM, qc, frm);
2303 continue;
2304 }
2305 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2306 strm_frm->offset.key = stream_desc->ack_offset;
2307 TRACE_PROTO("updated partially acked frame",
2308 QUIC_EV_CONN_PRSAFRM, qc, frm);
2309 }
2310
2311 break;
2312 }
2313
2314 default:
2315 break;
2316 }
2317
2318 dup_frm = pool_zalloc(pool_head_quic_frame);
2319 if (!dup_frm) {
2320 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2321 break;
2322 }
2323
2324 /* If <frm> is already a copy of another frame, we must take
2325 * its original frame as source for the copy.
2326 */
2327 origin = frm->origin ? frm->origin : frm;
2328 TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
2329 *dup_frm = *origin;
2330 LIST_INIT(&dup_frm->reflist);
2331 TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM,
2332 qc, NULL, &origin->pkt->pn_node.key);
2333 dup_frm->origin = origin;
2334 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2335 LIST_APPEND(&tmp, &dup_frm->list);
2336 }
2337
2338 LIST_SPLICE(out_frm_list, &tmp);
2339}
2340
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002341/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002342static void qc_prep_fast_retrans(struct quic_conn *qc,
2343 struct quic_enc_level *qel,
2344 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002345{
2346 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002347 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002348 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002349 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002350
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002351 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002352 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002353 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002354 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002355 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2356 node = eb64_next(node);
2357 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002358 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002359 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002360 }
2361
2362 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002363 return;
2364
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002365 /* When building a packet from another one, the field which may increase the
2366 * packet size is the packet number. And the maximum increase is 4 bytes.
2367 */
2368 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2369 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2370 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2371 return;
2372 }
2373
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002374 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2375 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2376 if (frms == frms1 && frms2) {
2377 frms = frms2;
2378 goto start;
2379 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002380}
2381
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002382/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002383 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002384 * Initial packets send them coalescing with others (Handshake here).
2385 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002386 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002387static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2388 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002389{
2390 struct list itmp = LIST_HEAD_INIT(itmp);
2391 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002392
2393 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2394 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2395 struct quic_enc_level *qel = iqel;
2396 struct eb_root *pkts;
2397 struct eb64_node *node;
2398 struct quic_tx_packet *pkt;
2399 struct list *tmp = &itmp;
2400
2401 start:
2402 pkt = NULL;
2403 pkts = &qel->pktns->tx.pkts;
2404 node = eb64_first(pkts);
2405 /* Skip the empty packet (they have already been retransmitted) */
2406 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002407 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002408 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002409 break;
2410 node = eb64_next(node);
2411 }
2412
2413 if (!pkt)
2414 goto end;
2415
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002416 /* When building a packet from another one, the field which may increase the
2417 * packet size is the packet number. And the maximum increase is 4 bytes.
2418 */
2419 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2420 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2421 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2422 goto end;
2423 }
2424
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002425 qel->pktns->tx.pto_probe += 1;
2426 requeue:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002427 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2428 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002429 if (qel == iqel) {
2430 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2431 pkt = pkt->next;
2432 tmp = &htmp;
2433 hqel->pktns->tx.pto_probe += 1;
2434 goto requeue;
2435 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002436 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002437
2438 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002439 LIST_SPLICE(ifrms, &itmp);
2440 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002441}
2442
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002443/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2444 * as I/O handler context and <qel> as encryption level.
2445 * Returns 1 if succeeded, 0 if failed.
2446 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002447static 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 +01002448 struct quic_enc_level *qel)
2449{
2450 struct quic_frame frm;
2451 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002452 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002453 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002454
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002455 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002456 /* Skip the AAD */
2457 pos = pkt->data + pkt->aad_len;
2458 end = pkt->data + pkt->len;
2459
2460 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002461 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002462 goto err;
2463
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002464 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002465 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002466 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002467 break;
2468 case QUIC_FT_PING:
2469 break;
2470 case QUIC_FT_ACK:
2471 {
2472 unsigned int rtt_sample;
2473
2474 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002475 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002476 goto err;
2477
2478 if (rtt_sample) {
2479 unsigned int ack_delay;
2480
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002481 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002482 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002483 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2484 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002485 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002486 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002487 break;
2488 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002489 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002490 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002491 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002492 {
2493 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002495 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002496 /* XXX TO DO: <cfdebug> is used only for the traces. */
2497 struct quic_rx_crypto_frm cfdebug = { };
2498
2499 cfdebug.offset_node.key = frm.crypto.offset;
2500 cfdebug.len = frm.crypto.len;
2501 TRACE_PROTO("CRYPTO data discarded",
2502 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2503 break;
2504 }
2505
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002506 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2507 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2508 /* XXX TO DO: <cfdebug> is used only for the traces. */
2509 struct quic_rx_crypto_frm cfdebug = { };
2510
2511 cfdebug.offset_node.key = frm.crypto.offset;
2512 cfdebug.len = frm.crypto.len;
2513 /* Nothing to do */
2514 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002515 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002516 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002517 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2518 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002519 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002520 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002521 else {
2522 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2523 /* XXX TO DO: <cfdebug> is used only for the traces. */
2524 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002526 cfdebug.offset_node.key = frm.crypto.offset;
2527 cfdebug.len = frm.crypto.len;
2528 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002529 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002530 frm.crypto.len -= diff;
2531 frm.crypto.data += diff;
2532 frm.crypto.offset = qel->rx.crypto.offset;
2533 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002535
2536 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002537 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002538 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002539
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002540 cfdebug.offset_node.key = frm.crypto.offset;
2541 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002542 if (!qc_provide_cdata(qel, ctx,
2543 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002544 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002545 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002546
2547 break;
2548 }
2549
2550 /* frm.crypto.offset > qel->rx.crypto.offset */
2551 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2552 if (!cf) {
2553 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002554 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002555 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002556 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002557
2558 cf->offset_node.key = frm.crypto.offset;
2559 cf->len = frm.crypto.len;
2560 cf->data = frm.crypto.data;
2561 cf->pkt = pkt;
2562 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2563 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002564 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002565 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002566 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002567 {
2568 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002569 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002570
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002571 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002572 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2573 goto err;
2574 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2575 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002576
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002577 /* The upper layer may not be allocated. */
2578 if (qc->mux_state != QC_MUX_READY) {
2579 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
2580 TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
2581 break;
2582 }
2583 else {
2584 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc);
2585 goto err;
2586 }
2587 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002588
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002589 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002590 goto err;
2591
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002592 break;
2593 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002594 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002595 if (qc->mux_state == QC_MUX_READY) {
2596 struct quic_max_data *data = &frm.max_data;
2597 qcc_recv_max_data(qc->qcc, data->max_data);
2598 }
2599 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002600 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002601 if (qc->mux_state == QC_MUX_READY) {
2602 struct quic_max_stream_data *data = &frm.max_stream_data;
2603 qcc_recv_max_stream_data(qc->qcc, data->id,
2604 data->max_stream_data);
2605 }
2606 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002607 case QUIC_FT_MAX_STREAMS_BIDI:
2608 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002609 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002610 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002611 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2612 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002613 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002614 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2615 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002616 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002617 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2618 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002619 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002620 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002621 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002622 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002623 case QUIC_FT_RETIRE_CONNECTION_ID:
2624 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002625 break;
2626 case QUIC_FT_CONNECTION_CLOSE:
2627 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille47756802022-03-25 09:12:16 +01002628 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille07968dc2022-05-20 20:53:20 +02002629 /* If the connection did not reached the handshake complete state,
2630 * the <conn_opening> counter was not decremented. Note that if
2631 * a TLS alert was received from the TLS stack, this counter
2632 * has already been decremented.
2633 */
2634 if (qc->state < QUIC_HS_ST_COMPLETE && !(qc->flags & QUIC_FL_CONN_TLS_ALERT))
2635 HA_ATOMIC_DEC(&qc->prx_counters->conn_opening);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002636 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2637 /* RFC 9000 10.2. Immediate Close:
2638 * The closing and draining connection states exist to ensure
2639 * that connections close cleanly and that delayed or reordered
2640 * packets are properly discarded. These states SHOULD persist
2641 * for at least three times the current PTO interval...
2642 *
2643 * Rearm the idle timeout only one time when entering draining
2644 * state.
2645 */
2646 qc_idle_timer_do_rearm(qc);
2647 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002648 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002649 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002651 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002652 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002653 goto err;
2654
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002655 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002656 break;
2657 default:
2658 goto err;
2659 }
2660 }
2661
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002662 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002663 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002664
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002665 if (fast_retrans) {
2666 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2667 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2668
2669 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2670 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002671
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002672 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2673 * has successfully parse a Handshake packet. The Initial encryption must also
2674 * be discarded.
2675 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002676 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002677 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002678 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2679 QUIC_FL_TLS_SECRETS_DCD)) {
2680 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2681 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2682 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2683 qc_set_timer(ctx->qc);
2684 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2685 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2686 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002687 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2688 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002689 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002690 }
2691
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002692 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002693 return 1;
2694
2695 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002696 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697 return 0;
2698}
2699
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002700/* Must be called only by a <cbuf> writer (packet builder).
2701 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2702 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2703 * index after having marked the end of written data. This the responsability
2704 * of the caller to ensure there is enough room in <cbuf> to write the end of
2705 * data made of a uint16_t null field.
2706 *
2707 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2708 * ^ ^
2709 * r w
2710 *
2711 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2712 * ^ ^
2713 * r w
2714
2715 * +--------------------------------------+ (empty buffer, can be reused)
2716 * ^
2717 * (r = w)
2718 *
2719 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2720 * ^ ^
2721 * w r
2722 */
2723static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2724{
2725 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2726
2727 /* We can reset the writer index only if in front of the reader index and
2728 * if the reader index is not null. Resetting the writer when the reader
2729 * index is null would empty the buffer.
2730 * XXX Note than the writer index cannot reach the reader index.
2731 * Only the reader index can reach the writer index.
2732 */
2733 if (rd && rd <= cbuf->wr) {
2734 /* Mark the end of contiguous data for the reader */
2735 write_u16(cb_wr(cbuf), 0);
2736 cb_add(cbuf, sizeof(uint16_t));
2737 cb_wr_reset(cbuf);
2738 return 1;
2739 }
2740
2741 return 0;
2742}
2743
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002744/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002745 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002746 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2747 * This function must be called only after having built a correct datagram.
2748 * Always succeeds.
2749 */
2750static inline void qc_set_dg(struct cbuf *cbuf,
2751 uint16_t dglen, struct quic_tx_packet *pkt)
2752{
2753 write_u16(cb_wr(cbuf), dglen);
2754 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2755 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2756}
2757
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002758/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2759 * with <frms> as ack-eliciting frame list to send, 0 if not.
2760 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2761 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2762 */
2763static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2764 struct quic_enc_level *qel, int cc, int probe)
2765{
2766 unsigned int must_ack =
2767 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2768
2769 /* Do not build any more packet if the TX secrets are not available or
2770 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2771 * and if there is no more packets to send upon PTO expiration
2772 * and if there is no more ack-eliciting frames to send or in flight
2773 * congestion control limit is reached for prepared data
2774 */
2775 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2776 (!cc && !probe && !must_ack &&
2777 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2778 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2779 return 0;
2780 }
2781
2782 return 1;
2783}
2784
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002785/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002786 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2787 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002788 * A header made of two fields is added to each datagram: the datagram length followed
2789 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002790 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2791 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002792 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002793static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2794 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002795{
2796 struct quic_enc_level *qel;
2797 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002798 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002799 struct quic_tx_packet *pkt;
2800 size_t total;
2801 size_t dg_headlen;
2802
2803 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2804 /* Each datagram is prepended with its length followed by the
2805 * address of the first packet in the datagram.
2806 */
2807 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2808 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2809 total = 0;
2810 start:
2811 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002812 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002813 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2814 * to ensure there is enough room to mark the end of prepared
2815 * contiguous data with a zero length.
2816 */
2817 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2818 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002819 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002820
2821 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002822 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002823 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002824 /* We do not probe if an immediate close was asked */
2825 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002826 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002827
2828 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002829 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002830
2831 /* Leave room for the datagram header */
2832 pos += dg_headlen;
2833 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2834 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2835 }
2836 else {
2837 end = pos + qc->path->mtu;
2838 }
2839
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002840 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002841 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002842 switch (err) {
2843 case -2:
2844 goto err;
2845 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002846 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2847 * MTU, we are here because of the congestion control window. There is
2848 * no need to try to reuse this buffer.
2849 */
2850 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002851 default:
2852 break;
2853 }
2854
2855 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2856 if (!pkt)
2857 goto err;
2858
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002859 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2860 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2861
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002862 total += pkt->len;
2863 /* Set the current datagram as prepared into <cbuf>. */
2864 qc_set_dg(cbuf, pkt->len, pkt);
2865 }
2866
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002867 /* Reset <wr> writer index if in front of <rd> index */
2868 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002869 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002870 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002871 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002872 }
2873
2874 out:
2875 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2876 return total;
2877
2878 err:
2879 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2880 return -1;
2881}
2882
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002883/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002884 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2885 * several packets in the same datagram. A header made of two fields is added
2886 * to each datagram: the datagram length followed by the address of the first
2887 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002888 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2889 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002891static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002892 enum quic_tls_enc_level tel, struct list *tel_frms,
2893 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002896 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002897 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002898 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2899 /* length of datagrams */
2900 uint16_t dglen;
2901 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002902 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002903 /* Each datagram is prepended with its length followed by the
2904 * address of the first packet in the datagram.
2905 */
2906 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002907 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002908
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002909 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2910
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002911 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002912 qel = &qc->els[tel];
2913 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002914 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002915 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002916 padding = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002917 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002918 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002919 /* Leave at least <dglen> bytes at the end of this buffer
2920 * to ensure there is enough room to mark the end of prepared
2921 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002923 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2924 first_pkt = prv_pkt = NULL;
2925 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002926 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002927 enum quic_pkt_type pkt_type;
2928
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002929 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002930 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002931 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002932 /* We do not probe if an immediate close was asked */
2933 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002934 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002935
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002936 if (!qc_may_build_pkt(qc, frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002937 if (prv_pkt)
2938 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002939 /* Let's select the next encryption level */
2940 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2941 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002942 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002943 qel = &qc->els[tel];
2944 /* Build a new datagram */
2945 prv_pkt = NULL;
2946 continue;
2947 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002948 break;
2949 }
2950
2951 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002952 if (!prv_pkt) {
2953 /* Leave room for the datagram header */
2954 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002955 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002956 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2957 }
2958 else {
2959 end = pos + qc->path->mtu;
2960 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002961 }
2962
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002963 cur_pkt = qc_build_pkt(&pos, end, qel, frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002964 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002965 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002966 case -2:
2967 goto err;
2968 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002969 /* If there was already a correct packet present, set the
2970 * current datagram as prepared into <cbuf>.
2971 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002972 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002973 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002974 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002975 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002976 break;
2977 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002978
Frédéric Lécaille63556772021-12-29 17:18:21 +01002979 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2980 if (!cur_pkt)
2981 goto err;
2982
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002983 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2984 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2985
Frédéric Lécaille63556772021-12-29 17:18:21 +01002986 total += cur_pkt->len;
2987 /* keep trace of the first packet in the datagram */
2988 if (!first_pkt)
2989 first_pkt = cur_pkt;
2990 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002991 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002992 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002993 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
2994 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01002995 /* Let's say we have to build a new dgram */
2996 prv_pkt = NULL;
2997 dglen += cur_pkt->len;
2998 /* Client: discard the Initial encryption keys as soon as
2999 * a handshake packet could be built.
3000 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003001 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003002 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3003 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3004 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3005 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3006 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003007 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003008 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003009 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003010 }
3011 /* If the data for the current encryption level have all been sent,
3012 * select the next level.
3013 */
3014 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003015 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 +01003016 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3017 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3018 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3019 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003020 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3021 frms = &qc->els[tel].pktns->tx.frms;
3022 else
3023 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003024 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003025 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003026 /* If there is data for the next level, do not
3027 * consume a datagram.
3028 */
3029 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003031 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003032 /* If we have to build a new datagram, set the current datagram as
3033 * prepared into <cbuf>.
3034 */
3035 if (!prv_pkt) {
3036 qc_set_dg(cbuf, dglen, first_pkt);
3037 first_pkt = NULL;
3038 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003039 padding = 0;
3040 }
3041 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003042 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003043 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3044 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003045 }
3046 }
3047
3048 stop_build:
3049 /* Reset <wr> writer index if in front of <rd> index */
3050 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003051 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003052 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003053 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003054 }
3055
3056 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003057 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003058 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003059
3060 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003061 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003062 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003063}
3064
3065/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003066 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003067 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003068int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069{
3070 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003071 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003072
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003073 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003074 cbuf = qr->cbuf;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003075 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003076 while (cb_contig_data(cbuf)) {
3077 unsigned char *pos;
3078 struct buffer tmpbuf = { };
3079 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3080 uint16_t dglen;
3081 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003082 unsigned int time_sent;
3083
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003084 pos = cb_rd(cbuf);
3085 dglen = read_u16(pos);
3086 /* End of prepared datagrams.
3087 * Reset the reader index only if in front of the writer index.
3088 */
3089 if (!dglen) {
3090 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3091
3092 if (wr && wr < cbuf->rd) {
3093 cb_rd_reset(cbuf);
3094 continue;
3095 }
3096 break;
3097 }
3098
3099 pos += sizeof dglen;
3100 first_pkt = read_ptr(pos);
3101 pos += sizeof first_pkt;
3102 tmpbuf.area = (char *)pos;
3103 tmpbuf.size = tmpbuf.data = dglen;
3104
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003105 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003106 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003107 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003108
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003109 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110 qc->tx.bytes += tmpbuf.data;
3111 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003112
3113 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3114 pkt->time_sent = time_sent;
3115 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3116 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003117 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003118 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3119 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003120 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003121 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3122 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3123 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003124 qc_notify_close(qc);
3125
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003126 /* RFC 9000 10.2. Immediate Close:
3127 * The closing and draining connection states exist to ensure
3128 * that connections close cleanly and that delayed or reordered
3129 * packets are properly discarded. These states SHOULD persist
3130 * for at least three times the current PTO interval...
3131 *
3132 * Rearm the idle timeout only one time when entering closing
3133 * state.
3134 */
3135 qc_idle_timer_do_rearm(qc);
3136 if (qc->timer_task) {
3137 task_destroy(qc->timer_task);
3138 qc->timer_task = NULL;
3139 }
3140 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003141 qc->path->in_flight += pkt->in_flight_len;
3142 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3143 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003144 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003145 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003146 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003147 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003148 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149 }
3150 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003151
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003152 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3153
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003154 return 1;
3155}
3156
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003157/* Copy into <buf> buffer a stateless reset token depending on the
3158 * <salt> salt input. This is the cluster secret which will be derived
3159 * as HKDF input secret to generate this token.
3160 * Return 1 if succeeded, 0 if not.
3161 */
3162static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len,
3163 const unsigned char *salt, size_t saltlen)
3164{
3165 /* Input secret */
3166 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3167 size_t keylen = strlen(global.cluster_secret);
3168 /* Info */
3169 const unsigned char label[] = "stateless token";
3170 size_t labellen = sizeof label - 1;
3171
3172 return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3173 key, keylen, salt, saltlen, label, labellen);
3174}
3175
3176/* Initialize the stateless reset token attached to <cid> connection ID.
3177 * Returns 1 if succeeded, 0 if not.
3178 */
3179static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid)
3180{
3181 if (global.cluster_secret) {
3182 /* Output secret */
3183 unsigned char *token = quic_cid->stateless_reset_token;
3184 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3185 /* Salt */
3186 const unsigned char *cid = quic_cid->cid.data;
3187 size_t cidlen = quic_cid->cid.len;
3188
3189 return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
3190 }
3191 else {
3192 return RAND_bytes(quic_cid->stateless_reset_token,
3193 sizeof quic_cid->stateless_reset_token) == 1;
3194 }
3195}
3196
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003197/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3198 * ebtree.
3199 *
3200 * The CID is randomly generated in part with the result altered to be
3201 * associated with the current thread ID. This means this function must only
3202 * be called by the quic_conn thread.
3203 *
3204 * Returns the new CID if succeeded, NULL if not.
3205 */
3206static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3207 struct quic_conn *qc,
3208 int seq_num)
3209{
3210 struct quic_connection_id *cid;
3211
3212 cid = pool_alloc(pool_head_quic_connection_id);
3213 if (!cid)
3214 return NULL;
3215
3216 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003217 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1)
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003218 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003219
3220 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003221 if (quic_stateless_reset_token_init(cid) != 1)
3222 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003223
3224 cid->qc = qc;
3225
3226 cid->seq_num.key = seq_num;
3227 cid->retire_prior_to = 0;
3228 /* insert the allocated CID in the quic_conn tree */
3229 eb64_insert(root, &cid->seq_num);
3230
3231 return cid;
3232
3233 err:
3234 pool_free(pool_head_quic_connection_id, cid);
3235 return NULL;
3236}
3237
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003238/* Build all the frames which must be sent just after the handshake have succeeded.
3239 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3240 * a HANDSHAKE_DONE frame.
3241 * Return 1 if succeeded, 0 if not.
3242 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003243static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003244{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003245 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003246 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003247 struct quic_frame *frm, *frmbak;
3248 struct list frm_list = LIST_HEAD_INIT(frm_list);
3249 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003250
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003251 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003253 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003254 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003255 if (!frm)
3256 return 0;
3257
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003258 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003259 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003260 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003261 }
3262
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003263 first = 1;
3264 max = qc->tx.params.active_connection_id_limit;
3265 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003266 struct quic_connection_id *cid;
3267
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003268 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003269 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003270 goto err;
3271
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003272 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003273 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003274 if (!cid)
3275 goto err;
3276
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003277 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003278 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003279
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003281 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003282 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003283
3284 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003285 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286
3287 return 1;
3288
3289 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003290 /* free the frames */
3291 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3292 pool_free(pool_head_quic_frame, frm);
3293
3294 node = eb64_first(&qc->cids);
3295 while (node) {
3296 struct quic_connection_id *cid;
3297
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003298
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003299 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003300 if (cid->seq_num.key >= max)
3301 break;
3302
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003303 if (cid->seq_num.key < first)
3304 continue;
3305
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003306 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003307 ebmb_delete(&cid->node);
3308 eb64_delete(&cid->seq_num);
3309 pool_free(pool_head_quic_connection_id, cid);
3310 }
3311
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003312 return 0;
3313}
3314
3315/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003316void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003317{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003318 struct eb64_node *n;
3319 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003321 n = eb64_first(&arngs->root);
3322 while (n) {
3323 struct eb64_node *next;
3324
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003325 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003326 next = eb64_next(n);
3327 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003328 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003329 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003330 }
3331}
3332
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003333/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3334 * descending order.
3335 */
3336static inline size_t sack_gap(struct quic_arng_node *p,
3337 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003338{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003339 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003340}
3341
3342
3343/* Remove the last elements of <ack_ranges> list of ack range updating its
3344 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003345 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003346 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003347static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003349 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003351 last = eb64_last(&arngs->root);
3352 while (last && arngs->enc_sz > limit) {
3353 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003354
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003355 prev = eb64_prev(last);
3356 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003357 return 0;
3358
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003359 last_node = eb64_entry(last, struct quic_arng_node, first);
3360 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003361 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3362 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3363 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3364 --arngs->sz;
3365 eb64_delete(last);
3366 pool_free(pool_head_quic_arng, last);
3367 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368 }
3369
3370 return 1;
3371}
3372
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003373/* Set the encoded size of <arngs> QUIC ack ranges. */
3374static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3375{
3376 struct eb64_node *node, *next;
3377 struct quic_arng_node *ar, *ar_next;
3378
3379 node = eb64_last(&arngs->root);
3380 if (!node)
3381 return;
3382
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003383 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003384 arngs->enc_sz = quic_int_getsize(ar->last) +
3385 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3386
3387 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003388 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003389 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3390 quic_int_getsize(ar_next->last - ar_next->first.key);
3391 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003392 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003393 }
3394}
3395
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003396/* Insert <ar> ack range into <argns> tree of ack ranges.
3397 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003398 */
3399static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003400struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3401 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003402{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003403 struct quic_arng_node *new_ar;
3404
3405 new_ar = pool_alloc(pool_head_quic_arng);
3406 if (new_ar) {
3407 new_ar->first.key = ar->first;
3408 new_ar->last = ar->last;
3409 eb64_insert(&arngs->root, &new_ar->first);
3410 arngs->sz++;
3411 }
3412
3413 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003414}
3415
3416/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003417 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003418 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003419 *
3420 * Descending order
3421 * ------------->
3422 * range1 range2
3423 * ..........|--------|..............|--------|
3424 * ^ ^ ^ ^
3425 * | | | |
3426 * last1 first1 last2 first2
3427 * ..........+--------+--------------+--------+......
3428 * diff1 gap12 diff2
3429 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003430 * To encode the previous list of ranges we must encode integers as follows in
3431 * descending order:
3432 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433 * with diff1 = last1 - first1
3434 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003435 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003437 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003438int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3439 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003440{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003441 struct eb64_node *le;
3442 struct quic_arng_node *new_node;
3443 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003444
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003445 new = NULL;
3446 if (eb_is_empty(&arngs->root)) {
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)
3449 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003450
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003451 goto out;
3452 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003453
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003454 le = eb64_lookup_le(&arngs->root, ar->first);
3455 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003456 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003457 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003458 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003459
3460 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003462 else {
3463 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003464 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003465
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003466 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003467 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003468 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003469
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003470 if (le_ar->last + 1 >= ar->first) {
3471 le_ar->last = ar->last;
3472 new = le;
3473 new_node = le_ar;
3474 }
3475 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003476 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003477 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003478 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003479
3480 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003481 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003482 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003484 /* Verify that the new inserted node does not overlap the nodes
3485 * which follow it.
3486 */
3487 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003488 struct eb64_node *next;
3489 struct quic_arng_node *next_node;
3490
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003491 while ((next = eb64_next(new))) {
3492 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003493 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003494 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003495 break;
3496
3497 if (next_node->last > new_node->last)
3498 new_node->last = next_node->last;
3499 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003500 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003501 /* Decrement the size of these ranges. */
3502 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003503 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003504 }
3505
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003506 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003507 quic_arngs_set_enc_sz(arngs);
3508
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003509 return 1;
3510}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003511/* Remove the header protection of packets at <el> encryption level.
3512 * Always succeeds.
3513 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003514static 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 +01003515{
3516 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003517 struct quic_rx_packet *pqpkt;
3518 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003519 struct quic_enc_level *app_qel;
3520
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003521 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3522 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003523 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003524 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003525 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003526 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003527 goto out;
3528 }
3529 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003530 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003531 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003532 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003533 pqpkt->data, pqpkt->data + pqpkt->len)) {
3534 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003535 /* XXX TO DO XXX */
3536 }
3537 else {
3538 /* The AAD includes the packet number field */
3539 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3540 /* Store the packet into the tree of packets to decrypt. */
3541 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003542 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003543 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3544 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003545 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003546 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003548 MT_LIST_DELETE_SAFE(pkttmp1);
3549 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003550 }
3551
3552 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003553 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003554}
3555
3556/* Process all the CRYPTO frame at <el> encryption level.
3557 * Return 1 if succeeded, 0 if not.
3558 */
3559static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003560 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003561{
3562 struct eb64_node *node;
3563
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003564 node = eb64_first(&el->rx.crypto.frms);
3565 while (node) {
3566 struct quic_rx_crypto_frm *cf;
3567
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003568 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569 if (cf->offset_node.key != el->rx.crypto.offset)
3570 break;
3571
3572 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3573 goto err;
3574
3575 node = eb64_next(node);
3576 quic_rx_packet_refdec(cf->pkt);
3577 eb64_delete(&cf->offset_node);
3578 pool_free(pool_head_quic_rx_crypto_frm, cf);
3579 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003580 return 1;
3581
3582 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003583 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003584 return 0;
3585}
3586
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003587/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003588 * 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 +02003589 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 * Return 1 if succeeded, 0 if not.
3591 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003592int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3593 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003595 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003596 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003597 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003598 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003599 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003601 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003602 qel = cur_el;
3603 next_tel:
3604 if (!qel)
3605 goto out;
3606
3607 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3608 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 while (node) {
3610 struct quic_rx_packet *pkt;
3611
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003612 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003613 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003614 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003615 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003616 /* Drop the packet */
3617 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003618 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003619 }
3620 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003621 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003622 /* Drop the packet */
3623 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003624 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625 }
3626 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003627 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3628
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003629 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3630 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3631 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003632 qc_idle_timer_rearm(qc, 1);
3633 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003634 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003635 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003636 largest_pn_time_received = pkt->time_received;
3637 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003638 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003639 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003640 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003641 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003642 }
3643 }
3644 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003645 eb64_delete(&pkt->pn_node);
3646 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003648 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003649
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003650 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3651 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003652 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003653 /* Update the largest acknowledged packet timestamps */
3654 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3655 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3656 }
3657
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003658 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003659 goto err;
3660
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003661 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003662 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003663 qel = next_el;
3664 goto next_tel;
3665 }
3666
3667 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003668 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669 return 1;
3670
3671 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003672 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003673 return 0;
3674}
3675
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003676/* Check if it's possible to remove header protection for packets related to
3677 * encryption level <qel>. If <qel> is NULL, assume it's false.
3678 *
3679 * Return true if the operation is possible else false.
3680 */
3681static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3682{
3683 enum quic_tls_enc_level tel;
3684
3685 if (!qel)
3686 return 0;
3687
3688 tel = ssl_to_quic_enc_level(qel->level);
3689
3690 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003691 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003692 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003693 return 0;
3694 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003695
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003696 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003697 return 0;
3698
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003699 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003700 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003701 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003702 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003703
3704 return 1;
3705}
3706
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003707/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003708int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003709{
3710 int ret;
3711 struct qring *qr;
3712
3713 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3714 if (!qr)
3715 /* Never happens */
3716 return 1;
3717
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003718 if (old_data)
3719 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003720 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003721 if (ret == -1)
3722 goto err;
3723 else if (ret == 0)
3724 goto out;
3725
3726 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3727 goto err;
3728
3729 out:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003730 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003731 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3732 return 1;
3733
3734 err:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003735 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003736 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3737 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3738 return 0;
3739}
3740
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003741/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3742 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
3743 * QUIC connection
3744 * Returns 1 if succeeded, 0 if not.
3745 */
3746int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
3747 enum quic_tls_enc_level tel, struct list *tel_frms,
3748 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3749{
3750 int ret;
3751 struct qring *qr;
3752
3753 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3754 if (!qr)
3755 /* Never happens */
3756 return 1;
3757
3758 if (old_data)
3759 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
3760 ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms);
3761 if (ret == -1)
3762 goto err;
3763 else if (ret == 0)
3764 goto out;
3765
3766 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3767 goto err;
3768
3769 out:
3770 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3771 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3772 return 1;
3773
3774 err:
3775 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3776 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3777 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3778 return 0;
3779}
3780
3781/* Retransmit up to two datagrams depending on packet number space */
3782static void qc_dgrams_retransmit(struct quic_conn *qc)
3783{
3784 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3785 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3786 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3787
3788 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3789 struct list ifrms = LIST_HEAD_INIT(ifrms);
3790 struct list hfrms = LIST_HEAD_INIT(hfrms);
3791
3792 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
3793 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
3794 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3795 if (!LIST_ISEMPTY(&ifrms)) {
3796 iqel->pktns->tx.pto_probe = 1;
3797 if (!LIST_ISEMPTY(&hfrms)) {
3798 hqel->pktns->tx.pto_probe = 1;
3799 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
3800 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
3801 }
3802 }
3803 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3804 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
3805 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3806 if (!LIST_ISEMPTY(&hfrms)) {
3807 hqel->pktns->tx.pto_probe = 1;
3808 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
3809 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3810 }
3811 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3812 }
3813 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3814 }
3815 else {
3816 int i;
3817 struct list frms1 = LIST_HEAD_INIT(frms1);
3818 struct list frms2 = LIST_HEAD_INIT(frms2);
3819
3820 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3821 hqel->pktns->tx.pto_probe = 0;
3822 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
3823 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
3824 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3825 if (!LIST_ISEMPTY(&frms1)) {
3826 hqel->pktns->tx.pto_probe = 1;
3827 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
3828 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3829 }
3830 }
3831 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3832 }
3833 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3834 aqel->pktns->tx.pto_probe = 0;
3835 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
3836 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3837 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
3838 if (!LIST_ISEMPTY(&frms1)) {
3839 aqel->pktns->tx.pto_probe = 1;
3840 qc_send_app_pkts(qc, 1, &frms1);
3841 }
3842 if (!LIST_ISEMPTY(&frms2)) {
3843 aqel->pktns->tx.pto_probe = 1;
3844 qc_send_app_pkts(qc, 1, &frms2);
3845 }
3846 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3847 }
3848 }
3849}
3850
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003851/* QUIC connection packet handler task (post handshake) */
3852static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3853{
3854 struct ssl_sock_ctx *ctx;
3855 struct quic_conn *qc;
3856 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003857
3858
3859 ctx = context;
3860 qc = ctx->qc;
3861 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003862
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003863 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003864
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003865 /* Retranmissions */
3866 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3867 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
3868 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3869 qc_dgrams_retransmit(qc);
3870 }
3871
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003872 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3873 qc_rm_hp_pkts(qc, qel);
3874
3875 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3876 goto err;
3877
Frédéric Lécaille47756802022-03-25 09:12:16 +01003878 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3879 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3880 goto out;
3881
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003882 if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003883 goto err;
3884
Frédéric Lécaille47756802022-03-25 09:12:16 +01003885out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003886 return t;
3887
3888 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003889 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003890 return t;
3891}
3892
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003893/* QUIC connection packet handler task. */
3894struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003895{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003896 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003897 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003898 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003899 enum quic_tls_enc_level tel, next_tel;
3900 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003901 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003902 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003903
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003904 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003905 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003906 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003907 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003908 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003909 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003910
3911 /* Retranmissions */
3912 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3913 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
3914 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3915 qc_dgrams_retransmit(qc);
3916 }
3917
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003918 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3919 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003920 /* The I/O handler has been woken up by the dgram listener
3921 * after the anti-amplification was reached.
3922 */
3923 qc_set_timer(qc);
3924 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3925 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3926 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003927 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003928 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3929 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3930 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003931 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003932 if (st >= QUIC_HS_ST_COMPLETE &&
3933 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3934 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3935 /* There may be remaining Handshake packets to treat and acknowledge. */
3936 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3937 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3938 }
3939 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003940 goto err;
3941
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003942 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003943 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003944
3945 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003946 /* Treat packets waiting for header packet protection decryption */
3947 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003948 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003949
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003950 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3951 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3952 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003953 goto err;
3954
Frédéric Lécaille47756802022-03-25 09:12:16 +01003955 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3956 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3957 goto out;
3958
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02003959 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
3960 !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
3961 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3962 qel = next_qel;
3963 next_qel = NULL;
3964 goto next_level;
3965 }
3966 else {
3967 struct quic_rx_packet *pkt;
3968 struct mt_list *elt1, elt2;
3969 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
3970
3971 /* Drop these 0-RTT packets */
3972 TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
3973 mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) {
3974 MT_LIST_DELETE_SAFE(elt1);
3975 quic_rx_packet_refdec(pkt);
3976 }
3977 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003978 }
3979
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003980 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003981 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003982 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003983 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003984 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003985
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003986 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003987 QUIC_FL_TLS_SECRETS_DCD)) {
3988 /* Discard the Handshake keys. */
3989 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3990 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3991 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3992 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003993 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003994 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003995 }
3996
3997 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3998 /* There may be remaining handshake to build (acks) */
3999 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4000 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004001 }
4002
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004003 if (!qr)
4004 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004005 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4006 * be considered.
4007 */
4008 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01004009 goto err;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004010 ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms,
4011 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004012 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004013 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004014 else if (ret == 0)
4015 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004016
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004017 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004018 goto err;
4019
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004020 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021 /* Check if there is something to do for the next level.
4022 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004023 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004024 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01004025 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004026 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004027 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004028 goto next_level;
4029 }
4030
Frédéric Lécaille47756802022-03-25 09:12:16 +01004031 out:
4032 if (qr)
4033 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004034 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004035 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004036
4037 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004038 if (qr)
4039 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004040 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004041 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004042}
4043
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004044/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004045static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
4046{
4047 int i;
4048
4049 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4050 if (qel->tx.crypto.bufs[i]) {
4051 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4052 qel->tx.crypto.bufs[i] = NULL;
4053 }
4054 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004055 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004056}
4057
4058/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004059 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004060 * Returns 1 if succeeded, 0 if not.
4061 */
4062static int quic_conn_enc_level_init(struct quic_conn *qc,
4063 enum quic_tls_enc_level level)
4064{
4065 struct quic_enc_level *qel;
4066
4067 qel = &qc->els[level];
4068 qel->level = quic_to_ssl_enc_level(level);
4069 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4070 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4071 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004072 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004073
4074 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02004075 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004076 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004077 qel->rx.crypto.offset = 0;
4078 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004079
4080 /* Allocate only one buffer. */
4081 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4082 if (!qel->tx.crypto.bufs)
4083 goto err;
4084
4085 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4086 if (!qel->tx.crypto.bufs[0])
4087 goto err;
4088
4089 qel->tx.crypto.bufs[0]->sz = 0;
4090 qel->tx.crypto.nb_buf = 1;
4091
4092 qel->tx.crypto.sz = 0;
4093 qel->tx.crypto.offset = 0;
4094
4095 return 1;
4096
4097 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004098 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004099 return 0;
4100}
4101
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004102/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4103 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004104 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004105 * This function must only be called by the thread responsible of the quic_conn
4106 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004107 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004108static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004109{
4110 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004111 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004112 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004113 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004114
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004115 /* We must not free the quic-conn if the MUX is still allocated. */
4116 BUG_ON(qc->mux_state == QC_MUX_READY);
4117
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004118 /* free remaining stream descriptors */
4119 node = eb64_first(&qc->streams_by_id);
4120 while (node) {
4121 struct qc_stream_desc *stream;
4122
4123 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4124 node = eb64_next(node);
4125
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004126 /* all streams attached to the quic-conn are released, so
4127 * qc_stream_desc_free will liberate the stream instance.
4128 */
4129 BUG_ON(!stream->release);
4130 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004131 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004132
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004133 if (qc->idle_timer_task) {
4134 task_destroy(qc->idle_timer_task);
4135 qc->idle_timer_task = NULL;
4136 }
4137
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004138 if (qc->timer_task) {
4139 task_destroy(qc->timer_task);
4140 qc->timer_task = NULL;
4141 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004142
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004143 /* remove the connection from receiver cids trees */
4144 ebmb_delete(&qc->odcid_node);
4145 ebmb_delete(&qc->scid_node);
4146 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004147
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004148 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004149 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004150 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004151 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004152 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004153 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004154
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004155 quic_tls_ku_free(qc);
4156 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4157 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004158 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004159 }
4160
4161 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4162 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4163 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004164
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004165 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
4166 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02004167 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004168 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004169
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004170 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4171 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004172 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004173}
4174
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004175static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004176{
4177 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004178 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004179
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004180 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004181
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004182 /* Next application data can be dropped. */
4183 qc->mux_state = QC_MUX_RELEASED;
4184
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004185 /* If the quic-conn timer has already expired free the quic-conn. */
4186 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4187 quic_conn_release(qc);
4188 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
4189 return;
4190 }
4191
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004192 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004193}
4194
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004195/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004196static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004198 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199 struct quic_conn *qc;
4200 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004201
4202 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004203 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004204 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004205 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004206 task->expire = TICK_ETERNITY;
4207 pktns = quic_loss_pktns(qc);
4208 if (tick_isset(pktns->tx.loss_time)) {
4209 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4210
4211 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4212 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004213 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4214 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 goto out;
4216 }
4217
4218 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004219 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004220 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4221 qc->qcc->subs->events & SUB_RETRY_SEND) {
4222 struct qcc *qcc = qc->qcc;
4223
4224 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4225 tasklet_wakeup(qcc->subs->tasklet);
4226 qcc->subs->events &= ~SUB_RETRY_SEND;
4227 if (!qcc->subs->events)
4228 qcc->subs = NULL;
4229 }
4230 else {
4231 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4232 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4233 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
4234 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
4235 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4236 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004237 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004238 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004239 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4241 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4242
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004243 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004244 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004245 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004246 iel->pktns->tx.pto_probe = 1;
4247 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004248
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004249 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4250 qc->path->loss.pto_count++;
4251
4252 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004253 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004254
4255 return task;
4256}
4257
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004258/* Parse the Retry token from buffer <token> with <end> a pointer to
4259 * one byte past the end of this buffer. This will extract the ODCID
4260 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004261 *
4262 * Returns 0 on success else non-zero.
4263 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004264static int parse_retry_token(const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004265 struct quic_cid *odcid)
4266{
4267 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004268 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004269
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004270 if (!quic_dec_int(&odcid_len, &token, end))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004271 return 1;
4272
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004273 /* RFC 9000 7.2. Negotiating Connection IDs:
4274 * When an Initial packet is sent by a client that has not previously
4275 * received an Initial or Retry packet from the server, the client
4276 * populates the Destination Connection ID field with an unpredictable
4277 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4278 */
4279 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN)
4280 return 1;
4281
4282 if (end - token < odcid_len + sizeof timestamp)
4283 return 1;
4284
4285 timestamp = ntohl(read_u32(token + odcid_len));
4286 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms)
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004287 return 1;
4288
4289 memcpy(odcid->data, token, odcid_len);
4290 odcid->len = odcid_len;
4291
4292 return 0;
4293}
4294
4295/* Initialize the transport parameters for <qc> QUIC connection attached
4296 * to <l> listener from <pkt> Initial packet information.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004297 * Returns 1 if succeeded, 0 if not.
4298 */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004299static int qc_lstnr_params_init(struct quic_conn *qc, struct listener *l,
4300 const unsigned char *token, size_t token_len,
Frédéric Lécaille806e6cf2022-05-09 16:22:29 +02004301 const struct quic_connection_id *icid,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004302 const struct quic_cid *dcid, const struct quic_cid *odcid)
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004303{
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004304 struct quic_cid *odcid_param = &qc->rx.params.original_destination_connection_id;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004305
4306 /* Copy the transport parameters. */
4307 qc->rx.params = l->bind_conf->quic_params;
Frédéric Lécaille806e6cf2022-05-09 16:22:29 +02004308 /* Copy the stateless reset token */
4309 memcpy(qc->rx.params.stateless_reset_token, icid->stateless_reset_token,
4310 sizeof qc->rx.params.stateless_reset_token);
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004311 /* Copy original_destination_connection_id transport parameter. */
4312 if (token_len) {
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004313 memcpy(odcid_param->data, odcid->data, odcid->len);
4314 odcid_param->len = odcid->len;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004315 /* Copy retry_source_connection_id transport parameter. */
4316 quic_cid_cpy(&qc->rx.params.retry_source_connection_id, dcid);
4317 }
4318 else {
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004319 memcpy(odcid_param->data, dcid->data, dcid->len);
4320 odcid_param->len = dcid->len;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004321 }
4322
4323 /* Copy the initial source connection ID. */
4324 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4325
4326 return 1;
4327}
4328
4329/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4330 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4331 * for QUIC servers (or haproxy listeners).
4332 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4333 * <token> the token found to be used for this connection with <token_len> as
4334 * length. <saddr> is the source address.
4335 * Returns the connection if succeeded, NULL if not.
4336 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004337static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004338 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004339 const struct quic_cid *odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004340 struct sockaddr_storage *saddr,
4341 const unsigned char *token, size_t token_len,
4342 int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004343{
4344 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004345 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004346 /* Initial CID. */
4347 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004348 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004349 struct listener *l = NULL;
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004350 const unsigned char *salt = initial_salt_v1;
4351 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004352
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004353 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004354 qc = pool_zalloc(pool_head_quic_conn);
4355 if (!qc) {
4356 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
4357 goto err;
4358 }
4359
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004360 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4361 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004362 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004363 goto err;
4364 }
4365
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004366 qc->cids = EB_ROOT;
4367 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004368 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004369 struct proxy *prx;
4370
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004371 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004372 prx = l->bind_conf->frontend;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004373
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004374 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4375 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004376 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004377 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004378 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004379 qc->odcid.len = dcid->len;
4380 qc->odcid.addrlen = dcid->addrlen;
4381 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004382
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004383 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004384 if (scid->len)
4385 memcpy(qc->dcid.data, scid->data, scid->len);
4386 qc->dcid.len = scid->len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02004387 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004388 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004389 }
4390 /* QUIC Client (outgoing connection to servers) */
4391 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004392 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004393 if (dcid->len)
4394 memcpy(qc->dcid.data, dcid->data, dcid->len);
4395 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004396 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004397 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004398
4399 /* Initialize the output buffer */
4400 qc->obuf.pos = qc->obuf.data;
4401
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004402 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004403 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004404 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004405 goto err;
4406 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004407
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004408 /* insert the allocated CID in the receiver datagram handler tree */
4409 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004410 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004411
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412 /* Select our SCID which is the first CID with 0 as sequence number. */
4413 qc->scid = icid->cid;
4414
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004415 /* Packet number spaces initialization. */
4416 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4417 quic_pktns_init(&qc->pktns[i]);
4418 /* QUIC encryption level context initialization. */
4419 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004420 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004421 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004422 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004423 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004424 /* Initialize the packet number space. */
4425 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4426 }
4427
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02004428 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004429 qc->tps_tls_ext = qc->version & 0xff000000 ?
4430 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4431 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004432 /* TX part. */
4433 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004434 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4435 qc->tx.wbuf = qc->tx.rbuf = 0;
4436 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004437 /* RX part. */
4438 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004439 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004440 for (i = 0; i < QCS_MAX_TYPES; i++)
4441 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004442
4443 qc->nb_pkt_for_cc = 1;
4444 qc->nb_pkt_since_cc = 0;
4445
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004446 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004447 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004448 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004449 goto err;
4450 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004451
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004452 /* XXX TO DO: Only one path at this time. */
4453 qc->path = &qc->paths[0];
4454 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4455
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004456 /* required to use MTLIST_IN_LIST */
4457 MT_LIST_INIT(&qc->accept_list);
4458
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004459 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004460 qc->stream_buf_count = 0;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02004461 qc->sendto_err = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004462 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4463
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004464 if (server && !qc_lstnr_params_init(qc, l, token, token_len, icid, dcid, odcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004465 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004466
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004467 qc->enc_params_len =
4468 quic_transport_params_encode(qc->enc_params,
4469 qc->enc_params + sizeof qc->enc_params,
4470 &qc->rx.params, 1);
4471 if (!qc->enc_params_len)
4472 goto err;
4473
4474 if (qc_conn_alloc_ssl_ctx(qc) ||
4475 !quic_conn_init_timer(qc) ||
4476 !quic_conn_init_idle_timer_task(qc))
4477 goto err;
4478
4479 if (version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4480 salt = initial_salt_draft_29;
4481 salt_len = sizeof initial_salt_draft_29;
4482 }
4483
4484 if (!qc_new_isecs(qc, salt, salt_len, dcid->data, dcid->len, 1))
4485 goto err;
4486
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004487 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004488
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004489 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004490
4491 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004492 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004493 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4494 if (qc)
4495 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004496 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004497 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498}
4499
4500/* Initialize the timer task of <qc> QUIC connection.
4501 * Returns 1 if succeeded, 0 if not.
4502 */
4503static int quic_conn_init_timer(struct quic_conn *qc)
4504{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004505 /* Attach this task to the same thread ID used for the connection */
4506 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004507 if (!qc->timer_task)
4508 return 0;
4509
4510 qc->timer = TICK_ETERNITY;
4511 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004512 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004513
4514 return 1;
4515}
4516
Frédéric Lécaille47756802022-03-25 09:12:16 +01004517/* Rearm the idle timer for <qc> QUIC connection. */
4518static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4519{
4520 unsigned int expire;
4521
4522 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4523 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4524}
4525
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004526/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4527 * which is set to 1 when receiving a packet , and 0 when sending packet
4528 */
4529static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4530{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004531 if (read) {
4532 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4533 }
4534 else {
4535 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4536 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004537 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004538}
4539
4540/* The task handling the idle timeout */
4541static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4542{
4543 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004544 struct quic_counters *prx_counters = qc->prx_counters;
4545 int qc_state = qc->state;
4546 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004547
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004548 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4549 * might free the quic-conn too early via quic_close().
4550 */
4551 qc_notify_close(qc);
4552
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004553 /* If the MUX is still alive, keep the quic-conn. The MUX is
4554 * responsible to call quic_close to release it.
4555 */
4556 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4557 if (qc->mux_state != QC_MUX_READY)
4558 quic_conn_release(qc);
4559
4560 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4561 * least clean some parts of it such as the tasklet.
4562 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004563
4564 /* If the connection did not reached the handshake complete state,
4565 * the <conn_opening> counter was not decremented. Note that if
4566 * a TLS alert was received from the TLS stack, this counter
4567 * has already been decremented.
4568 */
Frédéric Lécaillead9895d2022-05-20 20:50:59 +02004569 if (qc_state < QUIC_HS_ST_COMPLETE && !(qc_flags & QUIC_FL_CONN_TLS_ALERT))
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004570 HA_ATOMIC_DEC(&prx_counters->conn_opening);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004571
4572 return NULL;
4573}
4574
4575/* Initialize the idle timeout task for <qc>.
4576 * Returns 1 if succeeded, 0 if not.
4577 */
4578static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4579{
4580 qc->idle_timer_task = task_new_here();
4581 if (!qc->idle_timer_task)
4582 return 0;
4583
4584 qc->idle_timer_task->process = qc_idle_timer_task;
4585 qc->idle_timer_task->context = qc;
4586 qc_idle_timer_rearm(qc, 1);
4587 task_queue(qc->idle_timer_task);
4588
4589 return 1;
4590}
4591
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004592/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4593 * past one byte of this buffer.
4594 */
4595static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4596 struct quic_rx_packet *pkt)
4597{
4598 unsigned char dcid_len, scid_len;
4599
4600 /* Version */
4601 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4602 return 0;
4603
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004604 /* Destination Connection ID Length */
4605 dcid_len = *(*buf)++;
4606 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4607 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4608 /* XXX MUST BE DROPPED */
4609 return 0;
4610
4611 if (dcid_len) {
4612 /* Check that the length of this received DCID matches the CID lengths
4613 * of our implementation for non Initials packets only.
4614 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004615 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4616 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004617 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004618 return 0;
4619
4620 memcpy(pkt->dcid.data, *buf, dcid_len);
4621 }
4622
4623 pkt->dcid.len = dcid_len;
4624 *buf += dcid_len;
4625
4626 /* Source Connection ID Length */
4627 scid_len = *(*buf)++;
4628 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4629 /* XXX MUST BE DROPPED */
4630 return 0;
4631
4632 if (scid_len)
4633 memcpy(pkt->scid.data, *buf, scid_len);
4634 pkt->scid.len = scid_len;
4635 *buf += scid_len;
4636
4637 return 1;
4638}
4639
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004640/* Insert <pkt> RX packet in its <qel> RX packets tree */
4641static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4642{
4643 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004644 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004645 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4646 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4647 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004648}
4649
4650/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004651 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4652 * byte past the end of the buffer containing this packet and <beg> the address of
4653 * the packet first byte.
4654 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4655 * Returns 1 if succeeded, 0 if not.
4656 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004657static inline int qc_try_rm_hp(struct quic_conn *qc,
4658 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004659 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004661 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004662{
4663 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004664 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004665 struct quic_enc_level *qel;
4666 /* Only for traces. */
4667 struct quic_rx_packet *qpkt_trace;
4668
4669 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004670 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004671 /* The packet number is here. This is also the start minus
4672 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4673 * protection.
4674 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004675 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004676
4677 tel = quic_packet_type_enc_level(pkt->type);
4678 qel = &qc->els[tel];
4679
4680 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004681 /* Note that the following function enables us to unprotect the packet
4682 * number and its length subsequently used to decrypt the entire
4683 * packets.
4684 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004685 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4686 qel->pktns->rx.largest_pn, pn, beg, end)) {
4687 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004688 goto err;
4689 }
4690
4691 /* The AAD includes the packet number field found at <pn>. */
4692 pkt->aad_len = pn - beg + pkt->pnl;
4693 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004694 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004695 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004696 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004697 /* If the packet number space has been discarded, this packet
4698 * will be not parsed.
4699 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004700 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004701 goto out;
4702 }
4703
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004704 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004705 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004706 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4707 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004708 }
4709
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004710 *el = qel;
4711 /* No reference counter incrementation here!!! */
4712 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4713 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4714 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4715 b_add(&qc->rx.buf, pkt->len);
4716 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004717 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004718 return 1;
4719
4720 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004721 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004722 return 0;
4723}
4724
4725/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4726 * Also set <*long_header> to 1 if this form is long, 0 if not.
4727 */
4728static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4729 unsigned char byte0, int *long_header)
4730{
4731 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4732 pkt->type =
4733 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4734 *long_header = 1;
4735 }
4736 else {
4737 pkt->type = QUIC_PACKET_TYPE_SHORT;
4738 *long_header = 0;
4739 }
4740}
4741
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004742/*
4743 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4744 */
4745static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4746{
4747 int j = 0, version;
4748
4749 do {
4750 version = quic_supported_version[j];
4751 if (version == pkt->version)
4752 return 1;
4753
4754 version = quic_supported_version[++j];
4755 } while(version);
4756
4757 return 0;
4758}
4759
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004760/*
4761 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4762 * address <addr>.
4763 * Implementation of RFC9000 6. Version Negotiation
4764 *
4765 * TODO implement a rate-limiting sending of Version Negotiation packets
4766 *
4767 * Returns 0 on success else non-zero
4768 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004769static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4770 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004771{
4772 char buf[256];
4773 int i = 0, j, version;
4774 const socklen_t addrlen = get_addr_len(addr);
4775
4776 /*
4777 * header form
4778 * long header, fixed bit to 0 for Version Negotiation
4779 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004780 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4781 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004782
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004783 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004784 /* null version for Version Negotiation */
4785 buf[i++] = '\x00';
4786 buf[i++] = '\x00';
4787 buf[i++] = '\x00';
4788 buf[i++] = '\x00';
4789
4790 /* source connection id */
4791 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004792 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004793 i += pkt->scid.len;
4794
4795 /* destination connection id */
4796 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004797 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004798 i += pkt->dcid.len;
4799
4800 /* supported version */
4801 j = 0;
4802 do {
4803 version = htonl(quic_supported_version[j]);
4804 memcpy(&buf[i], &version, sizeof(version));
4805 i += sizeof(version);
4806
4807 version = quic_supported_version[++j];
4808 } while (version);
4809
4810 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4811 return 1;
4812
4813 return 0;
4814}
4815
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004816/* Send a stateless reset packet depending on <pkt> RX packet information
4817 * from <fd> UDP socket to <dst>
4818 * Return 1 if succeeded, 0 if not.
4819 */
4820static int send_stateless_reset(int fd, struct sockaddr_storage *dstaddr,
4821 struct quic_rx_packet *rxpkt)
4822{
4823 int pktlen, rndlen;
4824 unsigned char pkt[64];
4825 const socklen_t addrlen = get_addr_len(dstaddr);
4826
4827 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
4828 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
4829 * Reset is difficult to distinguish from other packets if the recipient requires
4830 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
4831 * that all packets it sends are at least 22 bytes longer than the minimum
4832 * connection ID length that it requests the peer to include in its packets,
4833 * adding PADDING frames as necessary. This ensures that any Stateless Reset
4834 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
4835 * An endpoint that sends a Stateless Reset in response to a packet that is
4836 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
4837 * than the packet it responds to.
4838 */
4839
4840 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
4841 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
4842 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
4843 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
4844 /* Put a header of random bytes */
4845 if (RAND_bytes(pkt, rndlen) != 1)
4846 return 0;
4847
4848 /* Clear the most significant bit, and set the second one */
4849 *pkt = (*pkt & ~0x80) | 0x40;
4850 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
4851 rxpkt->dcid.data, rxpkt->dcid.len))
4852 return 0;
4853
4854 if (sendto(fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
4855 return 0;
4856
4857 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
4858 return 1;
4859}
4860
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004861/* QUIC server only function.
4862 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
4863 * This is the responsability of the caller to check <aad> size is big enough
4864 * to contain these data.
4865 * Return the number of bytes copied to <aad>.
4866 */
4867static int quic_generate_retry_token_aad(unsigned char *aad,
4868 uint32_t version,
4869 const struct quic_cid *cid,
4870 const struct sockaddr_storage *addr)
4871{
4872 unsigned char *p;
4873
4874 p = aad;
4875 memcpy(p, &version, sizeof version);
4876 p += sizeof version;
4877 p += quic_saddr_cpy(p, addr);
4878 memcpy(p, cid->data, cid->len);
4879 p += cid->len;
4880
4881 return p - aad;
4882}
4883
4884/* QUIC server only function.
4885 * Generate the token to be used in Retry packets. The token is written to
4886 * <buf> whith <len> as length. <odcid> is the original destination connection
4887 * ID and <dcid> is our side destination connection ID (or client source
4888 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004889 * Returns the length of the encoded token or 0 on error.
4890 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004891static int quic_generate_retry_token(unsigned char *buf, size_t len,
4892 const uint32_t version,
4893 const struct quic_cid *odcid,
4894 const struct quic_cid *dcid,
4895 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004896{
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004897 unsigned char *p;
4898 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4899 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4900 size_t aadlen;
4901 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
4902 unsigned char key[QUIC_TLS_KEY_LEN];
4903 unsigned char iv[QUIC_TLS_IV_LEN];
4904 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4905 size_t seclen = strlen(global.cluster_secret);
4906 EVP_CIPHER_CTX *ctx = NULL;
4907 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4908 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004909
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004910 /* We copy the odcid into the token, prefixed by its one byte
4911 * length, the format token byte. It is followed by an AEAD TAG, and finally
4912 * the random bytes used to derive the secret to encrypt the token.
4913 */
4914 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004915 return 0;
4916
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004917 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
4918 if (RAND_bytes(salt, sizeof salt) != 1)
4919 goto err;
4920
4921 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
4922 salt, sizeof salt, sec, seclen))
4923 goto err;
4924
4925 if (!quic_tls_tx_ctx_init(&ctx, aead, key))
4926 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004927
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004928 /* Token build */
4929 p = buf;
4930 *p++ = QUIC_TOKEN_FMT_RETRY,
4931 *p++ = odcid->len;
4932 memcpy(p, odcid->data, odcid->len);
4933 p += odcid->len;
4934 write_u32(p, htonl(timestamp));
4935 p += sizeof timestamp;
4936
4937 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
4938 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv))
4939 goto err;
4940
4941 p += QUIC_TLS_TAG_LEN;
4942 memcpy(p, salt, sizeof salt);
4943 p += sizeof salt;
4944 EVP_CIPHER_CTX_free(ctx);
4945
4946 return p - buf;
4947
4948 err:
4949 if (ctx)
4950 EVP_CIPHER_CTX_free(ctx);
4951 return 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004952}
4953
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004954/* QUIC server only function.
4955 * Check the validity of the Retry token from <token> buffer with <tokenlen>
4956 * as length. If valid, the ODCID of <qc> QUIC connection will be put
4957 * into <odcid> connection ID. <dcid> is our side destination connection ID
4958 * of client source connection ID.
4959 * Return 1 if succeeded, 0 if not.
4960 */
4961static int quic_retry_token_check(unsigned char *token, size_t tokenlen,
4962 const uint32_t version,
4963 struct quic_cid *odcid,
4964 const struct quic_cid *dcid,
4965 struct quic_conn *qc,
4966 struct sockaddr_storage *addr)
4967{
4968 unsigned char buf[128];
4969 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4970 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4971 size_t aadlen;
4972 unsigned char *salt;
4973 unsigned char key[QUIC_TLS_KEY_LEN];
4974 unsigned char iv[QUIC_TLS_IV_LEN];
4975 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4976 size_t seclen = strlen(global.cluster_secret);
4977 EVP_CIPHER_CTX *ctx = NULL;
4978 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4979
4980 if (sizeof buf < tokenlen)
4981 return 0;
4982
4983 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
4984 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
4985 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
4986 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen))
4987 return 0;
4988
4989 if (!quic_tls_rx_ctx_init(&ctx, aead, key))
4990 goto err;
4991
4992 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
4993 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
4994 ctx, aead, key, iv))
4995 goto err;
4996
4997 if (parse_retry_token(buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
4998 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4999 goto err;
5000 }
5001
5002 EVP_CIPHER_CTX_free(ctx);
5003 return 1;
5004
5005 err:
5006 if (ctx)
5007 EVP_CIPHER_CTX_free(ctx);
5008 return 0;
5009}
5010
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005011/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
5012 * the Initial <pkt> packet.
5013 *
5014 * Returns 0 on success else non-zero.
5015 */
5016static int send_retry(int fd, struct sockaddr_storage *addr,
5017 struct quic_rx_packet *pkt)
5018{
5019 unsigned char buf[128];
5020 int i = 0, token_len;
5021 const socklen_t addrlen = get_addr_len(addr);
5022 struct quic_cid scid;
5023
5024 /* long header + fixed bit + packet type 0x3 */
5025 buf[i++] = 0xf0;
5026 /* version */
5027 buf[i++] = 0x00;
5028 buf[i++] = 0x00;
5029 buf[i++] = 0x00;
5030 buf[i++] = 0x01;
5031
5032 /* Use the SCID from <pkt> for Retry DCID. */
5033 buf[i++] = pkt->scid.len;
5034 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
5035 i += pkt->scid.len;
5036
5037 /* Generate a new CID to be used as SCID for the Retry packet. */
5038 scid.len = QUIC_HAP_CID_LEN;
5039 if (RAND_bytes(scid.data, scid.len) != 1)
5040 return 1;
5041
5042 buf[i++] = scid.len;
5043 memcpy(&buf[i], scid.data, scid.len);
5044 i += scid.len;
5045
5046 /* token */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005047 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, pkt->version,
5048 &pkt->dcid, &pkt->scid, addr)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005049 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01005050
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005051 i += token_len;
5052
5053 /* token integrity tag */
5054 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
5055 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
5056 pkt->dcid.len, buf, i)) {
5057 return 1;
5058 }
5059
5060 i += QUIC_TLS_TAG_LEN;
5061
5062 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
5063 return 1;
5064
5065 return 0;
5066}
5067
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005068/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5069 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5070 * concatenated to the <pkt> DCID field.
5071 *
5072 * Returns the instance or NULL if not found.
5073 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005074static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005075 struct listener *l,
5076 struct sockaddr_storage *saddr)
5077{
5078 struct quic_conn *qc = NULL;
5079 struct ebmb_node *node;
5080 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005081 /* set if the quic_conn is found in the second DCID tree */
5082 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005083
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005084 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5085 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5086 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5087 /* DCIDs of first packets coming from multiple clients may have
5088 * the same values. Let's distinguish them by concatenating the
5089 * socket addresses.
5090 */
5091 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005092 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005093 pkt->dcid.len + pkt->dcid.addrlen);
5094 if (node) {
5095 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5096 goto end;
5097 }
5098 }
5099
5100 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5101 * also for INITIAL/0-RTT non-first packets with the final DCID in
5102 * used.
5103 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005104 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005105 if (!node)
5106 goto end;
5107
5108 id = ebmb_entry(node, struct quic_connection_id, node);
5109 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005110 found_in_dcid = 1;
5111
5112 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005113 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5114 * If already done, this is a noop.
5115 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01005116 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005117 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005118
5119 return qc;
5120}
5121
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005122/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5123 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5124 * parameters of this session.
5125 * This is the responsibility of the caller to check the validity of all the
5126 * pointers passed as parameter to this function.
5127 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5128 * CO_ER_SSL_NO_MEM.
5129 */
5130static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5131 unsigned char *params, size_t params_len)
5132{
5133 int retry;
5134
5135 retry = 1;
5136 retry:
5137 *ssl = SSL_new(ssl_ctx);
5138 if (!*ssl) {
5139 if (!retry--)
5140 goto err;
5141
5142 pool_gc(NULL);
5143 goto retry;
5144 }
5145
5146 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5147 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
5148 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005149 SSL_free(*ssl);
5150 *ssl = NULL;
5151 if (!retry--)
5152 goto err;
5153
5154 pool_gc(NULL);
5155 goto retry;
5156 }
5157
5158 return 0;
5159
5160 err:
5161 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5162 return -1;
5163}
5164
5165/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5166 * used to process <qc> received packets. The allocated context is stored in
5167 * <qc.xprt_ctx>.
5168 *
5169 * Returns 0 on success else non-zero.
5170 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005171static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005172{
5173 struct bind_conf *bc = qc->li->bind_conf;
5174 struct ssl_sock_ctx *ctx = NULL;
5175
5176 ctx = pool_zalloc(pool_head_quic_conn_ctx);
5177 if (!ctx)
5178 goto err;
5179
5180 ctx->wait_event.tasklet = tasklet_new();
5181 if (!ctx->wait_event.tasklet)
5182 goto err;
5183
5184 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5185 ctx->wait_event.tasklet->context = ctx;
5186 ctx->wait_event.events = 0;
5187 ctx->subs = NULL;
5188 ctx->xprt_ctx = NULL;
5189 ctx->qc = qc;
5190
5191 /* Set tasklet tid based on the SCID selected by us for this
5192 * connection. The upper layer will also be binded on the same thread.
5193 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005194 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005195
5196 if (qc_is_listener(qc)) {
5197 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5198 qc->enc_params, qc->enc_params_len) == -1) {
5199 goto err;
5200 }
5201
5202 /* Enabling 0-RTT */
5203 if (bc->ssl_conf.early_data)
5204 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5205
5206 SSL_set_accept_state(ctx->ssl);
5207 }
5208
5209 ctx->xprt = xprt_get(XPRT_QUIC);
5210
5211 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005212 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005213
5214 return 0;
5215
5216 err:
5217 if (ctx && ctx->wait_event.tasklet)
5218 tasklet_free(ctx->wait_event.tasklet);
5219 pool_free(pool_head_quic_conn_ctx, ctx);
5220
5221 return 1;
5222}
5223
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005224/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5225 * end of this buffer past one byte and populate <pkt> RX packet structure
5226 * with the information collected from the packet.
5227 * This function sets ->len <pkt> field value to the end of the packet past one
5228 * byte to enable the caller to run this function again to continue to parse
5229 * the remaing QUIC packets carried by the datagram.
5230 * Note that this function always sets this ->len value. If a paquet could
5231 * not be correctly found, ->len value will be set to the remaining number
5232 * bytes in the datagram to entirely consume this latter.
5233 */
5234static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5235 struct quic_rx_packet *pkt, int first_pkt,
5236 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005238 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005239 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005240 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005241 struct proxy *prx;
5242 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005243 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005244 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005245 size_t b_cspace;
5246 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005247
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005248 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005249 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005250 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005251 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005252 TRACE_ENTER(QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005253 l = dgram->owner;
5254 prx = l->bind_conf->frontend;
5255 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005256 /* This ist only to please to traces and distinguish the
5257 * packet with parsed packet number from others.
5258 */
5259 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005260 if (end <= buf) {
5261 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5262 goto drop;
5263 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005264
5265 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005266 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005267 /* XXX TO BE DISCARDED */
5268 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005269 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005270 }
5271
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005272 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005273 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005274 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005275 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005276 struct quic_cid odcid;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005277
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005278 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005279 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005280 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005281 }
5282
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005283 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5284 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005285 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005286 }
5287 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5288 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005289 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005290 drop_no_conn = 1;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005291 }
5292
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005293 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5294 * they must have the same DCID.
5295 */
5296 if (!first_pkt &&
5297 (pkt->dcid.len != dgram->dcid_len ||
5298 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5299 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005300 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005301 }
5302
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005303 /* Retry of Version Negotiation packets are only sent by servers */
5304 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
5305 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005306 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005307 }
5308
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005309 /* RFC9000 6. Version Negotiation */
5310 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005311 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005312 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005313 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
5314 goto err;
5315 }
5316
5317 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005318 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005319 }
5320
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005321 /* For Initial packets, and for servers (QUIC clients connections),
5322 * there is no Initial connection IDs storage.
5323 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005324 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005325 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005326
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005327 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5328 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005329 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005330 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005331 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005332
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005333 /* TODO Retry should be automatically activated if
5334 * suspect network usage is detected.
5335 */
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005336 if (global.cluster_secret) {
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005337 if (!token_len) {
Willy Tarreau787e92a2022-05-20 16:06:01 +02005338 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005339 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
5340 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
5341 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
5342 goto err;
5343 }
5344
5345 HA_ATOMIC_INC(&prx_counters->retry_sent);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005346 goto err;
5347 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005348 }
5349 else {
5350 if (*buf == QUIC_TOKEN_FMT_RETRY) {
5351 if (!quic_retry_token_check(buf, token_len, pkt->version, &odcid,
5352 &pkt->scid, qc, &dgram->saddr)) {
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005353 HA_ATOMIC_INC(&prx_counters->retry_error);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005354 TRACE_PROTO("Wrong retry token", QUIC_EV_CONN_LPKT);
5355 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5356 * with an INVALID_TOKEN error.
5357 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005358 goto drop;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005359 }
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005360
5361 HA_ATOMIC_INC(&prx_counters->retry_validated);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005362 }
5363 else {
5364 /* TODO: New token check */
5365 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005366 goto drop;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005367 }
5368 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005369 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005370
5371 pkt->token = buf;
5372 pkt->token_len = token_len;
5373 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005374 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005375 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005376 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005377 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005378 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005379 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005380 }
5381
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005382 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5383 end - buf < len) {
5384 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005385 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005386 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005387
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005388 payload = buf;
5389 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005390 if (drop_no_conn)
5391 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005392
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005393 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005394 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005395 int ipv4;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005396 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005397
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005398 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01005399 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005400 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005401 }
5402
Willy Tarreau787e92a2022-05-20 16:06:01 +02005403 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005404 HA_ATOMIC_LOAD(&prx_counters->conn_opening) >= global.tune.quic_retry_threshold) {
5405 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
5406 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
5407 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
5408 goto err;
5409 }
5410
5411 HA_ATOMIC_INC(&prx_counters->retry_sent);
5412 goto err;
5413 }
5414
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005415 /* RFC 9000 7.2. Negotiating Connection IDs:
5416 * When an Initial packet is sent by a client that has not previously
5417 * received an Initial or Retry packet from the server, the client
5418 * populates the Destination Connection ID field with an unpredictable
5419 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5420 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01005421 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
5422 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005423 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01005424 }
5425
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005426 pkt->saddr = dgram->saddr;
5427 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005428 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &odcid, &pkt->saddr,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02005429 pkt->token, pkt->token_len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005430 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005431 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005432
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005433 HA_ATOMIC_INC(&prx_counters->conn_opening);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005434 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005435 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005436 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005437
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005438 /* If the insertion failed, it means that another
5439 * thread has already allocated a QUIC connection for
5440 * the same CID. Liberate our allocated connection.
5441 */
5442 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005443 qc_to_purge = qc;
5444
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005445 qc = ebmb_entry(n, struct quic_conn, odcid_node);
5446 pkt->qc = qc;
5447 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005448
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005449 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005450 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01005451 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005452 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005453 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01005454 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005455 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005456 }
5457 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005458 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005459 }
5460 }
5461 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005462 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005463 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005464 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005465 }
5466
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005467 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005468 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005469
5470 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5471 * they must have the same DCID.
5472 */
5473 if (!first_pkt &&
5474 (pkt->dcid.len != dgram->dcid_len ||
5475 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5476 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005477 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005478 }
5479
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005480 buf += QUIC_HAP_CID_LEN;
5481
5482 /* A short packet is the last one of a UDP datagram. */
5483 payload = buf;
5484 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005485
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005486 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005487 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005488 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005489 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005490 if (global.cluster_secret && !send_stateless_reset(l->rx.fd, &dgram->saddr, pkt))
5491 TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005492 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005493 }
5494
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005495 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005496 }
5497
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005498 if (qc->flags & QUIC_FL_CONN_CLOSING) {
5499 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
5500 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
5501 qc->nb_pkt_for_cc++;
5502 qc->nb_pkt_since_cc = 0;
5503 }
5504 /* Skip the entire datagram */
5505 pkt->len = end - beg;
5506 TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005507 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005508 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005509
5510 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5511 * they must have the same DCID.
5512 *
5513 * This check must be done after the final update to pkt.len to
5514 * properly drop the packet on failure.
5515 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005516 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005517 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005518 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
5519 QUIC_EV_CONN_LPKT, qc);
5520 /* Reset the anti-amplification bit. It will be set again
5521 * when sending the next packet if reached again.
5522 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005523 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
5524 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005525 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005526 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005527
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005528 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005529
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005530 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005531 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005532 goto out;
5533 }
5534
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005535 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005536 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005537 b_cspace = b_contig_space(&qc->rx.buf);
5538 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02005539 /* Do not consume buf if space not at the end. */
5540 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
5541 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
5542 goto err;
5543 }
5544
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005545 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005546 if (b_cspace) {
5547 b_putchr(&qc->rx.buf, 0x00);
5548 b_cspace--;
5549 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005550 b_add(&qc->rx.buf, b_cspace);
5551 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005552 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01005553 qc_list_all_rx_pkts(qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005554 goto drop;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005555 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005556 }
5557
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005558 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005559 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005560 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02005561 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005562
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005563 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005564 if (pkt->aad_len)
5565 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005566 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02005567 /* Wake up the connection packet handler task from here only if all
5568 * the contexts have been initialized, especially the mux context
5569 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
5570 * will start it if these contexts for the connection are not already
5571 * initialized.
5572 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005573 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005574 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005575 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005576
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005577 drop_no_conn:
5578 if (drop_no_conn)
5579 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005580 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005581
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005582 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005583
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005584 drop:
5585 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005586 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005587 /* Wakeup the I/O handler callback if the PTO timer must be armed.
5588 * This cannot be done by this thread.
5589 */
5590 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005591 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005592 if (conn_ctx && conn_ctx->wait_event.tasklet)
5593 tasklet_wakeup(conn_ctx->wait_event.tasklet);
5594 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01005595 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005596 if (!pkt->len)
5597 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005598 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005599 qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005600}
5601
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005602/* This function builds into <buf> buffer a QUIC long packet header.
5603 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005604 */
5605static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
5606 int type, size_t pn_len, struct quic_conn *conn)
5607{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005608 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005609 return 0;
5610
5611 /* #0 byte flags */
5612 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5613 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5614 /* Version */
5615 quic_write_uint32(buf, end, conn->version);
5616 *(*buf)++ = conn->dcid.len;
5617 /* Destination connection ID */
5618 if (conn->dcid.len) {
5619 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5620 *buf += conn->dcid.len;
5621 }
5622 /* Source connection ID */
5623 *(*buf)++ = conn->scid.len;
5624 if (conn->scid.len) {
5625 memcpy(*buf, conn->scid.data, conn->scid.len);
5626 *buf += conn->scid.len;
5627 }
5628
5629 return 1;
5630}
5631
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005632/* This function builds into <buf> buffer a QUIC short packet header.
5633 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005634 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005635static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5636 size_t pn_len, struct quic_conn *conn,
5637 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005638{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005639 if (end - *buf < 1 + conn->dcid.len)
5640 return 0;
5641
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005642 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005643 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5644 ((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 +01005645 /* Destination connection ID */
5646 if (conn->dcid.len) {
5647 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5648 *buf += conn->dcid.len;
5649 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005650
5651 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005652}
5653
5654/* Apply QUIC header protection to the packet with <buf> as first byte address,
5655 * <pn> as address of the Packet number field, <pnlen> being this field length
5656 * with <aead> as AEAD cipher and <key> as secret key.
5657 * Returns 1 if succeeded or 0 if failed.
5658 */
5659static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5660 const EVP_CIPHER *aead, const unsigned char *key)
5661{
5662 int i, ret, outlen;
5663 EVP_CIPHER_CTX *ctx;
5664 /* We need an IV of at least 5 bytes: one byte for bytes #0
5665 * and at most 4 bytes for the packet number
5666 */
5667 unsigned char mask[5] = {0};
5668
5669 ret = 0;
5670 ctx = EVP_CIPHER_CTX_new();
5671 if (!ctx)
5672 return 0;
5673
5674 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5675 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5676 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5677 goto out;
5678
5679 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5680 for (i = 0; i < pnlen; i++)
5681 pn[i] ^= mask[i + 1];
5682
5683 ret = 1;
5684
5685 out:
5686 EVP_CIPHER_CTX_free(ctx);
5687
5688 return ret;
5689}
5690
5691/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5692 * ACK ranges if needed to a value below <limit> in bytes.
5693 * Return 1 if succeeded, 0 if not.
5694 */
5695static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5696{
5697 size_t room, ack_delay_sz;
5698
5699 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5700 /* A frame is made of 1 byte for the frame type. */
5701 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005702 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005703 return 0;
5704
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005705 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005706}
5707
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005708/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5709 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5710 * with <room> as available room, and <*len> the packet Length field initialized
5711 * with the number of bytes already present in this buffer which must be taken
5712 * into an account for the Length packet field value. <headlen> is the number of
5713 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005714 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005715 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005716 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005717 * 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 +01005718 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005719static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005720 size_t room, size_t *len, size_t headlen,
5721 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005722 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005723{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005724 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005725 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005726
Frédéric Lécailleea604992020-12-24 13:01:37 +01005727 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005728 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005729 return 0;
5730
Frédéric Lécailleea604992020-12-24 13:01:37 +01005731 /* If we are not probing we must take into an account the congestion
5732 * control window.
5733 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005734 if (!qel->pktns->tx.pto_probe) {
5735 size_t remain = quic_path_prep_data(qc->path);
5736
5737 if (headlen > remain)
5738 return 0;
5739
5740 room = QUIC_MIN(room, remain - headlen);
5741 }
5742
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005743 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005744 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005745
5746 /* NOTE: switch/case block inside a loop, a successful status must be
5747 * returned by this function only if at least one frame could be built
5748 * in the switch/case block.
5749 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005750 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005751 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005752 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005753
Frédéric Lécailleea604992020-12-24 13:01:37 +01005754 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005755 break;
5756
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005757 switch (cf->type) {
5758 case QUIC_FT_CRYPTO:
5759 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005760 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005761 /* Compute the length of this CRYPTO frame header */
5762 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5763 /* Compute the data length of this CRyPTO frame. */
5764 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5765 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005766 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005767 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005768 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005769
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005770 /* CRYPTO frame length. */
5771 flen = hlen + quic_int_getsize(dlen) + dlen;
5772 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005773 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005774 /* Add the CRYPTO data length and its encoded length to the packet
5775 * length and the length of this length.
5776 */
5777 *len += flen;
5778 room -= flen;
5779 if (dlen == cf->crypto.len) {
5780 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005781 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005782 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005783 }
5784 else {
5785 struct quic_frame *new_cf;
5786
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005787 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005788 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005789 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005790 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005791 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005792
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005793 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005794 new_cf->type = QUIC_FT_CRYPTO;
5795 new_cf->crypto.len = dlen;
5796 new_cf->crypto.offset = cf->crypto.offset;
5797 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005798 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005799 /* Consume <dlen> bytes of the current frame. */
5800 cf->crypto.len -= dlen;
5801 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005802 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005803 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005804
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005805 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005806 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
5807 struct eb64_node *node = NULL;
5808 struct qc_stream_desc *stream_desc = NULL;
5809 struct quic_stream *strm = &cf->stream;
5810
5811 /* As this frame has been already lost, ensure the stream is always
5812 * available or the range of this frame is not consumed before
5813 * resending it.
5814 */
5815 node = eb64_lookup(&qc->streams_by_id, strm->id);
5816 if (!node) {
5817 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm);
5818 LIST_DELETE(&cf->list);
5819 pool_free(pool_head_quic_frame, cf);
5820 continue;
5821 }
5822
5823 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
5824 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
5825 TRACE_PROTO("ignored frame frame in already acked range",
5826 QUIC_EV_CONN_PRSAFRM, qc, strm);
5827 LIST_DELETE(&cf->list);
5828 pool_free(pool_head_quic_frame, cf);
5829 continue;
5830 }
5831 else if (strm->offset.key < stream_desc->ack_offset) {
5832 strm->offset.key = stream_desc->ack_offset;
5833 TRACE_PROTO("updated partially acked frame",
5834 QUIC_EV_CONN_PRSAFRM, qc, strm);
5835 }
5836 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005837 /* Note that these frames are accepted in short packets only without
5838 * "Length" packet field. Here, <*len> is used only to compute the
5839 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005840 *
5841 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005842 * excepting the variable ones. Note that +1 is for the type of this frame.
5843 */
5844 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005845 ((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 +02005846 /* Compute the data length of this STREAM frame. */
5847 avail_room = room - hlen - *len;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005848 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005849 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005850 if ((ssize_t)avail_room <= 0)
5851 continue;
5852
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005853 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5854 dlen = max_available_room(avail_room, &dlen_sz);
5855 if (dlen > cf->stream.len) {
5856 dlen = cf->stream.len;
5857 }
5858 dlen_sz = quic_int_getsize(dlen);
5859 flen = hlen + dlen_sz + dlen;
5860 }
5861 else {
5862 dlen = QUIC_MIN(avail_room, cf->stream.len);
5863 flen = hlen + dlen;
5864 }
5865 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005866 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005867 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005868 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005869 /* Add the STREAM data length and its encoded length to the packet
5870 * length and the length of this length.
5871 */
5872 *len += flen;
5873 room -= flen;
5874 if (dlen == cf->stream.len) {
5875 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005876 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005877 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005878
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005879 /* The MUX stream might be released at this
5880 * stage. This can most notably happen on
5881 * retransmission.
5882 */
5883 if (qc->mux_state == QC_MUX_READY &&
5884 !cf->stream.stream->release) {
5885 qcc_streams_sent_done(cf->stream.stream->ctx,
5886 cf->stream.len,
5887 cf->stream.offset.key);
5888 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005889 }
5890 else {
5891 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005892 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005893
5894 new_cf = pool_zalloc(pool_head_quic_frame);
5895 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005896 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005897 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005898 }
5899
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005900 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005901 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005902 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005903 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005904 new_cf->stream.id = cf->stream.id;
5905 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5906 new_cf->stream.offset = cf->stream.offset;
5907 new_cf->stream.len = dlen;
5908 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5909 /* FIN bit reset */
5910 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5911 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005912 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005913 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5914 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005915 cf_buf = b_make(b_orig(cf->stream.buf),
5916 b_size(cf->stream.buf),
5917 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005918 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005919 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005920 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005921
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005922 /* The MUX stream might be released at this
5923 * stage. This can most notably happen on
5924 * retransmission.
5925 */
5926 if (qc->mux_state == QC_MUX_READY &&
5927 !cf->stream.stream->release) {
5928 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5929 new_cf->stream.len,
5930 new_cf->stream.offset.key);
5931 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005932 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005933
5934 /* TODO the MUX is notified about the frame sending via
5935 * previous qcc_streams_sent_done call. However, the
5936 * sending can fail later, for example if the sendto
5937 * system call returns an error. As the MUX has been
5938 * notified, the transport layer is responsible to
5939 * bufferize and resent the announced data later.
5940 */
5941
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005942 break;
5943
5944 default:
5945 flen = qc_frm_len(cf);
5946 BUG_ON(!flen);
5947 if (flen > room)
5948 continue;
5949
5950 *len += flen;
5951 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005952 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005953 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005954 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005955 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005956
5957 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01005958 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005959 }
5960
Frédéric Lécailleea604992020-12-24 13:01:37 +01005961 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005962}
5963
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005964/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005965 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5966 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005967 * filling the buffer with as much frames as possible from <frms> list of
5968 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005969 * 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 +02005970 * 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 +02005971 * having returned from this function.
5972 * 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 +01005973 * number field in this packet. <pn_len> will also have the packet number
5974 * length as value.
5975 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005976 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005977 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005978static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5979 size_t dglen, struct quic_tx_packet *pkt,
5980 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005981 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005982 struct quic_enc_level *qel, struct quic_conn *qc,
5983 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005984{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005985 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005986 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005987 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5988 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005989 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005990 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005991 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005992 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005993 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005994 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005995
Frédéric Lécailleea604992020-12-24 13:01:37 +01005996 /* Length field value with CRYPTO frames if present. */
5997 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005998 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005999 /* When not probing, and no immediate close is required, reduce the size of this
6000 * buffer to respect the congestion controller window.
6001 * 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 +01006002 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006003 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006004 size_t path_room;
6005
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006006 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006007 if (end - beg > path_room)
6008 end = beg + path_room;
6009 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006010
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006011 /* Ensure there is enough room for the TLS encryption tag and a zero token
6012 * length field if any.
6013 */
6014 if (end - pos < QUIC_TLS_TAG_LEN +
6015 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6016 goto no_room;
6017
6018 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006019 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006020 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006021 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006022 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006023 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006024 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006025 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006026 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006027 goto no_room;
6028
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006029 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
6030 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006031 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006032 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006033 /* Build an ACK frame if required. */
6034 ack_frm_len = 0;
Frédéric Lécaille337108e2022-04-25 10:38:27 +02006035 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006036 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006037 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006038 ack_frm.tx_ack.arngs = arngs;
6039 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6040 qel->pktns->tx.ack_delay =
6041 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6042 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6043 }
6044 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006045 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6046 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6047 * that from here, we do not know if we will have to send a PING frame.
6048 * This will be decided after having computed the ack-eliciting frames
6049 * to be added to this packet.
6050 */
6051 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 +01006052 if (!ack_frm_len)
6053 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006054 }
6055
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006056 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006057 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006058 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006059 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006060 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006061
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006062 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006063 /* Initialize the length of the frames built below to <len>.
6064 * If any frame could be successfully built by qc_build_frms(),
6065 * we will have len_frms > len.
6066 */
6067 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006068 if (!qc_build_frms(&frm_list, frms,
6069 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01006070 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006071 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006072 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6073 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006074 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006075 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006076
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006077 /* Length (of the remaining data). Must not fail because, the buffer size
6078 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6079 * for the encryption tag. It must be taken into an account for the length
6080 * of this packet.
6081 */
6082 if (len_frms)
6083 len = len_frms + QUIC_TLS_TAG_LEN;
6084 else
6085 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006086 /* CONNECTION_CLOSE frame */
6087 if (cc) {
6088 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006089
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006090 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006091 len += qc_frm_len(&cc_frm);
6092 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006093 add_ping_frm = 0;
6094 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006095 len_sz = quic_int_getsize(len);
6096 /* Add this packet size to <dglen> */
6097 dglen += head_len + len_sz + len;
6098 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6099 /* This is a maximum padding size */
6100 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6101 /* The length field value is of this packet is <len> + <padding_len>
6102 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006103 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006104 * sizes from <padding_len>.
6105 */
6106 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6107 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006108 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006109 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006110 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006111 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006112 add_ping_frm = 1;
6113 len += 1;
6114 }
6115 /* 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 +01006116 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006117 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6118 }
6119
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006120 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6121 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006122
6123 /* Packet number field address. */
6124 *buf_pn = pos;
6125
6126 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006127 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6128 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006129
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006130 if (ack_frm_len) {
6131 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6132 goto no_room;
6133
6134 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006135 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006136 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006137
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006138 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006139 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006140 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006141 unsigned char *spos = pos;
6142
6143 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006144 ssize_t room = end - pos;
6145 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006146 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006147 /* TODO: this should not have happened if qc_build_frms()
6148 * had correctly computed and sized the frames to be
6149 * added to this packet. Note that <cf> was added
6150 * from <frm_list> to <frms> list by qc_build_frms().
6151 */
6152 LIST_DELETE(&cf->list);
6153 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006154 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006155 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006156
6157 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006158 quic_tx_packet_refinc(pkt);
6159 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006160 }
6161 }
6162
6163 /* Build a PING frame if needed. */
6164 if (add_ping_frm) {
6165 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006166 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006167 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006168 }
6169
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006170 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006171 if (cc) {
6172 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6173 goto no_room;
6174
6175 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6176 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006177
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006178 /* Build a PADDING frame if needed. */
6179 if (padding_len) {
6180 frm.type = QUIC_FT_PADDING;
6181 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006182 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006183 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006184 }
6185
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006186 /* If this packet is ack-eliciting and we are probing let's
6187 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006188 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006189 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6190 qel->pktns->tx.pto_probe)
6191 qel->pktns->tx.pto_probe--;
6192
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006193 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006194 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006195 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006196
6197 return 1;
6198
6199 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006200 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006201 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailled8b798d2022-04-25 17:48:40 +02006202 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006203
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006204 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006205}
6206
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006207static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006208{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006209 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006210 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006211 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006212 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006213 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006214 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006215 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006216 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006217 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006218 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006219}
6220
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006221/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006222 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6223 * of prebuilt frames.
6224 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006225 * Return -2 if the packet could not be allocated or encrypted for any reason,
6226 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006227 * XXX NOTE XXX
6228 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6229 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6230 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006231 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006232static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6233 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006234 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006235 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006236 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006237{
6238 /* The pointer to the packet number field. */
6239 unsigned char *buf_pn;
6240 unsigned char *beg, *end, *payload;
6241 int64_t pn;
6242 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006243 struct quic_tls_ctx *tls_ctx;
6244 struct quic_tx_packet *pkt;
6245
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006246 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006247 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006248 pkt = pool_alloc(pool_head_quic_tx_packet);
6249 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006250 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006251 *err = -2;
6252 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006253 }
6254
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006255 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006256 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006257 pn_len = 0;
6258 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006259
6260 pn = qel->pktns->tx.next_pn + 1;
6261 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 +02006262 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006263 *err = -1;
6264 goto err;
6265 }
6266
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006267 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006268 payload = buf_pn + pn_len;
6269 payload_len = end - payload;
6270 aad_len = payload - beg;
6271
6272 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006273 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006274 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006275 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006276 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006277
6278 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006279 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006280 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
6281 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006282 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006283 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006284 goto err;
6285 }
6286
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006287 /* Consume a packet number */
6288 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006289 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01006290 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006291 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006292 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6293 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006294 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006295 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006296 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006297 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006298 pkt->in_flight_len = pkt->len;
6299 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006300 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006301 /* Always reset this flags */
6302 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006303 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6304 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6305 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6306 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006307
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006308 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006309 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006310
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006311 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006312
6313 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006314 /* TODO: what about the frames which have been built
6315 * for this packet.
6316 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006317 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006318 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006319 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006320}
6321
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006322
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006323/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6324 * event subscriber <es> is not allowed to change from a previous call as long
6325 * as at least one event is still subscribed. The <event_type> must only be a
6326 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6327 */
6328static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6329{
Willy Tarreau784b8682022-04-11 14:18:10 +02006330 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006331
6332 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6333 BUG_ON(qcc->subs && qcc->subs != es);
6334
6335 es->events |= event_type;
6336 qcc->subs = es;
6337
6338 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02006339 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006340
6341 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02006342 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006343
6344 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006345}
6346
6347/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6348 * The <es> pointer is not allowed to differ from the one passed to the
6349 * subscribe() call. It always returns zero.
6350 */
6351static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6352{
6353 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
6354}
6355
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006356/* Store in <xprt_ctx> the context attached to <conn>.
6357 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006358 */
6359static int qc_conn_init(struct connection *conn, void **xprt_ctx)
6360{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01006361 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006362
6363 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
6364
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006365 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006366 if (*xprt_ctx)
6367 goto out;
6368
Willy Tarreau784b8682022-04-11 14:18:10 +02006369 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006370
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006371 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01006372 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006373
6374 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006375}
6376
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006377/* Start the QUIC transport layer */
6378static int qc_xprt_start(struct connection *conn, void *ctx)
6379{
6380 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02006381 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006382
Willy Tarreau784b8682022-04-11 14:18:10 +02006383 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006384 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
6385 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02006386 /* prepare a CONNECTION_CLOSE frame */
6387 qc->err_code = QC_ERR_APPLICATION_ERROR;
6388 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle05d4ae62022-04-13 17:40:26 +02006389 return -1;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006390 }
6391
6392 /* mux-quic can now be considered ready. */
6393 qc->mux_state = QC_MUX_READY;
6394
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006395 tasklet_wakeup(qctx->wait_event.tasklet);
6396 return 1;
6397}
6398
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006399static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
6400{
Willy Tarreau784b8682022-04-11 14:18:10 +02006401 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006402 return NULL;
6403
Willy Tarreau784b8682022-04-11 14:18:10 +02006404 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006405}
6406
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006407/* transport-layer operations for QUIC connections. */
6408static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02006409 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006410 .subscribe = quic_conn_subscribe,
6411 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006412 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006413 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006414 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
6415 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01006416 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006417 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006418 .name = "QUIC",
6419};
6420
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006421static void __quic_conn_init(void)
6422{
6423 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
6424 xprt_register(XPRT_QUIC, &ssl_quic);
6425}
Willy Tarreau79367f92022-04-25 19:18:24 +02006426INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006427
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006428static void __quic_conn_deinit(void)
6429{
6430 BIO_meth_free(ha_quic_meth);
6431}
Willy Tarreau79367f92022-04-25 19:18:24 +02006432REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006433
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006434/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
6435 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05006436 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006437 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006438struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006439{
6440 unsigned char *pos;
6441 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006442 struct quic_dghdlr *dghdlr = ctx;
6443 struct quic_dgram *dgram;
6444 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006445
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006446 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006447 pos = dgram->buf;
6448 end = pos + dgram->len;
6449 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006450 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006451
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006452 pkt = pool_zalloc(pool_head_quic_rx_packet);
6453 if (!pkt)
6454 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006455
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006456 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006457 quic_rx_packet_refinc(pkt);
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006458 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006459 first_pkt = 0;
6460 pos += pkt->len;
6461 quic_rx_packet_refdec(pkt);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006462 } while (pos < end);
6463
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006464 /* Increasing the received bytes counter by the UDP datagram length
6465 * if this datagram could be associated to a connection.
6466 */
6467 if (dgram->qc)
6468 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01006469
6470 /* Mark this datagram as consumed */
6471 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006472 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006473
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006474 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006475
6476 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006477 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006478}
6479
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006480/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
6481 * Returns 1 if succeeded, 0 if not.
6482 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02006483int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
6484 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006485{
6486 int long_header;
6487 size_t minlen, skip;
6488
6489 if (!(*buf & QUIC_PACKET_FIXED_BIT))
6490 goto err;
6491
6492 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02006493 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
6494 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006495 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01006496 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006497 goto err;
6498
6499 buf += skip;
6500 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
6501 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
6502 goto err;
6503
6504 *dcid = buf;
6505
6506 return 1;
6507
6508 err:
6509 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
6510 return 0;
6511}
6512
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02006513/* Notify the MUX layer if alive about an imminent close of <qc>. */
6514void qc_notify_close(struct quic_conn *qc)
6515{
6516 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
6517 return;
6518
6519 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
6520
6521 /* wake up the MUX */
6522 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
6523 qc->conn->mux->wake(qc->conn);
6524}
6525
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01006526/* Function to automatically activate QUIC traces on stdout.
6527 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
6528 * Main use for now is in the docker image for QUIC interop testing.
6529 */
6530static void quic_init_stdout_traces(void)
6531{
6532#ifdef ENABLE_QUIC_STDOUT_TRACES
6533 trace_quic.sink = sink_find("stdout");
6534 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01006535 trace_quic.state = TRACE_STATE_RUNNING;
6536#endif
6537}
6538INITCALL0(STG_INIT, quic_init_stdout_traces);
6539
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006540/*
6541 * Local variables:
6542 * c-indent-level: 8
6543 * c-basic-offset: 8
6544 * End:
6545 */