blob: 3a069df93a77b7935dc1e122ca4ccb6aa585e04d [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écaillea7e7ce92020-11-23 14:14:04 +0100124 { /* end */ }
125};
126
127static const struct name_desc quic_trace_lockon_args[4] = {
128 /* arg1 */ { /* already used by the connection */ },
129 /* arg2 */ { .name="quic", .desc="QUIC transport" },
130 /* arg3 */ { },
131 /* arg4 */ { }
132};
133
134static const struct name_desc quic_trace_decoding[] = {
135#define QUIC_VERB_CLEAN 1
136 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
137 { /* end */ }
138};
139
140
141struct trace_source trace_quic = {
142 .name = IST("quic"),
143 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100144 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100145 .default_cb = quic_trace,
146 .known_events = quic_trace_events,
147 .lockon_args = quic_trace_lockon_args,
148 .decoding = quic_trace_decoding,
149 .report_events = ~0, /* report everything by default */
150};
151
152#define TRACE_SOURCE &trace_quic
153INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
154
155static BIO_METHOD *ha_quic_meth;
156
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200157DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100158DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200160 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100161DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_POOL(pool_head_quic_connection_id,
163 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100164DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100167DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100168DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100169DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200170DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100171DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100172
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200173static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +0100174 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100175 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200176 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100177static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille47756802022-03-25 09:12:16 +0100178static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100179static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100180
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100181/* Only for debug purpose */
182struct enc_debug_info {
183 unsigned char *payload;
184 size_t payload_len;
185 unsigned char *aad;
186 size_t aad_len;
187 uint64_t pn;
188};
189
190/* Initializes a enc_debug_info struct (only for debug purpose) */
191static inline void enc_debug_info_init(struct enc_debug_info *edi,
192 unsigned char *payload, size_t payload_len,
193 unsigned char *aad, size_t aad_len, uint64_t pn)
194{
195 edi->payload = payload;
196 edi->payload_len = payload_len;
197 edi->aad = aad;
198 edi->aad_len = aad_len;
199 edi->pn = pn;
200}
201
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100202/* Trace callback for QUIC.
203 * These traces always expect that arg1, if non-null, is of type connection.
204 */
205static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
206 const struct ist where, const struct ist func,
207 const void *a1, const void *a2, const void *a3, const void *a4)
208{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100209 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100210
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100211 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100212 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100213
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100214 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 if ((mask & QUIC_EV_CONN_INIT) && qc) {
216 chunk_appendf(&trace_buf, "\n odcid");
217 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100218 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100219 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100220 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100221 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222 }
223
224 if (mask & QUIC_EV_CONN_ADDDATA) {
225 const enum ssl_encryption_level_t *level = a2;
226 const size_t *len = a3;
227
228 if (level) {
229 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
230
231 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
232 }
233 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100234 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100235 }
236 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
237 /* Initial read & write secrets. */
238 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
239 const unsigned char *rx_sec = a2;
240 const unsigned char *tx_sec = a3;
241
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100242 tls_ctx = &qc->els[level].tls_ctx;
243 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100244 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
245 if (rx_sec)
246 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100247 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100248 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
249 if (tx_sec)
250 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100251 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100252 }
253 }
254 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
255 const enum ssl_encryption_level_t *level = a2;
256 const unsigned char *secret = a3;
257 const size_t *secret_len = a4;
258
259 if (level) {
260 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
261
262 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
263 if (secret && secret_len)
264 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100265 tls_ctx = &qc->els[lvl].tls_ctx;
266 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
267 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100268 }
269 }
270
271 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
272 const enum ssl_encryption_level_t *level = a2;
273 const unsigned char *secret = a3;
274 const size_t *secret_len = a4;
275
276 if (level) {
277 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
278
279 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
280 if (secret && secret_len)
281 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100282 tls_ctx = &qc->els[lvl].tls_ctx;
283 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
284 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285 }
286
287 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200289 if (mask & QUIC_EV_CONN_FRMLIST) {
290 const struct list *l = a2;
291
292 if (l) {
293 const struct quic_frame *frm;
294 list_for_each_entry(frm, l, list)
295 chunk_frm_appendf(&trace_buf, frm);
296 }
297 }
298
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100299 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 const struct quic_tx_packet *pkt = a2;
301 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100302 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100303
304 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100305 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100306 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100307 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100308 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100309 (unsigned long long)qc->path->cwnd,
310 (unsigned long long)qc->path->prep_in_flight,
311 (unsigned long long)qc->path->in_flight,
312 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100313 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100314 }
315 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200316 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100317 if (pkt->pn_node.key != (uint64_t)-1)
318 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100320 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100321 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
322 (unsigned long long)qc->rx.bytes,
323 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100324 }
325
326 if (room) {
327 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
328 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
329 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 }
331 }
332
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100333 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100334 const enum quic_handshake_state *state = a2;
335 const int *err = a3;
336
337 if (state)
338 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
339 if (err)
340 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
341 }
342
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100343 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100344 const struct quic_rx_packet *pkt = a2;
345 const unsigned long *pktlen = a3;
346 const SSL *ssl = a4;
347
348 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200349 chunk_appendf(&trace_buf, " pkt@%p", pkt);
350 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
351 chunk_appendf(&trace_buf, " kp=%d",
352 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
353 chunk_appendf(&trace_buf, " el=%c",
354 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100355 if (pkt->pnl)
356 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
357 (unsigned long long)pkt->pn);
358 if (pkt->token_len)
359 chunk_appendf(&trace_buf, " toklen=%llu",
360 (unsigned long long)pkt->token_len);
361 if (pkt->aad_len)
362 chunk_appendf(&trace_buf, " aadlen=%llu",
363 (unsigned long long)pkt->aad_len);
364 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
365 pkt->flags, (unsigned long long)pkt->len);
366 }
367 if (pktlen)
368 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
369 if (ssl) {
370 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
371 chunk_appendf(&trace_buf, " el=%c",
372 quic_enc_level_char(ssl_to_quic_enc_level(level)));
373 }
374 }
375
376 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
377 const struct quic_rx_packet *pkt = a2;
378 const struct quic_rx_crypto_frm *cf = a3;
379 const SSL *ssl = a4;
380
381 if (pkt)
382 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
383 quic_packet_type_enc_level_char(pkt->type),
384 (unsigned long long)pkt->pn);
385 if (cf)
386 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
387 (unsigned long long)cf->offset_node.key,
388 (unsigned long long)cf->len);
389 if (ssl) {
390 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200391 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100392 quic_enc_level_char(ssl_to_quic_enc_level(level)));
393 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100394
395 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100396 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 }
398
399 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
400 const struct quic_frame *frm = a2;
401
402 if (frm)
403 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
404 }
405
406 if (mask & QUIC_EV_CONN_PHPKTS) {
407 const struct quic_enc_level *qel = a2;
408
409 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100410 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100411 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100412 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200414 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200415 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100416 (unsigned long long)qc->path->cwnd,
417 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100418 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100419 (unsigned long long)pktns->tx.in_flight,
420 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100421 }
422 }
423
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100424 if (mask & QUIC_EV_CONN_ENCPKT) {
425 const struct enc_debug_info *edi = a2;
426
427 if (edi)
428 chunk_appendf(&trace_buf,
429 " payload=@%p payload_len=%llu"
430 " aad=@%p aad_len=%llu pn=%llu",
431 edi->payload, (unsigned long long)edi->payload_len,
432 edi->aad, (unsigned long long)edi->aad_len,
433 (unsigned long long)edi->pn);
434 }
435
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100436 if (mask & QUIC_EV_CONN_RMHP) {
437 const struct quic_rx_packet *pkt = a2;
438
439 if (pkt) {
440 const int *ret = a3;
441
442 chunk_appendf(&trace_buf, " pkt@%p", pkt);
443 if (ret && *ret)
444 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
445 pkt->pnl, (unsigned long long)pkt->pn);
446 }
447 }
448
449 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200450 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100451 const unsigned long *val1 = a3;
452 const unsigned long *val2 = a4;
453
454 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100455 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100456 if (val1)
457 chunk_appendf(&trace_buf, " %lu", *val1);
458 if (val2)
459 chunk_appendf(&trace_buf, "..%lu", *val2);
460 }
461
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100462 if (mask & QUIC_EV_CONN_ACKSTRM) {
463 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200464 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100465
466 if (s)
467 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200468 if (stream)
469 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100470 }
471
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100472 if (mask & QUIC_EV_CONN_RTTUPDT) {
473 const unsigned int *rtt_sample = a2;
474 const unsigned int *ack_delay = a3;
475 const struct quic_loss *ql = a4;
476
477 if (rtt_sample)
478 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
479 if (ack_delay)
480 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
481 if (ql)
482 chunk_appendf(&trace_buf,
483 " srtt=%ums rttvar=%ums min_rtt=%ums",
484 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
485 }
486 if (mask & QUIC_EV_CONN_CC) {
487 const struct quic_cc_event *ev = a2;
488 const struct quic_cc *cc = a3;
489
490 if (a2)
491 quic_cc_event_trace(&trace_buf, ev);
492 if (a3)
493 quic_cc_state_trace(&trace_buf, cc);
494 }
495
496 if (mask & QUIC_EV_CONN_PKTLOSS) {
497 const struct quic_pktns *pktns = a2;
498 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100499
500 if (pktns) {
501 chunk_appendf(&trace_buf, " pktns=%s",
502 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
503 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
504 if (pktns->tx.loss_time)
505 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100506 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100507 }
508 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
509 struct quic_tx_packet *pkt;
510
511 chunk_appendf(&trace_buf, " lost_pkts:");
512 list_for_each_entry(pkt, lost_pkts, list)
513 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
514 }
515 }
516
517 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100518 const struct quic_pktns *pktns = a2;
519 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100520 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100521
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100522 if (ifae_pkts)
523 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
524 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100525 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100526 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100527 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100528 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
529 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100530 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
531 if (pktns->tx.in_flight)
532 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100533 if (pktns->tx.loss_time)
534 chunk_appendf(&trace_buf, " loss_time=%dms",
535 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
536 }
537 if (mask & QUIC_EV_CONN_SPTO) {
538 if (pktns->tx.time_of_last_eliciting)
539 chunk_appendf(&trace_buf, " tole=%dms",
540 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
541 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100542 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100543 }
544 }
545
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200546 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100547 chunk_appendf(&trace_buf,
548 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
549 }
550 }
551
552 if (mask & QUIC_EV_CONN_SPPKTS) {
553 const struct quic_tx_packet *pkt = a2;
554
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100555 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
556 (unsigned long long)qc->path->cwnd,
557 (unsigned long long)qc->path->prep_in_flight,
558 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100559 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100560 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100561 (unsigned long)pkt->pn_node.key,
562 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
563 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100564 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100565 }
566 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100567
568 if (mask & QUIC_EV_CONN_SSLALERT) {
569 const uint8_t *alert = a2;
570 const enum ssl_encryption_level_t *level = a3;
571
572 if (alert)
573 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
574 if (level)
575 chunk_appendf(&trace_buf, " el=%c",
576 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
577 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100578
579 if (mask & QUIC_EV_CONN_BCFRMS) {
580 const size_t *sz1 = a2;
581 const size_t *sz2 = a3;
582 const size_t *sz3 = a4;
583
584 if (sz1)
585 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
586 if (sz2)
587 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
588 if (sz3)
589 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
590 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100591
592 if (mask & QUIC_EV_CONN_PSTRM) {
593 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100594
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100595 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100596 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100597 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 }
599 if (mask & QUIC_EV_CONN_LPKT) {
600 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200601 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100602
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100603 if (pkt) {
604 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
605 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
606 if (pkt->pn_node.key != (uint64_t)-1)
607 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
608 }
609
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200610 if (len)
611 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 }
613
614}
615
616/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100617static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100618{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200619 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100621 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622 return 1;
623
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200624 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
625 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200626 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
627 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200628 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100629 return 1;
630
631 return 0;
632}
633
634/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
635 * both loss detection and PTO and schedule the task assiated to this timer if needed.
636 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100637static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100638{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100639 struct quic_pktns *pktns;
640 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200641 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100642
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100643 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100644 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100645
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100646 pktns = quic_loss_pktns(qc);
647 if (tick_isset(pktns->tx.loss_time)) {
648 qc->timer = pktns->tx.loss_time;
649 goto out;
650 }
651
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100652 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100653 * cancelled for a server which reached the anti-amplification limit.
654 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100655 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200656 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100657 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100658 qc->timer = TICK_ETERNITY;
659 goto out;
660 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100661
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100662 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100663 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100664 /* Timer cancellation. */
665 qc->timer = TICK_ETERNITY;
666 goto out;
667 }
668
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200669 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200670 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100671 if (tick_isset(pto))
672 qc->timer = pto;
673 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100674 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100675 if (tick_is_expired(qc->timer, now_ms)) {
676 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100677 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100678 }
679 else {
680 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100681 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100682 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100683 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100684 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100685}
686
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100687/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
688 * connection.
689 * Return 1 if succeeded, 0 if not.
690 */
691static int quic_tls_key_update(struct quic_conn *qc)
692{
693 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
694 struct quic_tls_secrets *rx, *tx;
695 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
696 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
697
698 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
699 rx = &tls_ctx->rx;
700 tx = &tls_ctx->tx;
701 nxt_rx = &qc->ku.nxt_rx;
702 nxt_tx = &qc->ku.nxt_tx;
703
704 /* Prepare new RX secrets */
705 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
706 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100707 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100708 return 0;
709 }
710
711 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
712 nxt_rx->key, nxt_rx->keylen,
713 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
714 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100715 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100716 return 0;
717 }
718
719 /* Prepare new TX secrets */
720 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
721 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100722 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100723 return 0;
724 }
725
726 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
727 nxt_tx->key, nxt_tx->keylen,
728 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
729 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100730 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100731 return 0;
732 }
733
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200734 if (nxt_rx->ctx) {
735 EVP_CIPHER_CTX_free(nxt_rx->ctx);
736 nxt_rx->ctx = NULL;
737 }
738
739 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
740 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
741 return 0;
742 }
743
744 if (nxt_tx->ctx) {
745 EVP_CIPHER_CTX_free(nxt_tx->ctx);
746 nxt_tx->ctx = NULL;
747 }
748
749 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
750 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
751 return 0;
752 }
753
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100754 return 1;
755}
756
757/* Rotate the Key Update information for <qc> QUIC connection.
758 * Must be used after having updated them.
759 * Always succeeds.
760 */
761static void quic_tls_rotate_keys(struct quic_conn *qc)
762{
763 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
764 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200765 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100766
767 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200768 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100769 curr_secret = tls_ctx->rx.secret;
770 curr_iv = tls_ctx->rx.iv;
771 curr_key = tls_ctx->rx.key;
772
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200773 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100774 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
775 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
776 tls_ctx->rx.key = qc->ku.nxt_rx.key;
777
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200778 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100779 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
780 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
781 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
782
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200783 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100784 qc->ku.prv_rx.secret = curr_secret;
785 qc->ku.prv_rx.iv = curr_iv;
786 qc->ku.prv_rx.key = curr_key;
787 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
788
789 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200790 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100791 curr_secret = tls_ctx->tx.secret;
792 curr_iv = tls_ctx->tx.iv;
793 curr_key = tls_ctx->tx.key;
794
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200795 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100796 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
797 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
798 tls_ctx->tx.key = qc->ku.nxt_tx.key;
799
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200800 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100801 qc->ku.nxt_tx.secret = curr_secret;
802 qc->ku.nxt_tx.iv = curr_iv;
803 qc->ku.nxt_tx.key = curr_key;
804}
805
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100806#ifndef OPENSSL_IS_BORINGSSL
807int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
808 const uint8_t *read_secret,
809 const uint8_t *write_secret, size_t secret_len)
810{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100811 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
812 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 +0100813 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100814 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100815
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100816 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100817 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200818 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100819 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100820 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100821 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100822
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100823 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100824 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100825 goto err;
826 }
827
828 rx = &tls_ctx->rx;
829 tx = &tls_ctx->tx;
830
831 rx->aead = tx->aead = tls_aead(cipher);
832 rx->md = tx->md = tls_md(cipher);
833 rx->hp = tx->hp = tls_hp(cipher);
834
835 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
836 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100837 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100838 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100839 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100840 }
841
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200842 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
843 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
844 goto err;
845 }
846
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100847 /* Enqueue this connection asap if we could derive O-RTT secrets as
848 * listener. Note that a listener derives only RX secrets for this
849 * level.
850 */
851 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
852 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100853
854 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100855 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100856
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100857 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
858 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100859 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100860 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100861 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 }
863
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200864 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
865 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
866 goto err;
867 }
868
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100869 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100870 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
871 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
872 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
873
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200874 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100875 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
876 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100877 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100878 goto err;
879 }
880
881 memcpy(rx->secret, read_secret, secret_len);
882 rx->secretlen = secret_len;
883 memcpy(tx->secret, write_secret, secret_len);
884 tx->secretlen = secret_len;
885 /* Initialize all the secret keys lengths */
886 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
887 /* Prepare the next key update */
888 if (!quic_tls_key_update(qc))
889 goto err;
890 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100891
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100892 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100893 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100894 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100895 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100896 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100897
898 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100899 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100900 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100901}
902#else
903/* ->set_read_secret callback to derive the RX secrets at <level> encryption
904 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500905 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100906 */
907int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
908 const SSL_CIPHER *cipher,
909 const uint8_t *secret, size_t secret_len)
910{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100911 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100912 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100913 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100914
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100915 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200916 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100917 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100918 goto out;
919 }
920
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100921 tls_ctx->rx.aead = tls_aead(cipher);
922 tls_ctx->rx.md = tls_md(cipher);
923 tls_ctx->rx.hp = tls_hp(cipher);
924
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100925 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
926 goto err;
927
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100928 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 +0100929 tls_ctx->rx.key, tls_ctx->rx.keylen,
930 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100931 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
932 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100933 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100934 goto err;
935 }
936
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100937 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100938 const unsigned char *buf;
939 size_t buflen;
940
941 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
942 if (!buflen)
943 goto err;
944
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100945 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100946 goto err;
947 }
948
949 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100950 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100951 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100952
953 return 1;
954
955 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100956 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100957 return 0;
958}
959
960/* ->set_write_secret callback to derive the TX secrets at <level>
961 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500962 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100963 */
964int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
965 const SSL_CIPHER *cipher,
966 const uint8_t *secret, size_t secret_len)
967{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100968 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
969 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 +0100970
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100971 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200972 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100973 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100974 goto out;
975 }
976
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100977 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
978 goto err;
979
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100980 tls_ctx->tx.aead = tls_aead(cipher);
981 tls_ctx->tx.md = tls_md(cipher);
982 tls_ctx->tx.hp = tls_hp(cipher);
983
984 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 +0100985 tls_ctx->tx.key, tls_ctx->tx.keylen,
986 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100987 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
988 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100989 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100990 goto err;
991 }
992
993 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100994 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100995 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100996 return 1;
997
998 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100999 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001000 return 0;
1001}
1002#endif
1003
1004/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1005 * with <len> as size in CRYPTO buffers dedicated to store the information about
1006 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1007 * It fails only if it could not managed to allocate enough CRYPTO buffers to
1008 * store all the data.
1009 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1010 */
1011static int quic_crypto_data_cpy(struct quic_enc_level *qel,
1012 const unsigned char *data, size_t len)
1013{
1014 struct quic_crypto_buf **qcb;
1015 /* The remaining byte to store in CRYPTO buffers. */
1016 size_t cf_offset, cf_len, *nb_buf;
1017 unsigned char *pos;
1018
1019 nb_buf = &qel->tx.crypto.nb_buf;
1020 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1021 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1022 cf_len = len;
1023
1024 while (len) {
1025 size_t to_copy, room;
1026
1027 pos = (*qcb)->data + (*qcb)->sz;
1028 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1029 to_copy = len > room ? room : len;
1030 if (to_copy) {
1031 memcpy(pos, data, to_copy);
1032 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1033 qel->tx.crypto.sz += to_copy;
1034 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001035 len -= to_copy;
1036 data += to_copy;
1037 }
1038 else {
1039 struct quic_crypto_buf **tmp;
1040
1041 tmp = realloc(qel->tx.crypto.bufs,
1042 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1043 if (tmp) {
1044 qel->tx.crypto.bufs = tmp;
1045 qcb = &qel->tx.crypto.bufs[*nb_buf];
1046 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1047 if (!*qcb)
1048 return 0;
1049
1050 (*qcb)->sz = 0;
1051 ++*nb_buf;
1052 }
1053 else {
1054 break;
1055 }
1056 }
1057 }
1058
1059 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1060 * have been buffered.
1061 */
1062 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001063 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001064 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001065
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001066 /* There is at most one CRYPTO frame in this packet number
1067 * space. Let's look for it.
1068 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001069 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001070 if (frm->type != QUIC_FT_CRYPTO)
1071 continue;
1072
1073 /* Found */
1074 found = frm;
1075 break;
1076 }
1077
1078 if (found) {
1079 found->crypto.len += cf_len;
1080 }
1081 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001082 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001083 if (!frm)
1084 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001085
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001086 LIST_INIT(&frm->reflist);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001087 frm->type = QUIC_FT_CRYPTO;
1088 frm->crypto.offset = cf_offset;
1089 frm->crypto.len = cf_len;
1090 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001091 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001092 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093 }
1094
1095 return len == 0;
1096}
1097
1098
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001099/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1100void quic_set_tls_alert(struct quic_conn *qc, int alert)
1101{
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001102 qc->err_code = QC_ERR_CRYPTO_ERROR | alert;
1103 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001104 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001105}
1106
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001107/* Set the application for <qc> QUIC connection.
1108 * Return 1 if succeeded, 0 if not.
1109 */
1110int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1111{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001112 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1113 qc->app_ops = &h3_ops;
1114 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1115 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001116 else
1117 return 0;
1118
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001119 return 1;
1120}
1121
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001122/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1123 * wants to provide the QUIC layer with CRYPTO data.
1124 * Returns 1 if succeeded, 0 if not.
1125 */
1126int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1127 const uint8_t *data, size_t len)
1128{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001129 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001130 enum quic_tls_enc_level tel;
1131 struct quic_enc_level *qel;
1132
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001133 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1134 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001135 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001136 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001137 goto out;
1138 }
1139
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001140 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001141 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001142 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143 goto err;
1144 }
1145
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001146 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001147 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001148 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001149 goto err;
1150 }
1151
1152 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001153 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001154
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001155 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001156 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001157 return 1;
1158
1159 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001160 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001161 return 0;
1162}
1163
1164int ha_quic_flush_flight(SSL *ssl)
1165{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001166 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001167
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001168 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1169 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001170
1171 return 1;
1172}
1173
1174int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1175{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001176 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001177
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001178 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1179 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001180 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001181 return 1;
1182}
1183
1184/* QUIC TLS methods */
1185static SSL_QUIC_METHOD ha_quic_method = {
1186#ifdef OPENSSL_IS_BORINGSSL
1187 .set_read_secret = ha_set_rsec,
1188 .set_write_secret = ha_set_wsec,
1189#else
1190 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1191#endif
1192 .add_handshake_data = ha_quic_add_handshake_data,
1193 .flush_flight = ha_quic_flush_flight,
1194 .send_alert = ha_quic_send_alert,
1195};
1196
1197/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1198 * Returns an error count.
1199 */
1200int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1201{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1203 int cfgerr = 0;
1204
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001205 long options =
1206 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1207 SSL_OP_SINGLE_ECDH_USE |
1208 SSL_OP_CIPHER_SERVER_PREFERENCE;
1209 SSL_CTX *ctx;
1210
1211 ctx = SSL_CTX_new(TLS_server_method());
1212 bind_conf->initial_ctx = ctx;
1213
1214 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001215 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1216 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1217 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001218
1219#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1220#ifdef OPENSSL_IS_BORINGSSL
1221 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1222 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1223#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1224 if (bind_conf->ssl_conf.early_data) {
1225 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001226 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001227 }
1228 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1229 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1230#else
1231 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1232#endif
1233 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1234#endif
1235 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1236
1237 return cfgerr;
1238}
1239
1240/* Decode an expected packet number from <truncated_on> its truncated value,
1241 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1242 * the number of bits used to encode this packet number (its length in bytes * 8).
1243 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1244 */
1245static uint64_t decode_packet_number(uint64_t largest_pn,
1246 uint32_t truncated_pn, unsigned int pn_nbits)
1247{
1248 uint64_t expected_pn = largest_pn + 1;
1249 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1250 uint64_t pn_hwin = pn_win / 2;
1251 uint64_t pn_mask = pn_win - 1;
1252 uint64_t candidate_pn;
1253
1254
1255 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1256 /* Note that <pn_win> > <pn_hwin>. */
1257 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1258 candidate_pn + pn_hwin <= expected_pn)
1259 return candidate_pn + pn_win;
1260
1261 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1262 return candidate_pn - pn_win;
1263
1264 return candidate_pn;
1265}
1266
1267/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1268 * cryptographic context.
1269 * <largest_pn> is the largest received packet number and <pn> the address of
1270 * the packet number field for this packet with <byte0> address of its first byte.
1271 * <end> points to one byte past the end of this packet.
1272 * Returns 1 if succeeded, 0 if not.
1273 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001274static int qc_do_rm_hp(struct quic_conn *qc,
1275 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001276 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001277 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001278{
1279 int ret, outlen, i, pnlen;
1280 uint64_t packet_number;
1281 uint32_t truncated_pn = 0;
1282 unsigned char mask[5] = {0};
1283 unsigned char *sample;
1284 EVP_CIPHER_CTX *cctx;
1285 unsigned char *hp_key;
1286
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001287 /* Check there is enough data in this packet. */
1288 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001289 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001290 return 0;
1291 }
1292
1293 cctx = EVP_CIPHER_CTX_new();
1294 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001295 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001296 return 0;
1297 }
1298
1299 ret = 0;
1300 sample = pn + QUIC_PACKET_PN_MAXLEN;
1301
1302 hp_key = tls_ctx->rx.hp_key;
1303 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1304 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1305 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001306 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 goto out;
1308 }
1309
1310 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1311 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1312 for (i = 0; i < pnlen; i++) {
1313 pn[i] ^= mask[i + 1];
1314 truncated_pn = (truncated_pn << 8) | pn[i];
1315 }
1316
1317 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1318 /* Store remaining information for this unprotected header */
1319 pkt->pn = packet_number;
1320 pkt->pnl = pnlen;
1321
1322 ret = 1;
1323
1324 out:
1325 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001326
1327 return ret;
1328}
1329
1330/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1331 * address, with <payload_len> as payload length, <aad> as address of
1332 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1333 * context.
1334 * Returns 1 if succeeded, 0 if not.
1335 */
1336static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1337 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001338 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001339{
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001340 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001341 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001342 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001343 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001344
1345 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001346 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001347 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001348 }
1349
1350 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001351 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001352 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001353 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001354 }
1355
1356 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001357
1358 err:
1359 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001360 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001361 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001362}
1363
1364/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1365 * Returns 1 if succeeded, 0 if not.
1366 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001367static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001368{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001369 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001370 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001371 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001372 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001373 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001374 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1375 unsigned char *rx_key = tls_ctx->rx.key;
1376
1377 kp_changed = 0;
1378 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1379 /* The two tested bits are not at the same position,
1380 * this is why they are first both inversed.
1381 */
1382 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1383 if (pkt->pn < tls_ctx->rx.pn) {
1384 /* The lowest packet number of a previous key phase
1385 * cannot be null if it really stores previous key phase
1386 * secrets.
1387 */
1388 if (!pkt->qc->ku.prv_rx.pn)
1389 return 0;
1390
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001391 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1392 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001393 rx_key = pkt->qc->ku.prv_rx.key;
1394 }
1395 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1396 /* Next key phase */
1397 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001398 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1399 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001400 rx_key = pkt->qc->ku.nxt_rx.key;
1401 }
1402 }
1403 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001404
1405 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1406 return 0;
1407
1408 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1409 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001410 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001411 if (!ret)
1412 return 0;
1413
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001414 /* Update the keys only if the packet decryption succeeded. */
1415 if (kp_changed) {
1416 quic_tls_rotate_keys(pkt->qc);
1417 /* Toggle the Key Phase bit */
1418 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1419 /* Store the lowest packet number received for the current key phase */
1420 tls_ctx->rx.pn = pkt->pn;
1421 /* Prepare the next key update */
1422 if (!quic_tls_key_update(pkt->qc))
1423 return 0;
1424 }
1425
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001426 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001427 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001428
1429 return 1;
1430}
1431
Frédéric Lécaille96367152022-04-25 09:40:19 +02001432
1433/* Remove references to <frm> frame */
1434static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1435{
1436 uint64_t pn;
1437 struct quic_frame *f, *tmp;
1438
1439 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1440 pn = f->pkt->pn_node.key;
1441 f->origin = NULL;
1442 LIST_DELETE(&f->ref);
1443 TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1444 }
1445}
1446
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001447/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001448void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001449{
1450 uint64_t pn;
1451 struct quic_frame *origin, *f, *tmp;
1452
1453 /* Identify this frame: a frame copy or one of its copies */
1454 origin = frm->origin ? frm->origin : frm;
1455 /* Ensure the source of the copies is flagged as acked, <frm> being
1456 * possibly a copy of <origin>
1457 */
1458 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1459 /* Mark all the copy of <origin> as acknowledged. We must
1460 * not release the packets (releasing the frames) at this time as
1461 * they are possibly also to be acknowledged alongside the
1462 * the current one.
1463 */
1464 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1465 pn = f->pkt->pn_node.key;
1466 TRACE_PROTO("mark frame as acked from packet",
1467 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1468 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1469 f->origin = NULL;
1470 LIST_DELETE(&f->ref);
1471 }
1472 LIST_DELETE(&frm->list);
1473 pn = frm->pkt->pn_node.key;
1474 quic_tx_packet_refdec(frm->pkt);
1475 TRACE_PROTO("freeing frame from packet",
1476 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1477 pool_free(pool_head_quic_frame, frm);
1478}
1479
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001480/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001481 *
1482 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001483 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001484static int quic_stream_try_to_consume(struct quic_conn *qc,
1485 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001486{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001487 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001488 struct eb64_node *frm_node;
1489
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001490 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001491 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001492 while (frm_node) {
1493 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001494 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001495 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001496
1497 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001498 offset = strm->offset.key;
1499 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001500
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001501 if (offset > stream->ack_offset)
1502 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001503
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001504 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001505 /* cf. next comment : frame may be freed at this stage. */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001506 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001507 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001508 ret = 1;
1509 }
1510
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001511 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1512 * has been freed. with the stream frames tree. Nothing to do
1513 * anymore in here.
1514 */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001515 if (!stream)
1516 return 1;
1517
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001518 frm_node = eb64_next(frm_node);
1519 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001520
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001521 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001522 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001523 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001524
1525 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001526}
1527
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001528/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001529static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1530 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001531{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001532 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001533
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001534 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001535 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001536 switch (frm->type) {
1537 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1538 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001539 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001540 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001541 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001542 const size_t offset = strm_frm->offset.key;
1543 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001544
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001545 /* do not use strm_frm->stream as the qc_stream_desc instance
1546 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001547 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001548 *
1549 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001550 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001551 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001552 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1553 if (!node) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001554 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001555 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001556 /* early return */
1557 return;
1558 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001559 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001560
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001561 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001562 if (offset <= stream->ack_offset) {
1563 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001564 stream_acked = 1;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001565 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1566 qc, strm_frm, stream);
1567 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001568
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001569 if (!stream) {
1570 /* no need to continue if stream freed. */
1571 TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001572 qc_release_frm(qc, frm);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001573 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001574 }
1575
Frédéric Lécailleda342552022-04-25 10:28:49 +02001576 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1577 qc, strm_frm, stream);
1578 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001579 }
1580 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001581 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001582 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001583
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001584 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001585 }
1586 break;
1587 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001588 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001589 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001590
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001591 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001592 struct qcc *qcc = qc->qcc;
1593
1594 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1595 tasklet_wakeup(qcc->subs->tasklet);
1596 qcc->subs->events &= ~SUB_RETRY_SEND;
1597 if (!qcc->subs->events)
1598 qcc->subs = NULL;
1599 }
1600 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601}
1602
1603/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1604 * deallocating them, and their TX frames.
1605 * Returns the last node reached to be used for the next range.
1606 * May be NULL if <largest> node could not be found.
1607 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001608static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1609 struct eb_root *pkts,
1610 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001611 struct list *newly_acked_pkts,
1612 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001613 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001614{
1615 struct eb64_node *node;
1616 struct quic_tx_packet *pkt;
1617
1618 if (largest_node)
1619 node = largest_node;
1620 else {
1621 node = eb64_lookup(pkts, largest);
1622 while (!node && largest > smallest) {
1623 node = eb64_lookup(pkts, --largest);
1624 }
1625 }
1626
1627 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001628 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629
1630 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1631 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001632 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001633 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001635 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001636 node = eb64_prev(node);
1637 eb64_delete(&pkt->pn_node);
1638 }
1639
1640 return node;
1641}
1642
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001643/* Remove all frames from <pkt_frm_list> and reinsert them in the
1644 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001645 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001646static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001647 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001648 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001649{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001650 struct quic_frame *frm, *frmbak;
1651 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001652 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001654 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001655 /* Only for debug */
1656 uint64_t pn;
1657
1658 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001659 LIST_DELETE(&frm->list);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001660 pn = frm->pkt->pn_node.key;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001661 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001662 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001663 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001664 /* Remove any reference to this frame */
1665 qc_frm_unref(qc, frm);
1666 switch (frm->type) {
1667 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1668 {
1669 struct quic_stream *strm_frm = &frm->stream;
1670 struct eb64_node *node = NULL;
1671 struct qc_stream_desc *stream_desc;
1672
1673 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1674 if (!node) {
1675 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm);
1676 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1677 qc, frm, &pn);
1678 pool_free(pool_head_quic_frame, frm);
1679 continue;
1680 }
1681
1682 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1683 /* Do not resend this frame if in the "already acked range" */
1684 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1685 TRACE_PROTO("ignored frame in already acked range",
1686 QUIC_EV_CONN_PRSAFRM, qc, frm);
1687 continue;
1688 }
1689 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1690 strm_frm->offset.key = stream_desc->ack_offset;
1691 TRACE_PROTO("updated partially acked frame",
1692 QUIC_EV_CONN_PRSAFRM, qc, frm);
1693 }
1694
1695 break;
1696 }
1697
1698 default:
1699 break;
1700 }
1701
1702 /* Do not resend probing packet with old data */
1703 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1704 TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1705 qc, frm, &pn);
1706 if (frm->origin)
1707 LIST_DELETE(&frm->ref);
1708 pool_free(pool_head_quic_frame, frm);
1709 continue;
1710 }
1711
1712 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1713 TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1714 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1715 qc, frm, &pn);
1716 pool_free(pool_head_quic_frame, frm);
1717 }
1718 else {
1719 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001720 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1721 /* Mark this STREAM frame as lost. A look up their stream descriptor
1722 * will be performed to check the stream is not consumed or released.
1723 */
1724 fprintf(stderr, "LOST STREAM FRAME\n");
1725 frm->flags = QUIC_FL_TX_FRAME_LOST;
1726 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001727 LIST_APPEND(&tmp, &frm->list);
1728 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001729 }
1730
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001731 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001732}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001733
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001734/* Free <pkt> TX packet and its attached frames.
1735 * This is the responsability of the caller to remove this packet of
1736 * any data structure it was possibly attached to.
1737 */
1738static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1739{
1740 struct quic_frame *frm, *frmbak;
1741
1742 if (!pkt)
1743 return;
1744
1745 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1746 LIST_DELETE(&frm->list);
1747 pool_free(pool_head_quic_frame, frm);
1748 }
1749 pool_free(pool_head_quic_tx_packet, pkt);
1750}
1751
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001752/* Free the TX packets of <pkts> list */
1753static inline void free_quic_tx_pkts(struct list *pkts)
1754{
1755 struct quic_tx_packet *pkt, *tmp;
1756
1757 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001758 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001759 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001760 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001761 }
1762}
1763
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001764/* Remove already sent ranges of acknowledged packet numbers from
1765 * <pktns> packet number space tree below <largest_acked_pn> possibly
1766 * updating the range which contains <largest_acked_pn>.
1767 * Never fails.
1768 */
1769static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1770 int64_t largest_acked_pn)
1771{
1772 struct eb64_node *ar, *next_ar;
1773 struct quic_arngs *arngs = &pktns->rx.arngs;
1774
1775 ar = eb64_first(&arngs->root);
1776 while (ar) {
1777 struct quic_arng_node *ar_node;
1778
1779 next_ar = eb64_next(ar);
1780 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
1781 if ((int64_t)ar_node->first.key > largest_acked_pn)
1782 break;
1783
1784 if (largest_acked_pn < ar_node->last) {
1785 eb64_delete(ar);
1786 ar_node->first.key = largest_acked_pn + 1;
1787 eb64_insert(&arngs->root, ar);
1788 break;
1789 }
1790
1791 eb64_delete(ar);
1792 pool_free(pool_head_quic_arng, ar_node);
1793 arngs->sz--;
1794 ar = next_ar;
1795 }
1796}
1797
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001798/* Send a packet ack event nofication for each newly acked packet of
1799 * <newly_acked_pkts> list and free them.
1800 * Always succeeds.
1801 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001802static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001803 struct list *newly_acked_pkts)
1804{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001805 struct quic_tx_packet *pkt, *tmp;
1806 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1807
1808 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1809 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001810 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001811 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001812 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001813 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001814 /* If this packet contained an ACK frame, proceed to the
1815 * acknowledging of range of acks from the largest acknowledged
1816 * packet number which was sent in an ACK frame by this packet.
1817 */
1818 if (pkt->largest_acked_pn != -1)
1819 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001820 ev.ack.acked = pkt->in_flight_len;
1821 ev.ack.time_sent = pkt->time_sent;
1822 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001823 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001824 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001825 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 }
1827
1828}
1829
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001830/* Release all the frames attached to <pktns> packet number space */
1831static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1832{
1833 struct quic_frame *frm, *frmbak;
1834
1835 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1836 LIST_DELETE(&frm->list);
1837 pool_free(pool_head_quic_frame, frm);
1838 }
1839}
1840
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001841/* Handle <pkts> list of lost packets detected at <now_us> handling
1842 * their TX frames.
1843 * Send a packet loss event to the congestion controller if
1844 * in flight packet have been lost.
1845 * Also frees the packet in <pkts> list.
1846 * Never fails.
1847 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001848static inline void qc_release_lost_pkts(struct quic_conn *qc,
1849 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001850 struct list *pkts,
1851 uint64_t now_us)
1852{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001853 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001854 uint64_t lost_bytes;
1855
1856 lost_bytes = 0;
1857 oldest_lost = newest_lost = NULL;
1858 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001859 struct list tmp = LIST_HEAD_INIT(tmp);
1860
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861 lost_bytes += pkt->in_flight_len;
1862 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001863 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001864 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001866 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001867 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001868 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001869 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001870 if (!oldest_lost) {
1871 oldest_lost = newest_lost = pkt;
1872 }
1873 else {
1874 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001875 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001876 newest_lost = pkt;
1877 }
1878 }
1879
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001880 if (newest_lost) {
1881 /* Sent a congestion event to the controller */
1882 struct quic_cc_event ev = {
1883 .type = QUIC_CC_EVT_LOSS,
1884 .loss.time_sent = newest_lost->time_sent,
1885 };
1886
1887 quic_cc_event(&qc->path->cc, &ev);
1888 }
1889
1890 /* If an RTT have been already sampled, <rtt_min> has been set.
1891 * We must check if we are experiencing a persistent congestion.
1892 * If this is the case, the congestion controller must re-enter
1893 * slow start state.
1894 */
1895 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1896 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1897
1898 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1899 now_ms, qc->max_ack_delay))
1900 qc->path->cc.algo->slow_start(&qc->path->cc);
1901 }
1902
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001903 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001904 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001905 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001906 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001907 }
1908}
1909
1910/* Look for packet loss from sent packets for <qel> encryption level of a
1911 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1912 * their tree if deemed as lost or set the <loss_time> value the packet number
1913 * space if any not deemed lost.
1914 * Should be called after having received an ACK frame with newly acknowledged
1915 * packets or when the the loss detection timer has expired.
1916 * Always succeeds.
1917 */
1918static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1919 struct quic_conn *qc,
1920 struct list *lost_pkts)
1921{
1922 struct eb_root *pkts;
1923 struct eb64_node *node;
1924 struct quic_loss *ql;
1925 unsigned int loss_delay;
1926
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001927 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001928 pkts = &pktns->tx.pkts;
1929 pktns->tx.loss_time = TICK_ETERNITY;
1930 if (eb_is_empty(pkts))
1931 goto out;
1932
1933 ql = &qc->path->loss;
1934 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001935 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1936
1937 node = eb64_first(pkts);
1938 while (node) {
1939 struct quic_tx_packet *pkt;
1940 int64_t largest_acked_pn;
1941 unsigned int loss_time_limit, time_sent;
1942
1943 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001944 largest_acked_pn = pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001945 node = eb64_next(node);
1946 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1947 break;
1948
1949 time_sent = pkt->time_sent;
1950 loss_time_limit = tick_add(time_sent, loss_delay);
1951 if (tick_is_le(time_sent, now_ms) ||
1952 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1953 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001954 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001955 }
1956 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001957 if (tick_isset(pktns->tx.loss_time))
1958 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1959 else
1960 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001961 }
1962 }
1963
1964 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001965 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001966}
1967
1968/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1969 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001970 * 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 +01001971 * acked ack-eliciting packet.
1972 * Return 1, if succeeded, 0 if not.
1973 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001974static inline int qc_parse_ack_frm(struct quic_conn *qc,
1975 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001976 struct quic_enc_level *qel,
1977 unsigned int *rtt_sample,
1978 const unsigned char **pos, const unsigned char *end)
1979{
1980 struct quic_ack *ack = &frm->ack;
1981 uint64_t smallest, largest;
1982 struct eb_root *pkts;
1983 struct eb64_node *largest_node;
1984 unsigned int time_sent, pkt_flags;
1985 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1986 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1987
1988 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1989 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001990 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001991 goto err;
1992 }
1993
1994 if (ack->first_ack_range > ack->largest_ack) {
1995 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001996 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001997 goto err;
1998 }
1999
2000 largest = ack->largest_ack;
2001 smallest = largest - ack->first_ack_range;
2002 pkts = &qel->pktns->tx.pkts;
2003 pkt_flags = 0;
2004 largest_node = NULL;
2005 time_sent = 0;
2006
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002007 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002008 largest_node = eb64_lookup(pkts, largest);
2009 if (!largest_node) {
2010 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002011 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01002012 }
2013 else {
2014 time_sent = eb64_entry(&largest_node->node,
2015 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002016 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002017 }
2018
2019 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002020 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002021 do {
2022 uint64_t gap, ack_range;
2023
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002024 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
2025 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002026 if (!ack->ack_range_num--)
2027 break;
2028
2029 if (!quic_dec_int(&gap, pos, end))
2030 goto err;
2031
2032 if (smallest < gap + 2) {
2033 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002034 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002035 goto err;
2036 }
2037
2038 largest = smallest - gap - 2;
2039 if (!quic_dec_int(&ack_range, pos, end))
2040 goto err;
2041
2042 if (largest < ack_range) {
2043 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002044 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002045 goto err;
2046 }
2047
2048 /* Do not use this node anymore. */
2049 largest_node = NULL;
2050 /* Next range */
2051 smallest = largest - ack_range;
2052
2053 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002054 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002055 } while (1);
2056
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002057 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2058 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002059 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002060 }
2061
2062 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
2063 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002064 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002065 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002066 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002067 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002068 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
2069 if (quic_peer_validated_addr(qc))
2070 qc->path->loss.pto_count = 0;
2071 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002072 }
2073
2074
2075 return 1;
2076
2077 err:
2078 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002079 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002080 return 0;
2081}
2082
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002083/* This function gives the detail of the SSL error. It is used only
2084 * if the debug mode and the verbose mode are activated. It dump all
2085 * the SSL error until the stack was empty.
2086 */
2087static forceinline void qc_ssl_dump_errors(struct connection *conn)
2088{
2089 if (unlikely(global.mode & MODE_DEBUG)) {
2090 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002091 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002092 unsigned long ret;
2093
Willy Tarreau325fc632022-04-11 18:47:38 +02002094 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002095 ret = ERR_get_error();
2096 if (!ret)
2097 return;
2098
2099 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002100 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002101 }
2102 }
2103}
2104
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002105int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2106 const char **str, int *len);
2107
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002108/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2109 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002110 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002111 * Return 1 if succeeded, 0 if not.
2112 */
2113static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002114 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002115 const unsigned char *data, size_t len,
2116 struct quic_rx_packet *pkt,
2117 struct quic_rx_crypto_frm *cf)
2118{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002119 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002120 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002121
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002122 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002123 qc = ctx->qc;
2124
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002125 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2126
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002127 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2128 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002129 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002130 goto err;
2131 }
2132
2133 el->rx.crypto.offset += len;
2134 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002135 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002136
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002137 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002138 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002139 ssl_err = SSL_do_handshake(ctx->ssl);
2140 if (ssl_err != 1) {
2141 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2142 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2143 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002144 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002145 goto out;
2146 }
2147
2148 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002149 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002150 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002151 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002152 goto err;
2153 }
2154
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002155 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002156
2157 /* Check the alpn could be negotiated */
2158 if (!qc->app_ops) {
2159 TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2160 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2161 goto err;
2162 }
2163
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002164 /* I/O callback switch */
2165 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002166 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002167 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002168 /* The connection is ready to be accepted. */
2169 quic_accept_push_qc(qc);
2170 }
2171 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002172 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002173 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002174 } else {
2175 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2176 if (ssl_err != 1) {
2177 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2178 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2179 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002180 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002181 goto out;
2182 }
2183
2184 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002185 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002186 goto err;
2187 }
2188
2189 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002190 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002192
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002193 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002194 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002195 return 1;
2196
2197 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002198 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002199 return 0;
2200}
2201
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002202/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
2203 * <pkt> RX packet.
2204 * Return it if succeeded, NULL if not.
2205 */
2206static inline
2207struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
2208 struct quic_rx_packet *pkt)
2209{
2210 struct quic_rx_strm_frm *frm;
2211
2212 frm = pool_alloc(pool_head_quic_rx_strm_frm);
2213 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002214 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002215 frm->len = stream_frm->len;
2216 frm->data = stream_frm->data;
2217 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002218 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002219 }
2220
2221 return frm;
2222}
2223
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002224/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01002225 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002226 */
2227static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2228{
2229 size_t ret;
2230
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01002231 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
2232 strm_frm->len -= ret;
2233 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002234
2235 return ret;
2236}
2237
2238/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2239 * streams may be open. The data are copied to the stream RX buffer if possible.
2240 * If not, the STREAM frame is stored to be treated again later.
2241 * We rely on the flow control so that not to store too much STREAM frames.
2242 * Return 1 if succeeded, 0 if not.
2243 */
2244static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2245 struct quic_stream *strm_frm,
2246 struct quic_conn *qc)
2247{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002248 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002249 struct eb64_node *frm_node;
2250 struct qcs *qcs = NULL;
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002251 size_t done, buf_was_full;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002252 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002253
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002254 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2255 strm_frm->offset.key, strm_frm->fin,
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002256 (char *)strm_frm->data, &qcs, &done);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002257
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002258 /* invalid frame */
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002259 if (ret == 1)
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002260 return 0;
2261
2262 /* already fully received offset */
2263 if (ret == 0 && done == 0)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002264 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002265
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002266 /* frame not handled (partially or completely) must be buffered */
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002267 if (ret == 2) {
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002268 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2269 if (!frm) {
2270 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002271 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002272 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002273 }
2274
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002275 /* frame partially handled by the MUX */
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002276 if (done) {
2277 BUG_ON(done >= frm->len); /* must never happen */
2278 frm->len -= done;
2279 frm->data += done;
2280 frm->offset_node.key += done;
2281 }
2282
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002283 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2284 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002285
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002286 /* interrupt only if frame was not received at all. */
2287 if (!done)
2288 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002289 }
2290
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002291 /* Decode the data if buffer is already full as it's not possible to
2292 * dequeue a frame in this condition.
2293 */
2294 if (b_full(&qcs->rx.buf))
2295 qcc_decode_qcs(qc->qcc, qcs);
2296
2297 retry:
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002298 /* Frame received (partially or not) by the mux.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002299 * If there is buffered frame for next offset, it may be possible to
2300 * receive them now.
2301 */
2302 frm_node = eb64_first(&qcs->rx.frms);
2303 while (frm_node) {
2304 frm = eb64_entry(&frm_node->node,
2305 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002306
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02002307 ret = qcc_recv(qc->qcc, qcs->id, frm->len,
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002308 frm->offset_node.key, frm->fin,
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002309 (char *)frm->data, &qcs, &done);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002310
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002311 BUG_ON(ret == 1); /* must never happen for buffered frames */
2312
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002313 /* interrupt the parsing if the frame cannot be handled
2314 * entirely for the moment only.
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002315 */
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002316 if (ret == 2) {
2317 if (done) {
2318 BUG_ON(done >= frm->len); /* must never happen */
2319 frm->len -= done;
2320 frm->data += done;
2321
2322 eb64_delete(&frm->offset_node);
2323 frm->offset_node.key += done;
2324 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2325 }
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002326 break;
Amaury Denoyelle3df8ca02022-04-26 11:36:40 +02002327 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002328
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002329 /* Remove a newly received frame or an invalid one. */
2330 frm_node = eb64_next(frm_node);
2331 eb64_delete(&frm->offset_node);
2332 quic_rx_packet_refdec(frm->pkt);
2333 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002334 }
2335
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002336 buf_was_full = b_full(&qcs->rx.buf);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002337 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002338 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002339
Amaury Denoyelle03cc62c2022-04-27 16:53:16 +02002340 /* Buffer was full so the reception was stopped. Now the buffer has
2341 * space available thanks to qcc_decode_qcs(). We can now retry to
2342 * handle more data.
2343 */
2344 if (buf_was_full && !b_full(&qcs->rx.buf))
2345 goto retry;
2346
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002347 return 1;
2348}
2349
2350/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2351 * streams may be open. The data are copied to the stream RX buffer if possible.
2352 * If not, the STREAM frame is stored to be treated again later.
2353 * We rely on the flow control so that not to store too much STREAM frames.
2354 * Return 1 if succeeded, 0 if not.
2355 */
2356static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2357 struct quic_stream *strm_frm,
2358 struct quic_conn *qc)
2359{
2360 struct qcs *strm;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002361 struct quic_rx_strm_frm *frm;
2362 size_t strm_frm_len;
2363
Amaury Denoyelle50742292022-03-29 14:57:19 +02002364 strm = qcc_get_qcs(qc->qcc, strm_frm->id);
2365 if (!strm) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002366 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002367 return 0;
2368 }
2369
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002370 if (strm_frm->offset.key < strm->rx.offset) {
2371 size_t diff;
2372
2373 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2374 TRACE_PROTO("Already received STREAM data",
2375 QUIC_EV_CONN_PSTRM, qc);
2376 goto out;
2377 }
2378
2379 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2380 diff = strm->rx.offset - strm_frm->offset.key;
2381 strm_frm->offset.key = strm->rx.offset;
2382 strm_frm->len -= diff;
2383 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002384 }
2385
2386 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002387 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002388 int ret;
2389
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002390 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002391 goto store_frm;
2392
2393 /* qc_strm_cpy() will modify the offset, depending on the number
2394 * of bytes copied.
2395 */
2396 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2397 /* Inform the application of the arrival of this new stream */
2398 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002399 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002400 return 0;
2401 }
2402
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002403 if (ret)
2404 qcs_notify_recv(strm);
2405
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002406 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002407 }
2408 /* Take this frame into an account for the stream flow control */
2409 strm->rx.offset += strm_frm_len;
2410 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002411 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002412 */
2413 if (!strm_frm->len)
2414 goto out;
2415
2416 store_frm:
2417 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2418 if (!frm) {
2419 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002420 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002421 return 0;
2422 }
2423
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002424 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002425 quic_rx_packet_refinc(pkt);
2426
2427 out:
2428 return 1;
2429}
2430
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002431/* Returns 1 on success or 0 on error. On error, the packet containing the
2432 * frame must not be acknowledged.
2433 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002434static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2435 struct quic_stream *strm_frm,
2436 struct quic_conn *qc)
2437{
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002438 /* RFC9000 13.1. Packet Processing
2439 *
2440 * A packet MUST NOT be acknowledged until packet protection has been
2441 * successfully removed and all frames contained in the packet have
2442 * been processed. For STREAM frames, this means the data has been
2443 * enqueued in preparation to be received by the application protocol,
2444 * but it does not require that data be delivered and consumed.
2445 */
2446
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002447 if (strm_frm->id & QCS_ID_DIR_BIT)
2448 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2449 else
2450 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2451}
2452
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002453/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2454 * for <qc> QUIC connection.
2455 * This is a best effort function which never fails even if no memory could be
2456 * allocated to duplicate these frames.
2457 */
2458static void qc_dup_pkt_frms(struct quic_conn *qc,
2459 struct list *pkt_frm_list, struct list *out_frm_list)
2460{
2461 struct quic_frame *frm, *frmbak;
2462 struct list tmp = LIST_HEAD_INIT(tmp);
2463
2464 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2465 struct quic_frame *dup_frm, *origin;
2466
2467 switch (frm->type) {
2468 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2469 {
2470 struct quic_stream *strm_frm = &frm->stream;
2471 struct eb64_node *node = NULL;
2472 struct qc_stream_desc *stream_desc;
2473
2474 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2475 if (!node) {
2476 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm_frm);
2477 continue;
2478 }
2479
2480 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2481 /* Do not resend this frame if in the "already acked range" */
2482 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2483 TRACE_PROTO("ignored frame frame in already acked range",
2484 QUIC_EV_CONN_PRSAFRM, qc, frm);
2485 continue;
2486 }
2487 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2488 strm_frm->offset.key = stream_desc->ack_offset;
2489 TRACE_PROTO("updated partially acked frame",
2490 QUIC_EV_CONN_PRSAFRM, qc, frm);
2491 }
2492
2493 break;
2494 }
2495
2496 default:
2497 break;
2498 }
2499
2500 dup_frm = pool_zalloc(pool_head_quic_frame);
2501 if (!dup_frm) {
2502 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2503 break;
2504 }
2505
2506 /* If <frm> is already a copy of another frame, we must take
2507 * its original frame as source for the copy.
2508 */
2509 origin = frm->origin ? frm->origin : frm;
2510 TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
2511 *dup_frm = *origin;
2512 LIST_INIT(&dup_frm->reflist);
2513 TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM,
2514 qc, NULL, &origin->pkt->pn_node.key);
2515 dup_frm->origin = origin;
2516 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2517 LIST_APPEND(&tmp, &dup_frm->list);
2518 }
2519
2520 LIST_SPLICE(out_frm_list, &tmp);
2521}
2522
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002523/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002524static void qc_prep_fast_retrans(struct quic_conn *qc,
2525 struct quic_enc_level *qel,
2526 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002527{
2528 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002529 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002530 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002531 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002532
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002533 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002534 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002535 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002536 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002537 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2538 node = eb64_next(node);
2539 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002540 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002541 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002542 }
2543
2544 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002545 return;
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 return;
2554 }
2555
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002556 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2557 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2558 if (frms == frms1 && frms2) {
2559 frms = frms2;
2560 goto start;
2561 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002562}
2563
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002564/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002565 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002566 * Initial packets send them coalescing with others (Handshake here).
2567 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002568 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002569static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2570 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002571{
2572 struct list itmp = LIST_HEAD_INIT(itmp);
2573 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002574
2575 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2576 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2577 struct quic_enc_level *qel = iqel;
2578 struct eb_root *pkts;
2579 struct eb64_node *node;
2580 struct quic_tx_packet *pkt;
2581 struct list *tmp = &itmp;
2582
2583 start:
2584 pkt = NULL;
2585 pkts = &qel->pktns->tx.pkts;
2586 node = eb64_first(pkts);
2587 /* Skip the empty packet (they have already been retransmitted) */
2588 while (node) {
2589 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002590 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002591 break;
2592 node = eb64_next(node);
2593 }
2594
2595 if (!pkt)
2596 goto end;
2597
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002598 /* When building a packet from another one, the field which may increase the
2599 * packet size is the packet number. And the maximum increase is 4 bytes.
2600 */
2601 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2602 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2603 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2604 goto end;
2605 }
2606
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002607 qel->pktns->tx.pto_probe += 1;
2608 requeue:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002609 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2610 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002611 if (qel == iqel) {
2612 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2613 pkt = pkt->next;
2614 tmp = &htmp;
2615 hqel->pktns->tx.pto_probe += 1;
2616 goto requeue;
2617 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002618 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002619
2620 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002621 LIST_SPLICE(ifrms, &itmp);
2622 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002623}
2624
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002625/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2626 * as I/O handler context and <qel> as encryption level.
2627 * Returns 1 if succeeded, 0 if failed.
2628 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002629static 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 +01002630 struct quic_enc_level *qel)
2631{
2632 struct quic_frame frm;
2633 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002634 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002635 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002636
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002637 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638 /* Skip the AAD */
2639 pos = pkt->data + pkt->aad_len;
2640 end = pkt->data + pkt->len;
2641
2642 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002643 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002644 goto err;
2645
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002646 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002647 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002648 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002649 break;
2650 case QUIC_FT_PING:
2651 break;
2652 case QUIC_FT_ACK:
2653 {
2654 unsigned int rtt_sample;
2655
2656 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002657 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002658 goto err;
2659
2660 if (rtt_sample) {
2661 unsigned int ack_delay;
2662
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002663 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002664 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002665 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2666 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002667 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002668 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002669 break;
2670 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002671 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002672 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002674 {
2675 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002676
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002677 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002678 /* XXX TO DO: <cfdebug> is used only for the traces. */
2679 struct quic_rx_crypto_frm cfdebug = { };
2680
2681 cfdebug.offset_node.key = frm.crypto.offset;
2682 cfdebug.len = frm.crypto.len;
2683 TRACE_PROTO("CRYPTO data discarded",
2684 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2685 break;
2686 }
2687
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002688 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2689 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2690 /* XXX TO DO: <cfdebug> is used only for the traces. */
2691 struct quic_rx_crypto_frm cfdebug = { };
2692
2693 cfdebug.offset_node.key = frm.crypto.offset;
2694 cfdebug.len = frm.crypto.len;
2695 /* Nothing to do */
2696 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002697 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002698 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002699 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2700 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002701 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002702 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002703 else {
2704 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2705 /* XXX TO DO: <cfdebug> is used only for the traces. */
2706 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002707
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002708 cfdebug.offset_node.key = frm.crypto.offset;
2709 cfdebug.len = frm.crypto.len;
2710 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002711 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002712 frm.crypto.len -= diff;
2713 frm.crypto.data += diff;
2714 frm.crypto.offset = qel->rx.crypto.offset;
2715 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002716 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002717
2718 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002719 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002720 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002721
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002722 cfdebug.offset_node.key = frm.crypto.offset;
2723 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002724 if (!qc_provide_cdata(qel, ctx,
2725 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002726 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002727 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002728
2729 break;
2730 }
2731
2732 /* frm.crypto.offset > qel->rx.crypto.offset */
2733 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2734 if (!cf) {
2735 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002736 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002737 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002738 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002739
2740 cf->offset_node.key = frm.crypto.offset;
2741 cf->len = frm.crypto.len;
2742 cf->data = frm.crypto.data;
2743 cf->pkt = pkt;
2744 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2745 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002746 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002747 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002748 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002749 {
2750 struct quic_stream *stream = &frm.stream;
2751
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002752 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002753 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2754 goto err;
2755 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2756 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002757
Amaury Denoyelled46e3352022-04-29 15:57:49 +02002758 /* The upper layer may not be allocated.
2759 *
2760 * TODO emit a CONNECTION_CLOSE if mux already freed.
2761 */
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002762 if (qc->mux_state != QC_MUX_READY)
Amaury Denoyelled46e3352022-04-29 15:57:49 +02002763 goto err;
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002764
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002765 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002766 goto err;
2767
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002768 break;
2769 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002770 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002771 if (qc->mux_state == QC_MUX_READY) {
2772 struct quic_max_data *data = &frm.max_data;
2773 qcc_recv_max_data(qc->qcc, data->max_data);
2774 }
2775 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002776 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002777 if (qc->mux_state == QC_MUX_READY) {
2778 struct quic_max_stream_data *data = &frm.max_stream_data;
2779 qcc_recv_max_stream_data(qc->qcc, data->id,
2780 data->max_stream_data);
2781 }
2782 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002783 case QUIC_FT_MAX_STREAMS_BIDI:
2784 case QUIC_FT_MAX_STREAMS_UNI:
2785 case QUIC_FT_DATA_BLOCKED:
2786 case QUIC_FT_STREAM_DATA_BLOCKED:
2787 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2788 case QUIC_FT_STREAMS_BLOCKED_UNI:
2789 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002790 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002791 case QUIC_FT_RETIRE_CONNECTION_ID:
2792 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002793 break;
2794 case QUIC_FT_CONNECTION_CLOSE:
2795 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille47756802022-03-25 09:12:16 +01002796 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
2797 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2798 /* RFC 9000 10.2. Immediate Close:
2799 * The closing and draining connection states exist to ensure
2800 * that connections close cleanly and that delayed or reordered
2801 * packets are properly discarded. These states SHOULD persist
2802 * for at least three times the current PTO interval...
2803 *
2804 * Rearm the idle timeout only one time when entering draining
2805 * state.
2806 */
2807 qc_idle_timer_do_rearm(qc);
2808 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002809 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002810 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002811 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002812 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002813 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814 goto err;
2815
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002816 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817 break;
2818 default:
2819 goto err;
2820 }
2821 }
2822
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002823 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002824 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002825
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002826 if (fast_retrans) {
2827 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2828 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2829
2830 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2831 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002832
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2834 * has successfully parse a Handshake packet. The Initial encryption must also
2835 * be discarded.
2836 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002837 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002838 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002839 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2840 QUIC_FL_TLS_SECRETS_DCD)) {
2841 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2842 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2843 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2844 qc_set_timer(ctx->qc);
2845 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2846 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2847 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002848 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2849 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002850 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002851 }
2852
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002853 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002854 return 1;
2855
2856 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002857 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858 return 0;
2859}
2860
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002861/* Must be called only by a <cbuf> writer (packet builder).
2862 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2863 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2864 * index after having marked the end of written data. This the responsability
2865 * of the caller to ensure there is enough room in <cbuf> to write the end of
2866 * data made of a uint16_t null field.
2867 *
2868 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2869 * ^ ^
2870 * r w
2871 *
2872 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2873 * ^ ^
2874 * r w
2875
2876 * +--------------------------------------+ (empty buffer, can be reused)
2877 * ^
2878 * (r = w)
2879 *
2880 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2881 * ^ ^
2882 * w r
2883 */
2884static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2885{
2886 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2887
2888 /* We can reset the writer index only if in front of the reader index and
2889 * if the reader index is not null. Resetting the writer when the reader
2890 * index is null would empty the buffer.
2891 * XXX Note than the writer index cannot reach the reader index.
2892 * Only the reader index can reach the writer index.
2893 */
2894 if (rd && rd <= cbuf->wr) {
2895 /* Mark the end of contiguous data for the reader */
2896 write_u16(cb_wr(cbuf), 0);
2897 cb_add(cbuf, sizeof(uint16_t));
2898 cb_wr_reset(cbuf);
2899 return 1;
2900 }
2901
2902 return 0;
2903}
2904
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002905/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002906 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002907 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2908 * This function must be called only after having built a correct datagram.
2909 * Always succeeds.
2910 */
2911static inline void qc_set_dg(struct cbuf *cbuf,
2912 uint16_t dglen, struct quic_tx_packet *pkt)
2913{
2914 write_u16(cb_wr(cbuf), dglen);
2915 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2916 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2917}
2918
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002919/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2920 * with <frms> as ack-eliciting frame list to send, 0 if not.
2921 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2922 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2923 */
2924static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2925 struct quic_enc_level *qel, int cc, int probe)
2926{
2927 unsigned int must_ack =
2928 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2929
2930 /* Do not build any more packet if the TX secrets are not available or
2931 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2932 * and if there is no more packets to send upon PTO expiration
2933 * and if there is no more ack-eliciting frames to send or in flight
2934 * congestion control limit is reached for prepared data
2935 */
2936 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2937 (!cc && !probe && !must_ack &&
2938 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2939 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2940 return 0;
2941 }
2942
2943 return 1;
2944}
2945
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002946/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002947 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2948 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002949 * A header made of two fields is added to each datagram: the datagram length followed
2950 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002951 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2952 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002953 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002954static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2955 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002956{
2957 struct quic_enc_level *qel;
2958 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002959 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002960 struct quic_tx_packet *pkt;
2961 size_t total;
2962 size_t dg_headlen;
2963
2964 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2965 /* Each datagram is prepended with its length followed by the
2966 * address of the first packet in the datagram.
2967 */
2968 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2969 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2970 total = 0;
2971 start:
2972 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002973 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002974 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2975 * to ensure there is enough room to mark the end of prepared
2976 * contiguous data with a zero length.
2977 */
2978 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2979 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002980 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002981
2982 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002983 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002984 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002985 /* We do not probe if an immediate close was asked */
2986 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002987 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002988
2989 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002990 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002991
2992 /* Leave room for the datagram header */
2993 pos += dg_headlen;
2994 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2995 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2996 }
2997 else {
2998 end = pos + qc->path->mtu;
2999 }
3000
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003001 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003002 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003003 switch (err) {
3004 case -2:
3005 goto err;
3006 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01003007 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
3008 * MTU, we are here because of the congestion control window. There is
3009 * no need to try to reuse this buffer.
3010 */
3011 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003012 default:
3013 break;
3014 }
3015
3016 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
3017 if (!pkt)
3018 goto err;
3019
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003020 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3021 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3022
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003023 total += pkt->len;
3024 /* Set the current datagram as prepared into <cbuf>. */
3025 qc_set_dg(cbuf, pkt->len, pkt);
3026 }
3027
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003028 /* Reset <wr> writer index if in front of <rd> index */
3029 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003030 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003031 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003032 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01003033 }
3034
3035 out:
3036 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
3037 return total;
3038
3039 err:
3040 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
3041 return -1;
3042}
3043
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01003044/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003045 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
3046 * several packets in the same datagram. A header made of two fields is added
3047 * to each datagram: the datagram length followed by the address of the first
3048 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01003049 * Returns the number of bytes prepared in packets if succeeded (may be 0),
3050 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003051 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003052static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003053 enum quic_tls_enc_level tel, struct list *tel_frms,
3054 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003056 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003057 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003058 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003059 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
3060 /* length of datagrams */
3061 uint16_t dglen;
3062 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003063 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003064 /* Each datagram is prepended with its length followed by the
3065 * address of the first packet in the datagram.
3066 */
3067 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003068 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003070 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
3071
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003072 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003073 qel = &qc->els[tel];
3074 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003075 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003076 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003077 padding = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003078 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003079 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003080 /* Leave at least <dglen> bytes at the end of this buffer
3081 * to ensure there is enough room to mark the end of prepared
3082 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003083 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003084 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
3085 first_pkt = prv_pkt = NULL;
3086 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003087 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003088 enum quic_pkt_type pkt_type;
3089
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003090 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003091 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003092 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003093 /* We do not probe if an immediate close was asked */
3094 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01003095 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003096
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003097 if (!qc_may_build_pkt(qc, frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003098 if (prv_pkt)
3099 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003100 /* Let's select the next encryption level */
3101 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
3102 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003103 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01003104 qel = &qc->els[tel];
3105 /* Build a new datagram */
3106 prv_pkt = NULL;
3107 continue;
3108 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003109 break;
3110 }
3111
3112 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003113 if (!prv_pkt) {
3114 /* Leave room for the datagram header */
3115 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003116 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01003117 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
3118 }
3119 else {
3120 end = pos + qc->path->mtu;
3121 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003122 }
3123
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003124 cur_pkt = qc_build_pkt(&pos, end, qel, frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003125 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003126 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003127 case -2:
3128 goto err;
3129 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003130 /* If there was already a correct packet present, set the
3131 * current datagram as prepared into <cbuf>.
3132 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01003133 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003134 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01003135 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003136 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01003137 break;
3138 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02003139
Frédéric Lécaille63556772021-12-29 17:18:21 +01003140 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
3141 if (!cur_pkt)
3142 goto err;
3143
Frédéric Lécaille1809c332022-04-25 10:24:12 +02003144 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
3145 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
3146
Frédéric Lécaille63556772021-12-29 17:18:21 +01003147 total += cur_pkt->len;
3148 /* keep trace of the first packet in the datagram */
3149 if (!first_pkt)
3150 first_pkt = cur_pkt;
3151 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003152 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003153 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02003154 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
3155 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01003156 /* Let's say we have to build a new dgram */
3157 prv_pkt = NULL;
3158 dglen += cur_pkt->len;
3159 /* Client: discard the Initial encryption keys as soon as
3160 * a handshake packet could be built.
3161 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003162 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01003163 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
3164 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3165 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
3166 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
3167 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01003168 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003169 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003170 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003171 }
3172 /* If the data for the current encryption level have all been sent,
3173 * select the next level.
3174 */
3175 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003176 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 +01003177 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
3178 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
3179 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3180 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003181 if (tel == QUIC_TLS_ENC_LEVEL_APP)
3182 frms = &qc->els[tel].pktns->tx.frms;
3183 else
3184 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01003185 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003186 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01003187 /* If there is data for the next level, do not
3188 * consume a datagram.
3189 */
3190 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003191 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003192 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003193 /* If we have to build a new datagram, set the current datagram as
3194 * prepared into <cbuf>.
3195 */
3196 if (!prv_pkt) {
3197 qc_set_dg(cbuf, dglen, first_pkt);
3198 first_pkt = NULL;
3199 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003200 padding = 0;
3201 }
3202 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003203 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01003204 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
3205 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003206 }
3207 }
3208
3209 stop_build:
3210 /* Reset <wr> writer index if in front of <rd> index */
3211 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003212 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01003213 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01003214 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215 }
3216
3217 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003218 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003219 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003220
3221 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003222 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003223 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003224}
3225
3226/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003227 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003228 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003229int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003230{
3231 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003232 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003233
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003234 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003235 cbuf = qr->cbuf;
3236 while (cb_contig_data(cbuf)) {
3237 unsigned char *pos;
3238 struct buffer tmpbuf = { };
3239 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3240 uint16_t dglen;
3241 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003242 unsigned int time_sent;
3243
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003244 pos = cb_rd(cbuf);
3245 dglen = read_u16(pos);
3246 /* End of prepared datagrams.
3247 * Reset the reader index only if in front of the writer index.
3248 */
3249 if (!dglen) {
3250 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3251
3252 if (wr && wr < cbuf->rd) {
3253 cb_rd_reset(cbuf);
3254 continue;
3255 }
3256 break;
3257 }
3258
3259 pos += sizeof dglen;
3260 first_pkt = read_ptr(pos);
3261 pos += sizeof first_pkt;
3262 tmpbuf.area = (char *)pos;
3263 tmpbuf.size = tmpbuf.data = dglen;
3264
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003265 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003266 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003267 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003268
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003269 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003270 qc->tx.bytes += tmpbuf.data;
3271 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003272
3273 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3274 pkt->time_sent = time_sent;
3275 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3276 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003277 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003278 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3279 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003281 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3282 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3283 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003284 qc_notify_close(qc);
3285
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003286 /* RFC 9000 10.2. Immediate Close:
3287 * The closing and draining connection states exist to ensure
3288 * that connections close cleanly and that delayed or reordered
3289 * packets are properly discarded. These states SHOULD persist
3290 * for at least three times the current PTO interval...
3291 *
3292 * Rearm the idle timeout only one time when entering closing
3293 * state.
3294 */
3295 qc_idle_timer_do_rearm(qc);
3296 if (qc->timer_task) {
3297 task_destroy(qc->timer_task);
3298 qc->timer_task = NULL;
3299 }
3300 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003301 qc->path->in_flight += pkt->in_flight_len;
3302 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3303 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003304 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003305 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003306 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003307 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003308 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003309 }
3310 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003311
3312 return 1;
3313}
3314
3315/* Build all the frames which must be sent just after the handshake have succeeded.
3316 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3317 * a HANDSHAKE_DONE frame.
3318 * Return 1 if succeeded, 0 if not.
3319 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003320static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003322 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003323 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003324 struct quic_frame *frm, *frmbak;
3325 struct list frm_list = LIST_HEAD_INIT(frm_list);
3326 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003327
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003328 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003329 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003330 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003331 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003332 if (!frm)
3333 return 0;
3334
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003335 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003336 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003337 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003338 }
3339
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003340 first = 1;
3341 max = qc->tx.params.active_connection_id_limit;
3342 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343 struct quic_connection_id *cid;
3344
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003345 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003346 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003347 goto err;
3348
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003349 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003350 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003351 if (!cid)
3352 goto err;
3353
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003354 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003355 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003356
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003357 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003358 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003360
3361 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003362 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003363
3364 return 1;
3365
3366 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003367 /* free the frames */
3368 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3369 pool_free(pool_head_quic_frame, frm);
3370
3371 node = eb64_first(&qc->cids);
3372 while (node) {
3373 struct quic_connection_id *cid;
3374
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003375
3376 cid = eb64_entry(&node->node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003377 if (cid->seq_num.key >= max)
3378 break;
3379
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003380 if (cid->seq_num.key < first)
3381 continue;
3382
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003383 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003384 ebmb_delete(&cid->node);
3385 eb64_delete(&cid->seq_num);
3386 pool_free(pool_head_quic_connection_id, cid);
3387 }
3388
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003389 return 0;
3390}
3391
3392/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003393void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003394{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003395 struct eb64_node *n;
3396 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003397
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003398 n = eb64_first(&arngs->root);
3399 while (n) {
3400 struct eb64_node *next;
3401
3402 ar = eb64_entry(&n->node, struct quic_arng_node, first);
3403 next = eb64_next(n);
3404 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003405 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003406 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 }
3408}
3409
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003410/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3411 * descending order.
3412 */
3413static inline size_t sack_gap(struct quic_arng_node *p,
3414 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003415{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003416 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003417}
3418
3419
3420/* Remove the last elements of <ack_ranges> list of ack range updating its
3421 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003422 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003424static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003425{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003426 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003427
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003428 last = eb64_last(&arngs->root);
3429 while (last && arngs->enc_sz > limit) {
3430 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003431
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003432 prev = eb64_prev(last);
3433 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003434 return 0;
3435
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003436 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
3437 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
3438 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3439 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3440 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3441 --arngs->sz;
3442 eb64_delete(last);
3443 pool_free(pool_head_quic_arng, last);
3444 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003445 }
3446
3447 return 1;
3448}
3449
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003450/* Set the encoded size of <arngs> QUIC ack ranges. */
3451static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3452{
3453 struct eb64_node *node, *next;
3454 struct quic_arng_node *ar, *ar_next;
3455
3456 node = eb64_last(&arngs->root);
3457 if (!node)
3458 return;
3459
3460 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3461 arngs->enc_sz = quic_int_getsize(ar->last) +
3462 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3463
3464 while ((next = eb64_prev(node))) {
3465 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
3466 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3467 quic_int_getsize(ar_next->last - ar_next->first.key);
3468 node = next;
3469 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3470 }
3471}
3472
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003473/* Insert <ar> ack range into <argns> tree of ack ranges.
3474 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003475 */
3476static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003477struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3478 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003479{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003480 struct quic_arng_node *new_ar;
3481
3482 new_ar = pool_alloc(pool_head_quic_arng);
3483 if (new_ar) {
3484 new_ar->first.key = ar->first;
3485 new_ar->last = ar->last;
3486 eb64_insert(&arngs->root, &new_ar->first);
3487 arngs->sz++;
3488 }
3489
3490 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003491}
3492
3493/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003495 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003496 *
3497 * Descending order
3498 * ------------->
3499 * range1 range2
3500 * ..........|--------|..............|--------|
3501 * ^ ^ ^ ^
3502 * | | | |
3503 * last1 first1 last2 first2
3504 * ..........+--------+--------------+--------+......
3505 * diff1 gap12 diff2
3506 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003507 * To encode the previous list of ranges we must encode integers as follows in
3508 * descending order:
3509 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510 * with diff1 = last1 - first1
3511 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003512 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003513 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003514 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003515int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3516 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003517{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003518 struct eb64_node *le;
3519 struct quic_arng_node *new_node;
3520 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003522 new = NULL;
3523 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003524 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003525 if (!new_node)
3526 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003527
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003528 goto out;
3529 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003530
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003531 le = eb64_lookup_le(&arngs->root, ar->first);
3532 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003533 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003534 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003535 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003536
3537 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003538 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003539 else {
3540 struct quic_arng_node *le_ar =
3541 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003542
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003543 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003544 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003545 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003546
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003547 if (le_ar->last + 1 >= ar->first) {
3548 le_ar->last = ar->last;
3549 new = le;
3550 new_node = le_ar;
3551 }
3552 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003553 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003554 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003555 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003556
3557 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003559 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003561 /* Verify that the new inserted node does not overlap the nodes
3562 * which follow it.
3563 */
3564 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003565 struct eb64_node *next;
3566 struct quic_arng_node *next_node;
3567
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003568 while ((next = eb64_next(new))) {
3569 next_node =
3570 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003571 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003572 break;
3573
3574 if (next_node->last > new_node->last)
3575 new_node->last = next_node->last;
3576 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003577 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003578 /* Decrement the size of these ranges. */
3579 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003580 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003581 }
3582
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003583 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003584 quic_arngs_set_enc_sz(arngs);
3585
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003586 return 1;
3587}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003588/* Remove the header protection of packets at <el> encryption level.
3589 * Always succeeds.
3590 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003591static 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 +01003592{
3593 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003594 struct quic_rx_packet *pqpkt;
3595 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003596 struct quic_enc_level *app_qel;
3597
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003598 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3599 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003601 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003602 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003603 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003604 goto out;
3605 }
3606 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003607 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003608 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003610 pqpkt->data, pqpkt->data + pqpkt->len)) {
3611 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612 /* XXX TO DO XXX */
3613 }
3614 else {
3615 /* The AAD includes the packet number field */
3616 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3617 /* Store the packet into the tree of packets to decrypt. */
3618 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003619 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003620 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3621 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003622 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003623 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003624 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003625 MT_LIST_DELETE_SAFE(pkttmp1);
3626 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627 }
3628
3629 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003630 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631}
3632
3633/* Process all the CRYPTO frame at <el> encryption level.
3634 * Return 1 if succeeded, 0 if not.
3635 */
3636static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003637 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003638{
3639 struct eb64_node *node;
3640
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003641 node = eb64_first(&el->rx.crypto.frms);
3642 while (node) {
3643 struct quic_rx_crypto_frm *cf;
3644
3645 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3646 if (cf->offset_node.key != el->rx.crypto.offset)
3647 break;
3648
3649 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3650 goto err;
3651
3652 node = eb64_next(node);
3653 quic_rx_packet_refdec(cf->pkt);
3654 eb64_delete(&cf->offset_node);
3655 pool_free(pool_head_quic_rx_crypto_frm, cf);
3656 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003657 return 1;
3658
3659 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003660 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003661 return 0;
3662}
3663
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003664/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003665 * 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 +02003666 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003667 * Return 1 if succeeded, 0 if not.
3668 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003669int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3670 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003671{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003672 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003673 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003674 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003675 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003676
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003677 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003678 qel = cur_el;
3679 next_tel:
3680 if (!qel)
3681 goto out;
3682
3683 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3684 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003685 while (node) {
3686 struct quic_rx_packet *pkt;
3687
3688 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003689 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003690 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003691 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003692 /* Drop the packet */
3693 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003694 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003695 }
3696 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003697 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003698 /* Drop the packet */
3699 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003700 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003701 }
3702 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003703 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3704
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003705 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3706 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3707 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003708 qc_idle_timer_rearm(qc, 1);
3709 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003710 if (pkt->pn > largest_pn)
3711 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003713 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003714 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003715 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003716 }
3717 }
3718 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003719 eb64_delete(&pkt->pn_node);
3720 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003721 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003722 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003723
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003724 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003725 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn)
3726 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003727 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003728 goto err;
3729
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003730 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003731 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003732 qel = next_el;
3733 goto next_tel;
3734 }
3735
3736 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003737 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003738 return 1;
3739
3740 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003741 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003742 return 0;
3743}
3744
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003745/* Check if it's possible to remove header protection for packets related to
3746 * encryption level <qel>. If <qel> is NULL, assume it's false.
3747 *
3748 * Return true if the operation is possible else false.
3749 */
3750static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3751{
3752 enum quic_tls_enc_level tel;
3753
3754 if (!qel)
3755 return 0;
3756
3757 tel = ssl_to_quic_enc_level(qel->level);
3758
3759 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003760 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003761 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003762 return 0;
3763 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003764
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003765 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003766 return 0;
3767
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003768 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003769 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003770 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003771 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003772
3773 return 1;
3774}
3775
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003776/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003777int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003778{
3779 int ret;
3780 struct qring *qr;
3781
3782 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3783 if (!qr)
3784 /* Never happens */
3785 return 1;
3786
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003787 if (old_data)
3788 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003789 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003790 if (ret == -1)
3791 goto err;
3792 else if (ret == 0)
3793 goto out;
3794
3795 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3796 goto err;
3797
3798 out:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003799 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003800 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3801 return 1;
3802
3803 err:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003804 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003805 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3806 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3807 return 0;
3808}
3809
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003810/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3811 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
3812 * QUIC connection
3813 * Returns 1 if succeeded, 0 if not.
3814 */
3815int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
3816 enum quic_tls_enc_level tel, struct list *tel_frms,
3817 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3818{
3819 int ret;
3820 struct qring *qr;
3821
3822 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3823 if (!qr)
3824 /* Never happens */
3825 return 1;
3826
3827 if (old_data)
3828 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
3829 ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms);
3830 if (ret == -1)
3831 goto err;
3832 else if (ret == 0)
3833 goto out;
3834
3835 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3836 goto err;
3837
3838 out:
3839 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3840 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3841 return 1;
3842
3843 err:
3844 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3845 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3846 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3847 return 0;
3848}
3849
3850/* Retransmit up to two datagrams depending on packet number space */
3851static void qc_dgrams_retransmit(struct quic_conn *qc)
3852{
3853 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3854 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3855 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3856
3857 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3858 struct list ifrms = LIST_HEAD_INIT(ifrms);
3859 struct list hfrms = LIST_HEAD_INIT(hfrms);
3860
3861 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
3862 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
3863 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3864 if (!LIST_ISEMPTY(&ifrms)) {
3865 iqel->pktns->tx.pto_probe = 1;
3866 if (!LIST_ISEMPTY(&hfrms)) {
3867 hqel->pktns->tx.pto_probe = 1;
3868 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
3869 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
3870 }
3871 }
3872 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3873 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
3874 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3875 if (!LIST_ISEMPTY(&hfrms)) {
3876 hqel->pktns->tx.pto_probe = 1;
3877 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
3878 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3879 }
3880 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3881 }
3882 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3883 }
3884 else {
3885 int i;
3886 struct list frms1 = LIST_HEAD_INIT(frms1);
3887 struct list frms2 = LIST_HEAD_INIT(frms2);
3888
3889 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3890 hqel->pktns->tx.pto_probe = 0;
3891 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
3892 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
3893 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3894 if (!LIST_ISEMPTY(&frms1)) {
3895 hqel->pktns->tx.pto_probe = 1;
3896 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
3897 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3898 }
3899 }
3900 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3901 }
3902 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3903 aqel->pktns->tx.pto_probe = 0;
3904 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
3905 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3906 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
3907 if (!LIST_ISEMPTY(&frms1)) {
3908 aqel->pktns->tx.pto_probe = 1;
3909 qc_send_app_pkts(qc, 1, &frms1);
3910 }
3911 if (!LIST_ISEMPTY(&frms2)) {
3912 aqel->pktns->tx.pto_probe = 1;
3913 qc_send_app_pkts(qc, 1, &frms2);
3914 }
3915 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3916 }
3917 }
3918}
3919
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003920/* QUIC connection packet handler task (post handshake) */
3921static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3922{
3923 struct ssl_sock_ctx *ctx;
3924 struct quic_conn *qc;
3925 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003926
3927
3928 ctx = context;
3929 qc = ctx->qc;
3930 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003931
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003932 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003933
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003934 /* Retranmissions */
3935 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3936 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
3937 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3938 qc_dgrams_retransmit(qc);
3939 }
3940
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003941 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3942 qc_rm_hp_pkts(qc, qel);
3943
3944 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3945 goto err;
3946
Frédéric Lécaille47756802022-03-25 09:12:16 +01003947 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3948 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3949 goto out;
3950
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003951 if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003952 goto err;
3953
Frédéric Lécaille47756802022-03-25 09:12:16 +01003954out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003955 return t;
3956
3957 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003958 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003959 return t;
3960}
3961
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003962/* QUIC connection packet handler task. */
3963struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003964{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003965 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003966 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003967 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003968 enum quic_tls_enc_level tel, next_tel;
3969 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003970 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003971 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003972
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003973 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003974 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003975 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003976 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003977 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003978 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003979
3980 /* Retranmissions */
3981 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3982 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
3983 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3984 qc_dgrams_retransmit(qc);
3985 }
3986
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003987 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3988 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003989 /* The I/O handler has been woken up by the dgram listener
3990 * after the anti-amplification was reached.
3991 */
3992 qc_set_timer(qc);
3993 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3994 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3995 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003996 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003997 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3998 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3999 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004000 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01004001 if (st >= QUIC_HS_ST_COMPLETE &&
4002 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
4003 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
4004 /* There may be remaining Handshake packets to treat and acknowledge. */
4005 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
4006 next_tel = QUIC_TLS_ENC_LEVEL_APP;
4007 }
4008 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004009 goto err;
4010
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004011 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02004012 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004013
4014 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004015 /* Treat packets waiting for header packet protection decryption */
4016 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004017 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004018
Frédéric Lécaille2766e782021-08-30 17:16:07 +02004019 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
4020 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4021 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004022 goto err;
4023
Frédéric Lécaille47756802022-03-25 09:12:16 +01004024 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
4025 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
4026 goto out;
4027
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004028 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004029 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004030 qel = next_qel;
4031 next_qel = NULL;
4032 goto next_level;
4033 }
4034
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004035 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004036 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004037 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004038 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004039 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01004040
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004041 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004042 QUIC_FL_TLS_SECRETS_DCD)) {
4043 /* Discard the Handshake keys. */
4044 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
4045 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
4046 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
4047 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004048 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01004049 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01004050 }
4051
4052 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
4053 /* There may be remaining handshake to build (acks) */
4054 st = QUIC_HS_ST_SERVER_HANDSHAKE;
4055 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004056 }
4057
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004058 if (!qr)
4059 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01004060 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
4061 * be considered.
4062 */
4063 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01004064 goto err;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02004065 ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms,
4066 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004067 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004068 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004069 else if (ret == 0)
4070 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004071
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004072 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004073 goto err;
4074
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004075 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004076 /* Check if there is something to do for the next level.
4077 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004078 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004079 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01004080 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004081 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02004082 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004083 goto next_level;
4084 }
4085
Frédéric Lécaille47756802022-03-25 09:12:16 +01004086 out:
4087 if (qr)
4088 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004089 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004090 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004091
4092 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004093 if (qr)
4094 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01004095 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02004096 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004097}
4098
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004099/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004100static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
4101{
4102 int i;
4103
4104 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
4105 if (qel->tx.crypto.bufs[i]) {
4106 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
4107 qel->tx.crypto.bufs[i] = NULL;
4108 }
4109 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004110 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004111}
4112
4113/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004114 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004115 * Returns 1 if succeeded, 0 if not.
4116 */
4117static int quic_conn_enc_level_init(struct quic_conn *qc,
4118 enum quic_tls_enc_level level)
4119{
4120 struct quic_enc_level *qel;
4121
4122 qel = &qc->els[level];
4123 qel->level = quic_to_ssl_enc_level(level);
4124 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4125 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4126 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004127 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004128
4129 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02004130 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004131 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004132 qel->rx.crypto.offset = 0;
4133 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004134
4135 /* Allocate only one buffer. */
4136 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4137 if (!qel->tx.crypto.bufs)
4138 goto err;
4139
4140 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4141 if (!qel->tx.crypto.bufs[0])
4142 goto err;
4143
4144 qel->tx.crypto.bufs[0]->sz = 0;
4145 qel->tx.crypto.nb_buf = 1;
4146
4147 qel->tx.crypto.sz = 0;
4148 qel->tx.crypto.offset = 0;
4149
4150 return 1;
4151
4152 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004153 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004154 return 0;
4155}
4156
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004157/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4158 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004159 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004160 * This function must only be called by the thread responsible of the quic_conn
4161 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004162 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004163static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004164{
4165 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004166 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004167 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004168 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004169
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004170 /* We must not free the quic-conn if the MUX is still allocated. */
4171 BUG_ON(qc->mux_state == QC_MUX_READY);
4172
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004173 /* free remaining stream descriptors */
4174 node = eb64_first(&qc->streams_by_id);
4175 while (node) {
4176 struct qc_stream_desc *stream;
4177
4178 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4179 node = eb64_next(node);
4180
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004181 /* all streams attached to the quic-conn are released, so
4182 * qc_stream_desc_free will liberate the stream instance.
4183 */
4184 BUG_ON(!stream->release);
4185 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004186 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004187
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004188 if (qc->idle_timer_task) {
4189 task_destroy(qc->idle_timer_task);
4190 qc->idle_timer_task = NULL;
4191 }
4192
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004193 if (qc->timer_task) {
4194 task_destroy(qc->timer_task);
4195 qc->timer_task = NULL;
4196 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004197
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004198 /* remove the connection from receiver cids trees */
4199 ebmb_delete(&qc->odcid_node);
4200 ebmb_delete(&qc->scid_node);
4201 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004202
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004203 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004204 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004205 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004206 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004207 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004208 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004209
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004210 quic_tls_ku_free(qc);
4211 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4212 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004213 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004214 }
4215
4216 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4217 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4218 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004219
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004220 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
4221 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02004222 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004223 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004224
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004225 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4226 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004227 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004228}
4229
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004230static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004231{
4232 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004233 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004234
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004235 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004236
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004237 /* Next application data can be dropped. */
4238 qc->mux_state = QC_MUX_RELEASED;
4239
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004240 /* If the quic-conn timer has already expired free the quic-conn. */
4241 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4242 quic_conn_release(qc);
4243 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
4244 return;
4245 }
4246
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004247 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004248}
4249
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004250/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004251static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004253 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004254 struct quic_conn *qc;
4255 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004256
4257 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004258 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004259 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004260 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004261 task->expire = TICK_ETERNITY;
4262 pktns = quic_loss_pktns(qc);
4263 if (tick_isset(pktns->tx.loss_time)) {
4264 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4265
4266 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4267 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004268 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4269 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004270 goto out;
4271 }
4272
4273 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004274 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004275 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4276 qc->qcc->subs->events & SUB_RETRY_SEND) {
4277 struct qcc *qcc = qc->qcc;
4278
4279 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4280 tasklet_wakeup(qcc->subs->tasklet);
4281 qcc->subs->events &= ~SUB_RETRY_SEND;
4282 if (!qcc->subs->events)
4283 qcc->subs = NULL;
4284 }
4285 else {
4286 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4287 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4288 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
4289 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
4290 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4291 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004292 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004293 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004294 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004295 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4296 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4297
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004298 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004299 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004300 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004301 iel->pktns->tx.pto_probe = 1;
4302 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004303
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004304 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4305 qc->path->loss.pto_count++;
4306
4307 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004308 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004309
4310 return task;
4311}
4312
4313/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
4314 * connections used to identify the first Initial packets of client connecting
4315 * to listeners. This parameter must be NULL for QUIC connections attached
4316 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
4317 * <scid> is the source connection ID with <scid_len> as length.
4318 * Returns 1 if succeeded, 0 if not.
4319 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004320static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004321 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004322 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004323{
4324 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004325 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004326 /* Initial CID. */
4327 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004328 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004329 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004330
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004331 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004332 qc = pool_zalloc(pool_head_quic_conn);
4333 if (!qc) {
4334 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
4335 goto err;
4336 }
4337
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004338 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4339 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004340 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004341 goto err;
4342 }
4343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004344 qc->cids = EB_ROOT;
4345 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004346 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004347 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004348
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004349 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004350 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004351 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004352 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004353 qc->odcid.addrlen = dcid_addr_len;
4354 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004355
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004356 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004357 if (scid_len)
4358 memcpy(qc->dcid.data, scid, scid_len);
4359 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02004360 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004361 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004362 }
4363 /* QUIC Client (outgoing connection to servers) */
4364 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004365 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004366 if (dcid_len)
4367 memcpy(qc->dcid.data, dcid, dcid_len);
4368 qc->dcid.len = dcid_len;
4369 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004370 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004371
4372 /* Initialize the output buffer */
4373 qc->obuf.pos = qc->obuf.data;
4374
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004375 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004376 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004377 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004378 goto err;
4379 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004380
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004381 /* insert the allocated CID in the receiver datagram handler tree */
4382 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004383 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004384
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004385 /* Select our SCID which is the first CID with 0 as sequence number. */
4386 qc->scid = icid->cid;
4387
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004388 /* Packet number spaces initialization. */
4389 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4390 quic_pktns_init(&qc->pktns[i]);
4391 /* QUIC encryption level context initialization. */
4392 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004393 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004394 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004395 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004396 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004397 /* Initialize the packet number space. */
4398 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4399 }
4400
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02004401 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004402 qc->tps_tls_ext = qc->version & 0xff000000 ?
4403 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4404 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004405 /* TX part. */
4406 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004407 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4408 qc->tx.wbuf = qc->tx.rbuf = 0;
4409 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410 /* RX part. */
4411 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004412 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004413
4414 qc->nb_pkt_for_cc = 1;
4415 qc->nb_pkt_since_cc = 0;
4416
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004417 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004418 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004419 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004420 goto err;
4421 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004422
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004423 /* XXX TO DO: Only one path at this time. */
4424 qc->path = &qc->paths[0];
4425 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4426
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004427 /* required to use MTLIST_IN_LIST */
4428 MT_LIST_INIT(&qc->accept_list);
4429
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004430 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004431 qc->stream_buf_count = 0;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004432
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004433 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004434
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004435 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004436
4437 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004438 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004439 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4440 if (qc)
4441 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004442 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004443 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004444}
4445
4446/* Initialize the timer task of <qc> QUIC connection.
4447 * Returns 1 if succeeded, 0 if not.
4448 */
4449static int quic_conn_init_timer(struct quic_conn *qc)
4450{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004451 /* Attach this task to the same thread ID used for the connection */
4452 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453 if (!qc->timer_task)
4454 return 0;
4455
4456 qc->timer = TICK_ETERNITY;
4457 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004458 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004459
4460 return 1;
4461}
4462
Frédéric Lécaille47756802022-03-25 09:12:16 +01004463/* Rearm the idle timer for <qc> QUIC connection. */
4464static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4465{
4466 unsigned int expire;
4467
4468 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4469 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4470}
4471
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004472/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4473 * which is set to 1 when receiving a packet , and 0 when sending packet
4474 */
4475static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4476{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004477 if (read) {
4478 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4479 }
4480 else {
4481 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4482 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004483 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004484}
4485
4486/* The task handling the idle timeout */
4487static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4488{
4489 struct quic_conn *qc = ctx;
4490
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004491 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4492 * might free the quic-conn too early via quic_close().
4493 */
4494 qc_notify_close(qc);
4495
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004496 /* If the MUX is still alive, keep the quic-conn. The MUX is
4497 * responsible to call quic_close to release it.
4498 */
4499 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4500 if (qc->mux_state != QC_MUX_READY)
4501 quic_conn_release(qc);
4502
4503 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4504 * least clean some parts of it such as the tasklet.
4505 */
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004506
4507 return NULL;
4508}
4509
4510/* Initialize the idle timeout task for <qc>.
4511 * Returns 1 if succeeded, 0 if not.
4512 */
4513static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4514{
4515 qc->idle_timer_task = task_new_here();
4516 if (!qc->idle_timer_task)
4517 return 0;
4518
4519 qc->idle_timer_task->process = qc_idle_timer_task;
4520 qc->idle_timer_task->context = qc;
4521 qc_idle_timer_rearm(qc, 1);
4522 task_queue(qc->idle_timer_task);
4523
4524 return 1;
4525}
4526
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004527/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4528 * past one byte of this buffer.
4529 */
4530static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4531 struct quic_rx_packet *pkt)
4532{
4533 unsigned char dcid_len, scid_len;
4534
4535 /* Version */
4536 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4537 return 0;
4538
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004539 /* Destination Connection ID Length */
4540 dcid_len = *(*buf)++;
4541 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4542 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4543 /* XXX MUST BE DROPPED */
4544 return 0;
4545
4546 if (dcid_len) {
4547 /* Check that the length of this received DCID matches the CID lengths
4548 * of our implementation for non Initials packets only.
4549 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004550 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4551 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004552 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004553 return 0;
4554
4555 memcpy(pkt->dcid.data, *buf, dcid_len);
4556 }
4557
4558 pkt->dcid.len = dcid_len;
4559 *buf += dcid_len;
4560
4561 /* Source Connection ID Length */
4562 scid_len = *(*buf)++;
4563 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4564 /* XXX MUST BE DROPPED */
4565 return 0;
4566
4567 if (scid_len)
4568 memcpy(pkt->scid.data, *buf, scid_len);
4569 pkt->scid.len = scid_len;
4570 *buf += scid_len;
4571
4572 return 1;
4573}
4574
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004575/* Insert <pkt> RX packet in its <qel> RX packets tree */
4576static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4577{
4578 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004579 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004580 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4581 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4582 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004583}
4584
4585/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004586 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4587 * byte past the end of the buffer containing this packet and <beg> the address of
4588 * the packet first byte.
4589 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4590 * Returns 1 if succeeded, 0 if not.
4591 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004592static inline int qc_try_rm_hp(struct quic_conn *qc,
4593 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004594 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004595 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004596 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004597{
4598 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004599 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004600 struct quic_enc_level *qel;
4601 /* Only for traces. */
4602 struct quic_rx_packet *qpkt_trace;
4603
4604 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004605 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004606 /* The packet number is here. This is also the start minus
4607 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4608 * protection.
4609 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004610 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004611
4612 tel = quic_packet_type_enc_level(pkt->type);
4613 qel = &qc->els[tel];
4614
4615 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004616 /* Note that the following function enables us to unprotect the packet
4617 * number and its length subsequently used to decrypt the entire
4618 * packets.
4619 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004620 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4621 qel->pktns->rx.largest_pn, pn, beg, end)) {
4622 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004623 goto err;
4624 }
4625
4626 /* The AAD includes the packet number field found at <pn>. */
4627 pkt->aad_len = pn - beg + pkt->pnl;
4628 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004629 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004630 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004631 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004632 /* If the packet number space has been discarded, this packet
4633 * will be not parsed.
4634 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004635 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004636 goto out;
4637 }
4638
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004639 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004640 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004641 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4642 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004643 }
4644
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004645 *el = qel;
4646 /* No reference counter incrementation here!!! */
4647 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4648 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4649 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4650 b_add(&qc->rx.buf, pkt->len);
4651 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004652 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004653 return 1;
4654
4655 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004656 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004657 return 0;
4658}
4659
4660/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4661 * Also set <*long_header> to 1 if this form is long, 0 if not.
4662 */
4663static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4664 unsigned char byte0, int *long_header)
4665{
4666 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4667 pkt->type =
4668 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4669 *long_header = 1;
4670 }
4671 else {
4672 pkt->type = QUIC_PACKET_TYPE_SHORT;
4673 *long_header = 0;
4674 }
4675}
4676
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004677/*
4678 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4679 */
4680static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4681{
4682 int j = 0, version;
4683
4684 do {
4685 version = quic_supported_version[j];
4686 if (version == pkt->version)
4687 return 1;
4688
4689 version = quic_supported_version[++j];
4690 } while(version);
4691
4692 return 0;
4693}
4694
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004695/*
4696 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4697 * address <addr>.
4698 * Implementation of RFC9000 6. Version Negotiation
4699 *
4700 * TODO implement a rate-limiting sending of Version Negotiation packets
4701 *
4702 * Returns 0 on success else non-zero
4703 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004704static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4705 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004706{
4707 char buf[256];
4708 int i = 0, j, version;
4709 const socklen_t addrlen = get_addr_len(addr);
4710
4711 /*
4712 * header form
4713 * long header, fixed bit to 0 for Version Negotiation
4714 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004715 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4716 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004717
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004718 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004719 /* null version for Version Negotiation */
4720 buf[i++] = '\x00';
4721 buf[i++] = '\x00';
4722 buf[i++] = '\x00';
4723 buf[i++] = '\x00';
4724
4725 /* source connection id */
4726 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004727 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004728 i += pkt->scid.len;
4729
4730 /* destination connection id */
4731 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004732 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004733 i += pkt->dcid.len;
4734
4735 /* supported version */
4736 j = 0;
4737 do {
4738 version = htonl(quic_supported_version[j]);
4739 memcpy(&buf[i], &version, sizeof(version));
4740 i += sizeof(version);
4741
4742 version = quic_supported_version[++j];
4743 } while (version);
4744
4745 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4746 return 1;
4747
4748 return 0;
4749}
4750
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004751/* Generate the token to be used in Retry packets. The token is written to
4752 * <buf> which is expected to be <len> bytes.
4753 *
4754 * Various parameters are expected to be encoded in the token. For now, only
4755 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4756 * as this CID must be repeated by the server in the transport parameters.
4757 *
4758 * TODO add the client address to validate the token origin.
4759 *
4760 * Returns the length of the encoded token or 0 on error.
4761 */
4762static int generate_retry_token(unsigned char *buf, unsigned char len,
4763 struct quic_rx_packet *pkt)
4764{
4765 const size_t token_len = 1 + pkt->dcid.len;
4766 unsigned char i = 0;
4767
4768 if (token_len > len)
4769 return 0;
4770
4771 buf[i++] = pkt->dcid.len;
4772 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4773 i += pkt->dcid.len;
4774
4775 return i;
4776}
4777
4778/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4779 * the Initial <pkt> packet.
4780 *
4781 * Returns 0 on success else non-zero.
4782 */
4783static int send_retry(int fd, struct sockaddr_storage *addr,
4784 struct quic_rx_packet *pkt)
4785{
4786 unsigned char buf[128];
4787 int i = 0, token_len;
4788 const socklen_t addrlen = get_addr_len(addr);
4789 struct quic_cid scid;
4790
4791 /* long header + fixed bit + packet type 0x3 */
4792 buf[i++] = 0xf0;
4793 /* version */
4794 buf[i++] = 0x00;
4795 buf[i++] = 0x00;
4796 buf[i++] = 0x00;
4797 buf[i++] = 0x01;
4798
4799 /* Use the SCID from <pkt> for Retry DCID. */
4800 buf[i++] = pkt->scid.len;
4801 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4802 i += pkt->scid.len;
4803
4804 /* Generate a new CID to be used as SCID for the Retry packet. */
4805 scid.len = QUIC_HAP_CID_LEN;
4806 if (RAND_bytes(scid.data, scid.len) != 1)
4807 return 1;
4808
4809 buf[i++] = scid.len;
4810 memcpy(&buf[i], scid.data, scid.len);
4811 i += scid.len;
4812
4813 /* token */
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004814 if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004815 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004816
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004817 i += token_len;
4818
4819 /* token integrity tag */
4820 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4821 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4822 pkt->dcid.len, buf, i)) {
4823 return 1;
4824 }
4825
4826 i += QUIC_TLS_TAG_LEN;
4827
4828 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4829 return 1;
4830
4831 return 0;
4832}
4833
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004834/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4835 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4836 * concatenated to the <pkt> DCID field.
4837 *
4838 * Returns the instance or NULL if not found.
4839 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004840static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004841 struct listener *l,
4842 struct sockaddr_storage *saddr)
4843{
4844 struct quic_conn *qc = NULL;
4845 struct ebmb_node *node;
4846 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004847 /* set if the quic_conn is found in the second DCID tree */
4848 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004849
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004850 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4851 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4852 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4853 /* DCIDs of first packets coming from multiple clients may have
4854 * the same values. Let's distinguish them by concatenating the
4855 * socket addresses.
4856 */
4857 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004858 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004859 pkt->dcid.len + pkt->dcid.addrlen);
4860 if (node) {
4861 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4862 goto end;
4863 }
4864 }
4865
4866 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4867 * also for INITIAL/0-RTT non-first packets with the final DCID in
4868 * used.
4869 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004870 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004871 if (!node)
4872 goto end;
4873
4874 id = ebmb_entry(node, struct quic_connection_id, node);
4875 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004876 found_in_dcid = 1;
4877
4878 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004879 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4880 * If already done, this is a noop.
4881 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004882 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004883 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004884
4885 return qc;
4886}
4887
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004888/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4889 * will extract the parameters stored in the token : <odcid>.
4890 *
4891 * Returns 0 on success else non-zero.
4892 */
4893static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4894 struct quic_cid *odcid)
4895{
4896 uint64_t odcid_len;
4897
4898 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4899 return 1;
4900
Frédéric Lécaillef1f812b2022-03-17 16:22:02 +01004901 if (odcid_len > QUIC_CID_MAXLEN)
4902 return 1;
4903
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004904 memcpy(odcid->data, token, odcid_len);
4905 odcid->len = odcid_len;
4906
4907 return 0;
4908}
4909
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004910/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4911 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4912 * parameters of this session.
4913 * This is the responsibility of the caller to check the validity of all the
4914 * pointers passed as parameter to this function.
4915 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4916 * CO_ER_SSL_NO_MEM.
4917 */
4918static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4919 unsigned char *params, size_t params_len)
4920{
4921 int retry;
4922
4923 retry = 1;
4924 retry:
4925 *ssl = SSL_new(ssl_ctx);
4926 if (!*ssl) {
4927 if (!retry--)
4928 goto err;
4929
4930 pool_gc(NULL);
4931 goto retry;
4932 }
4933
4934 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4935 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4936 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004937 SSL_free(*ssl);
4938 *ssl = NULL;
4939 if (!retry--)
4940 goto err;
4941
4942 pool_gc(NULL);
4943 goto retry;
4944 }
4945
4946 return 0;
4947
4948 err:
4949 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4950 return -1;
4951}
4952
4953/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4954 * used to process <qc> received packets. The allocated context is stored in
4955 * <qc.xprt_ctx>.
4956 *
4957 * Returns 0 on success else non-zero.
4958 */
4959int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4960{
4961 struct bind_conf *bc = qc->li->bind_conf;
4962 struct ssl_sock_ctx *ctx = NULL;
4963
4964 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4965 if (!ctx)
4966 goto err;
4967
4968 ctx->wait_event.tasklet = tasklet_new();
4969 if (!ctx->wait_event.tasklet)
4970 goto err;
4971
4972 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4973 ctx->wait_event.tasklet->context = ctx;
4974 ctx->wait_event.events = 0;
4975 ctx->subs = NULL;
4976 ctx->xprt_ctx = NULL;
4977 ctx->qc = qc;
4978
4979 /* Set tasklet tid based on the SCID selected by us for this
4980 * connection. The upper layer will also be binded on the same thread.
4981 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004982 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004983
4984 if (qc_is_listener(qc)) {
4985 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4986 qc->enc_params, qc->enc_params_len) == -1) {
4987 goto err;
4988 }
4989
4990 /* Enabling 0-RTT */
4991 if (bc->ssl_conf.early_data)
4992 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4993
4994 SSL_set_accept_state(ctx->ssl);
4995 }
4996
4997 ctx->xprt = xprt_get(XPRT_QUIC);
4998
4999 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005000 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005001
5002 return 0;
5003
5004 err:
5005 if (ctx && ctx->wait_event.tasklet)
5006 tasklet_free(ctx->wait_event.tasklet);
5007 pool_free(pool_head_quic_conn_ctx, ctx);
5008
5009 return 1;
5010}
5011
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005012/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5013 * end of this buffer past one byte and populate <pkt> RX packet structure
5014 * with the information collected from the packet.
5015 * This function sets ->len <pkt> field value to the end of the packet past one
5016 * byte to enable the caller to run this function again to continue to parse
5017 * the remaing QUIC packets carried by the datagram.
5018 * Note that this function always sets this ->len value. If a paquet could
5019 * not be correctly found, ->len value will be set to the remaining number
5020 * bytes in the datagram to entirely consume this latter.
5021 */
5022static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5023 struct quic_rx_packet *pkt, int first_pkt,
5024 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005025{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005026 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005027 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005028 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005029 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005030 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005031 size_t b_cspace;
5032 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005033
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005034 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005035 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005036 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005037 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005038 TRACE_ENTER(QUIC_EV_CONN_LPKT);
5039 /* This ist only to please to traces and distinguish the
5040 * packet with parsed packet number from others.
5041 */
5042 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005043 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005044 goto err;
5045
5046 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005047 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005048 /* XXX TO BE DISCARDED */
5049 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5050 goto err;
5051 }
5052
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005053 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005054 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005055 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005056 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005057 uint64_t len;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005058 int drop_no_con = 0;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005059
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005060 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005061 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5062 goto err;
5063 }
5064
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005065 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5066 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
5067 drop_no_con = 1;
5068 }
5069 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5070 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005071 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
5072 drop_no_con = 1;
5073 }
5074
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005075 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5076 * they must have the same DCID.
5077 */
5078 if (!first_pkt &&
5079 (pkt->dcid.len != dgram->dcid_len ||
5080 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5081 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
5082 goto err;
5083 }
5084
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005085 /* Retry of Version Negotiation packets are only sent by servers */
5086 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
5087 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5088 goto err;
5089 }
5090
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005091 /* RFC9000 6. Version Negotiation */
5092 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005093 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005094 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005095 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
5096 goto err;
5097 }
5098
5099 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005100 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005101 }
5102
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005103 /* For Initial packets, and for servers (QUIC clients connections),
5104 * there is no Initial connection IDs storage.
5105 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005106 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005107 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005108
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005109 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5110 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005111 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5112 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005113 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005114
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01005115 /* The token may be provided in a Retry packet or NEW_TOKEN frame
5116 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005117 */
5118 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005119
5120 /* TODO Retry should be automatically activated if
5121 * suspect network usage is detected.
5122 */
5123 if (!token_len && l->bind_conf->quic_force_retry) {
5124 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005125 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005126 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
5127 goto err;
5128 }
5129
5130 goto err;
5131 }
5132 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005133 pkt->token = buf;
5134 buf += pkt->token_len;
5135 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005137 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005138 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005139 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5140 goto err;
5141 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005142 }
5143
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005144 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5145 end - buf < len) {
5146 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5147 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005148 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005149
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005150 payload = buf;
5151 pkt->len = len + payload - beg;
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005152 if (drop_no_con)
5153 goto drop_no_con;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005154
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005155 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005156 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005157 int ipv4;
5158 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005159 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005160 const unsigned char *salt = initial_salt_v1;
5161 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005162
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005163 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01005164 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005165 goto err;
5166 }
5167
Frédéric Lécailledc364042022-01-27 16:51:54 +01005168 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
5169 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
5170 goto err;
5171 }
5172
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005173 pkt->saddr = dgram->saddr;
5174 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005175 qc = qc_new_conn(pkt->version, ipv4,
5176 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02005177 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005178 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005179 goto err;
5180
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01005181 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
5182
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005183 odcid = &qc->rx.params.original_destination_connection_id;
5184 /* Copy the transport parameters. */
5185 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005186
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005187 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005188 if (pkt->token_len) {
5189 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
5190 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
5191 goto err;
5192 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01005193 /* Copy retry_source_connection_id transport parameter. */
5194 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
5195 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005196 }
5197 else {
5198 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
5199 odcid->len = pkt->dcid.len;
5200 }
5201
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005202 /* Copy the initial source connection ID. */
5203 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5204 qc->enc_params_len =
5205 quic_transport_params_encode(qc->enc_params,
5206 qc->enc_params + sizeof qc->enc_params,
5207 &qc->rx.params, 1);
5208 if (!qc->enc_params_len)
5209 goto err;
5210
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005211 if (qc_conn_alloc_ssl_ctx(qc))
5212 goto err;
5213
Frédéric Lécaille789413c2022-01-31 10:16:18 +01005214 if (!quic_conn_init_timer(qc))
5215 goto err;
5216
Frédéric Lécaille530601c2022-03-10 15:11:57 +01005217 if (!quic_conn_init_idle_timer_task(qc))
5218 goto err;
5219
Frédéric Lécaille497fa782021-05-31 15:16:13 +02005220 /* NOTE: the socket address has been concatenated to the destination ID
5221 * chosen by the client for Initial packets.
5222 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005223 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
5224 salt = initial_salt_draft_29;
5225 salt_len = sizeof initial_salt_draft_29;
5226 }
5227 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005228 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005229 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02005230 goto err;
5231 }
5232
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005233 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005234 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005235 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005236
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005237 /* If the insertion failed, it means that another
5238 * thread has already allocated a QUIC connection for
5239 * the same CID. Liberate our allocated connection.
5240 */
5241 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005242 qc_to_purge = qc;
5243
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005244 qc = ebmb_entry(n, struct quic_conn, odcid_node);
5245 pkt->qc = qc;
5246 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005247
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005248 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005249 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01005250 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005251 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005252 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01005253 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005254 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005255 }
5256 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005257 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005258 }
5259 }
5260 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005261 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005262 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5263 goto err;
5264 }
5265
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005266 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005267 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005268
5269 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5270 * they must have the same DCID.
5271 */
5272 if (!first_pkt &&
5273 (pkt->dcid.len != dgram->dcid_len ||
5274 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5275 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
5276 goto err;
5277 }
5278
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005279 buf += QUIC_HAP_CID_LEN;
5280
5281 /* A short packet is the last one of a UDP datagram. */
5282 payload = buf;
5283 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005284
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005285 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005286 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005287 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005288 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
5289 goto err;
5290 }
5291
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005292 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005293 }
5294
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005295 if (qc->flags & QUIC_FL_CONN_CLOSING) {
5296 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
5297 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
5298 qc->nb_pkt_for_cc++;
5299 qc->nb_pkt_since_cc = 0;
5300 }
5301 /* Skip the entire datagram */
5302 pkt->len = end - beg;
5303 TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc);
5304 goto out;
5305 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005306
5307 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5308 * they must have the same DCID.
5309 *
5310 * This check must be done after the final update to pkt.len to
5311 * properly drop the packet on failure.
5312 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005313 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005314 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005315 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
5316 QUIC_EV_CONN_LPKT, qc);
5317 /* Reset the anti-amplification bit. It will be set again
5318 * when sending the next packet if reached again.
5319 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005320 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
5321 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005322 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005323 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005324
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005325 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005326
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005327 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005328 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005329 goto out;
5330 }
5331
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005332 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005333 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005334 b_cspace = b_contig_space(&qc->rx.buf);
5335 if (b_cspace < pkt->len) {
5336 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005337 if (b_cspace) {
5338 b_putchr(&qc->rx.buf, 0x00);
5339 b_cspace--;
5340 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005341 b_add(&qc->rx.buf, b_cspace);
5342 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005343 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01005344 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005345 goto err;
5346 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005347 }
5348
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005349 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005350 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02005351 goto err;
5352 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005353
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005354 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005355 if (pkt->aad_len)
5356 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005357 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02005358 /* Wake up the connection packet handler task from here only if all
5359 * the contexts have been initialized, especially the mux context
5360 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
5361 * will start it if these contexts for the connection are not already
5362 * initialized.
5363 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005364 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005365 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005366 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005367
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005368 drop_no_con:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005369 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005370
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005371 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005372
5373 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005374 /* Wakeup the I/O handler callback if the PTO timer must be armed.
5375 * This cannot be done by this thread.
5376 */
5377 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005378 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005379 if (conn_ctx && conn_ctx->wait_event.tasklet)
5380 tasklet_wakeup(conn_ctx->wait_event.tasklet);
5381 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01005382 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005383 if (!pkt->len)
5384 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005385 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005386 qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005387}
5388
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005389/* This function builds into <buf> buffer a QUIC long packet header.
5390 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005391 */
5392static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
5393 int type, size_t pn_len, struct quic_conn *conn)
5394{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005395 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005396 return 0;
5397
5398 /* #0 byte flags */
5399 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5400 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5401 /* Version */
5402 quic_write_uint32(buf, end, conn->version);
5403 *(*buf)++ = conn->dcid.len;
5404 /* Destination connection ID */
5405 if (conn->dcid.len) {
5406 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5407 *buf += conn->dcid.len;
5408 }
5409 /* Source connection ID */
5410 *(*buf)++ = conn->scid.len;
5411 if (conn->scid.len) {
5412 memcpy(*buf, conn->scid.data, conn->scid.len);
5413 *buf += conn->scid.len;
5414 }
5415
5416 return 1;
5417}
5418
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005419/* This function builds into <buf> buffer a QUIC short packet header.
5420 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005421 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005422static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5423 size_t pn_len, struct quic_conn *conn,
5424 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005425{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005426 if (end - *buf < 1 + conn->dcid.len)
5427 return 0;
5428
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005429 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005430 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5431 ((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 +01005432 /* Destination connection ID */
5433 if (conn->dcid.len) {
5434 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5435 *buf += conn->dcid.len;
5436 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005437
5438 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005439}
5440
5441/* Apply QUIC header protection to the packet with <buf> as first byte address,
5442 * <pn> as address of the Packet number field, <pnlen> being this field length
5443 * with <aead> as AEAD cipher and <key> as secret key.
5444 * Returns 1 if succeeded or 0 if failed.
5445 */
5446static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5447 const EVP_CIPHER *aead, const unsigned char *key)
5448{
5449 int i, ret, outlen;
5450 EVP_CIPHER_CTX *ctx;
5451 /* We need an IV of at least 5 bytes: one byte for bytes #0
5452 * and at most 4 bytes for the packet number
5453 */
5454 unsigned char mask[5] = {0};
5455
5456 ret = 0;
5457 ctx = EVP_CIPHER_CTX_new();
5458 if (!ctx)
5459 return 0;
5460
5461 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5462 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5463 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5464 goto out;
5465
5466 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5467 for (i = 0; i < pnlen; i++)
5468 pn[i] ^= mask[i + 1];
5469
5470 ret = 1;
5471
5472 out:
5473 EVP_CIPHER_CTX_free(ctx);
5474
5475 return ret;
5476}
5477
5478/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5479 * ACK ranges if needed to a value below <limit> in bytes.
5480 * Return 1 if succeeded, 0 if not.
5481 */
5482static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5483{
5484 size_t room, ack_delay_sz;
5485
5486 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5487 /* A frame is made of 1 byte for the frame type. */
5488 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005489 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005490 return 0;
5491
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005492 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005493}
5494
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005495/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5496 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5497 * with <room> as available room, and <*len> the packet Length field initialized
5498 * with the number of bytes already present in this buffer which must be taken
5499 * into an account for the Length packet field value. <headlen> is the number of
5500 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005501 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005502 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005503 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005504 * 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 +01005505 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005506static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005507 size_t room, size_t *len, size_t headlen,
5508 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005509 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005510{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005511 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005512 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005513
Frédéric Lécailleea604992020-12-24 13:01:37 +01005514 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005515 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005516 return 0;
5517
Frédéric Lécailleea604992020-12-24 13:01:37 +01005518 /* If we are not probing we must take into an account the congestion
5519 * control window.
5520 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005521 if (!qel->pktns->tx.pto_probe) {
5522 size_t remain = quic_path_prep_data(qc->path);
5523
5524 if (headlen > remain)
5525 return 0;
5526
5527 room = QUIC_MIN(room, remain - headlen);
5528 }
5529
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005530 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005531 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005532
5533 /* NOTE: switch/case block inside a loop, a successful status must be
5534 * returned by this function only if at least one frame could be built
5535 * in the switch/case block.
5536 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005537 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005538 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005539 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005540
Frédéric Lécailleea604992020-12-24 13:01:37 +01005541 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005542 break;
5543
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005544 switch (cf->type) {
5545 case QUIC_FT_CRYPTO:
5546 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005547 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005548 /* Compute the length of this CRYPTO frame header */
5549 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5550 /* Compute the data length of this CRyPTO frame. */
5551 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5552 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005553 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005554 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005555 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005556
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005557 /* CRYPTO frame length. */
5558 flen = hlen + quic_int_getsize(dlen) + dlen;
5559 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005560 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005561 /* Add the CRYPTO data length and its encoded length to the packet
5562 * length and the length of this length.
5563 */
5564 *len += flen;
5565 room -= flen;
5566 if (dlen == cf->crypto.len) {
5567 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005568 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005569 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005570 }
5571 else {
5572 struct quic_frame *new_cf;
5573
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005574 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005575 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005576 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005577 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005578 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005579
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005580 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005581 new_cf->type = QUIC_FT_CRYPTO;
5582 new_cf->crypto.len = dlen;
5583 new_cf->crypto.offset = cf->crypto.offset;
5584 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005585 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005586 /* Consume <dlen> bytes of the current frame. */
5587 cf->crypto.len -= dlen;
5588 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005589 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005590 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005591
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005592 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005593 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
5594 struct eb64_node *node = NULL;
5595 struct qc_stream_desc *stream_desc = NULL;
5596 struct quic_stream *strm = &cf->stream;
5597
5598 /* As this frame has been already lost, ensure the stream is always
5599 * available or the range of this frame is not consumed before
5600 * resending it.
5601 */
5602 node = eb64_lookup(&qc->streams_by_id, strm->id);
5603 if (!node) {
5604 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, strm);
5605 LIST_DELETE(&cf->list);
5606 pool_free(pool_head_quic_frame, cf);
5607 continue;
5608 }
5609
5610 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
5611 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
5612 TRACE_PROTO("ignored frame frame in already acked range",
5613 QUIC_EV_CONN_PRSAFRM, qc, strm);
5614 LIST_DELETE(&cf->list);
5615 pool_free(pool_head_quic_frame, cf);
5616 continue;
5617 }
5618 else if (strm->offset.key < stream_desc->ack_offset) {
5619 strm->offset.key = stream_desc->ack_offset;
5620 TRACE_PROTO("updated partially acked frame",
5621 QUIC_EV_CONN_PRSAFRM, qc, strm);
5622 }
5623 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005624 /* Note that these frames are accepted in short packets only without
5625 * "Length" packet field. Here, <*len> is used only to compute the
5626 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005627 *
5628 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005629 * excepting the variable ones. Note that +1 is for the type of this frame.
5630 */
5631 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005632 ((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 +02005633 /* Compute the data length of this STREAM frame. */
5634 avail_room = room - hlen - *len;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005635 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005636 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005637 if ((ssize_t)avail_room <= 0)
5638 continue;
5639
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005640 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5641 dlen = max_available_room(avail_room, &dlen_sz);
5642 if (dlen > cf->stream.len) {
5643 dlen = cf->stream.len;
5644 }
5645 dlen_sz = quic_int_getsize(dlen);
5646 flen = hlen + dlen_sz + dlen;
5647 }
5648 else {
5649 dlen = QUIC_MIN(avail_room, cf->stream.len);
5650 flen = hlen + dlen;
5651 }
5652 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005653 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005654 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005655 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005656 /* Add the STREAM data length and its encoded length to the packet
5657 * length and the length of this length.
5658 */
5659 *len += flen;
5660 room -= flen;
5661 if (dlen == cf->stream.len) {
5662 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005663 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005664 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005665
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005666 /* The MUX stream might be released at this
5667 * stage. This can most notably happen on
5668 * retransmission.
5669 */
5670 if (qc->mux_state == QC_MUX_READY &&
5671 !cf->stream.stream->release) {
5672 qcc_streams_sent_done(cf->stream.stream->ctx,
5673 cf->stream.len,
5674 cf->stream.offset.key);
5675 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005676 }
5677 else {
5678 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005679 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005680
5681 new_cf = pool_zalloc(pool_head_quic_frame);
5682 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005683 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005684 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005685 }
5686
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005687 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005688 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005689 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005690 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005691 new_cf->stream.id = cf->stream.id;
5692 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5693 new_cf->stream.offset = cf->stream.offset;
5694 new_cf->stream.len = dlen;
5695 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5696 /* FIN bit reset */
5697 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5698 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005699 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005700 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5701 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005702 cf_buf = b_make(b_orig(cf->stream.buf),
5703 b_size(cf->stream.buf),
5704 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005705 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005706 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005707 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005708
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005709 /* The MUX stream might be released at this
5710 * stage. This can most notably happen on
5711 * retransmission.
5712 */
5713 if (qc->mux_state == QC_MUX_READY &&
5714 !cf->stream.stream->release) {
5715 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5716 new_cf->stream.len,
5717 new_cf->stream.offset.key);
5718 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005719 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005720
5721 /* TODO the MUX is notified about the frame sending via
5722 * previous qcc_streams_sent_done call. However, the
5723 * sending can fail later, for example if the sendto
5724 * system call returns an error. As the MUX has been
5725 * notified, the transport layer is responsible to
5726 * bufferize and resent the announced data later.
5727 */
5728
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005729 break;
5730
5731 default:
5732 flen = qc_frm_len(cf);
5733 BUG_ON(!flen);
5734 if (flen > room)
5735 continue;
5736
5737 *len += flen;
5738 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005739 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005740 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005741 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005742 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005743
5744 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01005745 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005746 }
5747
Frédéric Lécailleea604992020-12-24 13:01:37 +01005748 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005749}
5750
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005751/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005752 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5753 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005754 * filling the buffer with as much frames as possible from <frms> list of
5755 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005756 * 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 +02005757 * 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 +02005758 * having returned from this function.
5759 * 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 +01005760 * number field in this packet. <pn_len> will also have the packet number
5761 * length as value.
5762 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005763 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005764 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005765static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5766 size_t dglen, struct quic_tx_packet *pkt,
5767 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005768 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005769 struct quic_enc_level *qel, struct quic_conn *qc,
5770 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005771{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005772 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005773 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005774 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5775 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005776 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005777 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005778 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005779 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005780 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005781 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005782
Frédéric Lécailleea604992020-12-24 13:01:37 +01005783 /* Length field value with CRYPTO frames if present. */
5784 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005785 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005786 /* When not probing, and no immediate close is required, reduce the size of this
5787 * buffer to respect the congestion controller window.
5788 * 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 +01005789 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005790 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005791 size_t path_room;
5792
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005793 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005794 if (end - beg > path_room)
5795 end = beg + path_room;
5796 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005797
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005798 /* Ensure there is enough room for the TLS encryption tag and a zero token
5799 * length field if any.
5800 */
5801 if (end - pos < QUIC_TLS_TAG_LEN +
5802 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5803 goto no_room;
5804
5805 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005806 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005807 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005808 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005809 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005810 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005811 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005812 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005813 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005814 goto no_room;
5815
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005816 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5817 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005818 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005819 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005820 /* Build an ACK frame if required. */
5821 ack_frm_len = 0;
Frédéric Lécaille337108e2022-04-25 10:38:27 +02005822 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005823 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005824 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005825 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005826 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5827 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5828 * that from here, we do not know if we will have to send a PING frame.
5829 * This will be decided after having computed the ack-eliciting frames
5830 * to be added to this packet.
5831 */
5832 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 +01005833 if (!ack_frm_len)
5834 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005835 }
5836
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005837 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005838 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005839 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005840 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005841 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005842
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005843 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005844 /* Initialize the length of the frames built below to <len>.
5845 * If any frame could be successfully built by qc_build_frms(),
5846 * we will have len_frms > len.
5847 */
5848 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005849 if (!qc_build_frms(&frm_list, frms,
5850 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005851 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005852 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02005853 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
5854 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005855 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005856 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005857
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005858 /* Length (of the remaining data). Must not fail because, the buffer size
5859 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5860 * for the encryption tag. It must be taken into an account for the length
5861 * of this packet.
5862 */
5863 if (len_frms)
5864 len = len_frms + QUIC_TLS_TAG_LEN;
5865 else
5866 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005867 /* CONNECTION_CLOSE frame */
5868 if (cc) {
5869 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005870
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005871 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005872 len += qc_frm_len(&cc_frm);
5873 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005874 add_ping_frm = 0;
5875 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005876 len_sz = quic_int_getsize(len);
5877 /* Add this packet size to <dglen> */
5878 dglen += head_len + len_sz + len;
5879 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5880 /* This is a maximum padding size */
5881 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5882 /* The length field value is of this packet is <len> + <padding_len>
5883 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005884 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005885 * sizes from <padding_len>.
5886 */
5887 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5888 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005889 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005890 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005891 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005892 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005893 add_ping_frm = 1;
5894 len += 1;
5895 }
5896 /* 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 +01005897 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005898 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5899 }
5900
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005901 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5902 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005903
5904 /* Packet number field address. */
5905 *buf_pn = pos;
5906
5907 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005908 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5909 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005910
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005911 if (ack_frm_len) {
5912 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
5913 goto no_room;
5914
5915 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005916 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005917 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005918
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005919 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005920 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005921 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005922 unsigned char *spos = pos;
5923
5924 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005925 ssize_t room = end - pos;
5926 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005927 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005928 /* TODO: this should not have happened if qc_build_frms()
5929 * had correctly computed and sized the frames to be
5930 * added to this packet. Note that <cf> was added
5931 * from <frm_list> to <frms> list by qc_build_frms().
5932 */
5933 LIST_DELETE(&cf->list);
5934 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005935 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005936 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005937
5938 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005939 quic_tx_packet_refinc(pkt);
5940 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005941 }
5942 }
5943
5944 /* Build a PING frame if needed. */
5945 if (add_ping_frm) {
5946 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005947 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005948 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005949 }
5950
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005951 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005952 if (cc) {
5953 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
5954 goto no_room;
5955
5956 pkt->flags |= QUIC_FL_TX_PACKET_CC;
5957 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005958
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005959 /* Build a PADDING frame if needed. */
5960 if (padding_len) {
5961 frm.type = QUIC_FT_PADDING;
5962 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005963 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005964 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005965 }
5966
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005967 /* If this packet is ack-eliciting and we are probing let's
5968 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005969 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005970 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5971 qel->pktns->tx.pto_probe)
5972 qel->pktns->tx.pto_probe--;
5973
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005974 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005975 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005976 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005977
5978 return 1;
5979
5980 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005981 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005982 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailled8b798d2022-04-25 17:48:40 +02005983 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005984
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005985 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005986}
5987
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005988static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005989{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005990 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005991 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005992 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005993 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005994 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005995 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005996 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005997 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005998 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005999 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006000}
6001
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006002/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006003 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6004 * of prebuilt frames.
6005 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006006 * Return -2 if the packet could not be allocated or encrypted for any reason,
6007 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006008 * XXX NOTE XXX
6009 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6010 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6011 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006012 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006013static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6014 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006015 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006016 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006017 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006018{
6019 /* The pointer to the packet number field. */
6020 unsigned char *buf_pn;
6021 unsigned char *beg, *end, *payload;
6022 int64_t pn;
6023 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006024 struct quic_tls_ctx *tls_ctx;
6025 struct quic_tx_packet *pkt;
6026
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006027 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006028 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006029 pkt = pool_alloc(pool_head_quic_tx_packet);
6030 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006031 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006032 *err = -2;
6033 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006034 }
6035
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006036 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006037 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006038 pn_len = 0;
6039 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006040
6041 pn = qel->pktns->tx.next_pn + 1;
6042 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 +02006043 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006044 *err = -1;
6045 goto err;
6046 }
6047
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006048 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006049 payload = buf_pn + pn_len;
6050 payload_len = end - payload;
6051 aad_len = payload - beg;
6052
6053 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006054 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006055 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006056 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006057 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006058
6059 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006060 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006061 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
6062 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006063 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006064 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006065 goto err;
6066 }
6067
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006068 /* Consume a packet number */
6069 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006070 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01006071 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006072 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006073 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6074 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006075 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006076 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006077 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006078 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006079 pkt->in_flight_len = pkt->len;
6080 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006081 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006082 /* Always reset this flags */
6083 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006084 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6085 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6086 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6087 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006088
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006089 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006090 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006091
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006092 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006093
6094 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006095 /* TODO: what about the frames which have been built
6096 * for this packet.
6097 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006098 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006099 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006100 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006101}
6102
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006103
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006104/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6105 * event subscriber <es> is not allowed to change from a previous call as long
6106 * as at least one event is still subscribed. The <event_type> must only be a
6107 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6108 */
6109static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6110{
Willy Tarreau784b8682022-04-11 14:18:10 +02006111 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006112
6113 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6114 BUG_ON(qcc->subs && qcc->subs != es);
6115
6116 es->events |= event_type;
6117 qcc->subs = es;
6118
6119 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02006120 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006121
6122 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02006123 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006124
6125 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006126}
6127
6128/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6129 * The <es> pointer is not allowed to differ from the one passed to the
6130 * subscribe() call. It always returns zero.
6131 */
6132static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6133{
6134 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
6135}
6136
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006137/* Store in <xprt_ctx> the context attached to <conn>.
6138 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006139 */
6140static int qc_conn_init(struct connection *conn, void **xprt_ctx)
6141{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01006142 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006143
6144 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
6145
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006146 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006147 if (*xprt_ctx)
6148 goto out;
6149
Willy Tarreau784b8682022-04-11 14:18:10 +02006150 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006151
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006152 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01006153 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006154
6155 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006156}
6157
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006158/* Start the QUIC transport layer */
6159static int qc_xprt_start(struct connection *conn, void *ctx)
6160{
6161 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02006162 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006163
Willy Tarreau784b8682022-04-11 14:18:10 +02006164 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006165 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
6166 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02006167 /* prepare a CONNECTION_CLOSE frame */
6168 qc->err_code = QC_ERR_APPLICATION_ERROR;
6169 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle05d4ae62022-04-13 17:40:26 +02006170 return -1;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006171 }
6172
6173 /* mux-quic can now be considered ready. */
6174 qc->mux_state = QC_MUX_READY;
6175
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006176 tasklet_wakeup(qctx->wait_event.tasklet);
6177 return 1;
6178}
6179
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006180static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
6181{
Willy Tarreau784b8682022-04-11 14:18:10 +02006182 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006183 return NULL;
6184
Willy Tarreau784b8682022-04-11 14:18:10 +02006185 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006186}
6187
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006188/* transport-layer operations for QUIC connections. */
6189static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02006190 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006191 .subscribe = quic_conn_subscribe,
6192 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006193 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006194 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006195 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
6196 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01006197 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006198 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006199 .name = "QUIC",
6200};
6201
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006202static void __quic_conn_init(void)
6203{
6204 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
6205 xprt_register(XPRT_QUIC, &ssl_quic);
6206}
Willy Tarreau79367f92022-04-25 19:18:24 +02006207INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006208
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006209static void __quic_conn_deinit(void)
6210{
6211 BIO_meth_free(ha_quic_meth);
6212}
Willy Tarreau79367f92022-04-25 19:18:24 +02006213REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006214
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006215/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
6216 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05006217 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006218 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006219struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006220{
6221 unsigned char *pos;
6222 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006223 struct quic_dghdlr *dghdlr = ctx;
6224 struct quic_dgram *dgram;
6225 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006226
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006227 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006228 pos = dgram->buf;
6229 end = pos + dgram->len;
6230 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006231 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006232
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006233 pkt = pool_zalloc(pool_head_quic_rx_packet);
6234 if (!pkt)
6235 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006236
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006237 quic_rx_packet_refinc(pkt);
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006238 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006239 first_pkt = 0;
6240 pos += pkt->len;
6241 quic_rx_packet_refdec(pkt);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006242 } while (pos < end);
6243
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006244 /* Increasing the received bytes counter by the UDP datagram length
6245 * if this datagram could be associated to a connection.
6246 */
6247 if (dgram->qc)
6248 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01006249
6250 /* Mark this datagram as consumed */
6251 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006252 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006253
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006254 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006255
6256 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006257 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006258}
6259
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006260/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
6261 * Returns 1 if succeeded, 0 if not.
6262 */
6263static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
6264 unsigned char **dcid, size_t *dcid_len)
6265{
6266 int long_header;
6267 size_t minlen, skip;
6268
6269 if (!(*buf & QUIC_PACKET_FIXED_BIT))
6270 goto err;
6271
6272 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
6273 minlen = long_header ?
6274 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
6275 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01006276 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006277 goto err;
6278
6279 buf += skip;
6280 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
6281 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
6282 goto err;
6283
6284 *dcid = buf;
6285
6286 return 1;
6287
6288 err:
6289 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
6290 return 0;
6291}
6292
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01006293/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
6294 * correct datagram handler.
6295 * Return 1 if a correct datagram could be found, 0 if not.
6296 */
6297int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
6298 struct sockaddr_storage *saddr,
6299 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006300{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006301 struct quic_dgram *dgram;
6302 unsigned char *dcid;
6303 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01006304 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006305
6306 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006307 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006308
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01006309 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006310 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006311 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006312
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01006313 cid_tid = quic_get_cid_tid(dcid);
6314
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006315 /* All the members must be initialized! */
6316 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006317 dgram->buf = buf;
6318 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006319 dgram->dcid = dcid;
6320 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006321 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006322 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006323 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01006324 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006325
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01006326 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006327
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006328 return 1;
6329
6330 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006331 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006332}
6333
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02006334/* Notify the MUX layer if alive about an imminent close of <qc>. */
6335void qc_notify_close(struct quic_conn *qc)
6336{
6337 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
6338 return;
6339
6340 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
6341
6342 /* wake up the MUX */
6343 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
6344 qc->conn->mux->wake(qc->conn);
6345}
6346
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01006347/* Function to automatically activate QUIC traces on stdout.
6348 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
6349 * Main use for now is in the docker image for QUIC interop testing.
6350 */
6351static void quic_init_stdout_traces(void)
6352{
6353#ifdef ENABLE_QUIC_STDOUT_TRACES
6354 trace_quic.sink = sink_find("stdout");
6355 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01006356 trace_quic.state = TRACE_STATE_RUNNING;
6357#endif
6358}
6359INITCALL0(STG_INIT, quic_init_stdout_traces);
6360
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006361/*
6362 * Local variables:
6363 * c-indent-level: 8
6364 * c-basic-offset: 8
6365 * End:
6366 */