blob: d4f3ef0bf3e2a4aa3587f905a2e88e55c0bc4d41 [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>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010041#include <haproxy/pipe.h>
42#include <haproxy/proxy.h>
43#include <haproxy/quic_cc.h>
44#include <haproxy/quic_frame.h>
45#include <haproxy/quic_loss.h>
Amaury Denoyellecfa2d562022-01-19 16:01:05 +010046#include <haproxy/quic_sock.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020047#include <haproxy/quic_stream.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020048#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010049#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010051#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010052#include <haproxy/ssl_sock.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010053#include <haproxy/task.h>
54#include <haproxy/trace.h>
55#include <haproxy/xprt_quic.h>
56
Amaury Denoyellea22d8602021-11-10 15:17:56 +010057/* list of supported QUIC versions by this implementation */
58static int quic_supported_version[] = {
59 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010060 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010061
62 /* placeholder, do not add entry after this */
63 0x0
64};
65
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020066/* This is the values of some QUIC transport parameters when absent.
67 * Should be used to initialize any transport parameters (local or remote)
68 * before updating them with customized values.
69 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010070struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020071 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020072 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
73 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020074 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010075};
76
77/* trace source and events */
78static void quic_trace(enum trace_level level, uint64_t mask, \
79 const struct trace_source *src,
80 const struct ist where, const struct ist func,
81 const void *a1, const void *a2, const void *a3, const void *a4);
82
83static const struct trace_event quic_trace_events[] = {
84 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
85 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
86 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
87 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
88 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
89 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
90 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050091 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010092 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
93 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
94 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaille00e24002022-02-18 17:13:45 +010095 { .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 +010096 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
97 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
98 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
99 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
100 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
101 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
102 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
103 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
104 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
105 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
106 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
107 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
108 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
109 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
110 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100111 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
112 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
113 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
114 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
115 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
116 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100117 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200118 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
119 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100120 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
121 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100122 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200123 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200124 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100125 { /* end */ }
126};
127
128static const struct name_desc quic_trace_lockon_args[4] = {
129 /* arg1 */ { /* already used by the connection */ },
130 /* arg2 */ { .name="quic", .desc="QUIC transport" },
131 /* arg3 */ { },
132 /* arg4 */ { }
133};
134
135static const struct name_desc quic_trace_decoding[] = {
136#define QUIC_VERB_CLEAN 1
137 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
138 { /* end */ }
139};
140
141
142struct trace_source trace_quic = {
143 .name = IST("quic"),
144 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100145 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100146 .default_cb = quic_trace,
147 .known_events = quic_trace_events,
148 .lockon_args = quic_trace_lockon_args,
149 .decoding = quic_trace_decoding,
150 .report_events = ~0, /* report everything by default */
151};
152
153#define TRACE_SOURCE &trace_quic
154INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
155
156static BIO_METHOD *ha_quic_meth;
157
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200158DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100159DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200161 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100163DECLARE_POOL(pool_head_quic_connection_id,
164 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100165DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166DECLARE_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 +0100167DECLARE_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 +0100168DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100169DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100170DECLARE_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 +0200171DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100172DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100173
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200174static 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 +0100175 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100176 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200177 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100178static 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 +0100179static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100180static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100181
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100182/* Only for debug purpose */
183struct enc_debug_info {
184 unsigned char *payload;
185 size_t payload_len;
186 unsigned char *aad;
187 size_t aad_len;
188 uint64_t pn;
189};
190
191/* Initializes a enc_debug_info struct (only for debug purpose) */
192static inline void enc_debug_info_init(struct enc_debug_info *edi,
193 unsigned char *payload, size_t payload_len,
194 unsigned char *aad, size_t aad_len, uint64_t pn)
195{
196 edi->payload = payload;
197 edi->payload_len = payload_len;
198 edi->aad = aad;
199 edi->aad_len = aad_len;
200 edi->pn = pn;
201}
202
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100203/* Trace callback for QUIC.
204 * These traces always expect that arg1, if non-null, is of type connection.
205 */
206static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
207 const struct ist where, const struct ist func,
208 const void *a1, const void *a2, const void *a3, const void *a4)
209{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100210 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100212 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100213 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100215 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 if ((mask & QUIC_EV_CONN_INIT) && qc) {
217 chunk_appendf(&trace_buf, "\n odcid");
218 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100219 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100220 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100221 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100223 }
224
225 if (mask & QUIC_EV_CONN_ADDDATA) {
226 const enum ssl_encryption_level_t *level = a2;
227 const size_t *len = a3;
228
229 if (level) {
230 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
231
232 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
233 }
234 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100235 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100236 }
237 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
238 /* Initial read & write secrets. */
239 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
240 const unsigned char *rx_sec = a2;
241 const unsigned char *tx_sec = a3;
242
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100243 tls_ctx = &qc->els[level].tls_ctx;
244 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
246 if (rx_sec)
247 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100248 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100249 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
250 if (tx_sec)
251 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100252 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100253 }
254 }
255 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
256 const enum ssl_encryption_level_t *level = a2;
257 const unsigned char *secret = a3;
258 const size_t *secret_len = a4;
259
260 if (level) {
261 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
262
263 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
264 if (secret && secret_len)
265 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100266 tls_ctx = &qc->els[lvl].tls_ctx;
267 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
268 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100269 }
270 }
271
272 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
273 const enum ssl_encryption_level_t *level = a2;
274 const unsigned char *secret = a3;
275 const size_t *secret_len = a4;
276
277 if (level) {
278 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
279
280 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
281 if (secret && secret_len)
282 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100283 tls_ctx = &qc->els[lvl].tls_ctx;
284 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
285 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100286 }
287
288 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200290 if (mask & QUIC_EV_CONN_FRMLIST) {
291 const struct list *l = a2;
292
293 if (l) {
294 const struct quic_frame *frm;
295 list_for_each_entry(frm, l, list)
296 chunk_frm_appendf(&trace_buf, frm);
297 }
298 }
299
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100300 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100301 const struct quic_tx_packet *pkt = a2;
302 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100303 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304
305 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100306 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100307 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100308 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100309 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100310 (unsigned long long)qc->path->cwnd,
311 (unsigned long long)qc->path->prep_in_flight,
312 (unsigned long long)qc->path->in_flight,
313 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100314 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100315 }
316 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200317 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100318 if (pkt->pn_node.key != (uint64_t)-1)
319 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100320 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100321 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100322 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
323 (unsigned long long)qc->rx.bytes,
324 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100325 }
326
327 if (room) {
328 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
329 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
330 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 }
332 }
333
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100334 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100335 const enum quic_handshake_state *state = a2;
336 const int *err = a3;
337
338 if (state)
339 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
340 if (err)
341 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
342 }
343
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100344 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100345 const struct quic_rx_packet *pkt = a2;
346 const unsigned long *pktlen = a3;
347 const SSL *ssl = a4;
348
349 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200350 chunk_appendf(&trace_buf, " pkt@%p", pkt);
351 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
352 chunk_appendf(&trace_buf, " kp=%d",
353 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
354 chunk_appendf(&trace_buf, " el=%c",
355 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100356 if (pkt->pnl)
357 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
358 (unsigned long long)pkt->pn);
359 if (pkt->token_len)
360 chunk_appendf(&trace_buf, " toklen=%llu",
361 (unsigned long long)pkt->token_len);
362 if (pkt->aad_len)
363 chunk_appendf(&trace_buf, " aadlen=%llu",
364 (unsigned long long)pkt->aad_len);
365 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
366 pkt->flags, (unsigned long long)pkt->len);
367 }
368 if (pktlen)
369 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
370 if (ssl) {
371 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
372 chunk_appendf(&trace_buf, " el=%c",
373 quic_enc_level_char(ssl_to_quic_enc_level(level)));
374 }
375 }
376
377 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
378 const struct quic_rx_packet *pkt = a2;
379 const struct quic_rx_crypto_frm *cf = a3;
380 const SSL *ssl = a4;
381
382 if (pkt)
383 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
384 quic_packet_type_enc_level_char(pkt->type),
385 (unsigned long long)pkt->pn);
386 if (cf)
387 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
388 (unsigned long long)cf->offset_node.key,
389 (unsigned long long)cf->len);
390 if (ssl) {
391 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200392 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 quic_enc_level_char(ssl_to_quic_enc_level(level)));
394 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100395
396 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100397 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100398 }
399
400 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
401 const struct quic_frame *frm = a2;
402
403 if (frm)
404 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
405 }
406
407 if (mask & QUIC_EV_CONN_PHPKTS) {
408 const struct quic_enc_level *qel = a2;
409
410 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100411 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100412 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100413 " 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 +0100414 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200415 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200416 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100417 (unsigned long long)qc->path->cwnd,
418 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100419 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100420 (unsigned long long)pktns->tx.in_flight,
421 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100422 }
423 }
424
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100425 if (mask & QUIC_EV_CONN_ENCPKT) {
426 const struct enc_debug_info *edi = a2;
427
428 if (edi)
429 chunk_appendf(&trace_buf,
430 " payload=@%p payload_len=%llu"
431 " aad=@%p aad_len=%llu pn=%llu",
432 edi->payload, (unsigned long long)edi->payload_len,
433 edi->aad, (unsigned long long)edi->aad_len,
434 (unsigned long long)edi->pn);
435 }
436
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100437 if (mask & QUIC_EV_CONN_RMHP) {
438 const struct quic_rx_packet *pkt = a2;
439
440 if (pkt) {
441 const int *ret = a3;
442
443 chunk_appendf(&trace_buf, " pkt@%p", pkt);
444 if (ret && *ret)
445 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
446 pkt->pnl, (unsigned long long)pkt->pn);
447 }
448 }
449
450 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200451 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100452 const unsigned long *val1 = a3;
453 const unsigned long *val2 = a4;
454
455 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100456 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100457 if (val1)
458 chunk_appendf(&trace_buf, " %lu", *val1);
459 if (val2)
460 chunk_appendf(&trace_buf, "..%lu", *val2);
461 }
462
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100463 if (mask & QUIC_EV_CONN_ACKSTRM) {
464 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200465 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100466
467 if (s)
468 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200469 if (stream)
470 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100471 }
472
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100473 if (mask & QUIC_EV_CONN_RTTUPDT) {
474 const unsigned int *rtt_sample = a2;
475 const unsigned int *ack_delay = a3;
476 const struct quic_loss *ql = a4;
477
478 if (rtt_sample)
479 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
480 if (ack_delay)
481 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
482 if (ql)
483 chunk_appendf(&trace_buf,
484 " srtt=%ums rttvar=%ums min_rtt=%ums",
485 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
486 }
487 if (mask & QUIC_EV_CONN_CC) {
488 const struct quic_cc_event *ev = a2;
489 const struct quic_cc *cc = a3;
490
491 if (a2)
492 quic_cc_event_trace(&trace_buf, ev);
493 if (a3)
494 quic_cc_state_trace(&trace_buf, cc);
495 }
496
497 if (mask & QUIC_EV_CONN_PKTLOSS) {
498 const struct quic_pktns *pktns = a2;
499 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500
501 if (pktns) {
502 chunk_appendf(&trace_buf, " pktns=%s",
503 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
504 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
505 if (pktns->tx.loss_time)
506 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100507 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508 }
509 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
510 struct quic_tx_packet *pkt;
511
512 chunk_appendf(&trace_buf, " lost_pkts:");
513 list_for_each_entry(pkt, lost_pkts, list)
514 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
515 }
516 }
517
518 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100519 const struct quic_pktns *pktns = a2;
520 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100521 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100522
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100523 if (ifae_pkts)
524 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
525 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100527 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
530 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100531 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
532 if (pktns->tx.in_flight)
533 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 if (pktns->tx.loss_time)
535 chunk_appendf(&trace_buf, " loss_time=%dms",
536 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
537 }
538 if (mask & QUIC_EV_CONN_SPTO) {
539 if (pktns->tx.time_of_last_eliciting)
540 chunk_appendf(&trace_buf, " tole=%dms",
541 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
542 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100543 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100544 }
545 }
546
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200547 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100548 chunk_appendf(&trace_buf,
549 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
550 }
551 }
552
553 if (mask & QUIC_EV_CONN_SPPKTS) {
554 const struct quic_tx_packet *pkt = a2;
555
Frédéric Lécaille8726d632022-05-03 10:32:21 +0200556 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 +0100557 (unsigned long long)qc->path->cwnd,
558 (unsigned long long)qc->path->prep_in_flight,
559 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100560 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100561 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100562 (unsigned long)pkt->pn_node.key,
563 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
564 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100565 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100566 }
567 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100568
569 if (mask & QUIC_EV_CONN_SSLALERT) {
570 const uint8_t *alert = a2;
571 const enum ssl_encryption_level_t *level = a3;
572
573 if (alert)
574 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
575 if (level)
576 chunk_appendf(&trace_buf, " el=%c",
577 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
578 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100579
580 if (mask & QUIC_EV_CONN_BCFRMS) {
581 const size_t *sz1 = a2;
582 const size_t *sz2 = a3;
583 const size_t *sz3 = a4;
584
585 if (sz1)
586 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
587 if (sz2)
588 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
589 if (sz3)
590 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
591 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100592
593 if (mask & QUIC_EV_CONN_PSTRM) {
594 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100595
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100596 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100597 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100598 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100599 }
600 if (mask & QUIC_EV_CONN_LPKT) {
601 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200602 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100603
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100604 if (pkt) {
605 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
606 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
607 if (pkt->pn_node.key != (uint64_t)-1)
608 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
609 }
610
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200611 if (len)
612 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 }
614
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200615 if (mask & QUIC_EV_STATELESS_RST) {
616 const struct quic_cid *cid = a2;
617
618 if (cid)
619 quic_cid_dump(&trace_buf, cid);
620 }
621
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622}
623
624/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100625static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200627 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100628
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100629 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100630 return 1;
631
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200632 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
633 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200634 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
635 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200636 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637 return 1;
638
639 return 0;
640}
641
642/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
643 * both loss detection and PTO and schedule the task assiated to this timer if needed.
644 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100645static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100647 struct quic_pktns *pktns;
648 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200649 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100650
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100651 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100652 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100653
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100654 pktns = quic_loss_pktns(qc);
655 if (tick_isset(pktns->tx.loss_time)) {
656 qc->timer = pktns->tx.loss_time;
657 goto out;
658 }
659
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100660 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100661 * cancelled for a server which reached the anti-amplification limit.
662 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100663 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200664 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100665 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100666 qc->timer = TICK_ETERNITY;
667 goto out;
668 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100669
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100670 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100671 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100672 /* Timer cancellation. */
673 qc->timer = TICK_ETERNITY;
674 goto out;
675 }
676
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200677 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200678 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100679 if (tick_isset(pto))
680 qc->timer = pto;
681 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100682 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100683 if (tick_is_expired(qc->timer, now_ms)) {
684 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100685 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100686 }
687 else {
688 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100689 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100690 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100691 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100692 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100693}
694
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100695/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
696 * connection.
697 * Return 1 if succeeded, 0 if not.
698 */
699static int quic_tls_key_update(struct quic_conn *qc)
700{
701 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
702 struct quic_tls_secrets *rx, *tx;
703 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
704 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
705
706 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
707 rx = &tls_ctx->rx;
708 tx = &tls_ctx->tx;
709 nxt_rx = &qc->ku.nxt_rx;
710 nxt_tx = &qc->ku.nxt_tx;
711
712 /* Prepare new RX secrets */
713 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
714 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100715 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100716 return 0;
717 }
718
719 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
720 nxt_rx->key, nxt_rx->keylen,
721 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
722 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100723 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100724 return 0;
725 }
726
727 /* Prepare new TX secrets */
728 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
729 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100730 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100731 return 0;
732 }
733
734 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
735 nxt_tx->key, nxt_tx->keylen,
736 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
737 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100738 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100739 return 0;
740 }
741
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200742 if (nxt_rx->ctx) {
743 EVP_CIPHER_CTX_free(nxt_rx->ctx);
744 nxt_rx->ctx = NULL;
745 }
746
747 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
748 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
749 return 0;
750 }
751
752 if (nxt_tx->ctx) {
753 EVP_CIPHER_CTX_free(nxt_tx->ctx);
754 nxt_tx->ctx = NULL;
755 }
756
757 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
758 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
759 return 0;
760 }
761
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100762 return 1;
763}
764
765/* Rotate the Key Update information for <qc> QUIC connection.
766 * Must be used after having updated them.
767 * Always succeeds.
768 */
769static void quic_tls_rotate_keys(struct quic_conn *qc)
770{
771 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
772 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200773 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100774
775 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200776 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100777 curr_secret = tls_ctx->rx.secret;
778 curr_iv = tls_ctx->rx.iv;
779 curr_key = tls_ctx->rx.key;
780
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200781 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100782 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
783 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
784 tls_ctx->rx.key = qc->ku.nxt_rx.key;
785
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200786 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100787 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
788 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
789 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
790
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200791 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100792 qc->ku.prv_rx.secret = curr_secret;
793 qc->ku.prv_rx.iv = curr_iv;
794 qc->ku.prv_rx.key = curr_key;
795 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
796
797 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200798 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100799 curr_secret = tls_ctx->tx.secret;
800 curr_iv = tls_ctx->tx.iv;
801 curr_key = tls_ctx->tx.key;
802
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200803 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100804 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
805 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
806 tls_ctx->tx.key = qc->ku.nxt_tx.key;
807
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200808 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100809 qc->ku.nxt_tx.secret = curr_secret;
810 qc->ku.nxt_tx.iv = curr_iv;
811 qc->ku.nxt_tx.key = curr_key;
812}
813
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100814#ifndef OPENSSL_IS_BORINGSSL
815int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
816 const uint8_t *read_secret,
817 const uint8_t *write_secret, size_t secret_len)
818{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100819 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
820 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 +0100821 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100822 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100823
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100824 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100825 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200826 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100827 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100828 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100829 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100830
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100831 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100832 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100833 goto err;
834 }
835
836 rx = &tls_ctx->rx;
837 tx = &tls_ctx->tx;
838
839 rx->aead = tx->aead = tls_aead(cipher);
840 rx->md = tx->md = tls_md(cipher);
841 rx->hp = tx->hp = tls_hp(cipher);
842
843 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
844 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100845 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100846 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100847 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100848 }
849
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200850 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
851 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
852 goto err;
853 }
854
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100855 /* Enqueue this connection asap if we could derive O-RTT secrets as
856 * listener. Note that a listener derives only RX secrets for this
857 * level.
858 */
859 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
860 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100861
862 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100863 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100864
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100865 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
866 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100867 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100868 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100869 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100870 }
871
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200872 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
873 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
874 goto err;
875 }
876
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100877 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100878 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
879 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
880 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
881
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200882 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100883 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
884 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100885 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100886 goto err;
887 }
888
889 memcpy(rx->secret, read_secret, secret_len);
890 rx->secretlen = secret_len;
891 memcpy(tx->secret, write_secret, secret_len);
892 tx->secretlen = secret_len;
893 /* Initialize all the secret keys lengths */
894 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
895 /* Prepare the next key update */
896 if (!quic_tls_key_update(qc))
897 goto err;
898 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100899
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100900 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100901 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100902 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100903 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100904 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100905
906 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100907 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100908 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100909}
910#else
911/* ->set_read_secret callback to derive the RX secrets at <level> encryption
912 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500913 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100914 */
915int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
916 const SSL_CIPHER *cipher,
917 const uint8_t *secret, size_t secret_len)
918{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100919 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100921 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100923 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200924 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100925 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100926 goto out;
927 }
928
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100929 tls_ctx->rx.aead = tls_aead(cipher);
930 tls_ctx->rx.md = tls_md(cipher);
931 tls_ctx->rx.hp = tls_hp(cipher);
932
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100933 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
934 goto err;
935
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100936 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 +0100937 tls_ctx->rx.key, tls_ctx->rx.keylen,
938 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100939 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
940 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100941 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100942 goto err;
943 }
944
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100945 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100946 const unsigned char *buf;
947 size_t buflen;
948
949 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
950 if (!buflen)
951 goto err;
952
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100953 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100954 goto err;
955 }
956
957 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100958 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100959 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100960
961 return 1;
962
963 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100964 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100965 return 0;
966}
967
968/* ->set_write_secret callback to derive the TX secrets at <level>
969 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500970 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100971 */
972int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
973 const SSL_CIPHER *cipher,
974 const uint8_t *secret, size_t secret_len)
975{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100976 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
977 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 +0100978
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100979 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200980 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100981 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100982 goto out;
983 }
984
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100985 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
986 goto err;
987
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100988 tls_ctx->tx.aead = tls_aead(cipher);
989 tls_ctx->tx.md = tls_md(cipher);
990 tls_ctx->tx.hp = tls_hp(cipher);
991
992 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 +0100993 tls_ctx->tx.key, tls_ctx->tx.keylen,
994 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100995 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
996 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100997 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100998 goto err;
999 }
1000
1001 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001002 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001003 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001004 return 1;
1005
1006 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001007 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001008 return 0;
1009}
1010#endif
1011
1012/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1013 * with <len> as size in CRYPTO buffers dedicated to store the information about
1014 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1015 * It fails only if it could not managed to allocate enough CRYPTO buffers to
1016 * store all the data.
1017 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1018 */
1019static int quic_crypto_data_cpy(struct quic_enc_level *qel,
1020 const unsigned char *data, size_t len)
1021{
1022 struct quic_crypto_buf **qcb;
1023 /* The remaining byte to store in CRYPTO buffers. */
1024 size_t cf_offset, cf_len, *nb_buf;
1025 unsigned char *pos;
1026
1027 nb_buf = &qel->tx.crypto.nb_buf;
1028 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1029 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1030 cf_len = len;
1031
1032 while (len) {
1033 size_t to_copy, room;
1034
1035 pos = (*qcb)->data + (*qcb)->sz;
1036 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1037 to_copy = len > room ? room : len;
1038 if (to_copy) {
1039 memcpy(pos, data, to_copy);
1040 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1041 qel->tx.crypto.sz += to_copy;
1042 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001043 len -= to_copy;
1044 data += to_copy;
1045 }
1046 else {
1047 struct quic_crypto_buf **tmp;
1048
1049 tmp = realloc(qel->tx.crypto.bufs,
1050 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1051 if (tmp) {
1052 qel->tx.crypto.bufs = tmp;
1053 qcb = &qel->tx.crypto.bufs[*nb_buf];
1054 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1055 if (!*qcb)
1056 return 0;
1057
1058 (*qcb)->sz = 0;
1059 ++*nb_buf;
1060 }
1061 else {
1062 break;
1063 }
1064 }
1065 }
1066
1067 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1068 * have been buffered.
1069 */
1070 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001071 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001072 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001073
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001074 /* There is at most one CRYPTO frame in this packet number
1075 * space. Let's look for it.
1076 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001077 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001078 if (frm->type != QUIC_FT_CRYPTO)
1079 continue;
1080
1081 /* Found */
1082 found = frm;
1083 break;
1084 }
1085
1086 if (found) {
1087 found->crypto.len += cf_len;
1088 }
1089 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001090 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001091 if (!frm)
1092 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001094 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001095 frm->type = QUIC_FT_CRYPTO;
1096 frm->crypto.offset = cf_offset;
1097 frm->crypto.len = cf_len;
1098 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001099 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001100 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001101 }
1102
1103 return len == 0;
1104}
1105
1106
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001107/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1108void quic_set_tls_alert(struct quic_conn *qc, int alert)
1109{
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001110 qc->err_code = QC_ERR_CRYPTO_ERROR | alert;
1111 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001112 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001113}
1114
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001115/* Set the application for <qc> QUIC connection.
1116 * Return 1 if succeeded, 0 if not.
1117 */
1118int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1119{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001120 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1121 qc->app_ops = &h3_ops;
1122 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1123 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001124 else
1125 return 0;
1126
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001127 return 1;
1128}
1129
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001130/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1131 * wants to provide the QUIC layer with CRYPTO data.
1132 * Returns 1 if succeeded, 0 if not.
1133 */
1134int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1135 const uint8_t *data, size_t len)
1136{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001137 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001138 enum quic_tls_enc_level tel;
1139 struct quic_enc_level *qel;
1140
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001141 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1142 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001143 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001144 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001145 goto out;
1146 }
1147
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001148 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001149 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001150 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001151 goto err;
1152 }
1153
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001154 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001156 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001157 goto err;
1158 }
1159
1160 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001161 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001162
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001163 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001164 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001165 return 1;
1166
1167 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001168 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001169 return 0;
1170}
1171
1172int ha_quic_flush_flight(SSL *ssl)
1173{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001174 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001175
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001176 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1177 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001178
1179 return 1;
1180}
1181
1182int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1183{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001184 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001185
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001186 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1187 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001188 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001189 return 1;
1190}
1191
1192/* QUIC TLS methods */
1193static SSL_QUIC_METHOD ha_quic_method = {
1194#ifdef OPENSSL_IS_BORINGSSL
1195 .set_read_secret = ha_set_rsec,
1196 .set_write_secret = ha_set_wsec,
1197#else
1198 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1199#endif
1200 .add_handshake_data = ha_quic_add_handshake_data,
1201 .flush_flight = ha_quic_flush_flight,
1202 .send_alert = ha_quic_send_alert,
1203};
1204
1205/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1206 * Returns an error count.
1207 */
1208int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1209{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001210 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1211 int cfgerr = 0;
1212
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001213 long options =
1214 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1215 SSL_OP_SINGLE_ECDH_USE |
1216 SSL_OP_CIPHER_SERVER_PREFERENCE;
1217 SSL_CTX *ctx;
1218
1219 ctx = SSL_CTX_new(TLS_server_method());
1220 bind_conf->initial_ctx = ctx;
1221
1222 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001223 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1224 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1225 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001226
1227#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1228#ifdef OPENSSL_IS_BORINGSSL
1229 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1230 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1231#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1232 if (bind_conf->ssl_conf.early_data) {
1233 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001234 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001235 }
1236 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1237 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1238#else
1239 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1240#endif
1241 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1242#endif
1243 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1244
1245 return cfgerr;
1246}
1247
1248/* Decode an expected packet number from <truncated_on> its truncated value,
1249 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1250 * the number of bits used to encode this packet number (its length in bytes * 8).
1251 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1252 */
1253static uint64_t decode_packet_number(uint64_t largest_pn,
1254 uint32_t truncated_pn, unsigned int pn_nbits)
1255{
1256 uint64_t expected_pn = largest_pn + 1;
1257 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1258 uint64_t pn_hwin = pn_win / 2;
1259 uint64_t pn_mask = pn_win - 1;
1260 uint64_t candidate_pn;
1261
1262
1263 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1264 /* Note that <pn_win> > <pn_hwin>. */
1265 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1266 candidate_pn + pn_hwin <= expected_pn)
1267 return candidate_pn + pn_win;
1268
1269 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1270 return candidate_pn - pn_win;
1271
1272 return candidate_pn;
1273}
1274
1275/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1276 * cryptographic context.
1277 * <largest_pn> is the largest received packet number and <pn> the address of
1278 * the packet number field for this packet with <byte0> address of its first byte.
1279 * <end> points to one byte past the end of this packet.
1280 * Returns 1 if succeeded, 0 if not.
1281 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001282static int qc_do_rm_hp(struct quic_conn *qc,
1283 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001284 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001285 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001286{
1287 int ret, outlen, i, pnlen;
1288 uint64_t packet_number;
1289 uint32_t truncated_pn = 0;
1290 unsigned char mask[5] = {0};
1291 unsigned char *sample;
1292 EVP_CIPHER_CTX *cctx;
1293 unsigned char *hp_key;
1294
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295 /* Check there is enough data in this packet. */
1296 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001297 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001298 return 0;
1299 }
1300
1301 cctx = EVP_CIPHER_CTX_new();
1302 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001303 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001304 return 0;
1305 }
1306
1307 ret = 0;
1308 sample = pn + QUIC_PACKET_PN_MAXLEN;
1309
1310 hp_key = tls_ctx->rx.hp_key;
1311 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1312 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1313 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001314 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001315 goto out;
1316 }
1317
1318 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1319 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1320 for (i = 0; i < pnlen; i++) {
1321 pn[i] ^= mask[i + 1];
1322 truncated_pn = (truncated_pn << 8) | pn[i];
1323 }
1324
1325 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1326 /* Store remaining information for this unprotected header */
1327 pkt->pn = packet_number;
1328 pkt->pnl = pnlen;
1329
1330 ret = 1;
1331
1332 out:
1333 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001334
1335 return ret;
1336}
1337
1338/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1339 * address, with <payload_len> as payload length, <aad> as address of
1340 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1341 * context.
1342 * Returns 1 if succeeded, 0 if not.
1343 */
1344static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1345 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001346 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001347{
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001348 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001349 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001350 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001351 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001352
1353 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001354 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001355 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001356 }
1357
1358 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001359 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001360 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001361 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001362 }
1363
1364 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001365
1366 err:
1367 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001368 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001369 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001370}
1371
1372/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1373 * Returns 1 if succeeded, 0 if not.
1374 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001375static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001376{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001377 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001378 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001379 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001380 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001381 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001382 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1383 unsigned char *rx_key = tls_ctx->rx.key;
1384
1385 kp_changed = 0;
1386 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1387 /* The two tested bits are not at the same position,
1388 * this is why they are first both inversed.
1389 */
1390 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1391 if (pkt->pn < tls_ctx->rx.pn) {
1392 /* The lowest packet number of a previous key phase
1393 * cannot be null if it really stores previous key phase
1394 * secrets.
1395 */
1396 if (!pkt->qc->ku.prv_rx.pn)
1397 return 0;
1398
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001399 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1400 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001401 rx_key = pkt->qc->ku.prv_rx.key;
1402 }
1403 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1404 /* Next key phase */
1405 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001406 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1407 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001408 rx_key = pkt->qc->ku.nxt_rx.key;
1409 }
1410 }
1411 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001412
1413 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1414 return 0;
1415
1416 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1417 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001418 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001419 if (!ret)
1420 return 0;
1421
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001422 /* Update the keys only if the packet decryption succeeded. */
1423 if (kp_changed) {
1424 quic_tls_rotate_keys(pkt->qc);
1425 /* Toggle the Key Phase bit */
1426 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1427 /* Store the lowest packet number received for the current key phase */
1428 tls_ctx->rx.pn = pkt->pn;
1429 /* Prepare the next key update */
1430 if (!quic_tls_key_update(pkt->qc))
1431 return 0;
1432 }
1433
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001434 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001435 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001436
1437 return 1;
1438}
1439
Frédéric Lécaille96367152022-04-25 09:40:19 +02001440
1441/* Remove references to <frm> frame */
1442static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1443{
1444 uint64_t pn;
1445 struct quic_frame *f, *tmp;
1446
1447 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1448 pn = f->pkt->pn_node.key;
1449 f->origin = NULL;
1450 LIST_DELETE(&f->ref);
1451 TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1452 }
1453}
1454
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001455/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001456void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001457{
1458 uint64_t pn;
1459 struct quic_frame *origin, *f, *tmp;
1460
1461 /* Identify this frame: a frame copy or one of its copies */
1462 origin = frm->origin ? frm->origin : frm;
1463 /* Ensure the source of the copies is flagged as acked, <frm> being
1464 * possibly a copy of <origin>
1465 */
1466 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1467 /* Mark all the copy of <origin> as acknowledged. We must
1468 * not release the packets (releasing the frames) at this time as
1469 * they are possibly also to be acknowledged alongside the
1470 * the current one.
1471 */
1472 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1473 pn = f->pkt->pn_node.key;
1474 TRACE_PROTO("mark frame as acked from packet",
1475 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1476 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1477 f->origin = NULL;
1478 LIST_DELETE(&f->ref);
1479 }
1480 LIST_DELETE(&frm->list);
1481 pn = frm->pkt->pn_node.key;
1482 quic_tx_packet_refdec(frm->pkt);
1483 TRACE_PROTO("freeing frame from packet",
1484 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1485 pool_free(pool_head_quic_frame, frm);
1486}
1487
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001488/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001489 *
1490 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001491 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001492static int quic_stream_try_to_consume(struct quic_conn *qc,
1493 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001494{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001495 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001496 struct eb64_node *frm_node;
1497
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001498 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001499 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001500 while (frm_node) {
1501 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001502 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001503 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001504
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001505 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001506 offset = strm->offset.key;
1507 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001508
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001509 if (offset > stream->ack_offset)
1510 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001511
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001512 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001513 /* cf. next comment : frame may be freed at this stage. */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001514 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001515 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001516 ret = 1;
1517 }
1518
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001519 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1520 * has been freed. with the stream frames tree. Nothing to do
1521 * anymore in here.
1522 */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001523 if (!stream)
1524 return 1;
1525
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001526 frm_node = eb64_next(frm_node);
1527 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001528
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001529 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001530 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001531 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001532
1533 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001534}
1535
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001536/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001537static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1538 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001539{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001540 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001541
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001542 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001543 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001544 switch (frm->type) {
1545 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1546 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001547 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001548 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001549 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001550 const size_t offset = strm_frm->offset.key;
1551 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001552
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001553 /* do not use strm_frm->stream as the qc_stream_desc instance
1554 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001555 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001556 *
1557 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001558 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001559 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001560 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1561 if (!node) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001562 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001563 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001564 /* early return */
1565 return;
1566 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001567 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001568
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001569 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001570 if (offset <= stream->ack_offset) {
1571 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001572 stream_acked = 1;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001573 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1574 qc, strm_frm, stream);
1575 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001576
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001577 if (!stream) {
1578 /* no need to continue if stream freed. */
1579 TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001580 qc_release_frm(qc, frm);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001581 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001582 }
1583
Frédéric Lécailleda342552022-04-25 10:28:49 +02001584 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1585 qc, strm_frm, stream);
1586 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001587 }
1588 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001589 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001590 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001591
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001592 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001593 }
1594 break;
1595 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001596 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001597 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001598
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001599 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001600 struct qcc *qcc = qc->qcc;
1601
1602 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1603 tasklet_wakeup(qcc->subs->tasklet);
1604 qcc->subs->events &= ~SUB_RETRY_SEND;
1605 if (!qcc->subs->events)
1606 qcc->subs = NULL;
1607 }
1608 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001609}
1610
1611/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1612 * deallocating them, and their TX frames.
1613 * Returns the last node reached to be used for the next range.
1614 * May be NULL if <largest> node could not be found.
1615 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001616static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1617 struct eb_root *pkts,
1618 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001619 struct list *newly_acked_pkts,
1620 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001621 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001622{
1623 struct eb64_node *node;
1624 struct quic_tx_packet *pkt;
1625
1626 if (largest_node)
1627 node = largest_node;
1628 else {
1629 node = eb64_lookup(pkts, largest);
1630 while (!node && largest > smallest) {
1631 node = eb64_lookup(pkts, --largest);
1632 }
1633 }
1634
1635 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001636 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001637
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001638 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001639 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001640 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001641 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001642 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001643 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001644 node = eb64_prev(node);
1645 eb64_delete(&pkt->pn_node);
1646 }
1647
1648 return node;
1649}
1650
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001651/* Remove all frames from <pkt_frm_list> and reinsert them in the
1652 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001654static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001655 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001656 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001657{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001658 struct quic_frame *frm, *frmbak;
1659 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001660 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001661
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001662 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001663 /* Only for debug */
1664 uint64_t pn;
1665
1666 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001667 LIST_DELETE(&frm->list);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001668 pn = frm->pkt->pn_node.key;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001669 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001670 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001671 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001672 /* Remove any reference to this frame */
1673 qc_frm_unref(qc, frm);
1674 switch (frm->type) {
1675 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1676 {
1677 struct quic_stream *strm_frm = &frm->stream;
1678 struct eb64_node *node = NULL;
1679 struct qc_stream_desc *stream_desc;
1680
1681 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1682 if (!node) {
1683 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm);
1684 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1685 qc, frm, &pn);
1686 pool_free(pool_head_quic_frame, frm);
1687 continue;
1688 }
1689
1690 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1691 /* Do not resend this frame if in the "already acked range" */
1692 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1693 TRACE_PROTO("ignored frame in already acked range",
1694 QUIC_EV_CONN_PRSAFRM, qc, frm);
1695 continue;
1696 }
1697 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1698 strm_frm->offset.key = stream_desc->ack_offset;
1699 TRACE_PROTO("updated partially acked frame",
1700 QUIC_EV_CONN_PRSAFRM, qc, frm);
1701 }
1702
1703 break;
1704 }
1705
1706 default:
1707 break;
1708 }
1709
1710 /* Do not resend probing packet with old data */
1711 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1712 TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1713 qc, frm, &pn);
1714 if (frm->origin)
1715 LIST_DELETE(&frm->ref);
1716 pool_free(pool_head_quic_frame, frm);
1717 continue;
1718 }
1719
1720 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1721 TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1722 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1723 qc, frm, &pn);
1724 pool_free(pool_head_quic_frame, frm);
1725 }
1726 else {
1727 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001728 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1729 /* Mark this STREAM frame as lost. A look up their stream descriptor
1730 * will be performed to check the stream is not consumed or released.
1731 */
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001732 frm->flags = QUIC_FL_TX_FRAME_LOST;
1733 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001734 LIST_APPEND(&tmp, &frm->list);
1735 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001736 }
1737
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001738 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001739}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001740
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001741/* Free <pkt> TX packet and its attached frames.
1742 * This is the responsability of the caller to remove this packet of
1743 * any data structure it was possibly attached to.
1744 */
1745static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1746{
1747 struct quic_frame *frm, *frmbak;
1748
1749 if (!pkt)
1750 return;
1751
1752 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1753 LIST_DELETE(&frm->list);
1754 pool_free(pool_head_quic_frame, frm);
1755 }
1756 pool_free(pool_head_quic_tx_packet, pkt);
1757}
1758
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001759/* Free the TX packets of <pkts> list */
1760static inline void free_quic_tx_pkts(struct list *pkts)
1761{
1762 struct quic_tx_packet *pkt, *tmp;
1763
1764 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001765 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001767 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001768 }
1769}
1770
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001771/* Remove already sent ranges of acknowledged packet numbers from
1772 * <pktns> packet number space tree below <largest_acked_pn> possibly
1773 * updating the range which contains <largest_acked_pn>.
1774 * Never fails.
1775 */
1776static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1777 int64_t largest_acked_pn)
1778{
1779 struct eb64_node *ar, *next_ar;
1780 struct quic_arngs *arngs = &pktns->rx.arngs;
1781
1782 ar = eb64_first(&arngs->root);
1783 while (ar) {
1784 struct quic_arng_node *ar_node;
1785
1786 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001787 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001788 if ((int64_t)ar_node->first.key > largest_acked_pn)
1789 break;
1790
1791 if (largest_acked_pn < ar_node->last) {
1792 eb64_delete(ar);
1793 ar_node->first.key = largest_acked_pn + 1;
1794 eb64_insert(&arngs->root, ar);
1795 break;
1796 }
1797
1798 eb64_delete(ar);
1799 pool_free(pool_head_quic_arng, ar_node);
1800 arngs->sz--;
1801 ar = next_ar;
1802 }
1803}
1804
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001805/* Send a packet ack event nofication for each newly acked packet of
1806 * <newly_acked_pkts> list and free them.
1807 * Always succeeds.
1808 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001809static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001810 struct list *newly_acked_pkts)
1811{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001812 struct quic_tx_packet *pkt, *tmp;
1813 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1814
1815 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1816 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001817 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001818 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001819 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001820 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001821 /* If this packet contained an ACK frame, proceed to the
1822 * acknowledging of range of acks from the largest acknowledged
1823 * packet number which was sent in an ACK frame by this packet.
1824 */
1825 if (pkt->largest_acked_pn != -1)
1826 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001827 ev.ack.acked = pkt->in_flight_len;
1828 ev.ack.time_sent = pkt->time_sent;
1829 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001830 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001831 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001832 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 }
1834
1835}
1836
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001837/* Release all the frames attached to <pktns> packet number space */
1838static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1839{
1840 struct quic_frame *frm, *frmbak;
1841
1842 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1843 LIST_DELETE(&frm->list);
1844 pool_free(pool_head_quic_frame, frm);
1845 }
1846}
1847
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001848/* Handle <pkts> list of lost packets detected at <now_us> handling
1849 * their TX frames.
1850 * Send a packet loss event to the congestion controller if
1851 * in flight packet have been lost.
1852 * Also frees the packet in <pkts> list.
1853 * Never fails.
1854 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001855static inline void qc_release_lost_pkts(struct quic_conn *qc,
1856 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001857 struct list *pkts,
1858 uint64_t now_us)
1859{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001860 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861 uint64_t lost_bytes;
1862
1863 lost_bytes = 0;
1864 oldest_lost = newest_lost = NULL;
1865 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001866 struct list tmp = LIST_HEAD_INIT(tmp);
1867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 lost_bytes += pkt->in_flight_len;
1869 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001870 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001871 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001872 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001873 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001874 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001875 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001876 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001877 if (!oldest_lost) {
1878 oldest_lost = newest_lost = pkt;
1879 }
1880 else {
1881 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001882 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001883 newest_lost = pkt;
1884 }
1885 }
1886
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001887 if (newest_lost) {
1888 /* Sent a congestion event to the controller */
1889 struct quic_cc_event ev = {
1890 .type = QUIC_CC_EVT_LOSS,
1891 .loss.time_sent = newest_lost->time_sent,
1892 };
1893
1894 quic_cc_event(&qc->path->cc, &ev);
1895 }
1896
1897 /* If an RTT have been already sampled, <rtt_min> has been set.
1898 * We must check if we are experiencing a persistent congestion.
1899 * If this is the case, the congestion controller must re-enter
1900 * slow start state.
1901 */
1902 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1903 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1904
1905 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1906 now_ms, qc->max_ack_delay))
1907 qc->path->cc.algo->slow_start(&qc->path->cc);
1908 }
1909
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001910 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001911 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001912 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001913 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001914 }
1915}
1916
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001917/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1918 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001919 * 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 +01001920 * acked ack-eliciting packet.
1921 * Return 1, if succeeded, 0 if not.
1922 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001923static inline int qc_parse_ack_frm(struct quic_conn *qc,
1924 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 struct quic_enc_level *qel,
1926 unsigned int *rtt_sample,
1927 const unsigned char **pos, const unsigned char *end)
1928{
1929 struct quic_ack *ack = &frm->ack;
1930 uint64_t smallest, largest;
1931 struct eb_root *pkts;
1932 struct eb64_node *largest_node;
1933 unsigned int time_sent, pkt_flags;
1934 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1935 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1936
1937 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1938 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001939 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940 goto err;
1941 }
1942
1943 if (ack->first_ack_range > ack->largest_ack) {
1944 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001945 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001946 goto err;
1947 }
1948
1949 largest = ack->largest_ack;
1950 smallest = largest - ack->first_ack_range;
1951 pkts = &qel->pktns->tx.pkts;
1952 pkt_flags = 0;
1953 largest_node = NULL;
1954 time_sent = 0;
1955
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001956 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001957 largest_node = eb64_lookup(pkts, largest);
1958 if (!largest_node) {
1959 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001960 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001961 }
1962 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001963 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001964 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001965 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001966 }
1967
1968 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001969 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001970 do {
1971 uint64_t gap, ack_range;
1972
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001973 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1974 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001975 if (!ack->ack_range_num--)
1976 break;
1977
1978 if (!quic_dec_int(&gap, pos, end))
1979 goto err;
1980
1981 if (smallest < gap + 2) {
1982 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001983 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001984 goto err;
1985 }
1986
1987 largest = smallest - gap - 2;
1988 if (!quic_dec_int(&ack_range, pos, end))
1989 goto err;
1990
1991 if (largest < ack_range) {
1992 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001993 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001994 goto err;
1995 }
1996
1997 /* Do not use this node anymore. */
1998 largest_node = NULL;
1999 /* Next range */
2000 smallest = largest - ack_range;
2001
2002 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002003 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002004 } while (1);
2005
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002006 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2007 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002008 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002009 }
2010
2011 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2012 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002013 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002014 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002015 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002016 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002017 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2018 if (quic_peer_validated_addr(qc))
2019 qc->path->loss.pto_count = 0;
2020 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002021 }
2022
2023
2024 return 1;
2025
2026 err:
2027 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002028 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002029 return 0;
2030}
2031
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002032/* This function gives the detail of the SSL error. It is used only
2033 * if the debug mode and the verbose mode are activated. It dump all
2034 * the SSL error until the stack was empty.
2035 */
2036static forceinline void qc_ssl_dump_errors(struct connection *conn)
2037{
2038 if (unlikely(global.mode & MODE_DEBUG)) {
2039 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002040 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002041 unsigned long ret;
2042
Willy Tarreau325fc632022-04-11 18:47:38 +02002043 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002044 ret = ERR_get_error();
2045 if (!ret)
2046 return;
2047
2048 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002049 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002050 }
2051 }
2052}
2053
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002054int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2055 const char **str, int *len);
2056
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002057/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2058 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002059 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002060 * Return 1 if succeeded, 0 if not.
2061 */
2062static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002063 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002064 const unsigned char *data, size_t len,
2065 struct quic_rx_packet *pkt,
2066 struct quic_rx_crypto_frm *cf)
2067{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002068 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002069 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002070
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002071 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002072 qc = ctx->qc;
2073
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002074 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2075
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002076 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2077 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002078 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002079 goto err;
2080 }
2081
2082 el->rx.crypto.offset += len;
2083 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002084 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002085
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002086 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002087 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002088 ssl_err = SSL_do_handshake(ctx->ssl);
2089 if (ssl_err != 1) {
2090 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2091 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2092 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002093 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002094 goto out;
2095 }
2096
2097 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002098 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002099 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002100 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 goto err;
2102 }
2103
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002104 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002105
2106 /* Check the alpn could be negotiated */
2107 if (!qc->app_ops) {
2108 TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2109 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2110 goto err;
2111 }
2112
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002113 /* I/O callback switch */
2114 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002115 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002116 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002117 /* The connection is ready to be accepted. */
2118 quic_accept_push_qc(qc);
2119 }
2120 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002121 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002122 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002123 } else {
2124 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2125 if (ssl_err != 1) {
2126 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2127 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2128 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002129 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002130 goto out;
2131 }
2132
2133 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002134 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002135 goto err;
2136 }
2137
2138 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002139 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002140 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002141
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002142 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002143 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002144 return 1;
2145
2146 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002147 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002148 return 0;
2149}
2150
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002151/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
2152 * <pkt> RX packet.
2153 * Return it if succeeded, NULL if not.
2154 */
2155static inline
2156struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
2157 struct quic_rx_packet *pkt)
2158{
2159 struct quic_rx_strm_frm *frm;
2160
2161 frm = pool_alloc(pool_head_quic_rx_strm_frm);
2162 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002163 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002164 frm->len = stream_frm->len;
2165 frm->data = stream_frm->data;
2166 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002167 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002168 }
2169
2170 return frm;
2171}
2172
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002173/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01002174 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002175 */
2176static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2177{
2178 size_t ret;
2179
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01002180 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
2181 strm_frm->len -= ret;
2182 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002183
2184 return ret;
2185}
2186
2187/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2188 * streams may be open. The data are copied to the stream RX buffer if possible.
2189 * If not, the STREAM frame is stored to be treated again later.
2190 * We rely on the flow control so that not to store too much STREAM frames.
2191 * Return 1 if succeeded, 0 if not.
2192 */
2193static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2194 struct quic_stream *strm_frm,
2195 struct quic_conn *qc)
2196{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002197 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002198 struct eb64_node *frm_node;
2199 struct qcs *qcs = NULL;
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002200 size_t done, buf_was_full;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002201 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002202
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002203 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2204 strm_frm->offset.key, strm_frm->fin,
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002205 (char *)strm_frm->data, &qcs, &done);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002206
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002207 /* invalid frame */
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002208 if (ret == 1)
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002209 return 0;
2210
2211 /* already fully received offset */
2212 if (ret == 0 && done == 0)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002213 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002214
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002215 /* frame not handled (partially or completely) must be buffered */
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002216 if (ret == 2) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002217 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2218 if (!frm) {
2219 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002220 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002221 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002222 }
2223
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002224 /* frame partially handled by the MUX */
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002225 if (done) {
2226 BUG_ON(done >= frm->len); /* must never happen */
2227 frm->len -= done;
2228 frm->data += done;
2229 frm->offset_node.key += done;
2230 }
2231
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002232 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2233 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002234
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002235 /* interrupt only if frame was not received at all. */
2236 if (!done)
2237 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002238 }
2239
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002240 /* Decode the data if buffer is already full as it's not possible to
2241 * dequeue a frame in this condition.
2242 */
2243 if (b_full(&qcs->rx.buf))
2244 qcc_decode_qcs(qc->qcc, qcs);
2245
2246 retry:
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002247 /* Frame received (partially or not) by the mux.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002248 * If there is buffered frame for next offset, it may be possible to
2249 * receive them now.
2250 */
2251 frm_node = eb64_first(&qcs->rx.frms);
2252 while (frm_node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002253 frm = eb64_entry(frm_node,
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002254 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002255
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02002256 ret = qcc_recv(qc->qcc, qcs->id, frm->len,
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002257 frm->offset_node.key, frm->fin,
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002258 (char *)frm->data, &qcs, &done);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002259
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002260 BUG_ON(ret == 1); /* must never happen for buffered frames */
2261
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002262 /* interrupt the parsing if the frame cannot be handled
2263 * entirely for the moment only.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002264 */
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002265 if (ret == 2) {
2266 if (done) {
2267 BUG_ON(done >= frm->len); /* must never happen */
2268 frm->len -= done;
2269 frm->data += done;
2270
2271 eb64_delete(&frm->offset_node);
2272 frm->offset_node.key += done;
2273 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2274 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002275 break;
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002276 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002277
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002278 /* Remove a newly received frame or an invalid one. */
2279 frm_node = eb64_next(frm_node);
2280 eb64_delete(&frm->offset_node);
2281 quic_rx_packet_refdec(frm->pkt);
2282 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002283 }
2284
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002285 buf_was_full = b_full(&qcs->rx.buf);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002286 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002287 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002288
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002289 /* Buffer was full so the reception was stopped. Now the buffer has
2290 * space available thanks to qcc_decode_qcs(). We can now retry to
2291 * handle more data.
2292 */
2293 if (buf_was_full && !b_full(&qcs->rx.buf))
2294 goto retry;
2295
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002296 return 1;
2297}
2298
2299/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2300 * streams may be open. The data are copied to the stream RX buffer if possible.
2301 * If not, the STREAM frame is stored to be treated again later.
2302 * We rely on the flow control so that not to store too much STREAM frames.
2303 * Return 1 if succeeded, 0 if not.
2304 */
2305static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2306 struct quic_stream *strm_frm,
2307 struct quic_conn *qc)
2308{
2309 struct qcs *strm;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002310 struct quic_rx_strm_frm *frm;
2311 size_t strm_frm_len;
2312
Amaury Denoyelle50742292022-03-29 14:57:19 +02002313 strm = qcc_get_qcs(qc->qcc, strm_frm->id);
2314 if (!strm) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002315 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002316 return 0;
2317 }
2318
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002319 if (strm_frm->offset.key < strm->rx.offset) {
2320 size_t diff;
2321
2322 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2323 TRACE_PROTO("Already received STREAM data",
2324 QUIC_EV_CONN_PSTRM, qc);
2325 goto out;
2326 }
2327
2328 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2329 diff = strm->rx.offset - strm_frm->offset.key;
2330 strm_frm->offset.key = strm->rx.offset;
2331 strm_frm->len -= diff;
2332 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002333 }
2334
2335 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002336 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002337 int ret;
2338
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002339 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002340 goto store_frm;
2341
2342 /* qc_strm_cpy() will modify the offset, depending on the number
2343 * of bytes copied.
2344 */
2345 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2346 /* Inform the application of the arrival of this new stream */
2347 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002348 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002349 return 0;
2350 }
2351
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002352 if (ret)
2353 qcs_notify_recv(strm);
2354
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002355 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002356 }
2357 /* Take this frame into an account for the stream flow control */
2358 strm->rx.offset += strm_frm_len;
2359 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002360 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002361 */
2362 if (!strm_frm->len)
2363 goto out;
2364
2365 store_frm:
2366 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2367 if (!frm) {
2368 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002369 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002370 return 0;
2371 }
2372
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002373 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002374 quic_rx_packet_refinc(pkt);
2375
2376 out:
2377 return 1;
2378}
2379
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002380/* Returns 1 on success or 0 on error. On error, the packet containing the
2381 * frame must not be acknowledged.
2382 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002383static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2384 struct quic_stream *strm_frm,
2385 struct quic_conn *qc)
2386{
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002387 /* RFC9000 13.1. Packet Processing
2388 *
2389 * A packet MUST NOT be acknowledged until packet protection has been
2390 * successfully removed and all frames contained in the packet have
2391 * been processed. For STREAM frames, this means the data has been
2392 * enqueued in preparation to be received by the application protocol,
2393 * but it does not require that data be delivered and consumed.
2394 */
2395
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002396 if (strm_frm->id & QCS_ID_DIR_BIT)
2397 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2398 else
2399 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2400}
2401
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002402/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2403 * for <qc> QUIC connection.
2404 * This is a best effort function which never fails even if no memory could be
2405 * allocated to duplicate these frames.
2406 */
2407static void qc_dup_pkt_frms(struct quic_conn *qc,
2408 struct list *pkt_frm_list, struct list *out_frm_list)
2409{
2410 struct quic_frame *frm, *frmbak;
2411 struct list tmp = LIST_HEAD_INIT(tmp);
2412
2413 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2414 struct quic_frame *dup_frm, *origin;
2415
2416 switch (frm->type) {
2417 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2418 {
2419 struct quic_stream *strm_frm = &frm->stream;
2420 struct eb64_node *node = NULL;
2421 struct qc_stream_desc *stream_desc;
2422
2423 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2424 if (!node) {
2425 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm);
2426 continue;
2427 }
2428
2429 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2430 /* Do not resend this frame if in the "already acked range" */
2431 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2432 TRACE_PROTO("ignored frame frame in already acked range",
2433 QUIC_EV_CONN_PRSAFRM, qc, frm);
2434 continue;
2435 }
2436 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2437 strm_frm->offset.key = stream_desc->ack_offset;
2438 TRACE_PROTO("updated partially acked frame",
2439 QUIC_EV_CONN_PRSAFRM, qc, frm);
2440 }
2441
2442 break;
2443 }
2444
2445 default:
2446 break;
2447 }
2448
2449 dup_frm = pool_zalloc(pool_head_quic_frame);
2450 if (!dup_frm) {
2451 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2452 break;
2453 }
2454
2455 /* If <frm> is already a copy of another frame, we must take
2456 * its original frame as source for the copy.
2457 */
2458 origin = frm->origin ? frm->origin : frm;
2459 TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
2460 *dup_frm = *origin;
2461 LIST_INIT(&dup_frm->reflist);
2462 TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM,
2463 qc, NULL, &origin->pkt->pn_node.key);
2464 dup_frm->origin = origin;
2465 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2466 LIST_APPEND(&tmp, &dup_frm->list);
2467 }
2468
2469 LIST_SPLICE(out_frm_list, &tmp);
2470}
2471
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002472/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002473static void qc_prep_fast_retrans(struct quic_conn *qc,
2474 struct quic_enc_level *qel,
2475 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002476{
2477 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002478 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002479 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002480 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002481
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002482 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002483 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002484 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002485 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002486 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2487 node = eb64_next(node);
2488 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002489 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002490 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002491 }
2492
2493 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002494 return;
2495
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002496 /* When building a packet from another one, the field which may increase the
2497 * packet size is the packet number. And the maximum increase is 4 bytes.
2498 */
2499 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2500 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2501 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2502 return;
2503 }
2504
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002505 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2506 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2507 if (frms == frms1 && frms2) {
2508 frms = frms2;
2509 goto start;
2510 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002511}
2512
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002513/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002514 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002515 * Initial packets send them coalescing with others (Handshake here).
2516 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002517 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002518static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2519 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002520{
2521 struct list itmp = LIST_HEAD_INIT(itmp);
2522 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002523
2524 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2525 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2526 struct quic_enc_level *qel = iqel;
2527 struct eb_root *pkts;
2528 struct eb64_node *node;
2529 struct quic_tx_packet *pkt;
2530 struct list *tmp = &itmp;
2531
2532 start:
2533 pkt = NULL;
2534 pkts = &qel->pktns->tx.pkts;
2535 node = eb64_first(pkts);
2536 /* Skip the empty packet (they have already been retransmitted) */
2537 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002538 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002539 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002540 break;
2541 node = eb64_next(node);
2542 }
2543
2544 if (!pkt)
2545 goto end;
2546
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002547 /* When building a packet from another one, the field which may increase the
2548 * packet size is the packet number. And the maximum increase is 4 bytes.
2549 */
2550 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2551 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2552 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2553 goto end;
2554 }
2555
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002556 qel->pktns->tx.pto_probe += 1;
2557 requeue:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002558 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2559 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002560 if (qel == iqel) {
2561 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2562 pkt = pkt->next;
2563 tmp = &htmp;
2564 hqel->pktns->tx.pto_probe += 1;
2565 goto requeue;
2566 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002567 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002568
2569 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002570 LIST_SPLICE(ifrms, &itmp);
2571 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002572}
2573
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002574/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2575 * as I/O handler context and <qel> as encryption level.
2576 * Returns 1 if succeeded, 0 if failed.
2577 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002578static 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 +01002579 struct quic_enc_level *qel)
2580{
2581 struct quic_frame frm;
2582 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002583 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002584 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002586 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 /* Skip the AAD */
2588 pos = pkt->data + pkt->aad_len;
2589 end = pkt->data + pkt->len;
2590
2591 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002592 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002593 goto err;
2594
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002595 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002596 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002597 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002598 break;
2599 case QUIC_FT_PING:
2600 break;
2601 case QUIC_FT_ACK:
2602 {
2603 unsigned int rtt_sample;
2604
2605 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002606 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002607 goto err;
2608
2609 if (rtt_sample) {
2610 unsigned int ack_delay;
2611
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002612 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002613 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002614 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2615 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002616 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002617 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002618 break;
2619 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002620 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002621 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002622 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002623 {
2624 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002625
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002626 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002627 /* XXX TO DO: <cfdebug> is used only for the traces. */
2628 struct quic_rx_crypto_frm cfdebug = { };
2629
2630 cfdebug.offset_node.key = frm.crypto.offset;
2631 cfdebug.len = frm.crypto.len;
2632 TRACE_PROTO("CRYPTO data discarded",
2633 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2634 break;
2635 }
2636
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002637 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2638 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2639 /* XXX TO DO: <cfdebug> is used only for the traces. */
2640 struct quic_rx_crypto_frm cfdebug = { };
2641
2642 cfdebug.offset_node.key = frm.crypto.offset;
2643 cfdebug.len = frm.crypto.len;
2644 /* Nothing to do */
2645 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002646 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002647 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002648 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2649 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002650 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002651 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002652 else {
2653 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2654 /* XXX TO DO: <cfdebug> is used only for the traces. */
2655 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002656
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002657 cfdebug.offset_node.key = frm.crypto.offset;
2658 cfdebug.len = frm.crypto.len;
2659 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002660 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002661 frm.crypto.len -= diff;
2662 frm.crypto.data += diff;
2663 frm.crypto.offset = qel->rx.crypto.offset;
2664 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002666
2667 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002669 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002670
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002671 cfdebug.offset_node.key = frm.crypto.offset;
2672 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 if (!qc_provide_cdata(qel, ctx,
2674 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002675 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002676 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002677
2678 break;
2679 }
2680
2681 /* frm.crypto.offset > qel->rx.crypto.offset */
2682 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2683 if (!cf) {
2684 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002685 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002686 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002687 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002688
2689 cf->offset_node.key = frm.crypto.offset;
2690 cf->len = frm.crypto.len;
2691 cf->data = frm.crypto.data;
2692 cf->pkt = pkt;
2693 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2694 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002695 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002696 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002697 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002698 {
2699 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002700 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002701
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002702 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002703 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2704 goto err;
2705 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2706 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002707
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002708 /* The upper layer may not be allocated. */
2709 if (qc->mux_state != QC_MUX_READY) {
2710 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
2711 TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
2712 break;
2713 }
2714 else {
2715 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc);
2716 goto err;
2717 }
2718 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002719
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002720 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002721 goto err;
2722
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002723 break;
2724 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002725 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002726 if (qc->mux_state == QC_MUX_READY) {
2727 struct quic_max_data *data = &frm.max_data;
2728 qcc_recv_max_data(qc->qcc, data->max_data);
2729 }
2730 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002731 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002732 if (qc->mux_state == QC_MUX_READY) {
2733 struct quic_max_stream_data *data = &frm.max_stream_data;
2734 qcc_recv_max_stream_data(qc->qcc, data->id,
2735 data->max_stream_data);
2736 }
2737 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002738 case QUIC_FT_MAX_STREAMS_BIDI:
2739 case QUIC_FT_MAX_STREAMS_UNI:
2740 case QUIC_FT_DATA_BLOCKED:
2741 case QUIC_FT_STREAM_DATA_BLOCKED:
2742 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2743 case QUIC_FT_STREAMS_BLOCKED_UNI:
2744 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002745 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002746 case QUIC_FT_RETIRE_CONNECTION_ID:
2747 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748 break;
2749 case QUIC_FT_CONNECTION_CLOSE:
2750 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille47756802022-03-25 09:12:16 +01002751 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
2752 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2753 /* RFC 9000 10.2. Immediate Close:
2754 * The closing and draining connection states exist to ensure
2755 * that connections close cleanly and that delayed or reordered
2756 * packets are properly discarded. These states SHOULD persist
2757 * for at least three times the current PTO interval...
2758 *
2759 * Rearm the idle timeout only one time when entering draining
2760 * state.
2761 */
2762 qc_idle_timer_do_rearm(qc);
2763 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002764 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002765 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002766 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002768 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769 goto err;
2770
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002771 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772 break;
2773 default:
2774 goto err;
2775 }
2776 }
2777
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002778 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002779 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002780
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002781 if (fast_retrans) {
2782 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2783 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2784
2785 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2786 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002787
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2789 * has successfully parse a Handshake packet. The Initial encryption must also
2790 * be discarded.
2791 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002792 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002793 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002794 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2795 QUIC_FL_TLS_SECRETS_DCD)) {
2796 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2797 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2798 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2799 qc_set_timer(ctx->qc);
2800 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2801 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2802 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002803 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2804 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002805 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002806 }
2807
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002808 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002809 return 1;
2810
2811 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002812 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813 return 0;
2814}
2815
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002816/* Must be called only by a <cbuf> writer (packet builder).
2817 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2818 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2819 * index after having marked the end of written data. This the responsability
2820 * of the caller to ensure there is enough room in <cbuf> to write the end of
2821 * data made of a uint16_t null field.
2822 *
2823 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2824 * ^ ^
2825 * r w
2826 *
2827 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2828 * ^ ^
2829 * r w
2830
2831 * +--------------------------------------+ (empty buffer, can be reused)
2832 * ^
2833 * (r = w)
2834 *
2835 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2836 * ^ ^
2837 * w r
2838 */
2839static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2840{
2841 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2842
2843 /* We can reset the writer index only if in front of the reader index and
2844 * if the reader index is not null. Resetting the writer when the reader
2845 * index is null would empty the buffer.
2846 * XXX Note than the writer index cannot reach the reader index.
2847 * Only the reader index can reach the writer index.
2848 */
2849 if (rd && rd <= cbuf->wr) {
2850 /* Mark the end of contiguous data for the reader */
2851 write_u16(cb_wr(cbuf), 0);
2852 cb_add(cbuf, sizeof(uint16_t));
2853 cb_wr_reset(cbuf);
2854 return 1;
2855 }
2856
2857 return 0;
2858}
2859
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002860/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002861 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002862 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2863 * This function must be called only after having built a correct datagram.
2864 * Always succeeds.
2865 */
2866static inline void qc_set_dg(struct cbuf *cbuf,
2867 uint16_t dglen, struct quic_tx_packet *pkt)
2868{
2869 write_u16(cb_wr(cbuf), dglen);
2870 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2871 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2872}
2873
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002874/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2875 * with <frms> as ack-eliciting frame list to send, 0 if not.
2876 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2877 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2878 */
2879static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2880 struct quic_enc_level *qel, int cc, int probe)
2881{
2882 unsigned int must_ack =
2883 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2884
2885 /* Do not build any more packet if the TX secrets are not available or
2886 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2887 * and if there is no more packets to send upon PTO expiration
2888 * and if there is no more ack-eliciting frames to send or in flight
2889 * congestion control limit is reached for prepared data
2890 */
2891 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2892 (!cc && !probe && !must_ack &&
2893 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2894 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2895 return 0;
2896 }
2897
2898 return 1;
2899}
2900
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002901/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002902 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2903 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002904 * A header made of two fields is added to each datagram: the datagram length followed
2905 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002906 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2907 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002908 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002909static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2910 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002911{
2912 struct quic_enc_level *qel;
2913 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002914 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002915 struct quic_tx_packet *pkt;
2916 size_t total;
2917 size_t dg_headlen;
2918
2919 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2920 /* Each datagram is prepended with its length followed by the
2921 * address of the first packet in the datagram.
2922 */
2923 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2924 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2925 total = 0;
2926 start:
2927 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002928 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002929 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2930 * to ensure there is enough room to mark the end of prepared
2931 * contiguous data with a zero length.
2932 */
2933 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2934 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002935 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002936
2937 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002938 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002939 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002940 /* We do not probe if an immediate close was asked */
2941 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002942 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002943
2944 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002945 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002946
2947 /* Leave room for the datagram header */
2948 pos += dg_headlen;
2949 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2950 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2951 }
2952 else {
2953 end = pos + qc->path->mtu;
2954 }
2955
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002956 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002957 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002958 switch (err) {
2959 case -2:
2960 goto err;
2961 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002962 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2963 * MTU, we are here because of the congestion control window. There is
2964 * no need to try to reuse this buffer.
2965 */
2966 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002967 default:
2968 break;
2969 }
2970
2971 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2972 if (!pkt)
2973 goto err;
2974
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002975 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2976 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2977
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002978 total += pkt->len;
2979 /* Set the current datagram as prepared into <cbuf>. */
2980 qc_set_dg(cbuf, pkt->len, pkt);
2981 }
2982
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002983 /* Reset <wr> writer index if in front of <rd> index */
2984 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002985 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002986 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002987 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002988 }
2989
2990 out:
2991 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2992 return total;
2993
2994 err:
2995 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2996 return -1;
2997}
2998
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002999/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003000 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
3001 * several packets in the same datagram. A header made of two fields is added
3002 * to each datagram: the datagram length followed by the address of the first
3003 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01003004 * Returns the number of bytes prepared in packets if succeeded (may be 0),
3005 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003006 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003007static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003008 enum quic_tls_enc_level tel, struct list *tel_frms,
3009 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003010{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003011 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003012 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003013 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003014 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3015 /* length of datagrams */
3016 uint16_t dglen;
3017 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003018 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003019 /* Each datagram is prepended with its length followed by the
3020 * address of the first packet in the datagram.
3021 */
3022 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003023 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003024
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003025 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3026
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003027 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003028 qel = &qc->els[tel];
3029 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003030 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003031 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003032 padding = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003033 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003034 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003035 /* Leave at least <dglen> bytes at the end of this buffer
3036 * to ensure there is enough room to mark the end of prepared
3037 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003038 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003039 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
3040 first_pkt = prv_pkt = NULL;
3041 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003042 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043 enum quic_pkt_type pkt_type;
3044
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003045 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003046 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003047 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003048 /* We do not probe if an immediate close was asked */
3049 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003050 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003051
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003052 if (!qc_may_build_pkt(qc, frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003053 if (prv_pkt)
3054 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003055 /* Let's select the next encryption level */
3056 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3057 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003058 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003059 qel = &qc->els[tel];
3060 /* Build a new datagram */
3061 prv_pkt = NULL;
3062 continue;
3063 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003064 break;
3065 }
3066
3067 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003068 if (!prv_pkt) {
3069 /* Leave room for the datagram header */
3070 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003071 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01003072 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3073 }
3074 else {
3075 end = pos + qc->path->mtu;
3076 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003077 }
3078
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003079 cur_pkt = qc_build_pkt(&pos, end, qel, frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003080 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003081 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003082 case -2:
3083 goto err;
3084 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003085 /* If there was already a correct packet present, set the
3086 * current datagram as prepared into <cbuf>.
3087 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01003088 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003089 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01003090 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003092 break;
3093 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003094
Frédéric Lécaille63556772021-12-29 17:18:21 +01003095 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3096 if (!cur_pkt)
3097 goto err;
3098
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003099 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3100 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3101
Frédéric Lécaille63556772021-12-29 17:18:21 +01003102 total += cur_pkt->len;
3103 /* keep trace of the first packet in the datagram */
3104 if (!first_pkt)
3105 first_pkt = cur_pkt;
3106 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003107 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003108 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003109 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3110 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003111 /* Let's say we have to build a new dgram */
3112 prv_pkt = NULL;
3113 dglen += cur_pkt->len;
3114 /* Client: discard the Initial encryption keys as soon as
3115 * a handshake packet could be built.
3116 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003117 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003118 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3119 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3120 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3121 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3122 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003123 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003124 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003125 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003126 }
3127 /* If the data for the current encryption level have all been sent,
3128 * select the next level.
3129 */
3130 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003131 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 +01003132 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3133 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3134 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3135 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003136 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3137 frms = &qc->els[tel].pktns->tx.frms;
3138 else
3139 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003140 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003141 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003142 /* If there is data for the next level, do not
3143 * consume a datagram.
3144 */
3145 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003147 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003148 /* If we have to build a new datagram, set the current datagram as
3149 * prepared into <cbuf>.
3150 */
3151 if (!prv_pkt) {
3152 qc_set_dg(cbuf, dglen, first_pkt);
3153 first_pkt = NULL;
3154 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003155 padding = 0;
3156 }
3157 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003158 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003159 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3160 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003161 }
3162 }
3163
3164 stop_build:
3165 /* Reset <wr> writer index if in front of <rd> index */
3166 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003167 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003168 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003169 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003170 }
3171
3172 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003173 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003174 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175
3176 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003177 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003178 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179}
3180
3181/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003182 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003183 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003184int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003185{
3186 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003187 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003188
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003189 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003190 cbuf = qr->cbuf;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003191 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003192 while (cb_contig_data(cbuf)) {
3193 unsigned char *pos;
3194 struct buffer tmpbuf = { };
3195 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3196 uint16_t dglen;
3197 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003198 unsigned int time_sent;
3199
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003200 pos = cb_rd(cbuf);
3201 dglen = read_u16(pos);
3202 /* End of prepared datagrams.
3203 * Reset the reader index only if in front of the writer index.
3204 */
3205 if (!dglen) {
3206 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3207
3208 if (wr && wr < cbuf->rd) {
3209 cb_rd_reset(cbuf);
3210 continue;
3211 }
3212 break;
3213 }
3214
3215 pos += sizeof dglen;
3216 first_pkt = read_ptr(pos);
3217 pos += sizeof first_pkt;
3218 tmpbuf.area = (char *)pos;
3219 tmpbuf.size = tmpbuf.data = dglen;
3220
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003221 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003222 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003223 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003224
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003225 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003226 qc->tx.bytes += tmpbuf.data;
3227 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003228
3229 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3230 pkt->time_sent = time_sent;
3231 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3232 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003233 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003234 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3235 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003236 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003237 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3238 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3239 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003240 qc_notify_close(qc);
3241
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003242 /* RFC 9000 10.2. Immediate Close:
3243 * The closing and draining connection states exist to ensure
3244 * that connections close cleanly and that delayed or reordered
3245 * packets are properly discarded. These states SHOULD persist
3246 * for at least three times the current PTO interval...
3247 *
3248 * Rearm the idle timeout only one time when entering closing
3249 * state.
3250 */
3251 qc_idle_timer_do_rearm(qc);
3252 if (qc->timer_task) {
3253 task_destroy(qc->timer_task);
3254 qc->timer_task = NULL;
3255 }
3256 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003257 qc->path->in_flight += pkt->in_flight_len;
3258 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3259 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003260 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003261 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003262 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003263 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003264 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003265 }
3266 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003267
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003268 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3269
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003270 return 1;
3271}
3272
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003273/* Copy into <buf> buffer a stateless reset token depending on the
3274 * <salt> salt input. This is the cluster secret which will be derived
3275 * as HKDF input secret to generate this token.
3276 * Return 1 if succeeded, 0 if not.
3277 */
3278static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len,
3279 const unsigned char *salt, size_t saltlen)
3280{
3281 /* Input secret */
3282 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3283 size_t keylen = strlen(global.cluster_secret);
3284 /* Info */
3285 const unsigned char label[] = "stateless token";
3286 size_t labellen = sizeof label - 1;
3287
3288 return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3289 key, keylen, salt, saltlen, label, labellen);
3290}
3291
3292/* Initialize the stateless reset token attached to <cid> connection ID.
3293 * Returns 1 if succeeded, 0 if not.
3294 */
3295static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid)
3296{
3297 if (global.cluster_secret) {
3298 /* Output secret */
3299 unsigned char *token = quic_cid->stateless_reset_token;
3300 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3301 /* Salt */
3302 const unsigned char *cid = quic_cid->cid.data;
3303 size_t cidlen = quic_cid->cid.len;
3304
3305 return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
3306 }
3307 else {
3308 return RAND_bytes(quic_cid->stateless_reset_token,
3309 sizeof quic_cid->stateless_reset_token) == 1;
3310 }
3311}
3312
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003313/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3314 * ebtree.
3315 *
3316 * The CID is randomly generated in part with the result altered to be
3317 * associated with the current thread ID. This means this function must only
3318 * be called by the quic_conn thread.
3319 *
3320 * Returns the new CID if succeeded, NULL if not.
3321 */
3322static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3323 struct quic_conn *qc,
3324 int seq_num)
3325{
3326 struct quic_connection_id *cid;
3327
3328 cid = pool_alloc(pool_head_quic_connection_id);
3329 if (!cid)
3330 return NULL;
3331
3332 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003333 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1)
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003334 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003335
3336 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003337 if (quic_stateless_reset_token_init(cid) != 1)
3338 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003339
3340 cid->qc = qc;
3341
3342 cid->seq_num.key = seq_num;
3343 cid->retire_prior_to = 0;
3344 /* insert the allocated CID in the quic_conn tree */
3345 eb64_insert(root, &cid->seq_num);
3346
3347 return cid;
3348
3349 err:
3350 pool_free(pool_head_quic_connection_id, cid);
3351 return NULL;
3352}
3353
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003354/* Build all the frames which must be sent just after the handshake have succeeded.
3355 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3356 * a HANDSHAKE_DONE frame.
3357 * Return 1 if succeeded, 0 if not.
3358 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003359static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003360{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003361 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003362 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003363 struct quic_frame *frm, *frmbak;
3364 struct list frm_list = LIST_HEAD_INIT(frm_list);
3365 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003366
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003367 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003369 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003370 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003371 if (!frm)
3372 return 0;
3373
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003374 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003376 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003377 }
3378
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003379 first = 1;
3380 max = qc->tx.params.active_connection_id_limit;
3381 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003382 struct quic_connection_id *cid;
3383
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003384 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003385 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 goto err;
3387
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003388 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003389 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003390 if (!cid)
3391 goto err;
3392
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003393 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003394 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003395
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003397 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003398 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003399
3400 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003401 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402
3403 return 1;
3404
3405 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003406 /* free the frames */
3407 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3408 pool_free(pool_head_quic_frame, frm);
3409
3410 node = eb64_first(&qc->cids);
3411 while (node) {
3412 struct quic_connection_id *cid;
3413
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003414
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003415 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003416 if (cid->seq_num.key >= max)
3417 break;
3418
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003419 if (cid->seq_num.key < first)
3420 continue;
3421
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003422 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003423 ebmb_delete(&cid->node);
3424 eb64_delete(&cid->seq_num);
3425 pool_free(pool_head_quic_connection_id, cid);
3426 }
3427
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428 return 0;
3429}
3430
3431/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003432void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003434 struct eb64_node *n;
3435 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003436
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003437 n = eb64_first(&arngs->root);
3438 while (n) {
3439 struct eb64_node *next;
3440
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003441 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003442 next = eb64_next(n);
3443 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003444 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003445 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003446 }
3447}
3448
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003449/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3450 * descending order.
3451 */
3452static inline size_t sack_gap(struct quic_arng_node *p,
3453 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003454{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003455 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003456}
3457
3458
3459/* Remove the last elements of <ack_ranges> list of ack range updating its
3460 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003461 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003462 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003463static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003464{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003465 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003466
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003467 last = eb64_last(&arngs->root);
3468 while (last && arngs->enc_sz > limit) {
3469 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003470
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003471 prev = eb64_prev(last);
3472 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003473 return 0;
3474
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003475 last_node = eb64_entry(last, struct quic_arng_node, first);
3476 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003477 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3478 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3479 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3480 --arngs->sz;
3481 eb64_delete(last);
3482 pool_free(pool_head_quic_arng, last);
3483 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003484 }
3485
3486 return 1;
3487}
3488
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003489/* Set the encoded size of <arngs> QUIC ack ranges. */
3490static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3491{
3492 struct eb64_node *node, *next;
3493 struct quic_arng_node *ar, *ar_next;
3494
3495 node = eb64_last(&arngs->root);
3496 if (!node)
3497 return;
3498
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003499 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003500 arngs->enc_sz = quic_int_getsize(ar->last) +
3501 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3502
3503 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003504 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003505 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3506 quic_int_getsize(ar_next->last - ar_next->first.key);
3507 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003508 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003509 }
3510}
3511
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003512/* Insert <ar> ack range into <argns> tree of ack ranges.
3513 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003514 */
3515static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003516struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3517 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003518{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003519 struct quic_arng_node *new_ar;
3520
3521 new_ar = pool_alloc(pool_head_quic_arng);
3522 if (new_ar) {
3523 new_ar->first.key = ar->first;
3524 new_ar->last = ar->last;
3525 eb64_insert(&arngs->root, &new_ar->first);
3526 arngs->sz++;
3527 }
3528
3529 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003530}
3531
3532/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003533 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003534 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003535 *
3536 * Descending order
3537 * ------------->
3538 * range1 range2
3539 * ..........|--------|..............|--------|
3540 * ^ ^ ^ ^
3541 * | | | |
3542 * last1 first1 last2 first2
3543 * ..........+--------+--------------+--------+......
3544 * diff1 gap12 diff2
3545 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003546 * To encode the previous list of ranges we must encode integers as follows in
3547 * descending order:
3548 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003549 * with diff1 = last1 - first1
3550 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003551 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003552 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003553 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003554int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3555 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003556{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003557 struct eb64_node *le;
3558 struct quic_arng_node *new_node;
3559 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003561 new = NULL;
3562 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003563 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003564 if (!new_node)
3565 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003566
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003567 goto out;
3568 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003570 le = eb64_lookup_le(&arngs->root, ar->first);
3571 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003572 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003573 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003574 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003575
3576 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003577 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003578 else {
3579 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003580 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003581
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003582 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003583 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003584 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003585
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003586 if (le_ar->last + 1 >= ar->first) {
3587 le_ar->last = ar->last;
3588 new = le;
3589 new_node = le_ar;
3590 }
3591 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003592 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003593 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003594 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003595
3596 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003597 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003598 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003599
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003600 /* Verify that the new inserted node does not overlap the nodes
3601 * which follow it.
3602 */
3603 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003604 struct eb64_node *next;
3605 struct quic_arng_node *next_node;
3606
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003607 while ((next = eb64_next(new))) {
3608 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003609 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003610 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003611 break;
3612
3613 if (next_node->last > new_node->last)
3614 new_node->last = next_node->last;
3615 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003616 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003617 /* Decrement the size of these ranges. */
3618 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003619 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003620 }
3621
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003622 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003623 quic_arngs_set_enc_sz(arngs);
3624
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625 return 1;
3626}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627/* Remove the header protection of packets at <el> encryption level.
3628 * Always succeeds.
3629 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003630static 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 +01003631{
3632 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003633 struct quic_rx_packet *pqpkt;
3634 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003635 struct quic_enc_level *app_qel;
3636
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003637 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3638 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003639 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003640 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003641 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003642 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003643 goto out;
3644 }
3645 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003646 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003647 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003648 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003649 pqpkt->data, pqpkt->data + pqpkt->len)) {
3650 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003651 /* XXX TO DO XXX */
3652 }
3653 else {
3654 /* The AAD includes the packet number field */
3655 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3656 /* Store the packet into the tree of packets to decrypt. */
3657 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003658 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003659 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3660 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003661 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003662 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003664 MT_LIST_DELETE_SAFE(pkttmp1);
3665 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666 }
3667
3668 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003669 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003670}
3671
3672/* Process all the CRYPTO frame at <el> encryption level.
3673 * Return 1 if succeeded, 0 if not.
3674 */
3675static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003676 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677{
3678 struct eb64_node *node;
3679
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003680 node = eb64_first(&el->rx.crypto.frms);
3681 while (node) {
3682 struct quic_rx_crypto_frm *cf;
3683
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003684 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003685 if (cf->offset_node.key != el->rx.crypto.offset)
3686 break;
3687
3688 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3689 goto err;
3690
3691 node = eb64_next(node);
3692 quic_rx_packet_refdec(cf->pkt);
3693 eb64_delete(&cf->offset_node);
3694 pool_free(pool_head_quic_rx_crypto_frm, cf);
3695 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003696 return 1;
3697
3698 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003699 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003700 return 0;
3701}
3702
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003703/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003704 * 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 +02003705 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003706 * Return 1 if succeeded, 0 if not.
3707 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003708int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3709 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003710{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003711 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003712 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003713 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003714 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003715 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003716
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003717 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003718 qel = cur_el;
3719 next_tel:
3720 if (!qel)
3721 goto out;
3722
3723 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3724 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725 while (node) {
3726 struct quic_rx_packet *pkt;
3727
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003728 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003729 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003730 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003731 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003732 /* Drop the packet */
3733 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003734 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003735 }
3736 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003737 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003738 /* Drop the packet */
3739 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003740 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003741 }
3742 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003743 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3744
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003745 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3746 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3747 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003748 qc_idle_timer_rearm(qc, 1);
3749 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003750 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003751 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003752 largest_pn_time_received = pkt->time_received;
3753 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003754 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003755 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003756 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003757 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003758 }
3759 }
3760 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003761 eb64_delete(&pkt->pn_node);
3762 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003764 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003765
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003766 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3767 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003768 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003769 /* Update the largest acknowledged packet timestamps */
3770 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3771 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3772 }
3773
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003774 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003775 goto err;
3776
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003777 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003778 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003779 qel = next_el;
3780 goto next_tel;
3781 }
3782
3783 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003784 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785 return 1;
3786
3787 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003788 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789 return 0;
3790}
3791
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003792/* Check if it's possible to remove header protection for packets related to
3793 * encryption level <qel>. If <qel> is NULL, assume it's false.
3794 *
3795 * Return true if the operation is possible else false.
3796 */
3797static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3798{
3799 enum quic_tls_enc_level tel;
3800
3801 if (!qel)
3802 return 0;
3803
3804 tel = ssl_to_quic_enc_level(qel->level);
3805
3806 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003807 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003808 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003809 return 0;
3810 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003811
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003812 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003813 return 0;
3814
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003815 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003816 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003817 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003818 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003819
3820 return 1;
3821}
3822
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003823/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003824int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003825{
3826 int ret;
3827 struct qring *qr;
3828
3829 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3830 if (!qr)
3831 /* Never happens */
3832 return 1;
3833
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003834 if (old_data)
3835 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003836 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003837 if (ret == -1)
3838 goto err;
3839 else if (ret == 0)
3840 goto out;
3841
3842 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3843 goto err;
3844
3845 out:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003846 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003847 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3848 return 1;
3849
3850 err:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003851 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003852 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3853 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3854 return 0;
3855}
3856
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003857/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3858 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
3859 * QUIC connection
3860 * Returns 1 if succeeded, 0 if not.
3861 */
3862int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
3863 enum quic_tls_enc_level tel, struct list *tel_frms,
3864 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3865{
3866 int ret;
3867 struct qring *qr;
3868
3869 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3870 if (!qr)
3871 /* Never happens */
3872 return 1;
3873
3874 if (old_data)
3875 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
3876 ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms);
3877 if (ret == -1)
3878 goto err;
3879 else if (ret == 0)
3880 goto out;
3881
3882 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3883 goto err;
3884
3885 out:
3886 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3887 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3888 return 1;
3889
3890 err:
3891 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3892 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3893 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3894 return 0;
3895}
3896
3897/* Retransmit up to two datagrams depending on packet number space */
3898static void qc_dgrams_retransmit(struct quic_conn *qc)
3899{
3900 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3901 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3902 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3903
3904 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3905 struct list ifrms = LIST_HEAD_INIT(ifrms);
3906 struct list hfrms = LIST_HEAD_INIT(hfrms);
3907
3908 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
3909 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
3910 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3911 if (!LIST_ISEMPTY(&ifrms)) {
3912 iqel->pktns->tx.pto_probe = 1;
3913 if (!LIST_ISEMPTY(&hfrms)) {
3914 hqel->pktns->tx.pto_probe = 1;
3915 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
3916 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
3917 }
3918 }
3919 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3920 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
3921 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3922 if (!LIST_ISEMPTY(&hfrms)) {
3923 hqel->pktns->tx.pto_probe = 1;
3924 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
3925 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3926 }
3927 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3928 }
3929 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3930 }
3931 else {
3932 int i;
3933 struct list frms1 = LIST_HEAD_INIT(frms1);
3934 struct list frms2 = LIST_HEAD_INIT(frms2);
3935
3936 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3937 hqel->pktns->tx.pto_probe = 0;
3938 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
3939 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
3940 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3941 if (!LIST_ISEMPTY(&frms1)) {
3942 hqel->pktns->tx.pto_probe = 1;
3943 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
3944 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3945 }
3946 }
3947 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3948 }
3949 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3950 aqel->pktns->tx.pto_probe = 0;
3951 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
3952 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3953 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
3954 if (!LIST_ISEMPTY(&frms1)) {
3955 aqel->pktns->tx.pto_probe = 1;
3956 qc_send_app_pkts(qc, 1, &frms1);
3957 }
3958 if (!LIST_ISEMPTY(&frms2)) {
3959 aqel->pktns->tx.pto_probe = 1;
3960 qc_send_app_pkts(qc, 1, &frms2);
3961 }
3962 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3963 }
3964 }
3965}
3966
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003967/* QUIC connection packet handler task (post handshake) */
3968static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3969{
3970 struct ssl_sock_ctx *ctx;
3971 struct quic_conn *qc;
3972 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003973
3974
3975 ctx = context;
3976 qc = ctx->qc;
3977 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003978
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003979 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003980
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003981 /* Retranmissions */
3982 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3983 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
3984 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3985 qc_dgrams_retransmit(qc);
3986 }
3987
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003988 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3989 qc_rm_hp_pkts(qc, qel);
3990
3991 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3992 goto err;
3993
Frédéric Lécaille47756802022-03-25 09:12:16 +01003994 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3995 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3996 goto out;
3997
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003998 if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003999 goto err;
4000
Frédéric Lécaille47756802022-03-25 09:12:16 +01004001out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004002 return t;
4003
4004 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004005 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004006 return t;
4007}
4008
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004009/* QUIC connection packet handler task. */
4010struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004011{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004012 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004013 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004014 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004015 enum quic_tls_enc_level tel, next_tel;
4016 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004017 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004018 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004019
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004020 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004021 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004022 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004023 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004024 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004025 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02004026
4027 /* Retranmissions */
4028 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
4029 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
4030 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
4031 qc_dgrams_retransmit(qc);
4032 }
4033
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004034 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
4035 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004036 /* The I/O handler has been woken up by the dgram listener
4037 * after the anti-amplification was reached.
4038 */
4039 qc_set_timer(qc);
4040 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
4041 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
4042 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004043 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01004044 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
4045 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
4046 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004047 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004048 if (st >= QUIC_HS_ST_COMPLETE &&
4049 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
4050 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
4051 /* There may be remaining Handshake packets to treat and acknowledge. */
4052 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4053 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4054 }
4055 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004056 goto err;
4057
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004058 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004059 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004060
4061 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004062 /* Treat packets waiting for header packet protection decryption */
4063 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004064 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004065
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004066 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4067 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4068 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004069 goto err;
4070
Frédéric Lécaille47756802022-03-25 09:12:16 +01004071 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4072 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4073 goto out;
4074
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02004075 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
4076 !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
4077 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
4078 qel = next_qel;
4079 next_qel = NULL;
4080 goto next_level;
4081 }
4082 else {
4083 struct quic_rx_packet *pkt;
4084 struct mt_list *elt1, elt2;
4085 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
4086
4087 /* Drop these 0-RTT packets */
4088 TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
4089 mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) {
4090 MT_LIST_DELETE_SAFE(elt1);
4091 quic_rx_packet_refdec(pkt);
4092 }
4093 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004094 }
4095
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004096 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004097 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004098 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004099 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004100 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004101
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004102 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004103 QUIC_FL_TLS_SECRETS_DCD)) {
4104 /* Discard the Handshake keys. */
4105 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4106 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4107 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4108 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004109 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01004110 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004111 }
4112
4113 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4114 /* There may be remaining handshake to build (acks) */
4115 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4116 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004117 }
4118
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004119 if (!qr)
4120 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004121 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4122 * be considered.
4123 */
4124 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01004125 goto err;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004126 ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms,
4127 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004128 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004129 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004130 else if (ret == 0)
4131 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004132
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004133 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004134 goto err;
4135
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004136 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004137 /* Check if there is something to do for the next level.
4138 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004139 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004140 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01004141 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004142 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004143 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004144 goto next_level;
4145 }
4146
Frédéric Lécaille47756802022-03-25 09:12:16 +01004147 out:
4148 if (qr)
4149 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004150 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004151 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004152
4153 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004154 if (qr)
4155 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004156 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004157 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004158}
4159
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004160/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004161static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
4162{
4163 int i;
4164
4165 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4166 if (qel->tx.crypto.bufs[i]) {
4167 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4168 qel->tx.crypto.bufs[i] = NULL;
4169 }
4170 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004171 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004172}
4173
4174/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004175 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004176 * Returns 1 if succeeded, 0 if not.
4177 */
4178static int quic_conn_enc_level_init(struct quic_conn *qc,
4179 enum quic_tls_enc_level level)
4180{
4181 struct quic_enc_level *qel;
4182
4183 qel = &qc->els[level];
4184 qel->level = quic_to_ssl_enc_level(level);
4185 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4186 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4187 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004188 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004189
4190 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02004191 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004192 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004193 qel->rx.crypto.offset = 0;
4194 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004195
4196 /* Allocate only one buffer. */
4197 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4198 if (!qel->tx.crypto.bufs)
4199 goto err;
4200
4201 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4202 if (!qel->tx.crypto.bufs[0])
4203 goto err;
4204
4205 qel->tx.crypto.bufs[0]->sz = 0;
4206 qel->tx.crypto.nb_buf = 1;
4207
4208 qel->tx.crypto.sz = 0;
4209 qel->tx.crypto.offset = 0;
4210
4211 return 1;
4212
4213 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004214 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 return 0;
4216}
4217
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004218/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4219 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004220 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004221 * This function must only be called by the thread responsible of the quic_conn
4222 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004223 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004224static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225{
4226 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004227 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004228 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004229 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004230
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004231 /* We must not free the quic-conn if the MUX is still allocated. */
4232 BUG_ON(qc->mux_state == QC_MUX_READY);
4233
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004234 /* free remaining stream descriptors */
4235 node = eb64_first(&qc->streams_by_id);
4236 while (node) {
4237 struct qc_stream_desc *stream;
4238
4239 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4240 node = eb64_next(node);
4241
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004242 /* all streams attached to the quic-conn are released, so
4243 * qc_stream_desc_free will liberate the stream instance.
4244 */
4245 BUG_ON(!stream->release);
4246 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004247 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004248
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004249 if (qc->idle_timer_task) {
4250 task_destroy(qc->idle_timer_task);
4251 qc->idle_timer_task = NULL;
4252 }
4253
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004254 if (qc->timer_task) {
4255 task_destroy(qc->timer_task);
4256 qc->timer_task = NULL;
4257 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004258
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004259 /* remove the connection from receiver cids trees */
4260 ebmb_delete(&qc->odcid_node);
4261 ebmb_delete(&qc->scid_node);
4262 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004263
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004264 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004265 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004266 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004267 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004268 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004269 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004270
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004271 quic_tls_ku_free(qc);
4272 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4273 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004274 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004275 }
4276
4277 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4278 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4279 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004280
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004281 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
4282 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02004283 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004284 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004285
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004286 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4287 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004288 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004289}
4290
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004291static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004292{
4293 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004294 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004295
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004296 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004297
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004298 /* Next application data can be dropped. */
4299 qc->mux_state = QC_MUX_RELEASED;
4300
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004301 /* If the quic-conn timer has already expired free the quic-conn. */
4302 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4303 quic_conn_release(qc);
4304 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
4305 return;
4306 }
4307
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004308 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004309}
4310
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004311/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004312static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004313{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004314 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004315 struct quic_conn *qc;
4316 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317
4318 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004319 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004320 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004321 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322 task->expire = TICK_ETERNITY;
4323 pktns = quic_loss_pktns(qc);
4324 if (tick_isset(pktns->tx.loss_time)) {
4325 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4326
4327 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4328 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004329 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4330 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004331 goto out;
4332 }
4333
4334 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004335 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004336 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4337 qc->qcc->subs->events & SUB_RETRY_SEND) {
4338 struct qcc *qcc = qc->qcc;
4339
4340 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4341 tasklet_wakeup(qcc->subs->tasklet);
4342 qcc->subs->events &= ~SUB_RETRY_SEND;
4343 if (!qcc->subs->events)
4344 qcc->subs = NULL;
4345 }
4346 else {
4347 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4348 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4349 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
4350 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
4351 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4352 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004353 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004354 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004355 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004356 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4357 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4358
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004359 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004360 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004361 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004362 iel->pktns->tx.pto_probe = 1;
4363 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004364
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004365 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4366 qc->path->loss.pto_count++;
4367
4368 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004369 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004370
4371 return task;
4372}
4373
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004374/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4375 * will extract the parameters stored in the token : <odcid>.
4376 *
4377 * Returns 0 on success else non-zero.
4378 */
4379static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4380 struct quic_cid *odcid)
4381{
4382 uint64_t odcid_len;
4383
4384 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4385 return 1;
4386
4387 if (odcid_len > QUIC_CID_MAXLEN)
4388 return 1;
4389
4390 memcpy(odcid->data, token, odcid_len);
4391 odcid->len = odcid_len;
4392
4393 return 0;
4394}
4395
4396/* Initialize the transport parameters for <qc> QUIC connection attached
4397 * to <l> listener from <pkt> Initial packet information.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004398 * Returns 1 if succeeded, 0 if not.
4399 */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004400static int qc_lstnr_params_init(struct quic_conn *qc, struct listener *l,
4401 const unsigned char *token, size_t token_len,
Frédéric Lécaille806e6cf2022-05-09 16:22:29 +02004402 const struct quic_connection_id *icid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004403 const struct quic_cid *dcid)
4404{
4405 struct quic_cid *odcid = &qc->rx.params.original_destination_connection_id;
4406
4407 /* Copy the transport parameters. */
4408 qc->rx.params = l->bind_conf->quic_params;
Frédéric Lécaille806e6cf2022-05-09 16:22:29 +02004409 /* Copy the stateless reset token */
4410 memcpy(qc->rx.params.stateless_reset_token, icid->stateless_reset_token,
4411 sizeof qc->rx.params.stateless_reset_token);
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004412 /* Copy original_destination_connection_id transport parameter. */
4413 if (token_len) {
4414 if (parse_retry_token(token, token_len, odcid)) {
4415 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4416 return 0;
4417 }
4418 /* Copy retry_source_connection_id transport parameter. */
4419 quic_cid_cpy(&qc->rx.params.retry_source_connection_id, dcid);
4420 }
4421 else {
4422 memcpy(odcid->data, dcid->data, dcid->len);
4423 odcid->len = dcid->len;
4424 }
4425
4426 /* Copy the initial source connection ID. */
4427 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4428
4429 return 1;
4430}
4431
4432/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4433 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4434 * for QUIC servers (or haproxy listeners).
4435 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4436 * <token> the token found to be used for this connection with <token_len> as
4437 * length. <saddr> is the source address.
4438 * Returns the connection if succeeded, NULL if not.
4439 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004440static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004441 struct quic_cid *dcid, struct quic_cid *scid,
4442 struct sockaddr_storage *saddr,
4443 const unsigned char *token, size_t token_len,
4444 int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004445{
4446 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004447 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004448 /* Initial CID. */
4449 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004450 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004451 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004452
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004453 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004454 qc = pool_zalloc(pool_head_quic_conn);
4455 if (!qc) {
4456 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
4457 goto err;
4458 }
4459
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004460 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4461 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004462 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004463 goto err;
4464 }
4465
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004466 qc->cids = EB_ROOT;
4467 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004468 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004469 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004470
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004471 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004472 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004473 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004474 qc->odcid.len = dcid->len;
4475 qc->odcid.addrlen = dcid->addrlen;
4476 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004478 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004479 if (scid->len)
4480 memcpy(qc->dcid.data, scid->data, scid->len);
4481 qc->dcid.len = scid->len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02004482 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004483 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004484 }
4485 /* QUIC Client (outgoing connection to servers) */
4486 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004487 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004488 if (dcid->len)
4489 memcpy(qc->dcid.data, dcid->data, dcid->len);
4490 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004492 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004493
4494 /* Initialize the output buffer */
4495 qc->obuf.pos = qc->obuf.data;
4496
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004497 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004498 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004499 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004500 goto err;
4501 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004503 /* insert the allocated CID in the receiver datagram handler tree */
4504 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004505 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004506
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004507 /* Select our SCID which is the first CID with 0 as sequence number. */
4508 qc->scid = icid->cid;
4509
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510 /* Packet number spaces initialization. */
4511 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4512 quic_pktns_init(&qc->pktns[i]);
4513 /* QUIC encryption level context initialization. */
4514 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004515 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004516 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004517 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004518 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004519 /* Initialize the packet number space. */
4520 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4521 }
4522
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02004523 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004524 qc->tps_tls_ext = qc->version & 0xff000000 ?
4525 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4526 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004527 /* TX part. */
4528 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004529 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4530 qc->tx.wbuf = qc->tx.rbuf = 0;
4531 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004532 /* RX part. */
4533 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004534 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004535 for (i = 0; i < QCS_MAX_TYPES; i++)
4536 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004537
4538 qc->nb_pkt_for_cc = 1;
4539 qc->nb_pkt_since_cc = 0;
4540
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004541 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004542 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004543 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004544 goto err;
4545 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004546
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004547 /* XXX TO DO: Only one path at this time. */
4548 qc->path = &qc->paths[0];
4549 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4550
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004551 /* required to use MTLIST_IN_LIST */
4552 MT_LIST_INIT(&qc->accept_list);
4553
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004554 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004555 qc->stream_buf_count = 0;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02004556 qc->sendto_err = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004557 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4558
Frédéric Lécaille806e6cf2022-05-09 16:22:29 +02004559 if (server && !qc_lstnr_params_init(qc, l, token, token_len, icid, dcid))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004560 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004561
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004562 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004563
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004564 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004565
4566 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004567 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004568 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4569 if (qc)
4570 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004571 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004572 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004573}
4574
4575/* Initialize the timer task of <qc> QUIC connection.
4576 * Returns 1 if succeeded, 0 if not.
4577 */
4578static int quic_conn_init_timer(struct quic_conn *qc)
4579{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004580 /* Attach this task to the same thread ID used for the connection */
4581 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004582 if (!qc->timer_task)
4583 return 0;
4584
4585 qc->timer = TICK_ETERNITY;
4586 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004587 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004588
4589 return 1;
4590}
4591
Frédéric Lécaille47756802022-03-25 09:12:16 +01004592/* Rearm the idle timer for <qc> QUIC connection. */
4593static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4594{
4595 unsigned int expire;
4596
4597 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4598 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4599}
4600
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004601/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4602 * which is set to 1 when receiving a packet , and 0 when sending packet
4603 */
4604static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4605{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004606 if (read) {
4607 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4608 }
4609 else {
4610 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4611 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004612 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004613}
4614
4615/* The task handling the idle timeout */
4616static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4617{
4618 struct quic_conn *qc = ctx;
4619
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004620 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4621 * might free the quic-conn too early via quic_close().
4622 */
4623 qc_notify_close(qc);
4624
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004625 /* If the MUX is still alive, keep the quic-conn. The MUX is
4626 * responsible to call quic_close to release it.
4627 */
4628 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4629 if (qc->mux_state != QC_MUX_READY)
4630 quic_conn_release(qc);
4631
4632 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4633 * least clean some parts of it such as the tasklet.
4634 */
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004635
4636 return NULL;
4637}
4638
4639/* Initialize the idle timeout task for <qc>.
4640 * Returns 1 if succeeded, 0 if not.
4641 */
4642static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4643{
4644 qc->idle_timer_task = task_new_here();
4645 if (!qc->idle_timer_task)
4646 return 0;
4647
4648 qc->idle_timer_task->process = qc_idle_timer_task;
4649 qc->idle_timer_task->context = qc;
4650 qc_idle_timer_rearm(qc, 1);
4651 task_queue(qc->idle_timer_task);
4652
4653 return 1;
4654}
4655
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004656/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4657 * past one byte of this buffer.
4658 */
4659static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4660 struct quic_rx_packet *pkt)
4661{
4662 unsigned char dcid_len, scid_len;
4663
4664 /* Version */
4665 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4666 return 0;
4667
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004668 /* Destination Connection ID Length */
4669 dcid_len = *(*buf)++;
4670 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4671 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4672 /* XXX MUST BE DROPPED */
4673 return 0;
4674
4675 if (dcid_len) {
4676 /* Check that the length of this received DCID matches the CID lengths
4677 * of our implementation for non Initials packets only.
4678 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004679 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4680 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004681 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004682 return 0;
4683
4684 memcpy(pkt->dcid.data, *buf, dcid_len);
4685 }
4686
4687 pkt->dcid.len = dcid_len;
4688 *buf += dcid_len;
4689
4690 /* Source Connection ID Length */
4691 scid_len = *(*buf)++;
4692 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4693 /* XXX MUST BE DROPPED */
4694 return 0;
4695
4696 if (scid_len)
4697 memcpy(pkt->scid.data, *buf, scid_len);
4698 pkt->scid.len = scid_len;
4699 *buf += scid_len;
4700
4701 return 1;
4702}
4703
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004704/* Insert <pkt> RX packet in its <qel> RX packets tree */
4705static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4706{
4707 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004708 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004709 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4710 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4711 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004712}
4713
4714/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004715 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4716 * byte past the end of the buffer containing this packet and <beg> the address of
4717 * the packet first byte.
4718 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4719 * Returns 1 if succeeded, 0 if not.
4720 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004721static inline int qc_try_rm_hp(struct quic_conn *qc,
4722 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004723 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004725 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004726{
4727 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004728 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004729 struct quic_enc_level *qel;
4730 /* Only for traces. */
4731 struct quic_rx_packet *qpkt_trace;
4732
4733 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004734 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004735 /* The packet number is here. This is also the start minus
4736 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4737 * protection.
4738 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004739 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004740
4741 tel = quic_packet_type_enc_level(pkt->type);
4742 qel = &qc->els[tel];
4743
4744 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004745 /* Note that the following function enables us to unprotect the packet
4746 * number and its length subsequently used to decrypt the entire
4747 * packets.
4748 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004749 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4750 qel->pktns->rx.largest_pn, pn, beg, end)) {
4751 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004752 goto err;
4753 }
4754
4755 /* The AAD includes the packet number field found at <pn>. */
4756 pkt->aad_len = pn - beg + pkt->pnl;
4757 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004758 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004759 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004760 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004761 /* If the packet number space has been discarded, this packet
4762 * will be not parsed.
4763 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004764 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004765 goto out;
4766 }
4767
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004768 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004769 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004770 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4771 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004772 }
4773
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004774 *el = qel;
4775 /* No reference counter incrementation here!!! */
4776 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4777 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4778 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4779 b_add(&qc->rx.buf, pkt->len);
4780 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004781 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004782 return 1;
4783
4784 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004785 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004786 return 0;
4787}
4788
4789/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4790 * Also set <*long_header> to 1 if this form is long, 0 if not.
4791 */
4792static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4793 unsigned char byte0, int *long_header)
4794{
4795 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4796 pkt->type =
4797 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4798 *long_header = 1;
4799 }
4800 else {
4801 pkt->type = QUIC_PACKET_TYPE_SHORT;
4802 *long_header = 0;
4803 }
4804}
4805
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004806/*
4807 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4808 */
4809static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4810{
4811 int j = 0, version;
4812
4813 do {
4814 version = quic_supported_version[j];
4815 if (version == pkt->version)
4816 return 1;
4817
4818 version = quic_supported_version[++j];
4819 } while(version);
4820
4821 return 0;
4822}
4823
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004824/*
4825 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4826 * address <addr>.
4827 * Implementation of RFC9000 6. Version Negotiation
4828 *
4829 * TODO implement a rate-limiting sending of Version Negotiation packets
4830 *
4831 * Returns 0 on success else non-zero
4832 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004833static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4834 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004835{
4836 char buf[256];
4837 int i = 0, j, version;
4838 const socklen_t addrlen = get_addr_len(addr);
4839
4840 /*
4841 * header form
4842 * long header, fixed bit to 0 for Version Negotiation
4843 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004844 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4845 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004846
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004847 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004848 /* null version for Version Negotiation */
4849 buf[i++] = '\x00';
4850 buf[i++] = '\x00';
4851 buf[i++] = '\x00';
4852 buf[i++] = '\x00';
4853
4854 /* source connection id */
4855 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004856 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004857 i += pkt->scid.len;
4858
4859 /* destination connection id */
4860 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004861 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004862 i += pkt->dcid.len;
4863
4864 /* supported version */
4865 j = 0;
4866 do {
4867 version = htonl(quic_supported_version[j]);
4868 memcpy(&buf[i], &version, sizeof(version));
4869 i += sizeof(version);
4870
4871 version = quic_supported_version[++j];
4872 } while (version);
4873
4874 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4875 return 1;
4876
4877 return 0;
4878}
4879
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004880/* Send a stateless reset packet depending on <pkt> RX packet information
4881 * from <fd> UDP socket to <dst>
4882 * Return 1 if succeeded, 0 if not.
4883 */
4884static int send_stateless_reset(int fd, struct sockaddr_storage *dstaddr,
4885 struct quic_rx_packet *rxpkt)
4886{
4887 int pktlen, rndlen;
4888 unsigned char pkt[64];
4889 const socklen_t addrlen = get_addr_len(dstaddr);
4890
4891 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
4892 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
4893 * Reset is difficult to distinguish from other packets if the recipient requires
4894 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
4895 * that all packets it sends are at least 22 bytes longer than the minimum
4896 * connection ID length that it requests the peer to include in its packets,
4897 * adding PADDING frames as necessary. This ensures that any Stateless Reset
4898 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
4899 * An endpoint that sends a Stateless Reset in response to a packet that is
4900 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
4901 * than the packet it responds to.
4902 */
4903
4904 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
4905 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
4906 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
4907 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
4908 /* Put a header of random bytes */
4909 if (RAND_bytes(pkt, rndlen) != 1)
4910 return 0;
4911
4912 /* Clear the most significant bit, and set the second one */
4913 *pkt = (*pkt & ~0x80) | 0x40;
4914 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
4915 rxpkt->dcid.data, rxpkt->dcid.len))
4916 return 0;
4917
4918 if (sendto(fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
4919 return 0;
4920
4921 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
4922 return 1;
4923}
4924
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004925/* Generate the token to be used in Retry packets. The token is written to
4926 * <buf> which is expected to be <len> bytes.
4927 *
4928 * Various parameters are expected to be encoded in the token. For now, only
4929 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4930 * as this CID must be repeated by the server in the transport parameters.
4931 *
4932 * TODO add the client address to validate the token origin.
4933 *
4934 * Returns the length of the encoded token or 0 on error.
4935 */
4936static int generate_retry_token(unsigned char *buf, unsigned char len,
4937 struct quic_rx_packet *pkt)
4938{
4939 const size_t token_len = 1 + pkt->dcid.len;
4940 unsigned char i = 0;
4941
4942 if (token_len > len)
4943 return 0;
4944
4945 buf[i++] = pkt->dcid.len;
4946 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4947 i += pkt->dcid.len;
4948
4949 return i;
4950}
4951
4952/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4953 * the Initial <pkt> packet.
4954 *
4955 * Returns 0 on success else non-zero.
4956 */
4957static int send_retry(int fd, struct sockaddr_storage *addr,
4958 struct quic_rx_packet *pkt)
4959{
4960 unsigned char buf[128];
4961 int i = 0, token_len;
4962 const socklen_t addrlen = get_addr_len(addr);
4963 struct quic_cid scid;
4964
4965 /* long header + fixed bit + packet type 0x3 */
4966 buf[i++] = 0xf0;
4967 /* version */
4968 buf[i++] = 0x00;
4969 buf[i++] = 0x00;
4970 buf[i++] = 0x00;
4971 buf[i++] = 0x01;
4972
4973 /* Use the SCID from <pkt> for Retry DCID. */
4974 buf[i++] = pkt->scid.len;
4975 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4976 i += pkt->scid.len;
4977
4978 /* Generate a new CID to be used as SCID for the Retry packet. */
4979 scid.len = QUIC_HAP_CID_LEN;
4980 if (RAND_bytes(scid.data, scid.len) != 1)
4981 return 1;
4982
4983 buf[i++] = scid.len;
4984 memcpy(&buf[i], scid.data, scid.len);
4985 i += scid.len;
4986
4987 /* token */
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004988 if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004989 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004990
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004991 i += token_len;
4992
4993 /* token integrity tag */
4994 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4995 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4996 pkt->dcid.len, buf, i)) {
4997 return 1;
4998 }
4999
5000 i += QUIC_TLS_TAG_LEN;
5001
5002 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
5003 return 1;
5004
5005 return 0;
5006}
5007
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005008/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5009 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5010 * concatenated to the <pkt> DCID field.
5011 *
5012 * Returns the instance or NULL if not found.
5013 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005014static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005015 struct listener *l,
5016 struct sockaddr_storage *saddr)
5017{
5018 struct quic_conn *qc = NULL;
5019 struct ebmb_node *node;
5020 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005021 /* set if the quic_conn is found in the second DCID tree */
5022 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005023
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005024 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5025 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5026 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5027 /* DCIDs of first packets coming from multiple clients may have
5028 * the same values. Let's distinguish them by concatenating the
5029 * socket addresses.
5030 */
5031 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005032 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005033 pkt->dcid.len + pkt->dcid.addrlen);
5034 if (node) {
5035 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5036 goto end;
5037 }
5038 }
5039
5040 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5041 * also for INITIAL/0-RTT non-first packets with the final DCID in
5042 * used.
5043 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005044 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005045 if (!node)
5046 goto end;
5047
5048 id = ebmb_entry(node, struct quic_connection_id, node);
5049 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005050 found_in_dcid = 1;
5051
5052 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005053 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5054 * If already done, this is a noop.
5055 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01005056 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005057 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005058
5059 return qc;
5060}
5061
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005062/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5063 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5064 * parameters of this session.
5065 * This is the responsibility of the caller to check the validity of all the
5066 * pointers passed as parameter to this function.
5067 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5068 * CO_ER_SSL_NO_MEM.
5069 */
5070static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5071 unsigned char *params, size_t params_len)
5072{
5073 int retry;
5074
5075 retry = 1;
5076 retry:
5077 *ssl = SSL_new(ssl_ctx);
5078 if (!*ssl) {
5079 if (!retry--)
5080 goto err;
5081
5082 pool_gc(NULL);
5083 goto retry;
5084 }
5085
5086 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5087 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
5088 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005089 SSL_free(*ssl);
5090 *ssl = NULL;
5091 if (!retry--)
5092 goto err;
5093
5094 pool_gc(NULL);
5095 goto retry;
5096 }
5097
5098 return 0;
5099
5100 err:
5101 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5102 return -1;
5103}
5104
5105/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5106 * used to process <qc> received packets. The allocated context is stored in
5107 * <qc.xprt_ctx>.
5108 *
5109 * Returns 0 on success else non-zero.
5110 */
5111int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
5112{
5113 struct bind_conf *bc = qc->li->bind_conf;
5114 struct ssl_sock_ctx *ctx = NULL;
5115
5116 ctx = pool_zalloc(pool_head_quic_conn_ctx);
5117 if (!ctx)
5118 goto err;
5119
5120 ctx->wait_event.tasklet = tasklet_new();
5121 if (!ctx->wait_event.tasklet)
5122 goto err;
5123
5124 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5125 ctx->wait_event.tasklet->context = ctx;
5126 ctx->wait_event.events = 0;
5127 ctx->subs = NULL;
5128 ctx->xprt_ctx = NULL;
5129 ctx->qc = qc;
5130
5131 /* Set tasklet tid based on the SCID selected by us for this
5132 * connection. The upper layer will also be binded on the same thread.
5133 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005134 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005135
5136 if (qc_is_listener(qc)) {
5137 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5138 qc->enc_params, qc->enc_params_len) == -1) {
5139 goto err;
5140 }
5141
5142 /* Enabling 0-RTT */
5143 if (bc->ssl_conf.early_data)
5144 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5145
5146 SSL_set_accept_state(ctx->ssl);
5147 }
5148
5149 ctx->xprt = xprt_get(XPRT_QUIC);
5150
5151 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005152 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005153
5154 return 0;
5155
5156 err:
5157 if (ctx && ctx->wait_event.tasklet)
5158 tasklet_free(ctx->wait_event.tasklet);
5159 pool_free(pool_head_quic_conn_ctx, ctx);
5160
5161 return 1;
5162}
5163
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005164/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5165 * end of this buffer past one byte and populate <pkt> RX packet structure
5166 * with the information collected from the packet.
5167 * This function sets ->len <pkt> field value to the end of the packet past one
5168 * byte to enable the caller to run this function again to continue to parse
5169 * the remaing QUIC packets carried by the datagram.
5170 * Note that this function always sets this ->len value. If a paquet could
5171 * not be correctly found, ->len value will be set to the remaining number
5172 * bytes in the datagram to entirely consume this latter.
5173 */
5174static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5175 struct quic_rx_packet *pkt, int first_pkt,
5176 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005177{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005178 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005179 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005180 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005181 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005182 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005183 size_t b_cspace;
5184 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005185
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005186 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005187 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005188 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005189 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005190 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5191 /* This ist only to please to traces and distinguish the
5192 * packet with parsed packet number from others.
5193 */
5194 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005195 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005196 goto err;
5197
5198 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005199 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005200 /* XXX TO BE DISCARDED */
5201 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5202 goto err;
5203 }
5204
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005205 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005206 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005207 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005208 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005209 uint64_t len;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005210 int drop_no_con = 0;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005211
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005212 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005213 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5214 goto err;
5215 }
5216
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005217 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5218 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
5219 drop_no_con = 1;
5220 }
5221 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5222 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005223 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
5224 drop_no_con = 1;
5225 }
5226
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005227 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5228 * they must have the same DCID.
5229 */
5230 if (!first_pkt &&
5231 (pkt->dcid.len != dgram->dcid_len ||
5232 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5233 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
5234 goto err;
5235 }
5236
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005237 /* Retry of Version Negotiation packets are only sent by servers */
5238 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
5239 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5240 goto err;
5241 }
5242
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005243 /* RFC9000 6. Version Negotiation */
5244 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005245 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005246 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005247 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
5248 goto err;
5249 }
5250
5251 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005252 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005253 }
5254
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005255 /* For Initial packets, and for servers (QUIC clients connections),
5256 * there is no Initial connection IDs storage.
5257 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005258 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005259 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005260
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005261 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5262 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005263 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5264 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005265 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005266
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01005267 /* The token may be provided in a Retry packet or NEW_TOKEN frame
5268 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005269 */
5270 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005271
5272 /* TODO Retry should be automatically activated if
5273 * suspect network usage is detected.
5274 */
5275 if (!token_len && l->bind_conf->quic_force_retry) {
5276 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005277 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005278 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
5279 goto err;
5280 }
5281
5282 goto err;
5283 }
5284 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005285 pkt->token = buf;
5286 buf += pkt->token_len;
5287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005288 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005289 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005290 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005291 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5292 goto err;
5293 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005294 }
5295
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005296 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5297 end - buf < len) {
5298 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5299 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005300 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005301
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005302 payload = buf;
5303 pkt->len = len + payload - beg;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005304 if (drop_no_con)
5305 goto drop_no_con;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005306
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005307 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005308 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005309 int ipv4;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005310 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005311 const unsigned char *salt = initial_salt_v1;
5312 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005313
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005314 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01005315 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005316 goto err;
5317 }
5318
Frédéric Lécailledc364042022-01-27 16:51:54 +01005319 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
5320 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
5321 goto err;
5322 }
5323
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005324 pkt->saddr = dgram->saddr;
5325 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02005326 qc = qc_new_conn(pkt->version, ipv4, &pkt->dcid, &pkt->scid, &pkt->saddr,
5327 pkt->token, pkt->token_len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005328 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005329 goto err;
5330
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005331 qc->enc_params_len =
5332 quic_transport_params_encode(qc->enc_params,
5333 qc->enc_params + sizeof qc->enc_params,
5334 &qc->rx.params, 1);
5335 if (!qc->enc_params_len)
5336 goto err;
5337
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005338 if (qc_conn_alloc_ssl_ctx(qc))
5339 goto err;
5340
Frédéric Lécaille789413c2022-01-31 10:16:18 +01005341 if (!quic_conn_init_timer(qc))
5342 goto err;
5343
Frédéric Lécaille530601c2022-03-10 15:11:57 +01005344 if (!quic_conn_init_idle_timer_task(qc))
5345 goto err;
5346
Frédéric Lécaille497fa782021-05-31 15:16:13 +02005347 /* NOTE: the socket address has been concatenated to the destination ID
5348 * chosen by the client for Initial packets.
5349 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005350 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
5351 salt = initial_salt_draft_29;
5352 salt_len = sizeof initial_salt_draft_29;
5353 }
5354 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005355 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005356 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02005357 goto err;
5358 }
5359
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005360 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005361 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005362 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005363
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005364 /* If the insertion failed, it means that another
5365 * thread has already allocated a QUIC connection for
5366 * the same CID. Liberate our allocated connection.
5367 */
5368 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005369 qc_to_purge = qc;
5370
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005371 qc = ebmb_entry(n, struct quic_conn, odcid_node);
5372 pkt->qc = qc;
5373 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005374
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005375 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005376 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01005377 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005378 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005379 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01005380 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005381 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005382 }
5383 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005384 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005385 }
5386 }
5387 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005388 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005389 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5390 goto err;
5391 }
5392
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005393 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005394 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005395
5396 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5397 * they must have the same DCID.
5398 */
5399 if (!first_pkt &&
5400 (pkt->dcid.len != dgram->dcid_len ||
5401 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5402 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
5403 goto err;
5404 }
5405
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005406 buf += QUIC_HAP_CID_LEN;
5407
5408 /* A short packet is the last one of a UDP datagram. */
5409 payload = buf;
5410 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005411
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005412 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005413 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005414 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005415 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005416 if (global.cluster_secret && !send_stateless_reset(l->rx.fd, &dgram->saddr, pkt))
5417 TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005418 goto err;
5419 }
5420
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005421 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005422 }
5423
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005424 if (qc->flags & QUIC_FL_CONN_CLOSING) {
5425 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
5426 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
5427 qc->nb_pkt_for_cc++;
5428 qc->nb_pkt_since_cc = 0;
5429 }
5430 /* Skip the entire datagram */
5431 pkt->len = end - beg;
5432 TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc);
5433 goto out;
5434 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005435
5436 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5437 * they must have the same DCID.
5438 *
5439 * This check must be done after the final update to pkt.len to
5440 * properly drop the packet on failure.
5441 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005442 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005443 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005444 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
5445 QUIC_EV_CONN_LPKT, qc);
5446 /* Reset the anti-amplification bit. It will be set again
5447 * when sending the next packet if reached again.
5448 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005449 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
5450 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005451 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005452 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005453
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005454 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005455
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005456 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005457 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005458 goto out;
5459 }
5460
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005461 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005462 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005463 b_cspace = b_contig_space(&qc->rx.buf);
5464 if (b_cspace < pkt->len) {
5465 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005466 if (b_cspace) {
5467 b_putchr(&qc->rx.buf, 0x00);
5468 b_cspace--;
5469 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005470 b_add(&qc->rx.buf, b_cspace);
5471 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005472 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01005473 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005474 goto err;
5475 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005476 }
5477
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005478 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005479 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02005480 goto err;
5481 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005482
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005483 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005484 if (pkt->aad_len)
5485 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005486 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02005487 /* Wake up the connection packet handler task from here only if all
5488 * the contexts have been initialized, especially the mux context
5489 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
5490 * will start it if these contexts for the connection are not already
5491 * initialized.
5492 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005493 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005494 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005495 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005496
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005497 drop_no_con:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005498 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005499
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005500 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005501
5502 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005503 /* Wakeup the I/O handler callback if the PTO timer must be armed.
5504 * This cannot be done by this thread.
5505 */
5506 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005507 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005508 if (conn_ctx && conn_ctx->wait_event.tasklet)
5509 tasklet_wakeup(conn_ctx->wait_event.tasklet);
5510 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01005511 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005512 if (!pkt->len)
5513 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005514 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005515 qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005516}
5517
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005518/* This function builds into <buf> buffer a QUIC long packet header.
5519 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005520 */
5521static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
5522 int type, size_t pn_len, struct quic_conn *conn)
5523{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005524 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005525 return 0;
5526
5527 /* #0 byte flags */
5528 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5529 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5530 /* Version */
5531 quic_write_uint32(buf, end, conn->version);
5532 *(*buf)++ = conn->dcid.len;
5533 /* Destination connection ID */
5534 if (conn->dcid.len) {
5535 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5536 *buf += conn->dcid.len;
5537 }
5538 /* Source connection ID */
5539 *(*buf)++ = conn->scid.len;
5540 if (conn->scid.len) {
5541 memcpy(*buf, conn->scid.data, conn->scid.len);
5542 *buf += conn->scid.len;
5543 }
5544
5545 return 1;
5546}
5547
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005548/* This function builds into <buf> buffer a QUIC short packet header.
5549 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005550 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005551static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5552 size_t pn_len, struct quic_conn *conn,
5553 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005554{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005555 if (end - *buf < 1 + conn->dcid.len)
5556 return 0;
5557
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005558 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005559 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5560 ((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 +01005561 /* Destination connection ID */
5562 if (conn->dcid.len) {
5563 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5564 *buf += conn->dcid.len;
5565 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005566
5567 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005568}
5569
5570/* Apply QUIC header protection to the packet with <buf> as first byte address,
5571 * <pn> as address of the Packet number field, <pnlen> being this field length
5572 * with <aead> as AEAD cipher and <key> as secret key.
5573 * Returns 1 if succeeded or 0 if failed.
5574 */
5575static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5576 const EVP_CIPHER *aead, const unsigned char *key)
5577{
5578 int i, ret, outlen;
5579 EVP_CIPHER_CTX *ctx;
5580 /* We need an IV of at least 5 bytes: one byte for bytes #0
5581 * and at most 4 bytes for the packet number
5582 */
5583 unsigned char mask[5] = {0};
5584
5585 ret = 0;
5586 ctx = EVP_CIPHER_CTX_new();
5587 if (!ctx)
5588 return 0;
5589
5590 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5591 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5592 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5593 goto out;
5594
5595 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5596 for (i = 0; i < pnlen; i++)
5597 pn[i] ^= mask[i + 1];
5598
5599 ret = 1;
5600
5601 out:
5602 EVP_CIPHER_CTX_free(ctx);
5603
5604 return ret;
5605}
5606
5607/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5608 * ACK ranges if needed to a value below <limit> in bytes.
5609 * Return 1 if succeeded, 0 if not.
5610 */
5611static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5612{
5613 size_t room, ack_delay_sz;
5614
5615 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5616 /* A frame is made of 1 byte for the frame type. */
5617 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005618 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005619 return 0;
5620
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005621 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005622}
5623
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005624/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5625 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5626 * with <room> as available room, and <*len> the packet Length field initialized
5627 * with the number of bytes already present in this buffer which must be taken
5628 * into an account for the Length packet field value. <headlen> is the number of
5629 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005630 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005631 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005632 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005633 * 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 +01005634 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005635static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005636 size_t room, size_t *len, size_t headlen,
5637 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005638 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005639{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005640 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005641 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005642
Frédéric Lécailleea604992020-12-24 13:01:37 +01005643 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005644 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005645 return 0;
5646
Frédéric Lécailleea604992020-12-24 13:01:37 +01005647 /* If we are not probing we must take into an account the congestion
5648 * control window.
5649 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005650 if (!qel->pktns->tx.pto_probe) {
5651 size_t remain = quic_path_prep_data(qc->path);
5652
5653 if (headlen > remain)
5654 return 0;
5655
5656 room = QUIC_MIN(room, remain - headlen);
5657 }
5658
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005659 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005660 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005661
5662 /* NOTE: switch/case block inside a loop, a successful status must be
5663 * returned by this function only if at least one frame could be built
5664 * in the switch/case block.
5665 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005666 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005667 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005668 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005669
Frédéric Lécailleea604992020-12-24 13:01:37 +01005670 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005671 break;
5672
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005673 switch (cf->type) {
5674 case QUIC_FT_CRYPTO:
5675 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005676 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005677 /* Compute the length of this CRYPTO frame header */
5678 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5679 /* Compute the data length of this CRyPTO frame. */
5680 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5681 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005682 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005683 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005684 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005685
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005686 /* CRYPTO frame length. */
5687 flen = hlen + quic_int_getsize(dlen) + dlen;
5688 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005689 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005690 /* Add the CRYPTO data length and its encoded length to the packet
5691 * length and the length of this length.
5692 */
5693 *len += flen;
5694 room -= flen;
5695 if (dlen == cf->crypto.len) {
5696 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005697 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005698 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005699 }
5700 else {
5701 struct quic_frame *new_cf;
5702
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005703 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005704 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005705 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005706 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005707 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005708
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005709 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005710 new_cf->type = QUIC_FT_CRYPTO;
5711 new_cf->crypto.len = dlen;
5712 new_cf->crypto.offset = cf->crypto.offset;
5713 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005714 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005715 /* Consume <dlen> bytes of the current frame. */
5716 cf->crypto.len -= dlen;
5717 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005718 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005719 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005720
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005721 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005722 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
5723 struct eb64_node *node = NULL;
5724 struct qc_stream_desc *stream_desc = NULL;
5725 struct quic_stream *strm = &cf->stream;
5726
5727 /* As this frame has been already lost, ensure the stream is always
5728 * available or the range of this frame is not consumed before
5729 * resending it.
5730 */
5731 node = eb64_lookup(&qc->streams_by_id, strm->id);
5732 if (!node) {
5733 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm);
5734 LIST_DELETE(&cf->list);
5735 pool_free(pool_head_quic_frame, cf);
5736 continue;
5737 }
5738
5739 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
5740 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
5741 TRACE_PROTO("ignored frame frame in already acked range",
5742 QUIC_EV_CONN_PRSAFRM, qc, strm);
5743 LIST_DELETE(&cf->list);
5744 pool_free(pool_head_quic_frame, cf);
5745 continue;
5746 }
5747 else if (strm->offset.key < stream_desc->ack_offset) {
5748 strm->offset.key = stream_desc->ack_offset;
5749 TRACE_PROTO("updated partially acked frame",
5750 QUIC_EV_CONN_PRSAFRM, qc, strm);
5751 }
5752 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005753 /* Note that these frames are accepted in short packets only without
5754 * "Length" packet field. Here, <*len> is used only to compute the
5755 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005756 *
5757 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005758 * excepting the variable ones. Note that +1 is for the type of this frame.
5759 */
5760 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005761 ((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 +02005762 /* Compute the data length of this STREAM frame. */
5763 avail_room = room - hlen - *len;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005764 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005765 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005766 if ((ssize_t)avail_room <= 0)
5767 continue;
5768
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005769 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5770 dlen = max_available_room(avail_room, &dlen_sz);
5771 if (dlen > cf->stream.len) {
5772 dlen = cf->stream.len;
5773 }
5774 dlen_sz = quic_int_getsize(dlen);
5775 flen = hlen + dlen_sz + dlen;
5776 }
5777 else {
5778 dlen = QUIC_MIN(avail_room, cf->stream.len);
5779 flen = hlen + dlen;
5780 }
5781 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005782 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005783 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005784 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005785 /* Add the STREAM data length and its encoded length to the packet
5786 * length and the length of this length.
5787 */
5788 *len += flen;
5789 room -= flen;
5790 if (dlen == cf->stream.len) {
5791 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005792 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005793 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005794
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005795 /* The MUX stream might be released at this
5796 * stage. This can most notably happen on
5797 * retransmission.
5798 */
5799 if (qc->mux_state == QC_MUX_READY &&
5800 !cf->stream.stream->release) {
5801 qcc_streams_sent_done(cf->stream.stream->ctx,
5802 cf->stream.len,
5803 cf->stream.offset.key);
5804 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005805 }
5806 else {
5807 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005808 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005809
5810 new_cf = pool_zalloc(pool_head_quic_frame);
5811 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005812 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005813 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005814 }
5815
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005816 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005817 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005818 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005819 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005820 new_cf->stream.id = cf->stream.id;
5821 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5822 new_cf->stream.offset = cf->stream.offset;
5823 new_cf->stream.len = dlen;
5824 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5825 /* FIN bit reset */
5826 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5827 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005828 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005829 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5830 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005831 cf_buf = b_make(b_orig(cf->stream.buf),
5832 b_size(cf->stream.buf),
5833 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005834 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005835 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005836 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005837
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005838 /* The MUX stream might be released at this
5839 * stage. This can most notably happen on
5840 * retransmission.
5841 */
5842 if (qc->mux_state == QC_MUX_READY &&
5843 !cf->stream.stream->release) {
5844 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5845 new_cf->stream.len,
5846 new_cf->stream.offset.key);
5847 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005848 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005849
5850 /* TODO the MUX is notified about the frame sending via
5851 * previous qcc_streams_sent_done call. However, the
5852 * sending can fail later, for example if the sendto
5853 * system call returns an error. As the MUX has been
5854 * notified, the transport layer is responsible to
5855 * bufferize and resent the announced data later.
5856 */
5857
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005858 break;
5859
5860 default:
5861 flen = qc_frm_len(cf);
5862 BUG_ON(!flen);
5863 if (flen > room)
5864 continue;
5865
5866 *len += flen;
5867 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005868 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005869 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005870 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005871 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005872
5873 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01005874 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005875 }
5876
Frédéric Lécailleea604992020-12-24 13:01:37 +01005877 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005878}
5879
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005880/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005881 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5882 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005883 * filling the buffer with as much frames as possible from <frms> list of
5884 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005885 * 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 +02005886 * 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 +02005887 * having returned from this function.
5888 * 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 +01005889 * number field in this packet. <pn_len> will also have the packet number
5890 * length as value.
5891 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005892 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005893 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005894static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5895 size_t dglen, struct quic_tx_packet *pkt,
5896 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005897 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005898 struct quic_enc_level *qel, struct quic_conn *qc,
5899 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005900{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005901 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005902 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005903 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5904 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005905 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005906 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005907 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005908 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005909 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005910 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005911
Frédéric Lécailleea604992020-12-24 13:01:37 +01005912 /* Length field value with CRYPTO frames if present. */
5913 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005914 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005915 /* When not probing, and no immediate close is required, reduce the size of this
5916 * buffer to respect the congestion controller window.
5917 * 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 +01005918 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005919 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005920 size_t path_room;
5921
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005922 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005923 if (end - beg > path_room)
5924 end = beg + path_room;
5925 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005926
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005927 /* Ensure there is enough room for the TLS encryption tag and a zero token
5928 * length field if any.
5929 */
5930 if (end - pos < QUIC_TLS_TAG_LEN +
5931 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5932 goto no_room;
5933
5934 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005935 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005936 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005937 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005938 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005939 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005940 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005941 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005942 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005943 goto no_room;
5944
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005945 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5946 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005947 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005948 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005949 /* Build an ACK frame if required. */
5950 ack_frm_len = 0;
Frédéric Lécaille337108e2022-04-25 10:38:27 +02005951 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02005952 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005953 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02005954 ack_frm.tx_ack.arngs = arngs;
5955 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
5956 qel->pktns->tx.ack_delay =
5957 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
5958 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
5959 }
5960 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005961 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5962 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5963 * that from here, we do not know if we will have to send a PING frame.
5964 * This will be decided after having computed the ack-eliciting frames
5965 * to be added to this packet.
5966 */
5967 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 +01005968 if (!ack_frm_len)
5969 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005970 }
5971
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005972 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005973 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005974 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005975 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005976 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005977
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005978 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005979 /* Initialize the length of the frames built below to <len>.
5980 * If any frame could be successfully built by qc_build_frms(),
5981 * we will have len_frms > len.
5982 */
5983 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005984 if (!qc_build_frms(&frm_list, frms,
5985 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005986 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005987 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02005988 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
5989 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005990 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005991 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005992
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005993 /* Length (of the remaining data). Must not fail because, the buffer size
5994 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5995 * for the encryption tag. It must be taken into an account for the length
5996 * of this packet.
5997 */
5998 if (len_frms)
5999 len = len_frms + QUIC_TLS_TAG_LEN;
6000 else
6001 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006002 /* CONNECTION_CLOSE frame */
6003 if (cc) {
6004 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006005
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006006 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006007 len += qc_frm_len(&cc_frm);
6008 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006009 add_ping_frm = 0;
6010 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006011 len_sz = quic_int_getsize(len);
6012 /* Add this packet size to <dglen> */
6013 dglen += head_len + len_sz + len;
6014 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6015 /* This is a maximum padding size */
6016 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6017 /* The length field value is of this packet is <len> + <padding_len>
6018 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006019 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006020 * sizes from <padding_len>.
6021 */
6022 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6023 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006024 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006025 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006026 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006027 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006028 add_ping_frm = 1;
6029 len += 1;
6030 }
6031 /* 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 +01006032 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006033 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6034 }
6035
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006036 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6037 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006038
6039 /* Packet number field address. */
6040 *buf_pn = pos;
6041
6042 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006043 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6044 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006045
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006046 if (ack_frm_len) {
6047 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6048 goto no_room;
6049
6050 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006051 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006052 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006053
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006054 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006055 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006056 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006057 unsigned char *spos = pos;
6058
6059 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006060 ssize_t room = end - pos;
6061 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006062 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006063 /* TODO: this should not have happened if qc_build_frms()
6064 * had correctly computed and sized the frames to be
6065 * added to this packet. Note that <cf> was added
6066 * from <frm_list> to <frms> list by qc_build_frms().
6067 */
6068 LIST_DELETE(&cf->list);
6069 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006070 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006071 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006072
6073 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006074 quic_tx_packet_refinc(pkt);
6075 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006076 }
6077 }
6078
6079 /* Build a PING frame if needed. */
6080 if (add_ping_frm) {
6081 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006082 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006083 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006084 }
6085
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006086 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006087 if (cc) {
6088 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6089 goto no_room;
6090
6091 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6092 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006093
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006094 /* Build a PADDING frame if needed. */
6095 if (padding_len) {
6096 frm.type = QUIC_FT_PADDING;
6097 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006098 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006099 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006100 }
6101
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006102 /* If this packet is ack-eliciting and we are probing let's
6103 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006104 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006105 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6106 qel->pktns->tx.pto_probe)
6107 qel->pktns->tx.pto_probe--;
6108
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006109 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006110 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006111 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006112
6113 return 1;
6114
6115 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006116 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006117 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailled8b798d2022-04-25 17:48:40 +02006118 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006119
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006120 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006121}
6122
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006123static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006124{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006125 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006126 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006127 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006128 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006129 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006130 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006131 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006132 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006133 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006134 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006135}
6136
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006137/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006138 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6139 * of prebuilt frames.
6140 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006141 * Return -2 if the packet could not be allocated or encrypted for any reason,
6142 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006143 * XXX NOTE XXX
6144 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6145 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6146 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006147 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006148static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6149 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006150 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006151 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006152 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006153{
6154 /* The pointer to the packet number field. */
6155 unsigned char *buf_pn;
6156 unsigned char *beg, *end, *payload;
6157 int64_t pn;
6158 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006159 struct quic_tls_ctx *tls_ctx;
6160 struct quic_tx_packet *pkt;
6161
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006162 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006163 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006164 pkt = pool_alloc(pool_head_quic_tx_packet);
6165 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006166 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006167 *err = -2;
6168 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006169 }
6170
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006171 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006172 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006173 pn_len = 0;
6174 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006175
6176 pn = qel->pktns->tx.next_pn + 1;
6177 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 +02006178 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006179 *err = -1;
6180 goto err;
6181 }
6182
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006183 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006184 payload = buf_pn + pn_len;
6185 payload_len = end - payload;
6186 aad_len = payload - beg;
6187
6188 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006189 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006190 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006191 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006192 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006193
6194 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006195 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006196 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
6197 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006198 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006199 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006200 goto err;
6201 }
6202
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006203 /* Consume a packet number */
6204 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006205 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01006206 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006207 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006208 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6209 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006210 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006211 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006212 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006213 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006214 pkt->in_flight_len = pkt->len;
6215 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006216 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006217 /* Always reset this flags */
6218 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006219 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6220 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6221 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6222 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006223
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006224 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006225 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006226
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006227 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006228
6229 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006230 /* TODO: what about the frames which have been built
6231 * for this packet.
6232 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006233 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006234 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006235 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006236}
6237
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006238
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006239/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6240 * event subscriber <es> is not allowed to change from a previous call as long
6241 * as at least one event is still subscribed. The <event_type> must only be a
6242 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6243 */
6244static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6245{
Willy Tarreau784b8682022-04-11 14:18:10 +02006246 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006247
6248 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6249 BUG_ON(qcc->subs && qcc->subs != es);
6250
6251 es->events |= event_type;
6252 qcc->subs = es;
6253
6254 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02006255 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006256
6257 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02006258 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006259
6260 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006261}
6262
6263/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6264 * The <es> pointer is not allowed to differ from the one passed to the
6265 * subscribe() call. It always returns zero.
6266 */
6267static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6268{
6269 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
6270}
6271
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006272/* Store in <xprt_ctx> the context attached to <conn>.
6273 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006274 */
6275static int qc_conn_init(struct connection *conn, void **xprt_ctx)
6276{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01006277 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006278
6279 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
6280
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006281 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006282 if (*xprt_ctx)
6283 goto out;
6284
Willy Tarreau784b8682022-04-11 14:18:10 +02006285 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006286
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006287 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01006288 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006289
6290 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006291}
6292
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006293/* Start the QUIC transport layer */
6294static int qc_xprt_start(struct connection *conn, void *ctx)
6295{
6296 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02006297 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006298
Willy Tarreau784b8682022-04-11 14:18:10 +02006299 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006300 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
6301 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02006302 /* prepare a CONNECTION_CLOSE frame */
6303 qc->err_code = QC_ERR_APPLICATION_ERROR;
6304 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle05d4ae62022-04-13 17:40:26 +02006305 return -1;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006306 }
6307
6308 /* mux-quic can now be considered ready. */
6309 qc->mux_state = QC_MUX_READY;
6310
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006311 tasklet_wakeup(qctx->wait_event.tasklet);
6312 return 1;
6313}
6314
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006315static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
6316{
Willy Tarreau784b8682022-04-11 14:18:10 +02006317 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006318 return NULL;
6319
Willy Tarreau784b8682022-04-11 14:18:10 +02006320 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006321}
6322
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006323/* transport-layer operations for QUIC connections. */
6324static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02006325 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006326 .subscribe = quic_conn_subscribe,
6327 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006328 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006329 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006330 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
6331 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01006332 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006333 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006334 .name = "QUIC",
6335};
6336
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006337static void __quic_conn_init(void)
6338{
6339 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
6340 xprt_register(XPRT_QUIC, &ssl_quic);
6341}
Willy Tarreau79367f92022-04-25 19:18:24 +02006342INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006344static void __quic_conn_deinit(void)
6345{
6346 BIO_meth_free(ha_quic_meth);
6347}
Willy Tarreau79367f92022-04-25 19:18:24 +02006348REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006349
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006350/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
6351 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05006352 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006353 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006354struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006355{
6356 unsigned char *pos;
6357 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006358 struct quic_dghdlr *dghdlr = ctx;
6359 struct quic_dgram *dgram;
6360 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006361
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006362 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006363 pos = dgram->buf;
6364 end = pos + dgram->len;
6365 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006366 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006367
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006368 pkt = pool_zalloc(pool_head_quic_rx_packet);
6369 if (!pkt)
6370 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006371
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006372 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006373 quic_rx_packet_refinc(pkt);
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006374 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006375 first_pkt = 0;
6376 pos += pkt->len;
6377 quic_rx_packet_refdec(pkt);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006378 } while (pos < end);
6379
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006380 /* Increasing the received bytes counter by the UDP datagram length
6381 * if this datagram could be associated to a connection.
6382 */
6383 if (dgram->qc)
6384 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01006385
6386 /* Mark this datagram as consumed */
6387 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006388 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006389
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006390 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006391
6392 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006393 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006394}
6395
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006396/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
6397 * Returns 1 if succeeded, 0 if not.
6398 */
6399static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
6400 unsigned char **dcid, size_t *dcid_len)
6401{
6402 int long_header;
6403 size_t minlen, skip;
6404
6405 if (!(*buf & QUIC_PACKET_FIXED_BIT))
6406 goto err;
6407
6408 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02006409 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
6410 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006411 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01006412 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006413 goto err;
6414
6415 buf += skip;
6416 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
6417 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
6418 goto err;
6419
6420 *dcid = buf;
6421
6422 return 1;
6423
6424 err:
6425 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
6426 return 0;
6427}
6428
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01006429/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
6430 * correct datagram handler.
6431 * Return 1 if a correct datagram could be found, 0 if not.
6432 */
6433int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
6434 struct sockaddr_storage *saddr,
6435 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006436{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006437 struct quic_dgram *dgram;
6438 unsigned char *dcid;
6439 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01006440 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006441
6442 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006443 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006444
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01006445 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006446 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006447 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006448
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01006449 cid_tid = quic_get_cid_tid(dcid);
6450
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006451 /* All the members must be initialized! */
6452 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006453 dgram->buf = buf;
6454 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006455 dgram->dcid = dcid;
6456 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006457 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006458 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006459 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01006460 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006461
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01006462 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006463
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006464 return 1;
6465
6466 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006467 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006468}
6469
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02006470/* Notify the MUX layer if alive about an imminent close of <qc>. */
6471void qc_notify_close(struct quic_conn *qc)
6472{
6473 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
6474 return;
6475
6476 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
6477
6478 /* wake up the MUX */
6479 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
6480 qc->conn->mux->wake(qc->conn);
6481}
6482
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01006483/* Function to automatically activate QUIC traces on stdout.
6484 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
6485 * Main use for now is in the docker image for QUIC interop testing.
6486 */
6487static void quic_init_stdout_traces(void)
6488{
6489#ifdef ENABLE_QUIC_STDOUT_TRACES
6490 trace_quic.sink = sink_find("stdout");
6491 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01006492 trace_quic.state = TRACE_STATE_RUNNING;
6493#endif
6494}
6495INITCALL0(STG_INIT, quic_init_stdout_traces);
6496
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006497/*
6498 * Local variables:
6499 * c-indent-level: 8
6500 * c-basic-offset: 8
6501 * End:
6502 */