blob: c1a7be1a9c4544c802ab75d0f810e7e214fe77a0 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <import/ebmbtree.h>
26
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010027#include <haproxy/buf-t.h>
28#include <haproxy/compat.h>
29#include <haproxy/api.h>
30#include <haproxy/debug.h>
31#include <haproxy/tools.h>
32#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010033
34#include <haproxy/connection.h>
35#include <haproxy/fd.h>
36#include <haproxy/freq_ctr.h>
37#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010039#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010041#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010042#include <haproxy/pipe.h>
43#include <haproxy/proxy.h>
44#include <haproxy/quic_cc.h>
45#include <haproxy/quic_frame.h>
46#include <haproxy/quic_loss.h>
Amaury Denoyellecfa2d562022-01-19 16:01:05 +010047#include <haproxy/quic_sock.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020048#include <haproxy/quic_stream.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020049#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010050#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010051#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010052#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010053#include <haproxy/ssl_sock.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010054#include <haproxy/task.h>
55#include <haproxy/trace.h>
56#include <haproxy/xprt_quic.h>
57
Amaury Denoyellea22d8602021-11-10 15:17:56 +010058/* list of supported QUIC versions by this implementation */
59static int quic_supported_version[] = {
60 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010061 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010062
63 /* placeholder, do not add entry after this */
64 0x0
65};
66
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020067/* This is the values of some QUIC transport parameters when absent.
68 * Should be used to initialize any transport parameters (local or remote)
69 * before updating them with customized values.
70 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010071struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020072 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020073 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
74 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020075 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010076};
77
78/* trace source and events */
79static void quic_trace(enum trace_level level, uint64_t mask, \
80 const struct trace_source *src,
81 const struct ist where, const struct ist func,
82 const void *a1, const void *a2, const void *a3, const void *a4);
83
84static const struct trace_event quic_trace_events[] = {
85 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
86 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
87 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
88 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
89 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
90 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
91 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050092 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
94 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
95 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaille00e24002022-02-18 17:13:45 +010096 { .mask = QUIC_EV_CONN_IO_CB, .name = "qc_io_cb", .desc = "QUIC conn. I/O processin" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010097 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
98 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
99 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
100 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
101 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
102 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
103 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
104 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
105 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
106 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
107 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
108 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
109 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
110 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
111 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100112 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
113 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
114 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
115 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
116 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
117 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100118 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200119 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
120 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100121 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
122 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100123 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200124 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100125 { /* end */ }
126};
127
128static const struct name_desc quic_trace_lockon_args[4] = {
129 /* arg1 */ { /* already used by the connection */ },
130 /* arg2 */ { .name="quic", .desc="QUIC transport" },
131 /* arg3 */ { },
132 /* arg4 */ { }
133};
134
135static const struct name_desc quic_trace_decoding[] = {
136#define QUIC_VERB_CLEAN 1
137 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
138 { /* end */ }
139};
140
141
142struct trace_source trace_quic = {
143 .name = IST("quic"),
144 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100145 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100146 .default_cb = quic_trace,
147 .known_events = quic_trace_events,
148 .lockon_args = quic_trace_lockon_args,
149 .decoding = quic_trace_decoding,
150 .report_events = ~0, /* report everything by default */
151};
152
153#define TRACE_SOURCE &trace_quic
154INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
155
156static BIO_METHOD *ha_quic_meth;
157
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200158DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100159DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200161 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100163DECLARE_POOL(pool_head_quic_connection_id,
164 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100165DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100166DECLARE_POOL(pool_head_quic_rx_packet, "quic_rx_packet_pool", sizeof(struct quic_rx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100167DECLARE_POOL(pool_head_quic_tx_packet, "quic_tx_packet_pool", sizeof(struct quic_tx_packet));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100168DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100169DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100170DECLARE_STATIC_POOL(pool_head_quic_crypto_buf, "quic_crypto_buf_pool", sizeof(struct quic_crypto_buf));
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200171DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100172DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100173
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200174static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +0100175 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100176 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200177 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100178static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille47756802022-03-25 09:12:16 +0100179static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100180static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100181
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100182/* Only for debug purpose */
183struct enc_debug_info {
184 unsigned char *payload;
185 size_t payload_len;
186 unsigned char *aad;
187 size_t aad_len;
188 uint64_t pn;
189};
190
191/* Initializes a enc_debug_info struct (only for debug purpose) */
192static inline void enc_debug_info_init(struct enc_debug_info *edi,
193 unsigned char *payload, size_t payload_len,
194 unsigned char *aad, size_t aad_len, uint64_t pn)
195{
196 edi->payload = payload;
197 edi->payload_len = payload_len;
198 edi->aad = aad;
199 edi->aad_len = aad_len;
200 edi->pn = pn;
201}
202
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100203/* Trace callback for QUIC.
204 * These traces always expect that arg1, if non-null, is of type connection.
205 */
206static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
207 const struct ist where, const struct ist func,
208 const void *a1, const void *a2, const void *a3, const void *a4)
209{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100210 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100212 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100213 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100215 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 if ((mask & QUIC_EV_CONN_INIT) && qc) {
217 chunk_appendf(&trace_buf, "\n odcid");
218 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100219 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100220 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100221 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100222 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100223 }
224
225 if (mask & QUIC_EV_CONN_ADDDATA) {
226 const enum ssl_encryption_level_t *level = a2;
227 const size_t *len = a3;
228
229 if (level) {
230 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
231
232 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
233 }
234 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100235 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100236 }
237 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
238 /* Initial read & write secrets. */
239 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
240 const unsigned char *rx_sec = a2;
241 const unsigned char *tx_sec = a3;
242
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100243 tls_ctx = &qc->els[level].tls_ctx;
244 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100245 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
246 if (rx_sec)
247 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100248 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100249 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
250 if (tx_sec)
251 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100252 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100253 }
254 }
255 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
256 const enum ssl_encryption_level_t *level = a2;
257 const unsigned char *secret = a3;
258 const size_t *secret_len = a4;
259
260 if (level) {
261 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
262
263 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
264 if (secret && secret_len)
265 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100266 tls_ctx = &qc->els[lvl].tls_ctx;
267 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
268 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100269 }
270 }
271
272 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
273 const enum ssl_encryption_level_t *level = a2;
274 const unsigned char *secret = a3;
275 const size_t *secret_len = a4;
276
277 if (level) {
278 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
279
280 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
281 if (secret && secret_len)
282 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100283 tls_ctx = &qc->els[lvl].tls_ctx;
284 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
285 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100286 }
287
288 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200290 if (mask & QUIC_EV_CONN_FRMLIST) {
291 const struct list *l = a2;
292
293 if (l) {
294 const struct quic_frame *frm;
295 list_for_each_entry(frm, l, list)
296 chunk_frm_appendf(&trace_buf, frm);
297 }
298 }
299
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100300 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100301 const struct quic_tx_packet *pkt = a2;
302 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100303 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304
305 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100306 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100307 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100308 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100309 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100310 (unsigned long long)qc->path->cwnd,
311 (unsigned long long)qc->path->prep_in_flight,
312 (unsigned long long)qc->path->in_flight,
313 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100314 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100315 }
316 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200317 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100318 if (pkt->pn_node.key != (uint64_t)-1)
319 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100320 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100321 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100322 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
323 (unsigned long long)qc->rx.bytes,
324 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100325 }
326
327 if (room) {
328 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
329 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
330 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 }
332 }
333
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100334 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100335 const enum quic_handshake_state *state = a2;
336 const int *err = a3;
337
338 if (state)
339 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
340 if (err)
341 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
342 }
343
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100344 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100345 const struct quic_rx_packet *pkt = a2;
346 const unsigned long *pktlen = a3;
347 const SSL *ssl = a4;
348
349 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200350 chunk_appendf(&trace_buf, " pkt@%p", pkt);
351 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
352 chunk_appendf(&trace_buf, " kp=%d",
353 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
354 chunk_appendf(&trace_buf, " el=%c",
355 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100356 if (pkt->pnl)
357 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
358 (unsigned long long)pkt->pn);
359 if (pkt->token_len)
360 chunk_appendf(&trace_buf, " toklen=%llu",
361 (unsigned long long)pkt->token_len);
362 if (pkt->aad_len)
363 chunk_appendf(&trace_buf, " aadlen=%llu",
364 (unsigned long long)pkt->aad_len);
365 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
366 pkt->flags, (unsigned long long)pkt->len);
367 }
368 if (pktlen)
369 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
370 if (ssl) {
371 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
372 chunk_appendf(&trace_buf, " el=%c",
373 quic_enc_level_char(ssl_to_quic_enc_level(level)));
374 }
375 }
376
377 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
378 const struct quic_rx_packet *pkt = a2;
379 const struct quic_rx_crypto_frm *cf = a3;
380 const SSL *ssl = a4;
381
382 if (pkt)
383 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
384 quic_packet_type_enc_level_char(pkt->type),
385 (unsigned long long)pkt->pn);
386 if (cf)
387 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
388 (unsigned long long)cf->offset_node.key,
389 (unsigned long long)cf->len);
390 if (ssl) {
391 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200392 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100393 quic_enc_level_char(ssl_to_quic_enc_level(level)));
394 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100395
396 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100397 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100398 }
399
400 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
401 const struct quic_frame *frm = a2;
402
403 if (frm)
404 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
405 }
406
407 if (mask & QUIC_EV_CONN_PHPKTS) {
408 const struct quic_enc_level *qel = a2;
409
410 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100411 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100412 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100413 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100414 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200415 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200416 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100417 (unsigned long long)qc->path->cwnd,
418 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100419 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100420 (unsigned long long)pktns->tx.in_flight,
421 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100422 }
423 }
424
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100425 if (mask & QUIC_EV_CONN_ENCPKT) {
426 const struct enc_debug_info *edi = a2;
427
428 if (edi)
429 chunk_appendf(&trace_buf,
430 " payload=@%p payload_len=%llu"
431 " aad=@%p aad_len=%llu pn=%llu",
432 edi->payload, (unsigned long long)edi->payload_len,
433 edi->aad, (unsigned long long)edi->aad_len,
434 (unsigned long long)edi->pn);
435 }
436
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100437 if (mask & QUIC_EV_CONN_RMHP) {
438 const struct quic_rx_packet *pkt = a2;
439
440 if (pkt) {
441 const int *ret = a3;
442
443 chunk_appendf(&trace_buf, " pkt@%p", pkt);
444 if (ret && *ret)
445 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
446 pkt->pnl, (unsigned long long)pkt->pn);
447 }
448 }
449
450 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200451 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100452 const unsigned long *val1 = a3;
453 const unsigned long *val2 = a4;
454
455 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100456 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100457 if (val1)
458 chunk_appendf(&trace_buf, " %lu", *val1);
459 if (val2)
460 chunk_appendf(&trace_buf, "..%lu", *val2);
461 }
462
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100463 if (mask & QUIC_EV_CONN_ACKSTRM) {
464 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200465 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100466
467 if (s)
468 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200469 if (stream)
470 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100471 }
472
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100473 if (mask & QUIC_EV_CONN_RTTUPDT) {
474 const unsigned int *rtt_sample = a2;
475 const unsigned int *ack_delay = a3;
476 const struct quic_loss *ql = a4;
477
478 if (rtt_sample)
479 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
480 if (ack_delay)
481 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
482 if (ql)
483 chunk_appendf(&trace_buf,
484 " srtt=%ums rttvar=%ums min_rtt=%ums",
485 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
486 }
487 if (mask & QUIC_EV_CONN_CC) {
488 const struct quic_cc_event *ev = a2;
489 const struct quic_cc *cc = a3;
490
491 if (a2)
492 quic_cc_event_trace(&trace_buf, ev);
493 if (a3)
494 quic_cc_state_trace(&trace_buf, cc);
495 }
496
497 if (mask & QUIC_EV_CONN_PKTLOSS) {
498 const struct quic_pktns *pktns = a2;
499 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500
501 if (pktns) {
502 chunk_appendf(&trace_buf, " pktns=%s",
503 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
504 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
505 if (pktns->tx.loss_time)
506 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100507 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508 }
509 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
510 struct quic_tx_packet *pkt;
511
512 chunk_appendf(&trace_buf, " lost_pkts:");
513 list_for_each_entry(pkt, lost_pkts, list)
514 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
515 }
516 }
517
518 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100519 const struct quic_pktns *pktns = a2;
520 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100521 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100522
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100523 if (ifae_pkts)
524 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
525 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100526 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100527 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
530 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100531 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
532 if (pktns->tx.in_flight)
533 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 if (pktns->tx.loss_time)
535 chunk_appendf(&trace_buf, " loss_time=%dms",
536 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
537 }
538 if (mask & QUIC_EV_CONN_SPTO) {
539 if (pktns->tx.time_of_last_eliciting)
540 chunk_appendf(&trace_buf, " tole=%dms",
541 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
542 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100543 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100544 }
545 }
546
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200547 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100548 chunk_appendf(&trace_buf,
549 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
550 }
551 }
552
553 if (mask & QUIC_EV_CONN_SPPKTS) {
554 const struct quic_tx_packet *pkt = a2;
555
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100556 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
557 (unsigned long long)qc->path->cwnd,
558 (unsigned long long)qc->path->prep_in_flight,
559 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100560 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100561 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100562 (unsigned long)pkt->pn_node.key,
563 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
564 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100565 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100566 }
567 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100568
569 if (mask & QUIC_EV_CONN_SSLALERT) {
570 const uint8_t *alert = a2;
571 const enum ssl_encryption_level_t *level = a3;
572
573 if (alert)
574 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
575 if (level)
576 chunk_appendf(&trace_buf, " el=%c",
577 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
578 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100579
580 if (mask & QUIC_EV_CONN_BCFRMS) {
581 const size_t *sz1 = a2;
582 const size_t *sz2 = a3;
583 const size_t *sz3 = a4;
584
585 if (sz1)
586 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
587 if (sz2)
588 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
589 if (sz3)
590 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
591 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100592
593 if (mask & QUIC_EV_CONN_PSTRM) {
594 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100595
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100596 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100597 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100598 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100599 }
600 if (mask & QUIC_EV_CONN_LPKT) {
601 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200602 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100603
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100604 if (pkt) {
605 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
606 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
607 if (pkt->pn_node.key != (uint64_t)-1)
608 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
609 }
610
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200611 if (len)
612 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613 }
614
615}
616
617/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100618static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100619{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200620 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100621
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100622 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100623 return 1;
624
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200625 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
626 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200627 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
628 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200629 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100630 return 1;
631
632 return 0;
633}
634
635/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
636 * both loss detection and PTO and schedule the task assiated to this timer if needed.
637 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100638static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100639{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100640 struct quic_pktns *pktns;
641 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200642 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100643
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100644 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100645 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100646
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100647 pktns = quic_loss_pktns(qc);
648 if (tick_isset(pktns->tx.loss_time)) {
649 qc->timer = pktns->tx.loss_time;
650 goto out;
651 }
652
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100653 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100654 * cancelled for a server which reached the anti-amplification limit.
655 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100656 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200657 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100658 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100659 qc->timer = TICK_ETERNITY;
660 goto out;
661 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100662
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100663 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100664 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100665 /* Timer cancellation. */
666 qc->timer = TICK_ETERNITY;
667 goto out;
668 }
669
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200670 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200671 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100672 if (tick_isset(pto))
673 qc->timer = pto;
674 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100675 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100676 if (tick_is_expired(qc->timer, now_ms)) {
677 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100678 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100679 }
680 else {
681 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100682 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100683 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100684 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100685 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100686}
687
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100688/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
689 * connection.
690 * Return 1 if succeeded, 0 if not.
691 */
692static int quic_tls_key_update(struct quic_conn *qc)
693{
694 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
695 struct quic_tls_secrets *rx, *tx;
696 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
697 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
698
699 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
700 rx = &tls_ctx->rx;
701 tx = &tls_ctx->tx;
702 nxt_rx = &qc->ku.nxt_rx;
703 nxt_tx = &qc->ku.nxt_tx;
704
705 /* Prepare new RX secrets */
706 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
707 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100708 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100709 return 0;
710 }
711
712 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
713 nxt_rx->key, nxt_rx->keylen,
714 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
715 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100716 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100717 return 0;
718 }
719
720 /* Prepare new TX secrets */
721 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
722 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100723 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100724 return 0;
725 }
726
727 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
728 nxt_tx->key, nxt_tx->keylen,
729 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
730 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100731 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100732 return 0;
733 }
734
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200735 if (nxt_rx->ctx) {
736 EVP_CIPHER_CTX_free(nxt_rx->ctx);
737 nxt_rx->ctx = NULL;
738 }
739
740 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
741 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
742 return 0;
743 }
744
745 if (nxt_tx->ctx) {
746 EVP_CIPHER_CTX_free(nxt_tx->ctx);
747 nxt_tx->ctx = NULL;
748 }
749
750 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
751 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
752 return 0;
753 }
754
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100755 return 1;
756}
757
758/* Rotate the Key Update information for <qc> QUIC connection.
759 * Must be used after having updated them.
760 * Always succeeds.
761 */
762static void quic_tls_rotate_keys(struct quic_conn *qc)
763{
764 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
765 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200766 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100767
768 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200769 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100770 curr_secret = tls_ctx->rx.secret;
771 curr_iv = tls_ctx->rx.iv;
772 curr_key = tls_ctx->rx.key;
773
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200774 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100775 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
776 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
777 tls_ctx->rx.key = qc->ku.nxt_rx.key;
778
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200779 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100780 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
781 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
782 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
783
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200784 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100785 qc->ku.prv_rx.secret = curr_secret;
786 qc->ku.prv_rx.iv = curr_iv;
787 qc->ku.prv_rx.key = curr_key;
788 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
789
790 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200791 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100792 curr_secret = tls_ctx->tx.secret;
793 curr_iv = tls_ctx->tx.iv;
794 curr_key = tls_ctx->tx.key;
795
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200796 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100797 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
798 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
799 tls_ctx->tx.key = qc->ku.nxt_tx.key;
800
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200801 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100802 qc->ku.nxt_tx.secret = curr_secret;
803 qc->ku.nxt_tx.iv = curr_iv;
804 qc->ku.nxt_tx.key = curr_key;
805}
806
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100807#ifndef OPENSSL_IS_BORINGSSL
808int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
809 const uint8_t *read_secret,
810 const uint8_t *write_secret, size_t secret_len)
811{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100812 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
813 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 +0100814 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100815 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100816
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100817 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100818 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200819 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100820 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100821 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100822 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100823
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100824 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100825 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100826 goto err;
827 }
828
829 rx = &tls_ctx->rx;
830 tx = &tls_ctx->tx;
831
832 rx->aead = tx->aead = tls_aead(cipher);
833 rx->md = tx->md = tls_md(cipher);
834 rx->hp = tx->hp = tls_hp(cipher);
835
836 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
837 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100838 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100839 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100840 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100841 }
842
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200843 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
844 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
845 goto err;
846 }
847
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100848 /* Enqueue this connection asap if we could derive O-RTT secrets as
849 * listener. Note that a listener derives only RX secrets for this
850 * level.
851 */
852 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
853 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100854
855 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100856 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100857
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100858 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
859 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100860 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100861 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100862 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100863 }
864
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200865 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
866 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
867 goto err;
868 }
869
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100870 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100871 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
872 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
873 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
874
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200875 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100876 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
877 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100878 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100879 goto err;
880 }
881
882 memcpy(rx->secret, read_secret, secret_len);
883 rx->secretlen = secret_len;
884 memcpy(tx->secret, write_secret, secret_len);
885 tx->secretlen = secret_len;
886 /* Initialize all the secret keys lengths */
887 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
888 /* Prepare the next key update */
889 if (!quic_tls_key_update(qc))
890 goto err;
891 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100892
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100893 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100894 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100895 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100896 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100898
899 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100900 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100901 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100902}
903#else
904/* ->set_read_secret callback to derive the RX secrets at <level> encryption
905 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500906 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100907 */
908int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
909 const SSL_CIPHER *cipher,
910 const uint8_t *secret, size_t secret_len)
911{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100912 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100913 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100914 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100915
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100916 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200917 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100918 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100919 goto out;
920 }
921
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922 tls_ctx->rx.aead = tls_aead(cipher);
923 tls_ctx->rx.md = tls_md(cipher);
924 tls_ctx->rx.hp = tls_hp(cipher);
925
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100926 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
927 goto err;
928
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100929 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 +0100930 tls_ctx->rx.key, tls_ctx->rx.keylen,
931 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100932 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
933 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100934 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935 goto err;
936 }
937
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100938 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100939 const unsigned char *buf;
940 size_t buflen;
941
942 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
943 if (!buflen)
944 goto err;
945
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100946 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100947 goto err;
948 }
949
950 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100951 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100952 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100953
954 return 1;
955
956 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100957 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100958 return 0;
959}
960
961/* ->set_write_secret callback to derive the TX secrets at <level>
962 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500963 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100964 */
965int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
966 const SSL_CIPHER *cipher,
967 const uint8_t *secret, size_t secret_len)
968{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100969 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
970 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 +0100971
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100972 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200973 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100974 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100975 goto out;
976 }
977
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100978 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
979 goto err;
980
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100981 tls_ctx->tx.aead = tls_aead(cipher);
982 tls_ctx->tx.md = tls_md(cipher);
983 tls_ctx->tx.hp = tls_hp(cipher);
984
985 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 +0100986 tls_ctx->tx.key, tls_ctx->tx.keylen,
987 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100988 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
989 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100990 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100991 goto err;
992 }
993
994 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100995 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100996 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100997 return 1;
998
999 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001000 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001001 return 0;
1002}
1003#endif
1004
1005/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1006 * with <len> as size in CRYPTO buffers dedicated to store the information about
1007 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1008 * It fails only if it could not managed to allocate enough CRYPTO buffers to
1009 * store all the data.
1010 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1011 */
1012static int quic_crypto_data_cpy(struct quic_enc_level *qel,
1013 const unsigned char *data, size_t len)
1014{
1015 struct quic_crypto_buf **qcb;
1016 /* The remaining byte to store in CRYPTO buffers. */
1017 size_t cf_offset, cf_len, *nb_buf;
1018 unsigned char *pos;
1019
1020 nb_buf = &qel->tx.crypto.nb_buf;
1021 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1022 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1023 cf_len = len;
1024
1025 while (len) {
1026 size_t to_copy, room;
1027
1028 pos = (*qcb)->data + (*qcb)->sz;
1029 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1030 to_copy = len > room ? room : len;
1031 if (to_copy) {
1032 memcpy(pos, data, to_copy);
1033 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1034 qel->tx.crypto.sz += to_copy;
1035 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001036 len -= to_copy;
1037 data += to_copy;
1038 }
1039 else {
1040 struct quic_crypto_buf **tmp;
1041
1042 tmp = realloc(qel->tx.crypto.bufs,
1043 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1044 if (tmp) {
1045 qel->tx.crypto.bufs = tmp;
1046 qcb = &qel->tx.crypto.bufs[*nb_buf];
1047 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1048 if (!*qcb)
1049 return 0;
1050
1051 (*qcb)->sz = 0;
1052 ++*nb_buf;
1053 }
1054 else {
1055 break;
1056 }
1057 }
1058 }
1059
1060 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1061 * have been buffered.
1062 */
1063 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001064 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001065 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001066
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001067 /* There is at most one CRYPTO frame in this packet number
1068 * space. Let's look for it.
1069 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001070 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001071 if (frm->type != QUIC_FT_CRYPTO)
1072 continue;
1073
1074 /* Found */
1075 found = frm;
1076 break;
1077 }
1078
1079 if (found) {
1080 found->crypto.len += cf_len;
1081 }
1082 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001083 frm = pool_alloc(pool_head_quic_frame);
1084 if (!frm)
1085 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001086
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
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001432/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001433 *
1434 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001435 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001436static int quic_stream_try_to_consume(struct quic_conn *qc,
1437 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001438{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001439 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001440 struct eb64_node *frm_node;
1441
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001442 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001443 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001444 while (frm_node) {
1445 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001446 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001447 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001448
1449 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001450 offset = strm->offset.key;
1451 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001452
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001453 if (offset > stream->ack_offset)
1454 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001455
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001456 if (qc_stream_desc_ack(&stream, offset, len)) {
1457 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1458 qc, strm, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001459 ret = 1;
1460 }
1461
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001462 if (!stream)
1463 return 1;
1464
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001465 frm_node = eb64_next(frm_node);
1466 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001467
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001468 frm = container_of(strm, struct quic_frame, stream);
1469 LIST_DELETE(&frm->list);
1470 quic_tx_packet_refdec(frm->pkt);
1471 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001472 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001473
1474 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001475}
1476
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001477/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001478static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1479 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001480{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001481 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001482
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001483 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001484 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001485 switch (frm->type) {
1486 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1487 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001488 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001489 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001490 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001491 const size_t offset = strm_frm->offset.key;
1492 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001493
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001494 /* do not use strm_frm->stream as the qc_stream_desc instance
1495 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001496 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001497 *
1498 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001499 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001500 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001501 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1502 if (!node) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001503 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001504 LIST_DELETE(&frm->list);
1505 quic_tx_packet_refdec(frm->pkt);
1506 pool_free(pool_head_quic_frame, frm);
1507
1508 /* early return */
1509 return;
1510 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001511 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001512
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001513 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001514 if (offset <= stream->ack_offset) {
1515 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001516 stream_acked = 1;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001517 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1518 qc, strm_frm, stream);
1519 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001520
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001521 if (!stream) {
1522 /* no need to continue if stream freed. */
1523 TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
1524 LIST_DELETE(&frm->list);
1525 quic_tx_packet_refdec(frm->pkt);
1526 pool_free(pool_head_quic_frame, frm);
1527 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001528 }
1529
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001530 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001531 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001532 pool_free(pool_head_quic_frame, frm);
1533 }
1534 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001535 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001536 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001537
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001538 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001539 }
1540 break;
1541 default:
1542 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001543 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001544 pool_free(pool_head_quic_frame, frm);
1545 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001546
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001547 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001548 struct qcc *qcc = qc->qcc;
1549
1550 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1551 tasklet_wakeup(qcc->subs->tasklet);
1552 qcc->subs->events &= ~SUB_RETRY_SEND;
1553 if (!qcc->subs->events)
1554 qcc->subs = NULL;
1555 }
1556 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001557}
1558
1559/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1560 * deallocating them, and their TX frames.
1561 * Returns the last node reached to be used for the next range.
1562 * May be NULL if <largest> node could not be found.
1563 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001564static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1565 struct eb_root *pkts,
1566 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001567 struct list *newly_acked_pkts,
1568 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001569 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001570{
1571 struct eb64_node *node;
1572 struct quic_tx_packet *pkt;
1573
1574 if (largest_node)
1575 node = largest_node;
1576 else {
1577 node = eb64_lookup(pkts, largest);
1578 while (!node && largest > smallest) {
1579 node = eb64_lookup(pkts, --largest);
1580 }
1581 }
1582
1583 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001584 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585
1586 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1587 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001588 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001589 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001590 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001591 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001592 node = eb64_prev(node);
1593 eb64_delete(&pkt->pn_node);
1594 }
1595
1596 return node;
1597}
1598
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001599/* Remove all frames from <pkt_frm_list> and reinsert them in the
1600 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001602static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1603 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001604 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001606 struct quic_frame *frm, *frmbak;
1607 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001609 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1610 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001611 quic_tx_packet_refdec(frm->pkt);
1612 /* This frame is not freed but removed from its packet */
1613 frm->pkt = NULL;
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001614 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001615 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001616 }
1617
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001618 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001619}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001621/* Free <pkt> TX packet and its attached frames.
1622 * This is the responsability of the caller to remove this packet of
1623 * any data structure it was possibly attached to.
1624 */
1625static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1626{
1627 struct quic_frame *frm, *frmbak;
1628
1629 if (!pkt)
1630 return;
1631
1632 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1633 LIST_DELETE(&frm->list);
1634 pool_free(pool_head_quic_frame, frm);
1635 }
1636 pool_free(pool_head_quic_tx_packet, pkt);
1637}
1638
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001639/* Free the TX packets of <pkts> list */
1640static inline void free_quic_tx_pkts(struct list *pkts)
1641{
1642 struct quic_tx_packet *pkt, *tmp;
1643
1644 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001645 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001646 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001647 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001648 }
1649}
1650
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001651/* Remove already sent ranges of acknowledged packet numbers from
1652 * <pktns> packet number space tree below <largest_acked_pn> possibly
1653 * updating the range which contains <largest_acked_pn>.
1654 * Never fails.
1655 */
1656static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1657 int64_t largest_acked_pn)
1658{
1659 struct eb64_node *ar, *next_ar;
1660 struct quic_arngs *arngs = &pktns->rx.arngs;
1661
1662 ar = eb64_first(&arngs->root);
1663 while (ar) {
1664 struct quic_arng_node *ar_node;
1665
1666 next_ar = eb64_next(ar);
1667 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
1668 if ((int64_t)ar_node->first.key > largest_acked_pn)
1669 break;
1670
1671 if (largest_acked_pn < ar_node->last) {
1672 eb64_delete(ar);
1673 ar_node->first.key = largest_acked_pn + 1;
1674 eb64_insert(&arngs->root, ar);
1675 break;
1676 }
1677
1678 eb64_delete(ar);
1679 pool_free(pool_head_quic_arng, ar_node);
1680 arngs->sz--;
1681 ar = next_ar;
1682 }
1683}
1684
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001685/* Send a packet ack event nofication for each newly acked packet of
1686 * <newly_acked_pkts> list and free them.
1687 * Always succeeds.
1688 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001689static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001690 struct list *newly_acked_pkts)
1691{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001692 struct quic_tx_packet *pkt, *tmp;
1693 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1694
1695 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1696 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001697 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001698 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001699 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001700 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001701 /* If this packet contained an ACK frame, proceed to the
1702 * acknowledging of range of acks from the largest acknowledged
1703 * packet number which was sent in an ACK frame by this packet.
1704 */
1705 if (pkt->largest_acked_pn != -1)
1706 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001707 ev.ack.acked = pkt->in_flight_len;
1708 ev.ack.time_sent = pkt->time_sent;
1709 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001710 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001712 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001713 }
1714
1715}
1716
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001717/* Release all the frames attached to <pktns> packet number space */
1718static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1719{
1720 struct quic_frame *frm, *frmbak;
1721
1722 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1723 LIST_DELETE(&frm->list);
1724 pool_free(pool_head_quic_frame, frm);
1725 }
1726}
1727
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001728/* Handle <pkts> list of lost packets detected at <now_us> handling
1729 * their TX frames.
1730 * Send a packet loss event to the congestion controller if
1731 * in flight packet have been lost.
1732 * Also frees the packet in <pkts> list.
1733 * Never fails.
1734 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001735static inline void qc_release_lost_pkts(struct quic_conn *qc,
1736 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001737 struct list *pkts,
1738 uint64_t now_us)
1739{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001740 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001741 uint64_t lost_bytes;
1742
1743 lost_bytes = 0;
1744 oldest_lost = newest_lost = NULL;
1745 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001746 struct list tmp = LIST_HEAD_INIT(tmp);
1747
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001748 lost_bytes += pkt->in_flight_len;
1749 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001750 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001751 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001752 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001753 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001754 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001755 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001756 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001757 if (!oldest_lost) {
1758 oldest_lost = newest_lost = pkt;
1759 }
1760 else {
1761 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001762 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001763 newest_lost = pkt;
1764 }
1765 }
1766
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001767 if (newest_lost) {
1768 /* Sent a congestion event to the controller */
1769 struct quic_cc_event ev = {
1770 .type = QUIC_CC_EVT_LOSS,
1771 .loss.time_sent = newest_lost->time_sent,
1772 };
1773
1774 quic_cc_event(&qc->path->cc, &ev);
1775 }
1776
1777 /* If an RTT have been already sampled, <rtt_min> has been set.
1778 * We must check if we are experiencing a persistent congestion.
1779 * If this is the case, the congestion controller must re-enter
1780 * slow start state.
1781 */
1782 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1783 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1784
1785 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1786 now_ms, qc->max_ack_delay))
1787 qc->path->cc.algo->slow_start(&qc->path->cc);
1788 }
1789
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001790 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001791 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001792 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001793 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001794 }
1795}
1796
1797/* Look for packet loss from sent packets for <qel> encryption level of a
1798 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1799 * their tree if deemed as lost or set the <loss_time> value the packet number
1800 * space if any not deemed lost.
1801 * Should be called after having received an ACK frame with newly acknowledged
1802 * packets or when the the loss detection timer has expired.
1803 * Always succeeds.
1804 */
1805static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1806 struct quic_conn *qc,
1807 struct list *lost_pkts)
1808{
1809 struct eb_root *pkts;
1810 struct eb64_node *node;
1811 struct quic_loss *ql;
1812 unsigned int loss_delay;
1813
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001814 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001815 pkts = &pktns->tx.pkts;
1816 pktns->tx.loss_time = TICK_ETERNITY;
1817 if (eb_is_empty(pkts))
1818 goto out;
1819
1820 ql = &qc->path->loss;
1821 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001822 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1823
1824 node = eb64_first(pkts);
1825 while (node) {
1826 struct quic_tx_packet *pkt;
1827 int64_t largest_acked_pn;
1828 unsigned int loss_time_limit, time_sent;
1829
1830 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001831 largest_acked_pn = pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001832 node = eb64_next(node);
1833 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1834 break;
1835
1836 time_sent = pkt->time_sent;
1837 loss_time_limit = tick_add(time_sent, loss_delay);
1838 if (tick_is_le(time_sent, now_ms) ||
1839 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1840 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001841 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001842 }
1843 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001844 if (tick_isset(pktns->tx.loss_time))
1845 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1846 else
1847 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001848 }
1849 }
1850
1851 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001852 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001853}
1854
1855/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1856 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001857 * 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 +01001858 * acked ack-eliciting packet.
1859 * Return 1, if succeeded, 0 if not.
1860 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001861static inline int qc_parse_ack_frm(struct quic_conn *qc,
1862 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001863 struct quic_enc_level *qel,
1864 unsigned int *rtt_sample,
1865 const unsigned char **pos, const unsigned char *end)
1866{
1867 struct quic_ack *ack = &frm->ack;
1868 uint64_t smallest, largest;
1869 struct eb_root *pkts;
1870 struct eb64_node *largest_node;
1871 unsigned int time_sent, pkt_flags;
1872 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1873 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1874
1875 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1876 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001877 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001878 goto err;
1879 }
1880
1881 if (ack->first_ack_range > ack->largest_ack) {
1882 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001883 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001884 goto err;
1885 }
1886
1887 largest = ack->largest_ack;
1888 smallest = largest - ack->first_ack_range;
1889 pkts = &qel->pktns->tx.pkts;
1890 pkt_flags = 0;
1891 largest_node = NULL;
1892 time_sent = 0;
1893
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001894 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001895 largest_node = eb64_lookup(pkts, largest);
1896 if (!largest_node) {
1897 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001898 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001899 }
1900 else {
1901 time_sent = eb64_entry(&largest_node->node,
1902 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001903 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 }
1905
1906 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001907 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001908 do {
1909 uint64_t gap, ack_range;
1910
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001911 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1912 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913 if (!ack->ack_range_num--)
1914 break;
1915
1916 if (!quic_dec_int(&gap, pos, end))
1917 goto err;
1918
1919 if (smallest < gap + 2) {
1920 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001921 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001922 goto err;
1923 }
1924
1925 largest = smallest - gap - 2;
1926 if (!quic_dec_int(&ack_range, pos, end))
1927 goto err;
1928
1929 if (largest < ack_range) {
1930 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001931 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932 goto err;
1933 }
1934
1935 /* Do not use this node anymore. */
1936 largest_node = NULL;
1937 /* Next range */
1938 smallest = largest - ack_range;
1939
1940 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001941 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 } while (1);
1943
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1945 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001946 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001947 }
1948
1949 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1950 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001951 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001952 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001953 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001954 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001955 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1956 if (quic_peer_validated_addr(qc))
1957 qc->path->loss.pto_count = 0;
1958 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001959 }
1960
1961
1962 return 1;
1963
1964 err:
1965 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001966 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001967 return 0;
1968}
1969
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001970/* This function gives the detail of the SSL error. It is used only
1971 * if the debug mode and the verbose mode are activated. It dump all
1972 * the SSL error until the stack was empty.
1973 */
1974static forceinline void qc_ssl_dump_errors(struct connection *conn)
1975{
1976 if (unlikely(global.mode & MODE_DEBUG)) {
1977 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02001978 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001979 unsigned long ret;
1980
Willy Tarreau325fc632022-04-11 18:47:38 +02001981 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001982 ret = ERR_get_error();
1983 if (!ret)
1984 return;
1985
1986 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02001987 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001988 }
1989 }
1990}
1991
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001992int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1993 const char **str, int *len);
1994
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001995/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1996 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001997 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001998 * Return 1 if succeeded, 0 if not.
1999 */
2000static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002001 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002002 const unsigned char *data, size_t len,
2003 struct quic_rx_packet *pkt,
2004 struct quic_rx_crypto_frm *cf)
2005{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002006 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002007 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002008
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002009 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002010 qc = ctx->qc;
2011
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002012 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2013
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002014 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2015 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002016 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002017 goto err;
2018 }
2019
2020 el->rx.crypto.offset += len;
2021 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002022 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002023
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002024 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002025 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002026 ssl_err = SSL_do_handshake(ctx->ssl);
2027 if (ssl_err != 1) {
2028 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2029 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2030 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002031 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002032 goto out;
2033 }
2034
2035 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002036 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002037 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002038 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002039 goto err;
2040 }
2041
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002042 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002043
2044 /* Check the alpn could be negotiated */
2045 if (!qc->app_ops) {
2046 TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2047 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2048 goto err;
2049 }
2050
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002051 /* I/O callback switch */
2052 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002053 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002054 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002055 /* The connection is ready to be accepted. */
2056 quic_accept_push_qc(qc);
2057 }
2058 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002059 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002060 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002061 } else {
2062 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2063 if (ssl_err != 1) {
2064 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2065 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2066 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002067 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002068 goto out;
2069 }
2070
2071 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002072 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002073 goto err;
2074 }
2075
2076 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002077 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002078 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002079
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002080 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002081 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002082 return 1;
2083
2084 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002085 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002086 return 0;
2087}
2088
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002089/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
2090 * <pkt> RX packet.
2091 * Return it if succeeded, NULL if not.
2092 */
2093static inline
2094struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
2095 struct quic_rx_packet *pkt)
2096{
2097 struct quic_rx_strm_frm *frm;
2098
2099 frm = pool_alloc(pool_head_quic_rx_strm_frm);
2100 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002101 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002102 frm->len = stream_frm->len;
2103 frm->data = stream_frm->data;
2104 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002105 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002106 }
2107
2108 return frm;
2109}
2110
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002111/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01002112 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002113 */
2114static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2115{
2116 size_t ret;
2117
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01002118 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
2119 strm_frm->len -= ret;
2120 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002121
2122 return ret;
2123}
2124
2125/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2126 * streams may be open. The data are copied to the stream RX buffer if possible.
2127 * If not, the STREAM frame is stored to be treated again later.
2128 * We rely on the flow control so that not to store too much STREAM frames.
2129 * Return 1 if succeeded, 0 if not.
2130 */
2131static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2132 struct quic_stream *strm_frm,
2133 struct quic_conn *qc)
2134{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002135 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002136 struct eb64_node *frm_node;
2137 struct qcs *qcs = NULL;
2138 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002140 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2141 strm_frm->offset.key, strm_frm->fin,
2142 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002143
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002144 /* invalid or already received frame */
2145 if (ret == 1)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002146 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002147
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002148 if (ret == 2) {
2149 /* frame cannot be parsed at the moment and should be
2150 * buffered.
2151 */
2152 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2153 if (!frm) {
2154 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002155 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002156 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002157 }
2158
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002159 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2160 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002161
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002162 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002163 }
2164
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002165 /* Frame correctly received by the mux.
2166 * If there is buffered frame for next offset, it may be possible to
2167 * receive them now.
2168 */
2169 frm_node = eb64_first(&qcs->rx.frms);
2170 while (frm_node) {
2171 frm = eb64_entry(&frm_node->node,
2172 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002173
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02002174 ret = qcc_recv(qc->qcc, qcs->id, frm->len,
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002175 frm->offset_node.key, frm->fin,
2176 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002177
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002178 /* interrupt the parsing if the frame cannot be handled for the
2179 * moment only by the MUX.
2180 */
2181 if (ret == 2)
2182 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002183
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002184 /* Remove a newly received frame or an invalid one. */
2185 frm_node = eb64_next(frm_node);
2186 eb64_delete(&frm->offset_node);
2187 quic_rx_packet_refdec(frm->pkt);
2188 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002189 }
2190
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002191 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002192 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002193
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002194 return 1;
2195}
2196
2197/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2198 * streams may be open. The data are copied to the stream RX buffer if possible.
2199 * If not, the STREAM frame is stored to be treated again later.
2200 * We rely on the flow control so that not to store too much STREAM frames.
2201 * Return 1 if succeeded, 0 if not.
2202 */
2203static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2204 struct quic_stream *strm_frm,
2205 struct quic_conn *qc)
2206{
2207 struct qcs *strm;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002208 struct quic_rx_strm_frm *frm;
2209 size_t strm_frm_len;
2210
Amaury Denoyelle50742292022-03-29 14:57:19 +02002211 strm = qcc_get_qcs(qc->qcc, strm_frm->id);
2212 if (!strm) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002213 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002214 return 0;
2215 }
2216
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002217 if (strm_frm->offset.key < strm->rx.offset) {
2218 size_t diff;
2219
2220 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2221 TRACE_PROTO("Already received STREAM data",
2222 QUIC_EV_CONN_PSTRM, qc);
2223 goto out;
2224 }
2225
2226 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2227 diff = strm->rx.offset - strm_frm->offset.key;
2228 strm_frm->offset.key = strm->rx.offset;
2229 strm_frm->len -= diff;
2230 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002231 }
2232
2233 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002234 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002235 int ret;
2236
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002237 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002238 goto store_frm;
2239
2240 /* qc_strm_cpy() will modify the offset, depending on the number
2241 * of bytes copied.
2242 */
2243 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2244 /* Inform the application of the arrival of this new stream */
2245 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002246 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002247 return 0;
2248 }
2249
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002250 if (ret)
2251 qcs_notify_recv(strm);
2252
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002253 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002254 }
2255 /* Take this frame into an account for the stream flow control */
2256 strm->rx.offset += strm_frm_len;
2257 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002258 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002259 */
2260 if (!strm_frm->len)
2261 goto out;
2262
2263 store_frm:
2264 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2265 if (!frm) {
2266 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002267 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002268 return 0;
2269 }
2270
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002271 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002272 quic_rx_packet_refinc(pkt);
2273
2274 out:
2275 return 1;
2276}
2277
2278static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2279 struct quic_stream *strm_frm,
2280 struct quic_conn *qc)
2281{
2282 if (strm_frm->id & QCS_ID_DIR_BIT)
2283 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2284 else
2285 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2286}
2287
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002288/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002289static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2290 struct quic_conn *qc)
2291{
2292 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002293 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002294 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002295
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002296 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002297 pkts = &qel->pktns->tx.pkts;
2298 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002299 /* Skip the empty packet (they have already been retransmitted) */
2300 while (node) {
2301 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2302 if (!LIST_ISEMPTY(&pkt->frms))
2303 break;
2304 node = eb64_next(node);
2305 }
2306
2307 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002308 return;
2309
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002310 /* When building a packet from another one, the field which may increase the
2311 * packet size is the packet number. And the maximum increase is 4 bytes.
2312 */
2313 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2314 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2315 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2316 return;
2317 }
2318
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002319 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002320}
2321
2322/* Prepare a fast retransmission during handshake after a client
2323 * has resent Initial packets. According to the RFC a server may retransmit
2324 * up to two datagrams of Initial packets if did not receive all Initial packets
2325 * and resend them coalescing with others (Handshake here).
2326 * (Listener only).
2327 */
2328static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2329{
2330 struct list itmp = LIST_HEAD_INIT(itmp);
2331 struct list htmp = LIST_HEAD_INIT(htmp);
2332 struct quic_frame *frm, *frmbak;
2333
2334 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2335 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2336 struct quic_enc_level *qel = iqel;
2337 struct eb_root *pkts;
2338 struct eb64_node *node;
2339 struct quic_tx_packet *pkt;
2340 struct list *tmp = &itmp;
2341
Frédéric Lécaille5cfb4ed2022-03-30 14:44:49 +02002342 /* Do not probe from a packet number space if some probing
2343 * was already asked.
2344 */
2345 if (qel->pktns->tx.pto_probe) {
2346 qel = hqel;
2347 if (qel->pktns->tx.pto_probe)
2348 return;
2349 }
2350
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002351 start:
2352 pkt = NULL;
2353 pkts = &qel->pktns->tx.pkts;
2354 node = eb64_first(pkts);
2355 /* Skip the empty packet (they have already been retransmitted) */
2356 while (node) {
2357 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2358 if (!LIST_ISEMPTY(&pkt->frms))
2359 break;
2360 node = eb64_next(node);
2361 }
2362
2363 if (!pkt)
2364 goto end;
2365
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002366 /* When building a packet from another one, the field which may increase the
2367 * packet size is the packet number. And the maximum increase is 4 bytes.
2368 */
2369 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2370 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2371 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2372 goto end;
2373 }
2374
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002375 qel->pktns->tx.pto_probe += 1;
2376 requeue:
2377 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Frédéric Lécaille009016c2022-03-30 14:58:55 +02002378 struct quic_frame *dup_frm;
2379
2380
2381 dup_frm = pool_alloc(pool_head_quic_frame);
2382 if (!dup_frm) {
2383 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2384 break;
2385 }
2386
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002387 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille009016c2022-03-30 14:58:55 +02002388 *dup_frm = *frm;
2389 LIST_APPEND(tmp, &dup_frm->list);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002390 }
2391
2392 if (qel == iqel) {
2393 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2394 pkt = pkt->next;
2395 tmp = &htmp;
2396 hqel->pktns->tx.pto_probe += 1;
2397 goto requeue;
2398 }
2399
2400 qel = hqel;
2401 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002402 goto start;
2403 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002404
2405 end:
2406 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2407 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002408}
2409
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002410/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2411 * as I/O handler context and <qel> as encryption level.
2412 * Returns 1 if succeeded, 0 if failed.
2413 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002414static 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 +01002415 struct quic_enc_level *qel)
2416{
2417 struct quic_frame frm;
2418 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002419 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002420 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002421
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002422 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002423 /* Skip the AAD */
2424 pos = pkt->data + pkt->aad_len;
2425 end = pkt->data + pkt->len;
2426
2427 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002428 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002429 goto err;
2430
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002431 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002432 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002433 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002434 break;
2435 case QUIC_FT_PING:
2436 break;
2437 case QUIC_FT_ACK:
2438 {
2439 unsigned int rtt_sample;
2440
2441 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002442 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002443 goto err;
2444
2445 if (rtt_sample) {
2446 unsigned int ack_delay;
2447
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002448 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002449 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002450 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2451 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002452 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002453 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002454 break;
2455 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002456 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002457 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002458 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002459 {
2460 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002461
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002462 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002463 /* XXX TO DO: <cfdebug> is used only for the traces. */
2464 struct quic_rx_crypto_frm cfdebug = { };
2465
2466 cfdebug.offset_node.key = frm.crypto.offset;
2467 cfdebug.len = frm.crypto.len;
2468 TRACE_PROTO("CRYPTO data discarded",
2469 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2470 break;
2471 }
2472
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002473 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2474 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2475 /* XXX TO DO: <cfdebug> is used only for the traces. */
2476 struct quic_rx_crypto_frm cfdebug = { };
2477
2478 cfdebug.offset_node.key = frm.crypto.offset;
2479 cfdebug.len = frm.crypto.len;
2480 /* Nothing to do */
2481 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002482 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002483 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002484 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2485 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002486 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002487 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002488 else {
2489 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2490 /* XXX TO DO: <cfdebug> is used only for the traces. */
2491 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002493 cfdebug.offset_node.key = frm.crypto.offset;
2494 cfdebug.len = frm.crypto.len;
2495 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002496 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002497 frm.crypto.len -= diff;
2498 frm.crypto.data += diff;
2499 frm.crypto.offset = qel->rx.crypto.offset;
2500 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002501 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002502
2503 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002504 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002505 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002506
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002507 cfdebug.offset_node.key = frm.crypto.offset;
2508 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002509 if (!qc_provide_cdata(qel, ctx,
2510 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002511 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002512 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002513
2514 break;
2515 }
2516
2517 /* frm.crypto.offset > qel->rx.crypto.offset */
2518 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2519 if (!cf) {
2520 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002521 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002522 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002523 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002524
2525 cf->offset_node.key = frm.crypto.offset;
2526 cf->len = frm.crypto.len;
2527 cf->data = frm.crypto.data;
2528 cf->pkt = pkt;
2529 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2530 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002531 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002532 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002533 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002534 {
2535 struct quic_stream *stream = &frm.stream;
2536
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002537 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002538 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2539 goto err;
2540 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2541 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002542
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002543 /* At the application layer the connection may have already been closed. */
2544 if (qc->mux_state != QC_MUX_READY)
2545 break;
2546
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002547 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002548 goto err;
2549
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002550 break;
2551 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002552 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002553 if (qc->mux_state == QC_MUX_READY) {
2554 struct quic_max_data *data = &frm.max_data;
2555 qcc_recv_max_data(qc->qcc, data->max_data);
2556 }
2557 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002558 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002559 if (qc->mux_state == QC_MUX_READY) {
2560 struct quic_max_stream_data *data = &frm.max_stream_data;
2561 qcc_recv_max_stream_data(qc->qcc, data->id,
2562 data->max_stream_data);
2563 }
2564 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002565 case QUIC_FT_MAX_STREAMS_BIDI:
2566 case QUIC_FT_MAX_STREAMS_UNI:
2567 case QUIC_FT_DATA_BLOCKED:
2568 case QUIC_FT_STREAM_DATA_BLOCKED:
2569 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2570 case QUIC_FT_STREAMS_BLOCKED_UNI:
2571 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002572 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002573 case QUIC_FT_RETIRE_CONNECTION_ID:
2574 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 break;
2576 case QUIC_FT_CONNECTION_CLOSE:
2577 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille47756802022-03-25 09:12:16 +01002578 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
2579 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2580 /* RFC 9000 10.2. Immediate Close:
2581 * The closing and draining connection states exist to ensure
2582 * that connections close cleanly and that delayed or reordered
2583 * packets are properly discarded. These states SHOULD persist
2584 * for at least three times the current PTO interval...
2585 *
2586 * Rearm the idle timeout only one time when entering draining
2587 * state.
2588 */
2589 qc_idle_timer_do_rearm(qc);
2590 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002591 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002592 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002593 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002594 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002595 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002596 goto err;
2597
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002598 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002599 break;
2600 default:
2601 goto err;
2602 }
2603 }
2604
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002605 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002606 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002607
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002608 if (fast_retrans)
2609 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002610
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002611 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2612 * has successfully parse a Handshake packet. The Initial encryption must also
2613 * be discarded.
2614 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002615 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002616 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002617 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2618 QUIC_FL_TLS_SECRETS_DCD)) {
2619 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2620 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2621 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2622 qc_set_timer(ctx->qc);
2623 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2624 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2625 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002626 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2627 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002628 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002629 }
2630
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002631 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002632 return 1;
2633
2634 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002635 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002636 return 0;
2637}
2638
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002639/* Must be called only by a <cbuf> writer (packet builder).
2640 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2641 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2642 * index after having marked the end of written data. This the responsability
2643 * of the caller to ensure there is enough room in <cbuf> to write the end of
2644 * data made of a uint16_t null field.
2645 *
2646 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2647 * ^ ^
2648 * r w
2649 *
2650 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2651 * ^ ^
2652 * r w
2653
2654 * +--------------------------------------+ (empty buffer, can be reused)
2655 * ^
2656 * (r = w)
2657 *
2658 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2659 * ^ ^
2660 * w r
2661 */
2662static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2663{
2664 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2665
2666 /* We can reset the writer index only if in front of the reader index and
2667 * if the reader index is not null. Resetting the writer when the reader
2668 * index is null would empty the buffer.
2669 * XXX Note than the writer index cannot reach the reader index.
2670 * Only the reader index can reach the writer index.
2671 */
2672 if (rd && rd <= cbuf->wr) {
2673 /* Mark the end of contiguous data for the reader */
2674 write_u16(cb_wr(cbuf), 0);
2675 cb_add(cbuf, sizeof(uint16_t));
2676 cb_wr_reset(cbuf);
2677 return 1;
2678 }
2679
2680 return 0;
2681}
2682
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002683/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002684 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002685 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2686 * This function must be called only after having built a correct datagram.
2687 * Always succeeds.
2688 */
2689static inline void qc_set_dg(struct cbuf *cbuf,
2690 uint16_t dglen, struct quic_tx_packet *pkt)
2691{
2692 write_u16(cb_wr(cbuf), dglen);
2693 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2694 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2695}
2696
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002697/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2698 * with <frms> as ack-eliciting frame list to send, 0 if not.
2699 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2700 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2701 */
2702static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2703 struct quic_enc_level *qel, int cc, int probe)
2704{
2705 unsigned int must_ack =
2706 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2707
2708 /* Do not build any more packet if the TX secrets are not available or
2709 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2710 * and if there is no more packets to send upon PTO expiration
2711 * and if there is no more ack-eliciting frames to send or in flight
2712 * congestion control limit is reached for prepared data
2713 */
2714 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2715 (!cc && !probe && !must_ack &&
2716 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2717 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2718 return 0;
2719 }
2720
2721 return 1;
2722}
2723
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002724/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002725 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2726 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002727 * A header made of two fields is added to each datagram: the datagram length followed
2728 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002729 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2730 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002731 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002732static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2733 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002734{
2735 struct quic_enc_level *qel;
2736 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002737 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002738 struct quic_tx_packet *pkt;
2739 size_t total;
2740 size_t dg_headlen;
2741
2742 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2743 /* Each datagram is prepended with its length followed by the
2744 * address of the first packet in the datagram.
2745 */
2746 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2747 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2748 total = 0;
2749 start:
2750 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002751 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002752 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2753 * to ensure there is enough room to mark the end of prepared
2754 * contiguous data with a zero length.
2755 */
2756 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2757 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002758 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002759
2760 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002761 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002762 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002763 /* We do not probe if an immediate close was asked */
2764 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002765 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002766
2767 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002768 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002769
2770 /* Leave room for the datagram header */
2771 pos += dg_headlen;
2772 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2773 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2774 }
2775 else {
2776 end = pos + qc->path->mtu;
2777 }
2778
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002779 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002780 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002781 switch (err) {
2782 case -2:
2783 goto err;
2784 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002785 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2786 * MTU, we are here because of the congestion control window. There is
2787 * no need to try to reuse this buffer.
2788 */
2789 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002790 default:
2791 break;
2792 }
2793
2794 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2795 if (!pkt)
2796 goto err;
2797
2798 total += pkt->len;
2799 /* Set the current datagram as prepared into <cbuf>. */
2800 qc_set_dg(cbuf, pkt->len, pkt);
2801 }
2802
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002803 /* Reset <wr> writer index if in front of <rd> index */
2804 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002805 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002806 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002807 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002808 }
2809
2810 out:
2811 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2812 return total;
2813
2814 err:
2815 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2816 return -1;
2817}
2818
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002819/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002820 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2821 * several packets in the same datagram. A header made of two fields is added
2822 * to each datagram: the datagram length followed by the address of the first
2823 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002824 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2825 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002826 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002827static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2828 enum quic_tls_enc_level tel,
2829 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002830{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002831 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002832 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002833 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002834 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2835 /* length of datagrams */
2836 uint16_t dglen;
2837 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002838 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002839 /* Each datagram is prepended with its length followed by the
2840 * address of the first packet in the datagram.
2841 */
2842 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002843
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002844 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2845
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002846 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002847 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002848 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002849 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002850 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002851 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002852 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002853 /* Leave at least <dglen> bytes at the end of this buffer
2854 * to ensure there is enough room to mark the end of prepared
2855 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002857 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2858 first_pkt = prv_pkt = NULL;
2859 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002860 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861 enum quic_pkt_type pkt_type;
2862
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002863 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002864 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002865 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002866 /* We do not probe if an immediate close was asked */
2867 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002868 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002869
2870 if (!qc_may_build_pkt(qc, &qel->pktns->tx.frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002871 if (prv_pkt)
2872 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002873 /* Let's select the next encryption level */
2874 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2875 tel = next_tel;
2876 qel = &qc->els[tel];
2877 /* Build a new datagram */
2878 prv_pkt = NULL;
2879 continue;
2880 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002881 break;
2882 }
2883
2884 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002885 if (!prv_pkt) {
2886 /* Leave room for the datagram header */
2887 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002888 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002889 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2890 }
2891 else {
2892 end = pos + qc->path->mtu;
2893 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002894 }
2895
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002896 cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002897 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002898 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899 case -2:
2900 goto err;
2901 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002902 /* If there was already a correct packet present, set the
2903 * current datagram as prepared into <cbuf>.
2904 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002905 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002906 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002907 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002908 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002909 break;
2910 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002911
Frédéric Lécaille63556772021-12-29 17:18:21 +01002912 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2913 if (!cur_pkt)
2914 goto err;
2915
2916 total += cur_pkt->len;
2917 /* keep trace of the first packet in the datagram */
2918 if (!first_pkt)
2919 first_pkt = cur_pkt;
2920 /* Attach the current one to the previous one */
2921 if (prv_pkt)
2922 prv_pkt->next = cur_pkt;
2923 /* Let's say we have to build a new dgram */
2924 prv_pkt = NULL;
2925 dglen += cur_pkt->len;
2926 /* Client: discard the Initial encryption keys as soon as
2927 * a handshake packet could be built.
2928 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002929 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01002930 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2931 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2932 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2933 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2934 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002935 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002936 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002937 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002938 }
2939 /* If the data for the current encryption level have all been sent,
2940 * select the next level.
2941 */
2942 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécailled6570e12022-03-29 17:41:57 +02002943 (LIST_ISEMPTY(&qel->pktns->tx.frms) && !qel->pktns->tx.pto_probe)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002944 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2945 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2946 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2947 tel = next_tel;
2948 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002949 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002950 /* If there is data for the next level, do not
2951 * consume a datagram.
2952 */
2953 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002954 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002955 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002956 /* If we have to build a new datagram, set the current datagram as
2957 * prepared into <cbuf>.
2958 */
2959 if (!prv_pkt) {
2960 qc_set_dg(cbuf, dglen, first_pkt);
2961 first_pkt = NULL;
2962 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002963 padding = 0;
2964 }
2965 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002966 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002967 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2968 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002969 }
2970 }
2971
2972 stop_build:
2973 /* Reset <wr> writer index if in front of <rd> index */
2974 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002975 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002976 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002977 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002978 }
2979
2980 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002981 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002982 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002983
2984 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002985 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002986 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987}
2988
2989/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002990 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002991 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002992int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993{
2994 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002995 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002997 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002998 cbuf = qr->cbuf;
2999 while (cb_contig_data(cbuf)) {
3000 unsigned char *pos;
3001 struct buffer tmpbuf = { };
3002 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3003 uint16_t dglen;
3004 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003005 unsigned int time_sent;
3006
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003007 pos = cb_rd(cbuf);
3008 dglen = read_u16(pos);
3009 /* End of prepared datagrams.
3010 * Reset the reader index only if in front of the writer index.
3011 */
3012 if (!dglen) {
3013 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3014
3015 if (wr && wr < cbuf->rd) {
3016 cb_rd_reset(cbuf);
3017 continue;
3018 }
3019 break;
3020 }
3021
3022 pos += sizeof dglen;
3023 first_pkt = read_ptr(pos);
3024 pos += sizeof first_pkt;
3025 tmpbuf.area = (char *)pos;
3026 tmpbuf.size = tmpbuf.data = dglen;
3027
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003028 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003029 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003030 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003031
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003032 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003033 qc->tx.bytes += tmpbuf.data;
3034 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003035
3036 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3037 pkt->time_sent = time_sent;
3038 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3039 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003040 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003041 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3042 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003043 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003044 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3045 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3046 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003047 qc_notify_close(qc);
3048
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003049 /* RFC 9000 10.2. Immediate Close:
3050 * The closing and draining connection states exist to ensure
3051 * that connections close cleanly and that delayed or reordered
3052 * packets are properly discarded. These states SHOULD persist
3053 * for at least three times the current PTO interval...
3054 *
3055 * Rearm the idle timeout only one time when entering closing
3056 * state.
3057 */
3058 qc_idle_timer_do_rearm(qc);
3059 if (qc->timer_task) {
3060 task_destroy(qc->timer_task);
3061 qc->timer_task = NULL;
3062 }
3063 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003064 qc->path->in_flight += pkt->in_flight_len;
3065 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3066 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003067 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003068 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003069 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003070 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003071 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003072 }
3073 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074
3075 return 1;
3076}
3077
3078/* Build all the frames which must be sent just after the handshake have succeeded.
3079 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3080 * a HANDSHAKE_DONE frame.
3081 * Return 1 if succeeded, 0 if not.
3082 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003083static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003084{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003085 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003086 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003087 struct quic_frame *frm, *frmbak;
3088 struct list frm_list = LIST_HEAD_INIT(frm_list);
3089 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003090
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003091 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003093 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003094 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003095 if (!frm)
3096 return 0;
3097
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003098 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003099 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003100 }
3101
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003102 first = 1;
3103 max = qc->tx.params.active_connection_id_limit;
3104 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105 struct quic_connection_id *cid;
3106
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003107 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003108 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003109 goto err;
3110
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003111 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003112 if (!cid)
3113 goto err;
3114
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003115 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003116 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003117
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003118 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003119 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003120 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003121
3122 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003123 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003124
3125 return 1;
3126
3127 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003128 /* free the frames */
3129 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3130 pool_free(pool_head_quic_frame, frm);
3131
3132 node = eb64_first(&qc->cids);
3133 while (node) {
3134 struct quic_connection_id *cid;
3135
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003136
3137 cid = eb64_entry(&node->node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003138 if (cid->seq_num.key >= max)
3139 break;
3140
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003141 if (cid->seq_num.key < first)
3142 continue;
3143
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003144 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003145 ebmb_delete(&cid->node);
3146 eb64_delete(&cid->seq_num);
3147 pool_free(pool_head_quic_connection_id, cid);
3148 }
3149
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003150 return 0;
3151}
3152
3153/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003154void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003156 struct eb64_node *n;
3157 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003158
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003159 n = eb64_first(&arngs->root);
3160 while (n) {
3161 struct eb64_node *next;
3162
3163 ar = eb64_entry(&n->node, struct quic_arng_node, first);
3164 next = eb64_next(n);
3165 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003166 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003167 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003168 }
3169}
3170
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003171/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3172 * descending order.
3173 */
3174static inline size_t sack_gap(struct quic_arng_node *p,
3175 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003176{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003177 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003178}
3179
3180
3181/* Remove the last elements of <ack_ranges> list of ack range updating its
3182 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003183 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003184 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003185static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003186{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003187 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003188
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003189 last = eb64_last(&arngs->root);
3190 while (last && arngs->enc_sz > limit) {
3191 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003192
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003193 prev = eb64_prev(last);
3194 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003195 return 0;
3196
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003197 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
3198 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
3199 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3200 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3201 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3202 --arngs->sz;
3203 eb64_delete(last);
3204 pool_free(pool_head_quic_arng, last);
3205 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003206 }
3207
3208 return 1;
3209}
3210
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003211/* Set the encoded size of <arngs> QUIC ack ranges. */
3212static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3213{
3214 struct eb64_node *node, *next;
3215 struct quic_arng_node *ar, *ar_next;
3216
3217 node = eb64_last(&arngs->root);
3218 if (!node)
3219 return;
3220
3221 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3222 arngs->enc_sz = quic_int_getsize(ar->last) +
3223 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3224
3225 while ((next = eb64_prev(node))) {
3226 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
3227 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3228 quic_int_getsize(ar_next->last - ar_next->first.key);
3229 node = next;
3230 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3231 }
3232}
3233
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003234/* Insert <ar> ack range into <argns> tree of ack ranges.
3235 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003236 */
3237static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003238struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3239 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003240{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003241 struct quic_arng_node *new_ar;
3242
3243 new_ar = pool_alloc(pool_head_quic_arng);
3244 if (new_ar) {
3245 new_ar->first.key = ar->first;
3246 new_ar->last = ar->last;
3247 eb64_insert(&arngs->root, &new_ar->first);
3248 arngs->sz++;
3249 }
3250
3251 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003252}
3253
3254/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003255 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003256 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003257 *
3258 * Descending order
3259 * ------------->
3260 * range1 range2
3261 * ..........|--------|..............|--------|
3262 * ^ ^ ^ ^
3263 * | | | |
3264 * last1 first1 last2 first2
3265 * ..........+--------+--------------+--------+......
3266 * diff1 gap12 diff2
3267 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003268 * To encode the previous list of ranges we must encode integers as follows in
3269 * descending order:
3270 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003271 * with diff1 = last1 - first1
3272 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003273 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003274 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003275 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003276int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3277 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003278{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003279 struct eb64_node *le;
3280 struct quic_arng_node *new_node;
3281 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003282
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003283 new = NULL;
3284 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003285 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003286 if (!new_node)
3287 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003288
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003289 goto out;
3290 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003291
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003292 le = eb64_lookup_le(&arngs->root, ar->first);
3293 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003294 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003295 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003296 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003297
3298 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003299 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003300 else {
3301 struct quic_arng_node *le_ar =
3302 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003303
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003304 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003305 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003306 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003307
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003308 if (le_ar->last + 1 >= ar->first) {
3309 le_ar->last = ar->last;
3310 new = le;
3311 new_node = le_ar;
3312 }
3313 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003314 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003315 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003316 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003317
3318 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003319 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003320 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003322 /* Verify that the new inserted node does not overlap the nodes
3323 * which follow it.
3324 */
3325 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003326 struct eb64_node *next;
3327 struct quic_arng_node *next_node;
3328
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003329 while ((next = eb64_next(new))) {
3330 next_node =
3331 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003332 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003333 break;
3334
3335 if (next_node->last > new_node->last)
3336 new_node->last = next_node->last;
3337 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003338 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003339 /* Decrement the size of these ranges. */
3340 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003341 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003342 }
3343
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003344 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003345 quic_arngs_set_enc_sz(arngs);
3346
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003347 return 1;
3348}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003349/* Remove the header protection of packets at <el> encryption level.
3350 * Always succeeds.
3351 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003352static 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 +01003353{
3354 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003355 struct quic_rx_packet *pqpkt;
3356 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003357 struct quic_enc_level *app_qel;
3358
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003359 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3360 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003361 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003362 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003363 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003364 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365 goto out;
3366 }
3367 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003368 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003369 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003370 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003371 pqpkt->data, pqpkt->data + pqpkt->len)) {
3372 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003373 /* XXX TO DO XXX */
3374 }
3375 else {
3376 /* The AAD includes the packet number field */
3377 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3378 /* Store the packet into the tree of packets to decrypt. */
3379 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003380 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003381 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3382 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003383 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003384 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003385 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003386 MT_LIST_DELETE_SAFE(pkttmp1);
3387 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003388 }
3389
3390 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003391 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003392}
3393
3394/* Process all the CRYPTO frame at <el> encryption level.
3395 * Return 1 if succeeded, 0 if not.
3396 */
3397static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003398 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003399{
3400 struct eb64_node *node;
3401
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003402 node = eb64_first(&el->rx.crypto.frms);
3403 while (node) {
3404 struct quic_rx_crypto_frm *cf;
3405
3406 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3407 if (cf->offset_node.key != el->rx.crypto.offset)
3408 break;
3409
3410 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3411 goto err;
3412
3413 node = eb64_next(node);
3414 quic_rx_packet_refdec(cf->pkt);
3415 eb64_delete(&cf->offset_node);
3416 pool_free(pool_head_quic_rx_crypto_frm, cf);
3417 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003418 return 1;
3419
3420 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003421 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003422 return 0;
3423}
3424
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003425/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003426 * 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 +02003427 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003428 * Return 1 if succeeded, 0 if not.
3429 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003430int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3431 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003432{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003434 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003435 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003436 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003437
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003438 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003439 qel = cur_el;
3440 next_tel:
3441 if (!qel)
3442 goto out;
3443
3444 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3445 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003446 while (node) {
3447 struct quic_rx_packet *pkt;
3448
3449 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003450 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003451 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003452 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003453 /* Drop the packet */
3454 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003455 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003456 }
3457 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003458 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003459 /* Drop the packet */
3460 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003461 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003462 }
3463 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003464 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3465
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003466 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3467 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3468 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003469 qc_idle_timer_rearm(qc, 1);
3470 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003471 if (pkt->pn > largest_pn)
3472 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003473 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003474 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003475 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003476 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003477 }
3478 }
3479 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003480 eb64_delete(&pkt->pn_node);
3481 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003482 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003483 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003484
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003485 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003486 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn)
3487 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003488 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003489 goto err;
3490
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003491 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003492 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003493 qel = next_el;
3494 goto next_tel;
3495 }
3496
3497 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003498 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003499 return 1;
3500
3501 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003502 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003503 return 0;
3504}
3505
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003506/* Check if it's possible to remove header protection for packets related to
3507 * encryption level <qel>. If <qel> is NULL, assume it's false.
3508 *
3509 * Return true if the operation is possible else false.
3510 */
3511static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3512{
3513 enum quic_tls_enc_level tel;
3514
3515 if (!qel)
3516 return 0;
3517
3518 tel = ssl_to_quic_enc_level(qel->level);
3519
3520 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003521 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003522 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003523 return 0;
3524 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003525
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003526 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003527 return 0;
3528
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003529 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003530 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003531 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003532 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003533
3534 return 1;
3535}
3536
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003537/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaillec2f561c2022-02-25 17:46:07 +01003538int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003539{
3540 int ret;
3541 struct qring *qr;
3542
3543 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3544 if (!qr)
3545 /* Never happens */
3546 return 1;
3547
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003548 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003549 if (ret == -1)
3550 goto err;
3551 else if (ret == 0)
3552 goto out;
3553
3554 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3555 goto err;
3556
3557 out:
3558 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3559 return 1;
3560
3561 err:
3562 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3563 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3564 return 0;
3565}
3566
3567/* QUIC connection packet handler task (post handshake) */
3568static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3569{
3570 struct ssl_sock_ctx *ctx;
3571 struct quic_conn *qc;
3572 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003573
3574
3575 ctx = context;
3576 qc = ctx->qc;
3577 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003578
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003579 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003580
3581 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3582 qc_rm_hp_pkts(qc, qel);
3583
3584 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3585 goto err;
3586
Frédéric Lécaille47756802022-03-25 09:12:16 +01003587 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3588 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3589 goto out;
3590
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003591 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003592 goto err;
3593
Frédéric Lécaille47756802022-03-25 09:12:16 +01003594out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003595 return t;
3596
3597 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003598 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003599 return t;
3600}
3601
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003602/* QUIC connection packet handler task. */
3603struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003604{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003605 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003606 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003607 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003608 enum quic_tls_enc_level tel, next_tel;
3609 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003610 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003611 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003613 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003614 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003615 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003616 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003617 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003618 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003619 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3620 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003621 /* The I/O handler has been woken up by the dgram listener
3622 * after the anti-amplification was reached.
3623 */
3624 qc_set_timer(qc);
3625 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3626 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3627 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003628 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003629 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3630 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3631 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003632 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003633 if (st >= QUIC_HS_ST_COMPLETE &&
3634 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3635 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3636 /* There may be remaining Handshake packets to treat and acknowledge. */
3637 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3638 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3639 }
3640 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003641 goto err;
3642
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003643 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003644 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003645
3646 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003647 /* Treat packets waiting for header packet protection decryption */
3648 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003649 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003650
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003651 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3652 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3653 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003654 goto err;
3655
Frédéric Lécaille47756802022-03-25 09:12:16 +01003656 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3657 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3658 goto out;
3659
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003660 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003661 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003662 qel = next_qel;
3663 next_qel = NULL;
3664 goto next_level;
3665 }
3666
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003667 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003668 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003669 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003670 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003671 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003672
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003673 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003674 QUIC_FL_TLS_SECRETS_DCD)) {
3675 /* Discard the Handshake keys. */
3676 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3677 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3678 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3679 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003680 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003681 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003682 }
3683
3684 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3685 /* There may be remaining handshake to build (acks) */
3686 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3687 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003688 }
3689
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003690 if (!qr)
3691 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003692 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3693 * be considered.
3694 */
3695 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003696 goto err;
3697 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003698 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003699 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003700 else if (ret == 0)
3701 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003702
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003703 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003704 goto err;
3705
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003706 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707 /* Check if there is something to do for the next level.
3708 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003709 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003710 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003711 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003713 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003714 goto next_level;
3715 }
3716
Frédéric Lécaille47756802022-03-25 09:12:16 +01003717 out:
3718 if (qr)
3719 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003720 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003721 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003722
3723 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003724 if (qr)
3725 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003726 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003727 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003728}
3729
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003730/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003731static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3732{
3733 int i;
3734
3735 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3736 if (qel->tx.crypto.bufs[i]) {
3737 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3738 qel->tx.crypto.bufs[i] = NULL;
3739 }
3740 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003741 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003742}
3743
3744/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003745 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003746 * Returns 1 if succeeded, 0 if not.
3747 */
3748static int quic_conn_enc_level_init(struct quic_conn *qc,
3749 enum quic_tls_enc_level level)
3750{
3751 struct quic_enc_level *qel;
3752
3753 qel = &qc->els[level];
3754 qel->level = quic_to_ssl_enc_level(level);
3755 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3756 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3757 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003758 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003759
3760 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003761 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003762 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003763 qel->rx.crypto.offset = 0;
3764 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003765
3766 /* Allocate only one buffer. */
3767 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3768 if (!qel->tx.crypto.bufs)
3769 goto err;
3770
3771 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3772 if (!qel->tx.crypto.bufs[0])
3773 goto err;
3774
3775 qel->tx.crypto.bufs[0]->sz = 0;
3776 qel->tx.crypto.nb_buf = 1;
3777
3778 qel->tx.crypto.sz = 0;
3779 qel->tx.crypto.offset = 0;
3780
3781 return 1;
3782
3783 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003784 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785 return 0;
3786}
3787
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003788/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
3789 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003790 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003791 * This function must only be called by the thread responsible of the quic_conn
3792 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003793 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003794static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003795{
3796 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003797 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003798 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003799 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003800
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02003801 /* We must not free the quic-conn if the MUX is still allocated. */
3802 BUG_ON(qc->mux_state == QC_MUX_READY);
3803
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003804 /* free remaining stream descriptors */
3805 node = eb64_first(&qc->streams_by_id);
3806 while (node) {
3807 struct qc_stream_desc *stream;
3808
3809 stream = eb64_entry(node, struct qc_stream_desc, by_id);
3810 node = eb64_next(node);
3811
Amaury Denoyellec9acc312022-04-01 16:41:21 +02003812 /* all streams attached to the quic-conn are released, so
3813 * qc_stream_desc_free will liberate the stream instance.
3814 */
3815 BUG_ON(!stream->release);
3816 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003817 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003818
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003819 if (qc->idle_timer_task) {
3820 task_destroy(qc->idle_timer_task);
3821 qc->idle_timer_task = NULL;
3822 }
3823
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003824 if (qc->timer_task) {
3825 task_destroy(qc->timer_task);
3826 qc->timer_task = NULL;
3827 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003828
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003829 /* remove the connection from receiver cids trees */
3830 ebmb_delete(&qc->odcid_node);
3831 ebmb_delete(&qc->scid_node);
3832 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003833
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003834 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003835 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003836 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003837 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003838 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003839 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003840
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003841 quic_tls_ku_free(qc);
3842 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3843 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003844 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003845 }
3846
3847 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
3848 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
3849 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003850
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02003851 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
3852 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02003853 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02003854 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02003855
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003856 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3857 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003858 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003859}
3860
Amaury Denoyellee0be5732022-04-05 17:34:18 +02003861static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003862{
3863 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003864 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003865
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003866 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003867
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003868 /* Next application data can be dropped. */
3869 qc->mux_state = QC_MUX_RELEASED;
3870
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02003871 /* If the quic-conn timer has already expired free the quic-conn. */
3872 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
3873 quic_conn_release(qc);
3874 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
3875 return;
3876 }
3877
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003878 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003879}
3880
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003881/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003882static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003883{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003884 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003885 struct quic_conn *qc;
3886 struct quic_pktns *pktns;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003887 int i;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003888
3889 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003890 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003891 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003892 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003893 task->expire = TICK_ETERNITY;
3894 pktns = quic_loss_pktns(qc);
3895 if (tick_isset(pktns->tx.loss_time)) {
3896 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3897
3898 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3899 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003900 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3901 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003902 goto out;
3903 }
3904
3905 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003906 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003907 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3908 pktns->tx.pto_probe = 1;
3909 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3910 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3911 }
3912 else {
3913 pktns->tx.pto_probe = 2;
3914 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003915 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003916 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003917 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3918 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3919
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003920 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003921 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003922 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003923 iel->pktns->tx.pto_probe = 1;
3924 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003925
3926 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003927 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3928 continue;
3929
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003930 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003931 }
3932
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003933 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3934 qc->path->loss.pto_count++;
3935
3936 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003937 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003938
3939 return task;
3940}
3941
3942/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3943 * connections used to identify the first Initial packets of client connecting
3944 * to listeners. This parameter must be NULL for QUIC connections attached
3945 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3946 * <scid> is the source connection ID with <scid_len> as length.
3947 * Returns 1 if succeeded, 0 if not.
3948 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003949static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003950 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003951 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003952{
3953 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003954 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003955 /* Initial CID. */
3956 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003957 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003958 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003960 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003961 qc = pool_zalloc(pool_head_quic_conn);
3962 if (!qc) {
3963 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3964 goto err;
3965 }
3966
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003967 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3968 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003969 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003970 goto err;
3971 }
3972
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003973 qc->cids = EB_ROOT;
3974 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003975 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003976 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003977
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003978 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003979 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003980 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003981 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003982 qc->odcid.addrlen = dcid_addr_len;
3983 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003984
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003985 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986 if (scid_len)
3987 memcpy(qc->dcid.data, scid, scid_len);
3988 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003989 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003990 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003991 }
3992 /* QUIC Client (outgoing connection to servers) */
3993 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003994 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003995 if (dcid_len)
3996 memcpy(qc->dcid.data, dcid, dcid_len);
3997 qc->dcid.len = dcid_len;
3998 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003999 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004000
4001 /* Initialize the output buffer */
4002 qc->obuf.pos = qc->obuf.data;
4003
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004004 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004005 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004006 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004007 goto err;
4008 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004009
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004010 /* insert the allocated CID in the receiver datagram handler tree */
4011 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004012 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004013
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004014 /* Select our SCID which is the first CID with 0 as sequence number. */
4015 qc->scid = icid->cid;
4016
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004017 /* Packet number spaces initialization. */
4018 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4019 quic_pktns_init(&qc->pktns[i]);
4020 /* QUIC encryption level context initialization. */
4021 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004022 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004023 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004024 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004025 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004026 /* Initialize the packet number space. */
4027 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4028 }
4029
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02004030 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004031 qc->tps_tls_ext = qc->version & 0xff000000 ?
4032 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4033 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004034 /* TX part. */
4035 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004036 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4037 qc->tx.wbuf = qc->tx.rbuf = 0;
4038 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004039 /* RX part. */
4040 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004041 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004042
4043 qc->nb_pkt_for_cc = 1;
4044 qc->nb_pkt_since_cc = 0;
4045
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004046 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004047 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004048 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004049 goto err;
4050 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004051
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004052 /* XXX TO DO: Only one path at this time. */
4053 qc->path = &qc->paths[0];
4054 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4055
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004056 /* required to use MTLIST_IN_LIST */
4057 MT_LIST_INIT(&qc->accept_list);
4058
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004059 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004060 qc->stream_buf_count = 0;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004061
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004062 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004063
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004064 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004065
4066 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004067 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004068 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4069 if (qc)
4070 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004071 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004072 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004073}
4074
4075/* Initialize the timer task of <qc> QUIC connection.
4076 * Returns 1 if succeeded, 0 if not.
4077 */
4078static int quic_conn_init_timer(struct quic_conn *qc)
4079{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004080 /* Attach this task to the same thread ID used for the connection */
4081 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082 if (!qc->timer_task)
4083 return 0;
4084
4085 qc->timer = TICK_ETERNITY;
4086 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004087 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004088
4089 return 1;
4090}
4091
Frédéric Lécaille47756802022-03-25 09:12:16 +01004092/* Rearm the idle timer for <qc> QUIC connection. */
4093static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4094{
4095 unsigned int expire;
4096
4097 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4098 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4099}
4100
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004101/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4102 * which is set to 1 when receiving a packet , and 0 when sending packet
4103 */
4104static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4105{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004106 if (read) {
4107 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4108 }
4109 else {
4110 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4111 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004112 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004113}
4114
4115/* The task handling the idle timeout */
4116static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4117{
4118 struct quic_conn *qc = ctx;
4119
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004120 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4121 * might free the quic-conn too early via quic_close().
4122 */
4123 qc_notify_close(qc);
4124
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004125 /* If the MUX is still alive, keep the quic-conn. The MUX is
4126 * responsible to call quic_close to release it.
4127 */
4128 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4129 if (qc->mux_state != QC_MUX_READY)
4130 quic_conn_release(qc);
4131
4132 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4133 * least clean some parts of it such as the tasklet.
4134 */
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004135
4136 return NULL;
4137}
4138
4139/* Initialize the idle timeout task for <qc>.
4140 * Returns 1 if succeeded, 0 if not.
4141 */
4142static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4143{
4144 qc->idle_timer_task = task_new_here();
4145 if (!qc->idle_timer_task)
4146 return 0;
4147
4148 qc->idle_timer_task->process = qc_idle_timer_task;
4149 qc->idle_timer_task->context = qc;
4150 qc_idle_timer_rearm(qc, 1);
4151 task_queue(qc->idle_timer_task);
4152
4153 return 1;
4154}
4155
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004156/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4157 * past one byte of this buffer.
4158 */
4159static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4160 struct quic_rx_packet *pkt)
4161{
4162 unsigned char dcid_len, scid_len;
4163
4164 /* Version */
4165 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4166 return 0;
4167
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168 /* Destination Connection ID Length */
4169 dcid_len = *(*buf)++;
4170 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4171 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4172 /* XXX MUST BE DROPPED */
4173 return 0;
4174
4175 if (dcid_len) {
4176 /* Check that the length of this received DCID matches the CID lengths
4177 * of our implementation for non Initials packets only.
4178 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004179 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4180 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004181 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004182 return 0;
4183
4184 memcpy(pkt->dcid.data, *buf, dcid_len);
4185 }
4186
4187 pkt->dcid.len = dcid_len;
4188 *buf += dcid_len;
4189
4190 /* Source Connection ID Length */
4191 scid_len = *(*buf)++;
4192 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4193 /* XXX MUST BE DROPPED */
4194 return 0;
4195
4196 if (scid_len)
4197 memcpy(pkt->scid.data, *buf, scid_len);
4198 pkt->scid.len = scid_len;
4199 *buf += scid_len;
4200
4201 return 1;
4202}
4203
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004204/* Insert <pkt> RX packet in its <qel> RX packets tree */
4205static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4206{
4207 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004208 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004209 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4210 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4211 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004212}
4213
4214/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4216 * byte past the end of the buffer containing this packet and <beg> the address of
4217 * the packet first byte.
4218 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4219 * Returns 1 if succeeded, 0 if not.
4220 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004221static inline int qc_try_rm_hp(struct quic_conn *qc,
4222 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004223 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004224 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004225 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004226{
4227 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004228 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229 struct quic_enc_level *qel;
4230 /* Only for traces. */
4231 struct quic_rx_packet *qpkt_trace;
4232
4233 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004234 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004235 /* The packet number is here. This is also the start minus
4236 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4237 * protection.
4238 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004239 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004240
4241 tel = quic_packet_type_enc_level(pkt->type);
4242 qel = &qc->els[tel];
4243
4244 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004245 /* Note that the following function enables us to unprotect the packet
4246 * number and its length subsequently used to decrypt the entire
4247 * packets.
4248 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004249 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4250 qel->pktns->rx.largest_pn, pn, beg, end)) {
4251 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252 goto err;
4253 }
4254
4255 /* The AAD includes the packet number field found at <pn>. */
4256 pkt->aad_len = pn - beg + pkt->pnl;
4257 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004258 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004259 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004260 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004261 /* If the packet number space has been discarded, this packet
4262 * will be not parsed.
4263 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004264 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004265 goto out;
4266 }
4267
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004268 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004269 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004270 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4271 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004272 }
4273
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004274 *el = qel;
4275 /* No reference counter incrementation here!!! */
4276 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4277 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4278 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4279 b_add(&qc->rx.buf, pkt->len);
4280 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004281 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004282 return 1;
4283
4284 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004285 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004286 return 0;
4287}
4288
4289/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4290 * Also set <*long_header> to 1 if this form is long, 0 if not.
4291 */
4292static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4293 unsigned char byte0, int *long_header)
4294{
4295 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4296 pkt->type =
4297 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4298 *long_header = 1;
4299 }
4300 else {
4301 pkt->type = QUIC_PACKET_TYPE_SHORT;
4302 *long_header = 0;
4303 }
4304}
4305
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004306/*
4307 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4308 */
4309static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4310{
4311 int j = 0, version;
4312
4313 do {
4314 version = quic_supported_version[j];
4315 if (version == pkt->version)
4316 return 1;
4317
4318 version = quic_supported_version[++j];
4319 } while(version);
4320
4321 return 0;
4322}
4323
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004324/*
4325 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4326 * address <addr>.
4327 * Implementation of RFC9000 6. Version Negotiation
4328 *
4329 * TODO implement a rate-limiting sending of Version Negotiation packets
4330 *
4331 * Returns 0 on success else non-zero
4332 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004333static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4334 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004335{
4336 char buf[256];
4337 int i = 0, j, version;
4338 const socklen_t addrlen = get_addr_len(addr);
4339
4340 /*
4341 * header form
4342 * long header, fixed bit to 0 for Version Negotiation
4343 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004344 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4345 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004346
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004347 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004348 /* null version for Version Negotiation */
4349 buf[i++] = '\x00';
4350 buf[i++] = '\x00';
4351 buf[i++] = '\x00';
4352 buf[i++] = '\x00';
4353
4354 /* source connection id */
4355 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004356 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004357 i += pkt->scid.len;
4358
4359 /* destination connection id */
4360 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004361 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004362 i += pkt->dcid.len;
4363
4364 /* supported version */
4365 j = 0;
4366 do {
4367 version = htonl(quic_supported_version[j]);
4368 memcpy(&buf[i], &version, sizeof(version));
4369 i += sizeof(version);
4370
4371 version = quic_supported_version[++j];
4372 } while (version);
4373
4374 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4375 return 1;
4376
4377 return 0;
4378}
4379
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004380/* Generate the token to be used in Retry packets. The token is written to
4381 * <buf> which is expected to be <len> bytes.
4382 *
4383 * Various parameters are expected to be encoded in the token. For now, only
4384 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4385 * as this CID must be repeated by the server in the transport parameters.
4386 *
4387 * TODO add the client address to validate the token origin.
4388 *
4389 * Returns the length of the encoded token or 0 on error.
4390 */
4391static int generate_retry_token(unsigned char *buf, unsigned char len,
4392 struct quic_rx_packet *pkt)
4393{
4394 const size_t token_len = 1 + pkt->dcid.len;
4395 unsigned char i = 0;
4396
4397 if (token_len > len)
4398 return 0;
4399
4400 buf[i++] = pkt->dcid.len;
4401 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4402 i += pkt->dcid.len;
4403
4404 return i;
4405}
4406
4407/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4408 * the Initial <pkt> packet.
4409 *
4410 * Returns 0 on success else non-zero.
4411 */
4412static int send_retry(int fd, struct sockaddr_storage *addr,
4413 struct quic_rx_packet *pkt)
4414{
4415 unsigned char buf[128];
4416 int i = 0, token_len;
4417 const socklen_t addrlen = get_addr_len(addr);
4418 struct quic_cid scid;
4419
4420 /* long header + fixed bit + packet type 0x3 */
4421 buf[i++] = 0xf0;
4422 /* version */
4423 buf[i++] = 0x00;
4424 buf[i++] = 0x00;
4425 buf[i++] = 0x00;
4426 buf[i++] = 0x01;
4427
4428 /* Use the SCID from <pkt> for Retry DCID. */
4429 buf[i++] = pkt->scid.len;
4430 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4431 i += pkt->scid.len;
4432
4433 /* Generate a new CID to be used as SCID for the Retry packet. */
4434 scid.len = QUIC_HAP_CID_LEN;
4435 if (RAND_bytes(scid.data, scid.len) != 1)
4436 return 1;
4437
4438 buf[i++] = scid.len;
4439 memcpy(&buf[i], scid.data, scid.len);
4440 i += scid.len;
4441
4442 /* token */
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004443 if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004444 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004445
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004446 i += token_len;
4447
4448 /* token integrity tag */
4449 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4450 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4451 pkt->dcid.len, buf, i)) {
4452 return 1;
4453 }
4454
4455 i += QUIC_TLS_TAG_LEN;
4456
4457 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4458 return 1;
4459
4460 return 0;
4461}
4462
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004463/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4464 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4465 * concatenated to the <pkt> DCID field.
4466 *
4467 * Returns the instance or NULL if not found.
4468 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004469static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004470 struct listener *l,
4471 struct sockaddr_storage *saddr)
4472{
4473 struct quic_conn *qc = NULL;
4474 struct ebmb_node *node;
4475 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004476 /* set if the quic_conn is found in the second DCID tree */
4477 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004478
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004479 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4480 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4481 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4482 /* DCIDs of first packets coming from multiple clients may have
4483 * the same values. Let's distinguish them by concatenating the
4484 * socket addresses.
4485 */
4486 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004487 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004488 pkt->dcid.len + pkt->dcid.addrlen);
4489 if (node) {
4490 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4491 goto end;
4492 }
4493 }
4494
4495 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4496 * also for INITIAL/0-RTT non-first packets with the final DCID in
4497 * used.
4498 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004499 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004500 if (!node)
4501 goto end;
4502
4503 id = ebmb_entry(node, struct quic_connection_id, node);
4504 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004505 found_in_dcid = 1;
4506
4507 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004508 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4509 * If already done, this is a noop.
4510 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004511 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004512 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004513
4514 return qc;
4515}
4516
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004517/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4518 * will extract the parameters stored in the token : <odcid>.
4519 *
4520 * Returns 0 on success else non-zero.
4521 */
4522static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4523 struct quic_cid *odcid)
4524{
4525 uint64_t odcid_len;
4526
4527 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4528 return 1;
4529
Frédéric Lécaillef1f812b2022-03-17 16:22:02 +01004530 if (odcid_len > QUIC_CID_MAXLEN)
4531 return 1;
4532
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004533 memcpy(odcid->data, token, odcid_len);
4534 odcid->len = odcid_len;
4535
4536 return 0;
4537}
4538
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004539/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4540 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4541 * parameters of this session.
4542 * This is the responsibility of the caller to check the validity of all the
4543 * pointers passed as parameter to this function.
4544 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4545 * CO_ER_SSL_NO_MEM.
4546 */
4547static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4548 unsigned char *params, size_t params_len)
4549{
4550 int retry;
4551
4552 retry = 1;
4553 retry:
4554 *ssl = SSL_new(ssl_ctx);
4555 if (!*ssl) {
4556 if (!retry--)
4557 goto err;
4558
4559 pool_gc(NULL);
4560 goto retry;
4561 }
4562
4563 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4564 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4565 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004566 SSL_free(*ssl);
4567 *ssl = NULL;
4568 if (!retry--)
4569 goto err;
4570
4571 pool_gc(NULL);
4572 goto retry;
4573 }
4574
4575 return 0;
4576
4577 err:
4578 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4579 return -1;
4580}
4581
4582/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4583 * used to process <qc> received packets. The allocated context is stored in
4584 * <qc.xprt_ctx>.
4585 *
4586 * Returns 0 on success else non-zero.
4587 */
4588int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4589{
4590 struct bind_conf *bc = qc->li->bind_conf;
4591 struct ssl_sock_ctx *ctx = NULL;
4592
4593 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4594 if (!ctx)
4595 goto err;
4596
4597 ctx->wait_event.tasklet = tasklet_new();
4598 if (!ctx->wait_event.tasklet)
4599 goto err;
4600
4601 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4602 ctx->wait_event.tasklet->context = ctx;
4603 ctx->wait_event.events = 0;
4604 ctx->subs = NULL;
4605 ctx->xprt_ctx = NULL;
4606 ctx->qc = qc;
4607
4608 /* Set tasklet tid based on the SCID selected by us for this
4609 * connection. The upper layer will also be binded on the same thread.
4610 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004611 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004612
4613 if (qc_is_listener(qc)) {
4614 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4615 qc->enc_params, qc->enc_params_len) == -1) {
4616 goto err;
4617 }
4618
4619 /* Enabling 0-RTT */
4620 if (bc->ssl_conf.early_data)
4621 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4622
4623 SSL_set_accept_state(ctx->ssl);
4624 }
4625
4626 ctx->xprt = xprt_get(XPRT_QUIC);
4627
4628 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004629 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004630
4631 return 0;
4632
4633 err:
4634 if (ctx && ctx->wait_event.tasklet)
4635 tasklet_free(ctx->wait_event.tasklet);
4636 pool_free(pool_head_quic_conn_ctx, ctx);
4637
4638 return 1;
4639}
4640
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004641static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004642 struct quic_rx_packet *pkt, int first_pkt,
4643 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004644{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004645 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004646 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004647 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004648 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004649 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004650 size_t b_cspace;
4651 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004652
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004653 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004654 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004655 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004656 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004657 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4658 /* This ist only to please to traces and distinguish the
4659 * packet with parsed packet number from others.
4660 */
4661 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004662 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004663 goto err;
4664
4665 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004666 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004667 /* XXX TO BE DISCARDED */
4668 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4669 goto err;
4670 }
4671
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004672 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004673 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004674 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004675 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004676 uint64_t len;
4677
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004678 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004679 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4680 goto err;
4681 }
4682
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004683 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4684 * they must have the same DCID.
4685 */
4686 if (!first_pkt &&
4687 (pkt->dcid.len != dgram->dcid_len ||
4688 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4689 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4690 goto err;
4691 }
4692
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004693 /* Retry of Version Negotiation packets are only sent by servers */
4694 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4695 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4696 goto err;
4697 }
4698
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004699 /* RFC9000 6. Version Negotiation */
4700 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004701 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004702 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004703 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4704 goto err;
4705 }
4706
4707 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004708 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004709 }
4710
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004711 /* For Initial packets, and for servers (QUIC clients connections),
4712 * there is no Initial connection IDs storage.
4713 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004714 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004715 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004716
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004717 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4718 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004719 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4720 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004721 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004722
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004723 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4724 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004725 */
4726 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004727
4728 /* TODO Retry should be automatically activated if
4729 * suspect network usage is detected.
4730 */
4731 if (!token_len && l->bind_conf->quic_force_retry) {
4732 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004733 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004734 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4735 goto err;
4736 }
4737
4738 goto err;
4739 }
4740 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004741 pkt->token = buf;
4742 buf += pkt->token_len;
4743 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004744 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004745 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004746 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004747 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4748 goto err;
4749 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004750 }
4751
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004752 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4753 end - buf < len) {
4754 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4755 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004756 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004757
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004758 payload = buf;
4759 pkt->len = len + payload - beg;
4760
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004761 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004762 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004763 int ipv4;
4764 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004765 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004766 const unsigned char *salt = initial_salt_v1;
4767 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004768
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004769 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004770 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004771 goto err;
4772 }
4773
Frédéric Lécailledc364042022-01-27 16:51:54 +01004774 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4775 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4776 goto err;
4777 }
4778
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004779 pkt->saddr = dgram->saddr;
4780 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004781 qc = qc_new_conn(pkt->version, ipv4,
4782 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004783 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004784 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004785 goto err;
4786
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004787 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4788
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004789 odcid = &qc->rx.params.original_destination_connection_id;
4790 /* Copy the transport parameters. */
4791 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004792
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004793 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004794 if (pkt->token_len) {
4795 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4796 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4797 goto err;
4798 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004799 /* Copy retry_source_connection_id transport parameter. */
4800 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4801 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004802 }
4803 else {
4804 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4805 odcid->len = pkt->dcid.len;
4806 }
4807
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004808 /* Copy the initial source connection ID. */
4809 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4810 qc->enc_params_len =
4811 quic_transport_params_encode(qc->enc_params,
4812 qc->enc_params + sizeof qc->enc_params,
4813 &qc->rx.params, 1);
4814 if (!qc->enc_params_len)
4815 goto err;
4816
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004817 if (qc_conn_alloc_ssl_ctx(qc))
4818 goto err;
4819
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004820 if (!quic_conn_init_timer(qc))
4821 goto err;
4822
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004823 if (!quic_conn_init_idle_timer_task(qc))
4824 goto err;
4825
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004826 /* NOTE: the socket address has been concatenated to the destination ID
4827 * chosen by the client for Initial packets.
4828 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004829 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4830 salt = initial_salt_draft_29;
4831 salt_len = sizeof initial_salt_draft_29;
4832 }
4833 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004834 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004835 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004836 goto err;
4837 }
4838
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004839 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004840 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004841 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004842
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004843 /* If the insertion failed, it means that another
4844 * thread has already allocated a QUIC connection for
4845 * the same CID. Liberate our allocated connection.
4846 */
4847 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004848 qc_to_purge = qc;
4849
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004850 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4851 pkt->qc = qc;
4852 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004853
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004854 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004855 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004856 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004857 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004858 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004859 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004860 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004861 }
4862 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004863 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004864 }
4865 }
4866 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004867 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004868 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4869 goto err;
4870 }
4871
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004872 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004873 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004874
4875 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4876 * they must have the same DCID.
4877 */
4878 if (!first_pkt &&
4879 (pkt->dcid.len != dgram->dcid_len ||
4880 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4881 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4882 goto err;
4883 }
4884
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004885 buf += QUIC_HAP_CID_LEN;
4886
4887 /* A short packet is the last one of a UDP datagram. */
4888 payload = buf;
4889 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004890
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004891 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004892 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004893 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004894 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4895 goto err;
4896 }
4897
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004898 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004899 }
4900
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004901 if (qc->flags & QUIC_FL_CONN_CLOSING) {
4902 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
4903 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
4904 qc->nb_pkt_for_cc++;
4905 qc->nb_pkt_since_cc = 0;
4906 }
4907 /* Skip the entire datagram */
4908 pkt->len = end - beg;
4909 TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc);
4910 goto out;
4911 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004912
4913 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4914 * they must have the same DCID.
4915 *
4916 * This check must be done after the final update to pkt.len to
4917 * properly drop the packet on failure.
4918 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004919 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004920 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004921 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4922 QUIC_EV_CONN_LPKT, qc);
4923 /* Reset the anti-amplification bit. It will be set again
4924 * when sending the next packet if reached again.
4925 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004926 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
4927 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004928 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004929 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004930
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004931 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004932
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004933 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004934 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004935 goto out;
4936 }
4937
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004938 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004939 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004940 b_cspace = b_contig_space(&qc->rx.buf);
4941 if (b_cspace < pkt->len) {
4942 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004943 if (b_cspace) {
4944 b_putchr(&qc->rx.buf, 0x00);
4945 b_cspace--;
4946 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004947 b_add(&qc->rx.buf, b_cspace);
4948 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004949 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004950 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004951 goto err;
4952 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004953 }
4954
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004955 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004956 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004957 goto err;
4958 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004959
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004960 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004961 if (pkt->aad_len)
4962 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004963 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004964 /* Wake up the connection packet handler task from here only if all
4965 * the contexts have been initialized, especially the mux context
4966 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4967 * will start it if these contexts for the connection are not already
4968 * initialized.
4969 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004970 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004971 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004972 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004973
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004974 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004975
4976 return pkt->len;
4977
4978 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004979 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4980 * This cannot be done by this thread.
4981 */
4982 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004983 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004984 if (conn_ctx && conn_ctx->wait_event.tasklet)
4985 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4986 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004987 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004988 if (!pkt->len)
4989 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004990 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004991 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004992
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004993 return -1;
4994}
4995
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004996/* This function builds into <buf> buffer a QUIC long packet header.
4997 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004998 */
4999static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
5000 int type, size_t pn_len, struct quic_conn *conn)
5001{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005002 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005003 return 0;
5004
5005 /* #0 byte flags */
5006 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5007 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5008 /* Version */
5009 quic_write_uint32(buf, end, conn->version);
5010 *(*buf)++ = conn->dcid.len;
5011 /* Destination connection ID */
5012 if (conn->dcid.len) {
5013 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5014 *buf += conn->dcid.len;
5015 }
5016 /* Source connection ID */
5017 *(*buf)++ = conn->scid.len;
5018 if (conn->scid.len) {
5019 memcpy(*buf, conn->scid.data, conn->scid.len);
5020 *buf += conn->scid.len;
5021 }
5022
5023 return 1;
5024}
5025
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005026/* This function builds into <buf> buffer a QUIC short packet header.
5027 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005028 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005029static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5030 size_t pn_len, struct quic_conn *conn,
5031 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005032{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005033 if (end - *buf < 1 + conn->dcid.len)
5034 return 0;
5035
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005036 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005037 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5038 ((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 +01005039 /* Destination connection ID */
5040 if (conn->dcid.len) {
5041 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5042 *buf += conn->dcid.len;
5043 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005044
5045 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005046}
5047
5048/* Apply QUIC header protection to the packet with <buf> as first byte address,
5049 * <pn> as address of the Packet number field, <pnlen> being this field length
5050 * with <aead> as AEAD cipher and <key> as secret key.
5051 * Returns 1 if succeeded or 0 if failed.
5052 */
5053static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5054 const EVP_CIPHER *aead, const unsigned char *key)
5055{
5056 int i, ret, outlen;
5057 EVP_CIPHER_CTX *ctx;
5058 /* We need an IV of at least 5 bytes: one byte for bytes #0
5059 * and at most 4 bytes for the packet number
5060 */
5061 unsigned char mask[5] = {0};
5062
5063 ret = 0;
5064 ctx = EVP_CIPHER_CTX_new();
5065 if (!ctx)
5066 return 0;
5067
5068 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5069 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5070 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5071 goto out;
5072
5073 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5074 for (i = 0; i < pnlen; i++)
5075 pn[i] ^= mask[i + 1];
5076
5077 ret = 1;
5078
5079 out:
5080 EVP_CIPHER_CTX_free(ctx);
5081
5082 return ret;
5083}
5084
5085/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5086 * ACK ranges if needed to a value below <limit> in bytes.
5087 * Return 1 if succeeded, 0 if not.
5088 */
5089static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5090{
5091 size_t room, ack_delay_sz;
5092
5093 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5094 /* A frame is made of 1 byte for the frame type. */
5095 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005096 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005097 return 0;
5098
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005099 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005100}
5101
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005102/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5103 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5104 * with <room> as available room, and <*len> the packet Length field initialized
5105 * with the number of bytes already present in this buffer which must be taken
5106 * into an account for the Length packet field value. <headlen> is the number of
5107 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005108 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005109 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005110 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005111 * Return 1 if succeeded, 0 if not.
5112 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005113static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005114 size_t room, size_t *len, size_t headlen,
5115 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005116 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005117{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005118 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005119 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005120
Frédéric Lécailleea604992020-12-24 13:01:37 +01005121 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005122 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005123 return 0;
5124
Frédéric Lécailleea604992020-12-24 13:01:37 +01005125 /* If we are not probing we must take into an account the congestion
5126 * control window.
5127 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005128 if (!qel->pktns->tx.pto_probe) {
5129 size_t remain = quic_path_prep_data(qc->path);
5130
5131 if (headlen > remain)
5132 return 0;
5133
5134 room = QUIC_MIN(room, remain - headlen);
5135 }
5136
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005137 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005138 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005139 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005140 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005141 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005142
Frédéric Lécailleea604992020-12-24 13:01:37 +01005143 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005144 break;
5145
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005146 switch (cf->type) {
5147 case QUIC_FT_CRYPTO:
5148 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005149 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005150 /* Compute the length of this CRYPTO frame header */
5151 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5152 /* Compute the data length of this CRyPTO frame. */
5153 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5154 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005155 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005156 if (!dlen)
5157 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005158
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005159 /* CRYPTO frame length. */
5160 flen = hlen + quic_int_getsize(dlen) + dlen;
5161 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005162 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005163 /* Add the CRYPTO data length and its encoded length to the packet
5164 * length and the length of this length.
5165 */
5166 *len += flen;
5167 room -= flen;
5168 if (dlen == cf->crypto.len) {
5169 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005170 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005171 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005172 }
5173 else {
5174 struct quic_frame *new_cf;
5175
5176 new_cf = pool_alloc(pool_head_quic_frame);
5177 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005178 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005179 return 0;
5180 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005181
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005182 new_cf->type = QUIC_FT_CRYPTO;
5183 new_cf->crypto.len = dlen;
5184 new_cf->crypto.offset = cf->crypto.offset;
5185 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005186 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005187 /* Consume <dlen> bytes of the current frame. */
5188 cf->crypto.len -= dlen;
5189 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005190 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005191 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005192
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005193 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005194 /* Note that these frames are accepted in short packets only without
5195 * "Length" packet field. Here, <*len> is used only to compute the
5196 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005197 *
5198 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005199 * excepting the variable ones. Note that +1 is for the type of this frame.
5200 */
5201 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005202 ((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 +02005203 /* Compute the data length of this STREAM frame. */
5204 avail_room = room - hlen - *len;
5205 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005206 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005207
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005208 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005209 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005210 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5211 dlen = max_available_room(avail_room, &dlen_sz);
5212 if (dlen > cf->stream.len) {
5213 dlen = cf->stream.len;
5214 }
5215 dlen_sz = quic_int_getsize(dlen);
5216 flen = hlen + dlen_sz + dlen;
5217 }
5218 else {
5219 dlen = QUIC_MIN(avail_room, cf->stream.len);
5220 flen = hlen + dlen;
5221 }
5222 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005223 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005224 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005225 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005226 /* Add the STREAM data length and its encoded length to the packet
5227 * length and the length of this length.
5228 */
5229 *len += flen;
5230 room -= flen;
5231 if (dlen == cf->stream.len) {
5232 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005233 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005234 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005235
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005236 /* The MUX stream might be released at this
5237 * stage. This can most notably happen on
5238 * retransmission.
5239 */
5240 if (qc->mux_state == QC_MUX_READY &&
5241 !cf->stream.stream->release) {
5242 qcc_streams_sent_done(cf->stream.stream->ctx,
5243 cf->stream.len,
5244 cf->stream.offset.key);
5245 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005246 }
5247 else {
5248 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005249 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005250
5251 new_cf = pool_zalloc(pool_head_quic_frame);
5252 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005253 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005254 return 0;
5255 }
5256
5257 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005258 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005259 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005260 new_cf->stream.id = cf->stream.id;
5261 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5262 new_cf->stream.offset = cf->stream.offset;
5263 new_cf->stream.len = dlen;
5264 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5265 /* FIN bit reset */
5266 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5267 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005268 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005269 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5270 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005271 cf_buf = b_make(b_orig(cf->stream.buf),
5272 b_size(cf->stream.buf),
5273 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005274 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005275 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005276 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005277
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005278 /* The MUX stream might be released at this
5279 * stage. This can most notably happen on
5280 * retransmission.
5281 */
5282 if (qc->mux_state == QC_MUX_READY &&
5283 !cf->stream.stream->release) {
5284 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5285 new_cf->stream.len,
5286 new_cf->stream.offset.key);
5287 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005288 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005289
5290 /* TODO the MUX is notified about the frame sending via
5291 * previous qcc_streams_sent_done call. However, the
5292 * sending can fail later, for example if the sendto
5293 * system call returns an error. As the MUX has been
5294 * notified, the transport layer is responsible to
5295 * bufferize and resent the announced data later.
5296 */
5297
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005298 break;
5299
5300 default:
5301 flen = qc_frm_len(cf);
5302 BUG_ON(!flen);
5303 if (flen > room)
5304 continue;
5305
5306 *len += flen;
5307 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005308 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005309 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005310 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005311 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01005312 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005313 }
5314
Frédéric Lécailleea604992020-12-24 13:01:37 +01005315 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005316}
5317
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005318/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005319 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5320 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005321 * filling the buffer with as much frames as possible from <frms> list of
5322 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005323 * 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 +02005324 * 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 +02005325 * having returned from this function.
5326 * 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 +01005327 * number field in this packet. <pn_len> will also have the packet number
5328 * length as value.
5329 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005330 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005331 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005332static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5333 size_t dglen, struct quic_tx_packet *pkt,
5334 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005335 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005336 struct quic_enc_level *qel, struct quic_conn *qc,
5337 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005338{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005339 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005340 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005341 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5342 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005343 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005344 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005345 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005346 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005347 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005348 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005349
Frédéric Lécailleea604992020-12-24 13:01:37 +01005350 /* Length field value with CRYPTO frames if present. */
5351 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005352 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005353 /* When not probing, and no immediate close is required, reduce the size of this
5354 * buffer to respect the congestion controller window.
5355 * 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 +01005356 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005357 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005358 size_t path_room;
5359
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005360 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005361 if (end - beg > path_room)
5362 end = beg + path_room;
5363 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005364
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005365 /* Ensure there is enough room for the TLS encryption tag and a zero token
5366 * length field if any.
5367 */
5368 if (end - pos < QUIC_TLS_TAG_LEN +
5369 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5370 goto no_room;
5371
5372 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005373 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005374 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005375 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005376 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005377 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005378 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005379 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005380 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005381 goto no_room;
5382
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005383 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5384 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005385 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005386 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005387 /* Build an ACK frame if required. */
5388 ack_frm_len = 0;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005389 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
5390 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005391 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005392 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005393 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5394 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5395 * that from here, we do not know if we will have to send a PING frame.
5396 * This will be decided after having computed the ack-eliciting frames
5397 * to be added to this packet.
5398 */
5399 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 +01005400 if (!ack_frm_len)
5401 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005402 }
5403
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005404 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005405 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005406 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005407 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005408 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005409
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005410 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005411 /* Initialize the length of the frames built below to <len>.
5412 * If any frame could be successfully built by qc_build_frms(),
5413 * we will have len_frms > len.
5414 */
5415 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005416 if (!qc_build_frms(&frm_list, frms,
5417 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005418 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005419 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005420 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005421 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005422
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005423 /* Length (of the remaining data). Must not fail because, the buffer size
5424 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5425 * for the encryption tag. It must be taken into an account for the length
5426 * of this packet.
5427 */
5428 if (len_frms)
5429 len = len_frms + QUIC_TLS_TAG_LEN;
5430 else
5431 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005432 /* CONNECTION_CLOSE frame */
5433 if (cc) {
5434 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005435
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005436 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005437 len += qc_frm_len(&cc_frm);
5438 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005439 add_ping_frm = 0;
5440 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005441 len_sz = quic_int_getsize(len);
5442 /* Add this packet size to <dglen> */
5443 dglen += head_len + len_sz + len;
5444 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5445 /* This is a maximum padding size */
5446 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5447 /* The length field value is of this packet is <len> + <padding_len>
5448 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005449 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005450 * sizes from <padding_len>.
5451 */
5452 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5453 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005454 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005455 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005456 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005457 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005458 add_ping_frm = 1;
5459 len += 1;
5460 }
5461 /* 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 +01005462 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005463 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5464 }
5465
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005466 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5467 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005468
5469 /* Packet number field address. */
5470 *buf_pn = pos;
5471
5472 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005473 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5474 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005475
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005476 if (ack_frm_len) {
5477 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
5478 goto no_room;
5479
5480 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005481 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005482 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005483
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005484 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005485 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005486 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005487 unsigned char *spos = pos;
5488
5489 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005490 ssize_t room = end - pos;
5491 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005492 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005493 /* TODO: this should not have happened if qc_build_frms()
5494 * had correctly computed and sized the frames to be
5495 * added to this packet. Note that <cf> was added
5496 * from <frm_list> to <frms> list by qc_build_frms().
5497 */
5498 LIST_DELETE(&cf->list);
5499 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005500 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005501 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005502
5503 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005504 quic_tx_packet_refinc(pkt);
5505 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005506 }
5507 }
5508
5509 /* Build a PING frame if needed. */
5510 if (add_ping_frm) {
5511 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005512 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005513 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005514 }
5515
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005516 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005517 if (cc) {
5518 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
5519 goto no_room;
5520
5521 pkt->flags |= QUIC_FL_TX_PACKET_CC;
5522 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005523
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005524 /* Build a PADDING frame if needed. */
5525 if (padding_len) {
5526 frm.type = QUIC_FT_PADDING;
5527 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005528 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005529 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005530 }
5531
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005532 /* If this packet is ack-eliciting and we are probing let's
5533 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005534 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005535 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5536 qel->pktns->tx.pto_probe)
5537 qel->pktns->tx.pto_probe--;
5538
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005539 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005540 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005541 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005542
5543 return 1;
5544
5545 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005546 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005547 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005548 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
5549
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005550 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005551}
5552
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005553static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005554{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005555 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005556 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005557 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005558 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005559 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005560 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005561 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005562 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005563 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005564 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005565}
5566
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005567/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005568 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
5569 * of prebuilt frames.
5570 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005571 * Return -2 if the packet could not be allocated or encrypted for any reason,
5572 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01005573 * XXX NOTE XXX
5574 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
5575 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
5576 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005577 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005578static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5579 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005580 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005581 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005582 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005583{
5584 /* The pointer to the packet number field. */
5585 unsigned char *buf_pn;
5586 unsigned char *beg, *end, *payload;
5587 int64_t pn;
5588 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005589 struct quic_tls_ctx *tls_ctx;
5590 struct quic_tx_packet *pkt;
5591
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005592 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005593 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005594 pkt = pool_alloc(pool_head_quic_tx_packet);
5595 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005596 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005597 *err = -2;
5598 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005599 }
5600
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005601 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005602 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005603 pn_len = 0;
5604 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005605
5606 pn = qel->pktns->tx.next_pn + 1;
5607 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 +02005608 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005609 *err = -1;
5610 goto err;
5611 }
5612
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005613 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005614 payload = buf_pn + pn_len;
5615 payload_len = end - payload;
5616 aad_len = payload - beg;
5617
5618 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005619 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005620 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005621 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005622 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005623
5624 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005625 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005626 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5627 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005628 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005629 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005630 goto err;
5631 }
5632
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005633 /* Consume a packet number */
5634 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005635 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01005636 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005637 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005638 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5639 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005640 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005641 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005642 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005643 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005644 pkt->in_flight_len = pkt->len;
5645 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005646 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01005647 /* Always reset this flags */
5648 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005649 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
5650 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
5651 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
5652 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005653
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005654 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005655 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005656
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005657 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005658
5659 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005660 /* TODO: what about the frames which have been built
5661 * for this packet.
5662 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005663 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005664 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005665 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005666}
5667
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005668
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005669/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5670 * event subscriber <es> is not allowed to change from a previous call as long
5671 * as at least one event is still subscribed. The <event_type> must only be a
5672 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5673 */
5674static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5675{
Willy Tarreau784b8682022-04-11 14:18:10 +02005676 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005677
5678 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5679 BUG_ON(qcc->subs && qcc->subs != es);
5680
5681 es->events |= event_type;
5682 qcc->subs = es;
5683
5684 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02005685 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005686
5687 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02005688 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005689
5690 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005691}
5692
5693/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5694 * The <es> pointer is not allowed to differ from the one passed to the
5695 * subscribe() call. It always returns zero.
5696 */
5697static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5698{
5699 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5700}
5701
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005702/* Store in <xprt_ctx> the context attached to <conn>.
5703 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005704 */
5705static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5706{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005707 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005708
5709 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5710
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005711 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005712 if (*xprt_ctx)
5713 goto out;
5714
Willy Tarreau784b8682022-04-11 14:18:10 +02005715 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005716
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005717 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005718 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005719
5720 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005721}
5722
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005723/* Start the QUIC transport layer */
5724static int qc_xprt_start(struct connection *conn, void *ctx)
5725{
5726 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005727 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005728
Willy Tarreau784b8682022-04-11 14:18:10 +02005729 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005730 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5731 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02005732 /* prepare a CONNECTION_CLOSE frame */
5733 qc->err_code = QC_ERR_APPLICATION_ERROR;
5734 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle05d4ae62022-04-13 17:40:26 +02005735 return -1;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005736 }
5737
5738 /* mux-quic can now be considered ready. */
5739 qc->mux_state = QC_MUX_READY;
5740
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005741 tasklet_wakeup(qctx->wait_event.tasklet);
5742 return 1;
5743}
5744
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005745static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
5746{
Willy Tarreau784b8682022-04-11 14:18:10 +02005747 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005748 return NULL;
5749
Willy Tarreau784b8682022-04-11 14:18:10 +02005750 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005751}
5752
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005753/* transport-layer operations for QUIC connections. */
5754static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005755 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005756 .subscribe = quic_conn_subscribe,
5757 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005758 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005759 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005760 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5761 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005762 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005763 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005764 .name = "QUIC",
5765};
5766
5767__attribute__((constructor))
5768static void __quic_conn_init(void)
5769{
5770 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5771 xprt_register(XPRT_QUIC, &ssl_quic);
5772}
5773
5774__attribute__((destructor))
5775static void __quic_conn_deinit(void)
5776{
5777 BIO_meth_free(ha_quic_meth);
5778}
5779
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005780/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5781 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005782 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005783 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005784struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005785{
5786 unsigned char *pos;
5787 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005788 struct quic_dghdlr *dghdlr = ctx;
5789 struct quic_dgram *dgram;
5790 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005791
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005792 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005793 pos = dgram->buf;
5794 end = pos + dgram->len;
5795 do {
5796 int ret;
5797 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005798
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005799 pkt = pool_zalloc(pool_head_quic_rx_packet);
5800 if (!pkt)
5801 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005802
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005803 quic_rx_packet_refinc(pkt);
5804 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5805 first_pkt = 0;
5806 pos += pkt->len;
5807 quic_rx_packet_refdec(pkt);
5808 if (ret == -1)
5809 /* If the packet length could not be found, we cannot continue. */
5810 break;
5811 } while (pos < end);
5812
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005813 /* Increasing the received bytes counter by the UDP datagram length
5814 * if this datagram could be associated to a connection.
5815 */
5816 if (dgram->qc)
5817 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005818
5819 /* Mark this datagram as consumed */
5820 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005821 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005822
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005823 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005824
5825 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005826 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005827}
5828
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005829/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5830 * Returns 1 if succeeded, 0 if not.
5831 */
5832static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5833 unsigned char **dcid, size_t *dcid_len)
5834{
5835 int long_header;
5836 size_t minlen, skip;
5837
5838 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5839 goto err;
5840
5841 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5842 minlen = long_header ?
5843 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5844 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005845 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005846 goto err;
5847
5848 buf += skip;
5849 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5850 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5851 goto err;
5852
5853 *dcid = buf;
5854
5855 return 1;
5856
5857 err:
5858 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5859 return 0;
5860}
5861
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005862/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5863 * correct datagram handler.
5864 * Return 1 if a correct datagram could be found, 0 if not.
5865 */
5866int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5867 struct sockaddr_storage *saddr,
5868 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005869{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005870 struct quic_dgram *dgram;
5871 unsigned char *dcid;
5872 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005873 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005874
5875 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005876 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005877
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005878 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005879 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005880 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005881
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005882 cid_tid = quic_get_cid_tid(dcid);
5883
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005884 /* All the members must be initialized! */
5885 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005886 dgram->buf = buf;
5887 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005888 dgram->dcid = dcid;
5889 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005890 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005891 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005892 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005893 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005894
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005895 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005896
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005897 return 1;
5898
5899 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005900 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005901}
5902
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02005903/* Notify the MUX layer if alive about an imminent close of <qc>. */
5904void qc_notify_close(struct quic_conn *qc)
5905{
5906 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
5907 return;
5908
5909 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
5910
5911 /* wake up the MUX */
5912 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
5913 qc->conn->mux->wake(qc->conn);
5914}
5915
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005916/* Function to automatically activate QUIC traces on stdout.
5917 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5918 * Main use for now is in the docker image for QUIC interop testing.
5919 */
5920static void quic_init_stdout_traces(void)
5921{
5922#ifdef ENABLE_QUIC_STDOUT_TRACES
5923 trace_quic.sink = sink_find("stdout");
5924 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005925 trace_quic.state = TRACE_STATE_RUNNING;
5926#endif
5927}
5928INITCALL0(STG_INIT, quic_init_stdout_traces);
5929
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005930/*
5931 * Local variables:
5932 * c-indent-level: 8
5933 * c-basic-offset: 8
5934 * End:
5935 */