blob: abbedfd2ac62a12c78c2dfdb51a528e5e174442d [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>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020048#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010049#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010051#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010052#include <haproxy/ssl_sock.h>
53#include <haproxy/stream_interface.h>
54#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));
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +0200173DECLARE_STATIC_POOL(pool_head_quic_conn_stream, "qc_stream_desc", sizeof(struct qc_stream_desc));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100174
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200175static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +0100176 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100177 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200178 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100179static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state);
Frédéric Lécaille47756802022-03-25 09:12:16 +0100180static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100181static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100182
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100183/* Only for debug purpose */
184struct enc_debug_info {
185 unsigned char *payload;
186 size_t payload_len;
187 unsigned char *aad;
188 size_t aad_len;
189 uint64_t pn;
190};
191
192/* Initializes a enc_debug_info struct (only for debug purpose) */
193static inline void enc_debug_info_init(struct enc_debug_info *edi,
194 unsigned char *payload, size_t payload_len,
195 unsigned char *aad, size_t aad_len, uint64_t pn)
196{
197 edi->payload = payload;
198 edi->payload_len = payload_len;
199 edi->aad = aad;
200 edi->aad_len = aad_len;
201 edi->pn = pn;
202}
203
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100204/* Trace callback for QUIC.
205 * These traces always expect that arg1, if non-null, is of type connection.
206 */
207static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
208 const struct ist where, const struct ist func,
209 const void *a1, const void *a2, const void *a3, const void *a4)
210{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100211 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100212
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100213 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100214 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100216 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100217 if ((mask & QUIC_EV_CONN_INIT) && qc) {
218 chunk_appendf(&trace_buf, "\n odcid");
219 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100220 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100221 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100222 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100223 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100224 }
225
226 if (mask & QUIC_EV_CONN_ADDDATA) {
227 const enum ssl_encryption_level_t *level = a2;
228 const size_t *len = a3;
229
230 if (level) {
231 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
232
233 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
234 }
235 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100236 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100237 }
238 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
239 /* Initial read & write secrets. */
240 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
241 const unsigned char *rx_sec = a2;
242 const unsigned char *tx_sec = a3;
243
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100244 tls_ctx = &qc->els[level].tls_ctx;
245 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100246 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
247 if (rx_sec)
248 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100249 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100250 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
251 if (tx_sec)
252 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100253 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100254 }
255 }
256 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
257 const enum ssl_encryption_level_t *level = a2;
258 const unsigned char *secret = a3;
259 const size_t *secret_len = a4;
260
261 if (level) {
262 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
263
264 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
265 if (secret && secret_len)
266 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100267 tls_ctx = &qc->els[lvl].tls_ctx;
268 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
269 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100270 }
271 }
272
273 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
274 const enum ssl_encryption_level_t *level = a2;
275 const unsigned char *secret = a3;
276 const size_t *secret_len = a4;
277
278 if (level) {
279 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
280
281 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
282 if (secret && secret_len)
283 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100284 tls_ctx = &qc->els[lvl].tls_ctx;
285 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
286 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287 }
288
289 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200291 if (mask & QUIC_EV_CONN_FRMLIST) {
292 const struct list *l = a2;
293
294 if (l) {
295 const struct quic_frame *frm;
296 list_for_each_entry(frm, l, list)
297 chunk_frm_appendf(&trace_buf, frm);
298 }
299 }
300
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100301 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100302 const struct quic_tx_packet *pkt = a2;
303 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100304 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100305
306 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100307 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100308 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100309 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100310 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 (unsigned long long)qc->path->cwnd,
312 (unsigned long long)qc->path->prep_in_flight,
313 (unsigned long long)qc->path->in_flight,
314 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100315 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200318 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100319 if (pkt->pn_node.key != (uint64_t)-1)
320 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100321 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100322 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100323 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
324 (unsigned long long)qc->rx.bytes,
325 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100326 }
327
328 if (room) {
329 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
330 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
331 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100332 }
333 }
334
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100335 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100336 const enum quic_handshake_state *state = a2;
337 const int *err = a3;
338
339 if (state)
340 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
341 if (err)
342 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
343 }
344
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100345 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100346 const struct quic_rx_packet *pkt = a2;
347 const unsigned long *pktlen = a3;
348 const SSL *ssl = a4;
349
350 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200351 chunk_appendf(&trace_buf, " pkt@%p", pkt);
352 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
353 chunk_appendf(&trace_buf, " kp=%d",
354 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
355 chunk_appendf(&trace_buf, " el=%c",
356 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100357 if (pkt->pnl)
358 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
359 (unsigned long long)pkt->pn);
360 if (pkt->token_len)
361 chunk_appendf(&trace_buf, " toklen=%llu",
362 (unsigned long long)pkt->token_len);
363 if (pkt->aad_len)
364 chunk_appendf(&trace_buf, " aadlen=%llu",
365 (unsigned long long)pkt->aad_len);
366 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
367 pkt->flags, (unsigned long long)pkt->len);
368 }
369 if (pktlen)
370 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
371 if (ssl) {
372 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
373 chunk_appendf(&trace_buf, " el=%c",
374 quic_enc_level_char(ssl_to_quic_enc_level(level)));
375 }
376 }
377
378 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
379 const struct quic_rx_packet *pkt = a2;
380 const struct quic_rx_crypto_frm *cf = a3;
381 const SSL *ssl = a4;
382
383 if (pkt)
384 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
385 quic_packet_type_enc_level_char(pkt->type),
386 (unsigned long long)pkt->pn);
387 if (cf)
388 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
389 (unsigned long long)cf->offset_node.key,
390 (unsigned long long)cf->len);
391 if (ssl) {
392 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200393 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100394 quic_enc_level_char(ssl_to_quic_enc_level(level)));
395 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100396
397 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100398 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100399 }
400
401 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
402 const struct quic_frame *frm = a2;
403
404 if (frm)
405 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
406 }
407
408 if (mask & QUIC_EV_CONN_PHPKTS) {
409 const struct quic_enc_level *qel = a2;
410
411 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100412 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100414 " 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 +0100415 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200416 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200417 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100418 (unsigned long long)qc->path->cwnd,
419 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100420 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100421 (unsigned long long)pktns->tx.in_flight,
422 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100423 }
424 }
425
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100426 if (mask & QUIC_EV_CONN_ENCPKT) {
427 const struct enc_debug_info *edi = a2;
428
429 if (edi)
430 chunk_appendf(&trace_buf,
431 " payload=@%p payload_len=%llu"
432 " aad=@%p aad_len=%llu pn=%llu",
433 edi->payload, (unsigned long long)edi->payload_len,
434 edi->aad, (unsigned long long)edi->aad_len,
435 (unsigned long long)edi->pn);
436 }
437
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100438 if (mask & QUIC_EV_CONN_RMHP) {
439 const struct quic_rx_packet *pkt = a2;
440
441 if (pkt) {
442 const int *ret = a3;
443
444 chunk_appendf(&trace_buf, " pkt@%p", pkt);
445 if (ret && *ret)
446 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
447 pkt->pnl, (unsigned long long)pkt->pn);
448 }
449 }
450
451 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200452 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100453 const unsigned long *val1 = a3;
454 const unsigned long *val2 = a4;
455
456 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100457 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100458 if (val1)
459 chunk_appendf(&trace_buf, " %lu", *val1);
460 if (val2)
461 chunk_appendf(&trace_buf, "..%lu", *val2);
462 }
463
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100464 if (mask & QUIC_EV_CONN_ACKSTRM) {
465 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200466 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100467
468 if (s)
469 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200470 if (stream)
471 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100472 }
473
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100474 if (mask & QUIC_EV_CONN_RTTUPDT) {
475 const unsigned int *rtt_sample = a2;
476 const unsigned int *ack_delay = a3;
477 const struct quic_loss *ql = a4;
478
479 if (rtt_sample)
480 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
481 if (ack_delay)
482 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
483 if (ql)
484 chunk_appendf(&trace_buf,
485 " srtt=%ums rttvar=%ums min_rtt=%ums",
486 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
487 }
488 if (mask & QUIC_EV_CONN_CC) {
489 const struct quic_cc_event *ev = a2;
490 const struct quic_cc *cc = a3;
491
492 if (a2)
493 quic_cc_event_trace(&trace_buf, ev);
494 if (a3)
495 quic_cc_state_trace(&trace_buf, cc);
496 }
497
498 if (mask & QUIC_EV_CONN_PKTLOSS) {
499 const struct quic_pktns *pktns = a2;
500 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100501
502 if (pktns) {
503 chunk_appendf(&trace_buf, " pktns=%s",
504 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
505 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
506 if (pktns->tx.loss_time)
507 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100508 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509 }
510 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
511 struct quic_tx_packet *pkt;
512
513 chunk_appendf(&trace_buf, " lost_pkts:");
514 list_for_each_entry(pkt, lost_pkts, list)
515 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
516 }
517 }
518
519 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100520 const struct quic_pktns *pktns = a2;
521 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100522 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100523
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100524 if (ifae_pkts)
525 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
526 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100527 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100528 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100529 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100530 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
531 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100532 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
533 if (pktns->tx.in_flight)
534 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100535 if (pktns->tx.loss_time)
536 chunk_appendf(&trace_buf, " loss_time=%dms",
537 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
538 }
539 if (mask & QUIC_EV_CONN_SPTO) {
540 if (pktns->tx.time_of_last_eliciting)
541 chunk_appendf(&trace_buf, " tole=%dms",
542 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
543 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100544 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100545 }
546 }
547
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200548 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100549 chunk_appendf(&trace_buf,
550 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
551 }
552 }
553
554 if (mask & QUIC_EV_CONN_SPPKTS) {
555 const struct quic_tx_packet *pkt = a2;
556
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100557 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
558 (unsigned long long)qc->path->cwnd,
559 (unsigned long long)qc->path->prep_in_flight,
560 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100561 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100562 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100563 (unsigned long)pkt->pn_node.key,
564 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
565 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100566 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100567 }
568 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100569
570 if (mask & QUIC_EV_CONN_SSLALERT) {
571 const uint8_t *alert = a2;
572 const enum ssl_encryption_level_t *level = a3;
573
574 if (alert)
575 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
576 if (level)
577 chunk_appendf(&trace_buf, " el=%c",
578 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
579 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100580
581 if (mask & QUIC_EV_CONN_BCFRMS) {
582 const size_t *sz1 = a2;
583 const size_t *sz2 = a3;
584 const size_t *sz3 = a4;
585
586 if (sz1)
587 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
588 if (sz2)
589 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
590 if (sz3)
591 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
592 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100593
594 if (mask & QUIC_EV_CONN_PSTRM) {
595 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100596
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100597 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100598 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100599 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100600 }
601 if (mask & QUIC_EV_CONN_LPKT) {
602 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200603 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100604
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100605 if (pkt) {
606 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
607 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
608 if (pkt->pn_node.key != (uint64_t)-1)
609 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
610 }
611
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200612 if (len)
613 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100614 }
615
616}
617
618/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100619static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200621 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100623 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100624 return 1;
625
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200626 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
627 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200628 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
629 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200630 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100631 return 1;
632
633 return 0;
634}
635
636/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
637 * both loss detection and PTO and schedule the task assiated to this timer if needed.
638 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100639static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100640{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100641 struct quic_pktns *pktns;
642 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200643 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100644
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100645 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100646 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100647
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100648 pktns = quic_loss_pktns(qc);
649 if (tick_isset(pktns->tx.loss_time)) {
650 qc->timer = pktns->tx.loss_time;
651 goto out;
652 }
653
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100654 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100655 * cancelled for a server which reached the anti-amplification limit.
656 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100657 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200658 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100659 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100660 qc->timer = TICK_ETERNITY;
661 goto out;
662 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100663
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100664 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100665 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100666 /* Timer cancellation. */
667 qc->timer = TICK_ETERNITY;
668 goto out;
669 }
670
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200671 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200672 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100673 if (tick_isset(pto))
674 qc->timer = pto;
675 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100676 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100677 if (tick_is_expired(qc->timer, now_ms)) {
678 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100679 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100680 }
681 else {
682 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100683 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100684 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100685 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100686 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100687}
688
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100689/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
690 * connection.
691 * Return 1 if succeeded, 0 if not.
692 */
693static int quic_tls_key_update(struct quic_conn *qc)
694{
695 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
696 struct quic_tls_secrets *rx, *tx;
697 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
698 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
699
700 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
701 rx = &tls_ctx->rx;
702 tx = &tls_ctx->tx;
703 nxt_rx = &qc->ku.nxt_rx;
704 nxt_tx = &qc->ku.nxt_tx;
705
706 /* Prepare new RX secrets */
707 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
708 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100709 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100710 return 0;
711 }
712
713 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
714 nxt_rx->key, nxt_rx->keylen,
715 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
716 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100717 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100718 return 0;
719 }
720
721 /* Prepare new TX secrets */
722 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
723 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100724 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100725 return 0;
726 }
727
728 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
729 nxt_tx->key, nxt_tx->keylen,
730 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
731 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100732 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100733 return 0;
734 }
735
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200736 if (nxt_rx->ctx) {
737 EVP_CIPHER_CTX_free(nxt_rx->ctx);
738 nxt_rx->ctx = NULL;
739 }
740
741 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
742 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
743 return 0;
744 }
745
746 if (nxt_tx->ctx) {
747 EVP_CIPHER_CTX_free(nxt_tx->ctx);
748 nxt_tx->ctx = NULL;
749 }
750
751 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
752 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
753 return 0;
754 }
755
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100756 return 1;
757}
758
759/* Rotate the Key Update information for <qc> QUIC connection.
760 * Must be used after having updated them.
761 * Always succeeds.
762 */
763static void quic_tls_rotate_keys(struct quic_conn *qc)
764{
765 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
766 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200767 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100768
769 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200770 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100771 curr_secret = tls_ctx->rx.secret;
772 curr_iv = tls_ctx->rx.iv;
773 curr_key = tls_ctx->rx.key;
774
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200775 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100776 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
777 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
778 tls_ctx->rx.key = qc->ku.nxt_rx.key;
779
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200780 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100781 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
782 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
783 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
784
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200785 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100786 qc->ku.prv_rx.secret = curr_secret;
787 qc->ku.prv_rx.iv = curr_iv;
788 qc->ku.prv_rx.key = curr_key;
789 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
790
791 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200792 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100793 curr_secret = tls_ctx->tx.secret;
794 curr_iv = tls_ctx->tx.iv;
795 curr_key = tls_ctx->tx.key;
796
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200797 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100798 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
799 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
800 tls_ctx->tx.key = qc->ku.nxt_tx.key;
801
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200802 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100803 qc->ku.nxt_tx.secret = curr_secret;
804 qc->ku.nxt_tx.iv = curr_iv;
805 qc->ku.nxt_tx.key = curr_key;
806}
807
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100808#ifndef OPENSSL_IS_BORINGSSL
809int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
810 const uint8_t *read_secret,
811 const uint8_t *write_secret, size_t secret_len)
812{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100813 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
814 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 +0100815 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100816 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100817
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100818 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100819 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200820 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100821 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100822 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100823 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100824
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100825 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100826 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100827 goto err;
828 }
829
830 rx = &tls_ctx->rx;
831 tx = &tls_ctx->tx;
832
833 rx->aead = tx->aead = tls_aead(cipher);
834 rx->md = tx->md = tls_md(cipher);
835 rx->hp = tx->hp = tls_hp(cipher);
836
837 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
838 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100839 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100840 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100841 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100842 }
843
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200844 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
845 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
846 goto err;
847 }
848
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100849 /* Enqueue this connection asap if we could derive O-RTT secrets as
850 * listener. Note that a listener derives only RX secrets for this
851 * level.
852 */
853 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
854 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100855
856 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100857 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100858
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100859 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
860 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100861 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100862 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100863 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100864 }
865
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200866 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
867 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
868 goto err;
869 }
870
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100871 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100872 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
873 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
874 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
875
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200876 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100877 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
878 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100879 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100880 goto err;
881 }
882
883 memcpy(rx->secret, read_secret, secret_len);
884 rx->secretlen = secret_len;
885 memcpy(tx->secret, write_secret, secret_len);
886 tx->secretlen = secret_len;
887 /* Initialize all the secret keys lengths */
888 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
889 /* Prepare the next key update */
890 if (!quic_tls_key_update(qc))
891 goto err;
892 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100893
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100894 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100895 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100896 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100897 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100898 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100899
900 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100901 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100902 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100903}
904#else
905/* ->set_read_secret callback to derive the RX secrets at <level> encryption
906 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500907 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100908 */
909int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
910 const SSL_CIPHER *cipher,
911 const uint8_t *secret, size_t secret_len)
912{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100913 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100914 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100915 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100916
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100917 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200918 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100919 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100920 goto out;
921 }
922
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100923 tls_ctx->rx.aead = tls_aead(cipher);
924 tls_ctx->rx.md = tls_md(cipher);
925 tls_ctx->rx.hp = tls_hp(cipher);
926
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100927 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
928 goto err;
929
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930 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 +0100931 tls_ctx->rx.key, tls_ctx->rx.keylen,
932 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100933 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
934 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100935 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100936 goto err;
937 }
938
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100939 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100940 const unsigned char *buf;
941 size_t buflen;
942
943 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
944 if (!buflen)
945 goto err;
946
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100947 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100948 goto err;
949 }
950
951 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100952 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100953 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100954
955 return 1;
956
957 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100958 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100959 return 0;
960}
961
962/* ->set_write_secret callback to derive the TX secrets at <level>
963 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500964 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100965 */
966int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
967 const SSL_CIPHER *cipher,
968 const uint8_t *secret, size_t secret_len)
969{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100970 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
971 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 +0100972
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100973 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200974 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100975 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100976 goto out;
977 }
978
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100979 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
980 goto err;
981
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100982 tls_ctx->tx.aead = tls_aead(cipher);
983 tls_ctx->tx.md = tls_md(cipher);
984 tls_ctx->tx.hp = tls_hp(cipher);
985
986 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 +0100987 tls_ctx->tx.key, tls_ctx->tx.keylen,
988 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100989 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
990 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100991 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100992 goto err;
993 }
994
995 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100996 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100997 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100998 return 1;
999
1000 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001001 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001002 return 0;
1003}
1004#endif
1005
1006/* This function copies the CRYPTO data provided by the TLS stack found at <data>
1007 * with <len> as size in CRYPTO buffers dedicated to store the information about
1008 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
1009 * It fails only if it could not managed to allocate enough CRYPTO buffers to
1010 * store all the data.
1011 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
1012 */
1013static int quic_crypto_data_cpy(struct quic_enc_level *qel,
1014 const unsigned char *data, size_t len)
1015{
1016 struct quic_crypto_buf **qcb;
1017 /* The remaining byte to store in CRYPTO buffers. */
1018 size_t cf_offset, cf_len, *nb_buf;
1019 unsigned char *pos;
1020
1021 nb_buf = &qel->tx.crypto.nb_buf;
1022 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
1023 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
1024 cf_len = len;
1025
1026 while (len) {
1027 size_t to_copy, room;
1028
1029 pos = (*qcb)->data + (*qcb)->sz;
1030 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
1031 to_copy = len > room ? room : len;
1032 if (to_copy) {
1033 memcpy(pos, data, to_copy);
1034 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
1035 qel->tx.crypto.sz += to_copy;
1036 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001037 len -= to_copy;
1038 data += to_copy;
1039 }
1040 else {
1041 struct quic_crypto_buf **tmp;
1042
1043 tmp = realloc(qel->tx.crypto.bufs,
1044 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1045 if (tmp) {
1046 qel->tx.crypto.bufs = tmp;
1047 qcb = &qel->tx.crypto.bufs[*nb_buf];
1048 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1049 if (!*qcb)
1050 return 0;
1051
1052 (*qcb)->sz = 0;
1053 ++*nb_buf;
1054 }
1055 else {
1056 break;
1057 }
1058 }
1059 }
1060
1061 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1062 * have been buffered.
1063 */
1064 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001065 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001066 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001067
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001068 /* There is at most one CRYPTO frame in this packet number
1069 * space. Let's look for it.
1070 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001071 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001072 if (frm->type != QUIC_FT_CRYPTO)
1073 continue;
1074
1075 /* Found */
1076 found = frm;
1077 break;
1078 }
1079
1080 if (found) {
1081 found->crypto.len += cf_len;
1082 }
1083 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001084 frm = pool_alloc(pool_head_quic_frame);
1085 if (!frm)
1086 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001087
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001088 frm->type = QUIC_FT_CRYPTO;
1089 frm->crypto.offset = cf_offset;
1090 frm->crypto.len = cf_len;
1091 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001092 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001093 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001094 }
1095
1096 return len == 0;
1097}
1098
1099
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001100/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1101void quic_set_tls_alert(struct quic_conn *qc, int alert)
1102{
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001103 qc->err_code = QC_ERR_CRYPTO_ERROR | alert;
1104 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001105 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001106}
1107
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001108/* Set the application for <qc> QUIC connection.
1109 * Return 1 if succeeded, 0 if not.
1110 */
1111int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1112{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001113 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1114 qc->app_ops = &h3_ops;
1115 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1116 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001117 else
1118 return 0;
1119
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001120 return 1;
1121}
1122
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001123/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1124 * wants to provide the QUIC layer with CRYPTO data.
1125 * Returns 1 if succeeded, 0 if not.
1126 */
1127int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1128 const uint8_t *data, size_t len)
1129{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001130 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001131 enum quic_tls_enc_level tel;
1132 struct quic_enc_level *qel;
1133
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001134 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1135 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001136 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001137 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001138 goto out;
1139 }
1140
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001141 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001142 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001143 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144 goto err;
1145 }
1146
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001147 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001148 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001149 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001150 goto err;
1151 }
1152
1153 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001154 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001156 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001157 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001158 return 1;
1159
1160 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001161 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001162 return 0;
1163}
1164
1165int ha_quic_flush_flight(SSL *ssl)
1166{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001167 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001168
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001169 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1170 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171
1172 return 1;
1173}
1174
1175int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1176{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001177 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001178
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001179 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1180 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001181 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001182 return 1;
1183}
1184
1185/* QUIC TLS methods */
1186static SSL_QUIC_METHOD ha_quic_method = {
1187#ifdef OPENSSL_IS_BORINGSSL
1188 .set_read_secret = ha_set_rsec,
1189 .set_write_secret = ha_set_wsec,
1190#else
1191 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1192#endif
1193 .add_handshake_data = ha_quic_add_handshake_data,
1194 .flush_flight = ha_quic_flush_flight,
1195 .send_alert = ha_quic_send_alert,
1196};
1197
1198/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1199 * Returns an error count.
1200 */
1201int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1202{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001203 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1204 int cfgerr = 0;
1205
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001206 long options =
1207 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1208 SSL_OP_SINGLE_ECDH_USE |
1209 SSL_OP_CIPHER_SERVER_PREFERENCE;
1210 SSL_CTX *ctx;
1211
1212 ctx = SSL_CTX_new(TLS_server_method());
1213 bind_conf->initial_ctx = ctx;
1214
1215 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001216 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1217 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1218 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001219
1220#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1221#ifdef OPENSSL_IS_BORINGSSL
1222 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1223 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1224#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1225 if (bind_conf->ssl_conf.early_data) {
1226 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001227 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001228 }
1229 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1230 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1231#else
1232 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1233#endif
1234 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1235#endif
1236 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1237
1238 return cfgerr;
1239}
1240
1241/* Decode an expected packet number from <truncated_on> its truncated value,
1242 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1243 * the number of bits used to encode this packet number (its length in bytes * 8).
1244 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1245 */
1246static uint64_t decode_packet_number(uint64_t largest_pn,
1247 uint32_t truncated_pn, unsigned int pn_nbits)
1248{
1249 uint64_t expected_pn = largest_pn + 1;
1250 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1251 uint64_t pn_hwin = pn_win / 2;
1252 uint64_t pn_mask = pn_win - 1;
1253 uint64_t candidate_pn;
1254
1255
1256 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1257 /* Note that <pn_win> > <pn_hwin>. */
1258 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1259 candidate_pn + pn_hwin <= expected_pn)
1260 return candidate_pn + pn_win;
1261
1262 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1263 return candidate_pn - pn_win;
1264
1265 return candidate_pn;
1266}
1267
1268/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1269 * cryptographic context.
1270 * <largest_pn> is the largest received packet number and <pn> the address of
1271 * the packet number field for this packet with <byte0> address of its first byte.
1272 * <end> points to one byte past the end of this packet.
1273 * Returns 1 if succeeded, 0 if not.
1274 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001275static int qc_do_rm_hp(struct quic_conn *qc,
1276 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001277 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001278 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001279{
1280 int ret, outlen, i, pnlen;
1281 uint64_t packet_number;
1282 uint32_t truncated_pn = 0;
1283 unsigned char mask[5] = {0};
1284 unsigned char *sample;
1285 EVP_CIPHER_CTX *cctx;
1286 unsigned char *hp_key;
1287
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001288 /* Check there is enough data in this packet. */
1289 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001290 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001291 return 0;
1292 }
1293
1294 cctx = EVP_CIPHER_CTX_new();
1295 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001296 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001297 return 0;
1298 }
1299
1300 ret = 0;
1301 sample = pn + QUIC_PACKET_PN_MAXLEN;
1302
1303 hp_key = tls_ctx->rx.hp_key;
1304 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1305 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1306 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001307 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308 goto out;
1309 }
1310
1311 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1312 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1313 for (i = 0; i < pnlen; i++) {
1314 pn[i] ^= mask[i + 1];
1315 truncated_pn = (truncated_pn << 8) | pn[i];
1316 }
1317
1318 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1319 /* Store remaining information for this unprotected header */
1320 pkt->pn = packet_number;
1321 pkt->pnl = pnlen;
1322
1323 ret = 1;
1324
1325 out:
1326 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001327
1328 return ret;
1329}
1330
1331/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1332 * address, with <payload_len> as payload length, <aad> as address of
1333 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1334 * context.
1335 * Returns 1 if succeeded, 0 if not.
1336 */
1337static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1338 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001339 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001340{
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001341 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001342 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001343 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001344 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345
1346 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001347 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001348 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001349 }
1350
1351 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001352 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001353 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001354 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001355 }
1356
1357 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001358
1359 err:
1360 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001361 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001362 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001363}
1364
1365/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1366 * Returns 1 if succeeded, 0 if not.
1367 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001368static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001369{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001370 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001371 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001372 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001373 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001374 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001375 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1376 unsigned char *rx_key = tls_ctx->rx.key;
1377
1378 kp_changed = 0;
1379 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1380 /* The two tested bits are not at the same position,
1381 * this is why they are first both inversed.
1382 */
1383 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1384 if (pkt->pn < tls_ctx->rx.pn) {
1385 /* The lowest packet number of a previous key phase
1386 * cannot be null if it really stores previous key phase
1387 * secrets.
1388 */
1389 if (!pkt->qc->ku.prv_rx.pn)
1390 return 0;
1391
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001392 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1393 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001394 rx_key = pkt->qc->ku.prv_rx.key;
1395 }
1396 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1397 /* Next key phase */
1398 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001399 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1400 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001401 rx_key = pkt->qc->ku.nxt_rx.key;
1402 }
1403 }
1404 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001405
1406 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1407 return 0;
1408
1409 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1410 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001411 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001412 if (!ret)
1413 return 0;
1414
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001415 /* Update the keys only if the packet decryption succeeded. */
1416 if (kp_changed) {
1417 quic_tls_rotate_keys(pkt->qc);
1418 /* Toggle the Key Phase bit */
1419 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1420 /* Store the lowest packet number received for the current key phase */
1421 tls_ctx->rx.pn = pkt->pn;
1422 /* Prepare the next key update */
1423 if (!quic_tls_key_update(pkt->qc))
1424 return 0;
1425 }
1426
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001427 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001428 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001429
1430 return 1;
1431}
1432
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02001433/* Free the stream descriptor <stream> buffer. This function should be used
1434 * when all its data have been acknowledged. If the stream was released by the
1435 * upper layer, the stream descriptor will be freed.
1436 *
1437 * Returns 0 if the stream was not freed else non-zero.
1438 */
1439static int qc_stream_desc_free(struct qc_stream_desc *stream)
1440{
1441 b_free(&stream->buf);
1442 offer_buffers(NULL, 1);
1443
1444 if (stream->release) {
1445 eb64_delete(&stream->by_id);
1446 pool_free(pool_head_quic_conn_stream, stream);
1447
1448 return 1;
1449 }
1450
1451 return 0;
1452}
1453
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001454/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001455 *
1456 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001457 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001458static int quic_stream_try_to_consume(struct quic_conn *qc,
1459 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001460{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001461 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001462 struct eb64_node *frm_node;
1463
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001464 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001465 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001466 while (frm_node) {
1467 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001468 struct quic_frame *frm;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001469
1470 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001471 if (strm->offset.key > stream->ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001472 break;
1473
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001474 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001475 qc, strm, stream);
1476
1477 if (strm->offset.key + strm->len > stream->ack_offset) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001478 const size_t diff = strm->offset.key + strm->len -
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001479 stream->ack_offset;
1480 stream->ack_offset += diff;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001481 b_del(strm->buf, diff);
1482 ret = 1;
1483 }
1484
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001485 frm_node = eb64_next(frm_node);
1486 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001487
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001488 frm = container_of(strm, struct quic_frame, stream);
1489 LIST_DELETE(&frm->list);
1490 quic_tx_packet_refdec(frm->pkt);
1491 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001492 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001493
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001494 if (!b_data(&stream->buf))
1495 qc_stream_desc_free(stream);
1496
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001497 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001498}
1499
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001500/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001501static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1502 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001503{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001504 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001505
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001506 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001507 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001508 switch (frm->type) {
1509 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1510 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001511 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001512 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001513 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001514
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001515 /* do not use strm_frm->stream as the qc_stream_desc instance
1516 * might be freed at this stage. Use the id to do a proper
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001517 * lookup. First search in the MUX then in the released stream
1518 * list.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001519 *
1520 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001521 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001522 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001523 if (qc->mux_state == QC_MUX_READY)
1524 stream = qcc_get_stream(qc->qcc, strm_frm->id);
1525 if (!stream) {
1526 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1527 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1528 }
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001529
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001530 if (!stream) {
1531 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001532 LIST_DELETE(&frm->list);
1533 quic_tx_packet_refdec(frm->pkt);
1534 pool_free(pool_head_quic_frame, frm);
1535
1536 /* early return */
1537 return;
1538 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001539
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001540 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1541 if (strm_frm->offset.key <= stream->ack_offset) {
1542 if (strm_frm->offset.key + strm_frm->len > stream->ack_offset) {
1543 const size_t diff = strm_frm->offset.key + strm_frm->len -
1544 stream->ack_offset;
1545 stream->ack_offset += diff;
1546 b_del(strm_frm->buf, diff);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001547 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001548
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001549 if (!b_data(strm_frm->buf)) {
1550 if (qc_stream_desc_free(stream)) {
1551 /* early return */
1552 return;
1553 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001554 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001555 }
1556
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001557 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001558 qc, strm_frm, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001559 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001560 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001561 pool_free(pool_head_quic_frame, frm);
1562 }
1563 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001564 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001565 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001566
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001567 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001568 }
1569 break;
1570 default:
1571 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001572 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001573 pool_free(pool_head_quic_frame, frm);
1574 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001575
1576 if (stream_acked) {
1577 struct qcc *qcc = qc->qcc;
1578
1579 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1580 tasklet_wakeup(qcc->subs->tasklet);
1581 qcc->subs->events &= ~SUB_RETRY_SEND;
1582 if (!qcc->subs->events)
1583 qcc->subs = NULL;
1584 }
1585 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001586}
1587
1588/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1589 * deallocating them, and their TX frames.
1590 * Returns the last node reached to be used for the next range.
1591 * May be NULL if <largest> node could not be found.
1592 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001593static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1594 struct eb_root *pkts,
1595 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001596 struct list *newly_acked_pkts,
1597 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001598 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001599{
1600 struct eb64_node *node;
1601 struct quic_tx_packet *pkt;
1602
1603 if (largest_node)
1604 node = largest_node;
1605 else {
1606 node = eb64_lookup(pkts, largest);
1607 while (!node && largest > smallest) {
1608 node = eb64_lookup(pkts, --largest);
1609 }
1610 }
1611
1612 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001613 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001614
1615 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1616 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001617 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001618 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001619 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001620 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001621 node = eb64_prev(node);
1622 eb64_delete(&pkt->pn_node);
1623 }
1624
1625 return node;
1626}
1627
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001628/* Remove all frames from <pkt_frm_list> and reinsert them in the
1629 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001630 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001631static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1632 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001633 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001635 struct quic_frame *frm, *frmbak;
1636 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001637
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001638 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1639 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001640 quic_tx_packet_refdec(frm->pkt);
1641 /* This frame is not freed but removed from its packet */
1642 frm->pkt = NULL;
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001643 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001644 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001645 }
1646
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001647 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001648}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001649
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001650/* Free <pkt> TX packet and its attached frames.
1651 * This is the responsability of the caller to remove this packet of
1652 * any data structure it was possibly attached to.
1653 */
1654static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1655{
1656 struct quic_frame *frm, *frmbak;
1657
1658 if (!pkt)
1659 return;
1660
1661 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1662 LIST_DELETE(&frm->list);
1663 pool_free(pool_head_quic_frame, frm);
1664 }
1665 pool_free(pool_head_quic_tx_packet, pkt);
1666}
1667
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001668/* Free the TX packets of <pkts> list */
1669static inline void free_quic_tx_pkts(struct list *pkts)
1670{
1671 struct quic_tx_packet *pkt, *tmp;
1672
1673 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001674 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001675 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001676 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001677 }
1678}
1679
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001680/* Remove already sent ranges of acknowledged packet numbers from
1681 * <pktns> packet number space tree below <largest_acked_pn> possibly
1682 * updating the range which contains <largest_acked_pn>.
1683 * Never fails.
1684 */
1685static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1686 int64_t largest_acked_pn)
1687{
1688 struct eb64_node *ar, *next_ar;
1689 struct quic_arngs *arngs = &pktns->rx.arngs;
1690
1691 ar = eb64_first(&arngs->root);
1692 while (ar) {
1693 struct quic_arng_node *ar_node;
1694
1695 next_ar = eb64_next(ar);
1696 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
1697 if ((int64_t)ar_node->first.key > largest_acked_pn)
1698 break;
1699
1700 if (largest_acked_pn < ar_node->last) {
1701 eb64_delete(ar);
1702 ar_node->first.key = largest_acked_pn + 1;
1703 eb64_insert(&arngs->root, ar);
1704 break;
1705 }
1706
1707 eb64_delete(ar);
1708 pool_free(pool_head_quic_arng, ar_node);
1709 arngs->sz--;
1710 ar = next_ar;
1711 }
1712}
1713
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001714/* Send a packet ack event nofication for each newly acked packet of
1715 * <newly_acked_pkts> list and free them.
1716 * Always succeeds.
1717 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001718static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001719 struct list *newly_acked_pkts)
1720{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001721 struct quic_tx_packet *pkt, *tmp;
1722 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1723
1724 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1725 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001726 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001727 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001728 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001729 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001730 /* If this packet contained an ACK frame, proceed to the
1731 * acknowledging of range of acks from the largest acknowledged
1732 * packet number which was sent in an ACK frame by this packet.
1733 */
1734 if (pkt->largest_acked_pn != -1)
1735 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001736 ev.ack.acked = pkt->in_flight_len;
1737 ev.ack.time_sent = pkt->time_sent;
1738 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001739 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001740 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001741 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001742 }
1743
1744}
1745
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001746/* Release all the frames attached to <pktns> packet number space */
1747static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1748{
1749 struct quic_frame *frm, *frmbak;
1750
1751 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1752 LIST_DELETE(&frm->list);
1753 pool_free(pool_head_quic_frame, frm);
1754 }
1755}
1756
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001757/* Handle <pkts> list of lost packets detected at <now_us> handling
1758 * their TX frames.
1759 * Send a packet loss event to the congestion controller if
1760 * in flight packet have been lost.
1761 * Also frees the packet in <pkts> list.
1762 * Never fails.
1763 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001764static inline void qc_release_lost_pkts(struct quic_conn *qc,
1765 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 struct list *pkts,
1767 uint64_t now_us)
1768{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001769 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001770 uint64_t lost_bytes;
1771
1772 lost_bytes = 0;
1773 oldest_lost = newest_lost = NULL;
1774 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001775 struct list tmp = LIST_HEAD_INIT(tmp);
1776
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001777 lost_bytes += pkt->in_flight_len;
1778 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001779 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001780 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001781 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001782 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001783 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001784 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001785 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001786 if (!oldest_lost) {
1787 oldest_lost = newest_lost = pkt;
1788 }
1789 else {
1790 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001791 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001792 newest_lost = pkt;
1793 }
1794 }
1795
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001796 if (newest_lost) {
1797 /* Sent a congestion event to the controller */
1798 struct quic_cc_event ev = {
1799 .type = QUIC_CC_EVT_LOSS,
1800 .loss.time_sent = newest_lost->time_sent,
1801 };
1802
1803 quic_cc_event(&qc->path->cc, &ev);
1804 }
1805
1806 /* If an RTT have been already sampled, <rtt_min> has been set.
1807 * We must check if we are experiencing a persistent congestion.
1808 * If this is the case, the congestion controller must re-enter
1809 * slow start state.
1810 */
1811 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1812 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1813
1814 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1815 now_ms, qc->max_ack_delay))
1816 qc->path->cc.algo->slow_start(&qc->path->cc);
1817 }
1818
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001819 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001820 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001821 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001822 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001823 }
1824}
1825
1826/* Look for packet loss from sent packets for <qel> encryption level of a
1827 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1828 * their tree if deemed as lost or set the <loss_time> value the packet number
1829 * space if any not deemed lost.
1830 * Should be called after having received an ACK frame with newly acknowledged
1831 * packets or when the the loss detection timer has expired.
1832 * Always succeeds.
1833 */
1834static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1835 struct quic_conn *qc,
1836 struct list *lost_pkts)
1837{
1838 struct eb_root *pkts;
1839 struct eb64_node *node;
1840 struct quic_loss *ql;
1841 unsigned int loss_delay;
1842
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001843 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001844 pkts = &pktns->tx.pkts;
1845 pktns->tx.loss_time = TICK_ETERNITY;
1846 if (eb_is_empty(pkts))
1847 goto out;
1848
1849 ql = &qc->path->loss;
1850 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1852
1853 node = eb64_first(pkts);
1854 while (node) {
1855 struct quic_tx_packet *pkt;
1856 int64_t largest_acked_pn;
1857 unsigned int loss_time_limit, time_sent;
1858
1859 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001860 largest_acked_pn = pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861 node = eb64_next(node);
1862 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1863 break;
1864
1865 time_sent = pkt->time_sent;
1866 loss_time_limit = tick_add(time_sent, loss_delay);
1867 if (tick_is_le(time_sent, now_ms) ||
1868 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1869 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001870 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871 }
1872 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001873 if (tick_isset(pktns->tx.loss_time))
1874 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1875 else
1876 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001877 }
1878 }
1879
1880 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001881 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001882}
1883
1884/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1885 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001886 * 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 +01001887 * acked ack-eliciting packet.
1888 * Return 1, if succeeded, 0 if not.
1889 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001890static inline int qc_parse_ack_frm(struct quic_conn *qc,
1891 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001892 struct quic_enc_level *qel,
1893 unsigned int *rtt_sample,
1894 const unsigned char **pos, const unsigned char *end)
1895{
1896 struct quic_ack *ack = &frm->ack;
1897 uint64_t smallest, largest;
1898 struct eb_root *pkts;
1899 struct eb64_node *largest_node;
1900 unsigned int time_sent, pkt_flags;
1901 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1902 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1903
1904 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1905 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001906 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001907 goto err;
1908 }
1909
1910 if (ack->first_ack_range > ack->largest_ack) {
1911 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001912 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913 goto err;
1914 }
1915
1916 largest = ack->largest_ack;
1917 smallest = largest - ack->first_ack_range;
1918 pkts = &qel->pktns->tx.pkts;
1919 pkt_flags = 0;
1920 largest_node = NULL;
1921 time_sent = 0;
1922
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001923 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001924 largest_node = eb64_lookup(pkts, largest);
1925 if (!largest_node) {
1926 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001927 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001928 }
1929 else {
1930 time_sent = eb64_entry(&largest_node->node,
1931 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001932 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001933 }
1934
1935 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001936 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001937 do {
1938 uint64_t gap, ack_range;
1939
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001940 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1941 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 if (!ack->ack_range_num--)
1943 break;
1944
1945 if (!quic_dec_int(&gap, pos, end))
1946 goto err;
1947
1948 if (smallest < gap + 2) {
1949 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001950 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001951 goto err;
1952 }
1953
1954 largest = smallest - gap - 2;
1955 if (!quic_dec_int(&ack_range, pos, end))
1956 goto err;
1957
1958 if (largest < ack_range) {
1959 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001960 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001961 goto err;
1962 }
1963
1964 /* Do not use this node anymore. */
1965 largest_node = NULL;
1966 /* Next range */
1967 smallest = largest - ack_range;
1968
1969 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001970 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001971 } while (1);
1972
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001973 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1974 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001975 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001976 }
1977
1978 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1979 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001980 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001981 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001982 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001983 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001984 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1985 if (quic_peer_validated_addr(qc))
1986 qc->path->loss.pto_count = 0;
1987 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001988 }
1989
1990
1991 return 1;
1992
1993 err:
1994 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001995 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001996 return 0;
1997}
1998
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001999/* This function gives the detail of the SSL error. It is used only
2000 * if the debug mode and the verbose mode are activated. It dump all
2001 * the SSL error until the stack was empty.
2002 */
2003static forceinline void qc_ssl_dump_errors(struct connection *conn)
2004{
2005 if (unlikely(global.mode & MODE_DEBUG)) {
2006 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002007 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002008 unsigned long ret;
2009
Willy Tarreau325fc632022-04-11 18:47:38 +02002010 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002011 ret = ERR_get_error();
2012 if (!ret)
2013 return;
2014
2015 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002016 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002017 }
2018 }
2019}
2020
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002021int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2022 const char **str, int *len);
2023
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2025 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002026 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002027 * Return 1 if succeeded, 0 if not.
2028 */
2029static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002030 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002031 const unsigned char *data, size_t len,
2032 struct quic_rx_packet *pkt,
2033 struct quic_rx_crypto_frm *cf)
2034{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002035 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002036 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002038 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002039 qc = ctx->qc;
2040
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002041 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2042
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002043 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2044 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002045 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002046 goto err;
2047 }
2048
2049 el->rx.crypto.offset += len;
2050 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002051 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002052
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002053 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002054 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002055 ssl_err = SSL_do_handshake(ctx->ssl);
2056 if (ssl_err != 1) {
2057 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2058 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2059 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002060 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002061 goto out;
2062 }
2063
2064 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002065 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002066 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002067 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002068 goto err;
2069 }
2070
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002071 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2072 /* I/O callback switch */
2073 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002074 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002075 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002076 /* The connection is ready to be accepted. */
2077 quic_accept_push_qc(qc);
2078 }
2079 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002080 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002081 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002082 } else {
2083 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2084 if (ssl_err != 1) {
2085 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2086 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2087 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002088 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002089 goto out;
2090 }
2091
2092 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002093 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002094 goto err;
2095 }
2096
2097 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002098 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002099 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002100
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002102 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 return 1;
2104
2105 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002106 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002107 return 0;
2108}
2109
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002110/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
2111 * <pkt> RX packet.
2112 * Return it if succeeded, NULL if not.
2113 */
2114static inline
2115struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
2116 struct quic_rx_packet *pkt)
2117{
2118 struct quic_rx_strm_frm *frm;
2119
2120 frm = pool_alloc(pool_head_quic_rx_strm_frm);
2121 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002122 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002123 frm->len = stream_frm->len;
2124 frm->data = stream_frm->data;
2125 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002126 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002127 }
2128
2129 return frm;
2130}
2131
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002132/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01002133 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002134 */
2135static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2136{
2137 size_t ret;
2138
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01002139 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
2140 strm_frm->len -= ret;
2141 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002142
2143 return ret;
2144}
2145
2146/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2147 * streams may be open. The data are copied to the stream RX buffer if possible.
2148 * If not, the STREAM frame is stored to be treated again later.
2149 * We rely on the flow control so that not to store too much STREAM frames.
2150 * Return 1 if succeeded, 0 if not.
2151 */
2152static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2153 struct quic_stream *strm_frm,
2154 struct quic_conn *qc)
2155{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002156 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002157 struct eb64_node *frm_node;
2158 struct qcs *qcs = NULL;
2159 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002160
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002161 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2162 strm_frm->offset.key, strm_frm->fin,
2163 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002164
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002165 /* invalid or already received frame */
2166 if (ret == 1)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002167 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002168
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002169 if (ret == 2) {
2170 /* frame cannot be parsed at the moment and should be
2171 * buffered.
2172 */
2173 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2174 if (!frm) {
2175 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002176 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002177 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002178 }
2179
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002180 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2181 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002182
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002183 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002184 }
2185
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002186 /* Frame correctly received by the mux.
2187 * If there is buffered frame for next offset, it may be possible to
2188 * receive them now.
2189 */
2190 frm_node = eb64_first(&qcs->rx.frms);
2191 while (frm_node) {
2192 frm = eb64_entry(&frm_node->node,
2193 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002194
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02002195 ret = qcc_recv(qc->qcc, qcs->id, frm->len,
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002196 frm->offset_node.key, frm->fin,
2197 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002198
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002199 /* interrupt the parsing if the frame cannot be handled for the
2200 * moment only by the MUX.
2201 */
2202 if (ret == 2)
2203 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002204
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002205 /* Remove a newly received frame or an invalid one. */
2206 frm_node = eb64_next(frm_node);
2207 eb64_delete(&frm->offset_node);
2208 quic_rx_packet_refdec(frm->pkt);
2209 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002210 }
2211
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002212 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002213 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002214
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002215 return 1;
2216}
2217
2218/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2219 * streams may be open. The data are copied to the stream RX buffer if possible.
2220 * If not, the STREAM frame is stored to be treated again later.
2221 * We rely on the flow control so that not to store too much STREAM frames.
2222 * Return 1 if succeeded, 0 if not.
2223 */
2224static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2225 struct quic_stream *strm_frm,
2226 struct quic_conn *qc)
2227{
2228 struct qcs *strm;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002229 struct quic_rx_strm_frm *frm;
2230 size_t strm_frm_len;
2231
Amaury Denoyelle50742292022-03-29 14:57:19 +02002232 strm = qcc_get_qcs(qc->qcc, strm_frm->id);
2233 if (!strm) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002234 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002235 return 0;
2236 }
2237
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002238 if (strm_frm->offset.key < strm->rx.offset) {
2239 size_t diff;
2240
2241 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2242 TRACE_PROTO("Already received STREAM data",
2243 QUIC_EV_CONN_PSTRM, qc);
2244 goto out;
2245 }
2246
2247 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2248 diff = strm->rx.offset - strm_frm->offset.key;
2249 strm_frm->offset.key = strm->rx.offset;
2250 strm_frm->len -= diff;
2251 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002252 }
2253
2254 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002255 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002256 int ret;
2257
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002258 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002259 goto store_frm;
2260
2261 /* qc_strm_cpy() will modify the offset, depending on the number
2262 * of bytes copied.
2263 */
2264 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2265 /* Inform the application of the arrival of this new stream */
2266 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002267 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002268 return 0;
2269 }
2270
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002271 if (ret)
2272 qcs_notify_recv(strm);
2273
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002274 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002275 }
2276 /* Take this frame into an account for the stream flow control */
2277 strm->rx.offset += strm_frm_len;
2278 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002279 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002280 */
2281 if (!strm_frm->len)
2282 goto out;
2283
2284 store_frm:
2285 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2286 if (!frm) {
2287 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002288 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002289 return 0;
2290 }
2291
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002292 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002293 quic_rx_packet_refinc(pkt);
2294
2295 out:
2296 return 1;
2297}
2298
2299static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2300 struct quic_stream *strm_frm,
2301 struct quic_conn *qc)
2302{
2303 if (strm_frm->id & QCS_ID_DIR_BIT)
2304 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2305 else
2306 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2307}
2308
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002309/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002310static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2311 struct quic_conn *qc)
2312{
2313 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002314 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002315 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002316
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002317 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002318 pkts = &qel->pktns->tx.pkts;
2319 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002320 /* Skip the empty packet (they have already been retransmitted) */
2321 while (node) {
2322 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2323 if (!LIST_ISEMPTY(&pkt->frms))
2324 break;
2325 node = eb64_next(node);
2326 }
2327
2328 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002329 return;
2330
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002331 /* When building a packet from another one, the field which may increase the
2332 * packet size is the packet number. And the maximum increase is 4 bytes.
2333 */
2334 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2335 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2336 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2337 return;
2338 }
2339
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002340 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002341}
2342
2343/* Prepare a fast retransmission during handshake after a client
2344 * has resent Initial packets. According to the RFC a server may retransmit
2345 * up to two datagrams of Initial packets if did not receive all Initial packets
2346 * and resend them coalescing with others (Handshake here).
2347 * (Listener only).
2348 */
2349static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2350{
2351 struct list itmp = LIST_HEAD_INIT(itmp);
2352 struct list htmp = LIST_HEAD_INIT(htmp);
2353 struct quic_frame *frm, *frmbak;
2354
2355 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2356 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2357 struct quic_enc_level *qel = iqel;
2358 struct eb_root *pkts;
2359 struct eb64_node *node;
2360 struct quic_tx_packet *pkt;
2361 struct list *tmp = &itmp;
2362
Frédéric Lécaille5cfb4ed2022-03-30 14:44:49 +02002363 /* Do not probe from a packet number space if some probing
2364 * was already asked.
2365 */
2366 if (qel->pktns->tx.pto_probe) {
2367 qel = hqel;
2368 if (qel->pktns->tx.pto_probe)
2369 return;
2370 }
2371
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002372 start:
2373 pkt = NULL;
2374 pkts = &qel->pktns->tx.pkts;
2375 node = eb64_first(pkts);
2376 /* Skip the empty packet (they have already been retransmitted) */
2377 while (node) {
2378 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2379 if (!LIST_ISEMPTY(&pkt->frms))
2380 break;
2381 node = eb64_next(node);
2382 }
2383
2384 if (!pkt)
2385 goto end;
2386
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002387 /* When building a packet from another one, the field which may increase the
2388 * packet size is the packet number. And the maximum increase is 4 bytes.
2389 */
2390 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2391 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2392 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2393 goto end;
2394 }
2395
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002396 qel->pktns->tx.pto_probe += 1;
2397 requeue:
2398 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Frédéric Lécaille009016c2022-03-30 14:58:55 +02002399 struct quic_frame *dup_frm;
2400
2401
2402 dup_frm = pool_alloc(pool_head_quic_frame);
2403 if (!dup_frm) {
2404 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2405 break;
2406 }
2407
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002408 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille009016c2022-03-30 14:58:55 +02002409 *dup_frm = *frm;
2410 LIST_APPEND(tmp, &dup_frm->list);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002411 }
2412
2413 if (qel == iqel) {
2414 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2415 pkt = pkt->next;
2416 tmp = &htmp;
2417 hqel->pktns->tx.pto_probe += 1;
2418 goto requeue;
2419 }
2420
2421 qel = hqel;
2422 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002423 goto start;
2424 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002425
2426 end:
2427 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2428 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002429}
2430
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002431/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2432 * as I/O handler context and <qel> as encryption level.
2433 * Returns 1 if succeeded, 0 if failed.
2434 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002435static 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 +01002436 struct quic_enc_level *qel)
2437{
2438 struct quic_frame frm;
2439 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002440 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002441 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002443 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002444 /* Skip the AAD */
2445 pos = pkt->data + pkt->aad_len;
2446 end = pkt->data + pkt->len;
2447
2448 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002449 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450 goto err;
2451
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002452 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002453 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002454 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002455 break;
2456 case QUIC_FT_PING:
2457 break;
2458 case QUIC_FT_ACK:
2459 {
2460 unsigned int rtt_sample;
2461
2462 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002463 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002464 goto err;
2465
2466 if (rtt_sample) {
2467 unsigned int ack_delay;
2468
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002469 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002470 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002471 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2472 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002473 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002474 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002475 break;
2476 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002477 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002478 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002479 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002480 {
2481 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002482
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002483 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002484 /* XXX TO DO: <cfdebug> is used only for the traces. */
2485 struct quic_rx_crypto_frm cfdebug = { };
2486
2487 cfdebug.offset_node.key = frm.crypto.offset;
2488 cfdebug.len = frm.crypto.len;
2489 TRACE_PROTO("CRYPTO data discarded",
2490 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2491 break;
2492 }
2493
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002494 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2495 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2496 /* XXX TO DO: <cfdebug> is used only for the traces. */
2497 struct quic_rx_crypto_frm cfdebug = { };
2498
2499 cfdebug.offset_node.key = frm.crypto.offset;
2500 cfdebug.len = frm.crypto.len;
2501 /* Nothing to do */
2502 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002503 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002504 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002505 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2506 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002507 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002508 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002509 else {
2510 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2511 /* XXX TO DO: <cfdebug> is used only for the traces. */
2512 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002513
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002514 cfdebug.offset_node.key = frm.crypto.offset;
2515 cfdebug.len = frm.crypto.len;
2516 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002517 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002518 frm.crypto.len -= diff;
2519 frm.crypto.data += diff;
2520 frm.crypto.offset = qel->rx.crypto.offset;
2521 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002522 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002523
2524 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002526 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002527
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002528 cfdebug.offset_node.key = frm.crypto.offset;
2529 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002530 if (!qc_provide_cdata(qel, ctx,
2531 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002532 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002534
2535 break;
2536 }
2537
2538 /* frm.crypto.offset > qel->rx.crypto.offset */
2539 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2540 if (!cf) {
2541 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002542 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002543 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002544 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002545
2546 cf->offset_node.key = frm.crypto.offset;
2547 cf->len = frm.crypto.len;
2548 cf->data = frm.crypto.data;
2549 cf->pkt = pkt;
2550 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2551 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002552 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002553 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002554 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002555 {
2556 struct quic_stream *stream = &frm.stream;
2557
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002558 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002559 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2560 goto err;
2561 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2562 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002563
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002564 /* At the application layer the connection may have already been closed. */
2565 if (qc->mux_state != QC_MUX_READY)
2566 break;
2567
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002568 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002569 goto err;
2570
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002571 break;
2572 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002573 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002574 if (qc->mux_state == QC_MUX_READY) {
2575 struct quic_max_data *data = &frm.max_data;
2576 qcc_recv_max_data(qc->qcc, data->max_data);
2577 }
2578 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002579 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002580 if (qc->mux_state == QC_MUX_READY) {
2581 struct quic_max_stream_data *data = &frm.max_stream_data;
2582 qcc_recv_max_stream_data(qc->qcc, data->id,
2583 data->max_stream_data);
2584 }
2585 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002586 case QUIC_FT_MAX_STREAMS_BIDI:
2587 case QUIC_FT_MAX_STREAMS_UNI:
2588 case QUIC_FT_DATA_BLOCKED:
2589 case QUIC_FT_STREAM_DATA_BLOCKED:
2590 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2591 case QUIC_FT_STREAMS_BLOCKED_UNI:
2592 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002593 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002594 case QUIC_FT_RETIRE_CONNECTION_ID:
2595 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002596 break;
2597 case QUIC_FT_CONNECTION_CLOSE:
2598 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille47756802022-03-25 09:12:16 +01002599 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
2600 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2601 /* RFC 9000 10.2. Immediate Close:
2602 * The closing and draining connection states exist to ensure
2603 * that connections close cleanly and that delayed or reordered
2604 * packets are properly discarded. These states SHOULD persist
2605 * for at least three times the current PTO interval...
2606 *
2607 * Rearm the idle timeout only one time when entering draining
2608 * state.
2609 */
2610 qc_idle_timer_do_rearm(qc);
2611 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002612 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002613 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002614 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002615 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002616 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002617 goto err;
2618
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002619 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002620 break;
2621 default:
2622 goto err;
2623 }
2624 }
2625
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002626 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002627 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002628
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002629 if (fast_retrans)
2630 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002631
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002632 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2633 * has successfully parse a Handshake packet. The Initial encryption must also
2634 * be discarded.
2635 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002636 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002637 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002638 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2639 QUIC_FL_TLS_SECRETS_DCD)) {
2640 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2641 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2642 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2643 qc_set_timer(ctx->qc);
2644 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2645 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2646 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002647 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2648 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002649 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650 }
2651
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002652 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002653 return 1;
2654
2655 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002656 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657 return 0;
2658}
2659
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002660/* Must be called only by a <cbuf> writer (packet builder).
2661 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2662 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2663 * index after having marked the end of written data. This the responsability
2664 * of the caller to ensure there is enough room in <cbuf> to write the end of
2665 * data made of a uint16_t null field.
2666 *
2667 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2668 * ^ ^
2669 * r w
2670 *
2671 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2672 * ^ ^
2673 * r w
2674
2675 * +--------------------------------------+ (empty buffer, can be reused)
2676 * ^
2677 * (r = w)
2678 *
2679 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2680 * ^ ^
2681 * w r
2682 */
2683static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2684{
2685 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2686
2687 /* We can reset the writer index only if in front of the reader index and
2688 * if the reader index is not null. Resetting the writer when the reader
2689 * index is null would empty the buffer.
2690 * XXX Note than the writer index cannot reach the reader index.
2691 * Only the reader index can reach the writer index.
2692 */
2693 if (rd && rd <= cbuf->wr) {
2694 /* Mark the end of contiguous data for the reader */
2695 write_u16(cb_wr(cbuf), 0);
2696 cb_add(cbuf, sizeof(uint16_t));
2697 cb_wr_reset(cbuf);
2698 return 1;
2699 }
2700
2701 return 0;
2702}
2703
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002704/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002705 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002706 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2707 * This function must be called only after having built a correct datagram.
2708 * Always succeeds.
2709 */
2710static inline void qc_set_dg(struct cbuf *cbuf,
2711 uint16_t dglen, struct quic_tx_packet *pkt)
2712{
2713 write_u16(cb_wr(cbuf), dglen);
2714 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2715 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2716}
2717
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002718/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2719 * with <frms> as ack-eliciting frame list to send, 0 if not.
2720 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2721 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2722 */
2723static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2724 struct quic_enc_level *qel, int cc, int probe)
2725{
2726 unsigned int must_ack =
2727 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2728
2729 /* Do not build any more packet if the TX secrets are not available or
2730 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2731 * and if there is no more packets to send upon PTO expiration
2732 * and if there is no more ack-eliciting frames to send or in flight
2733 * congestion control limit is reached for prepared data
2734 */
2735 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2736 (!cc && !probe && !must_ack &&
2737 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2738 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2739 return 0;
2740 }
2741
2742 return 1;
2743}
2744
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002745/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002746 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2747 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002748 * A header made of two fields is added to each datagram: the datagram length followed
2749 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002750 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2751 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002752 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002753static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2754 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002755{
2756 struct quic_enc_level *qel;
2757 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002758 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002759 struct quic_tx_packet *pkt;
2760 size_t total;
2761 size_t dg_headlen;
2762
2763 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2764 /* Each datagram is prepended with its length followed by the
2765 * address of the first packet in the datagram.
2766 */
2767 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2768 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2769 total = 0;
2770 start:
2771 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002772 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002773 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2774 * to ensure there is enough room to mark the end of prepared
2775 * contiguous data with a zero length.
2776 */
2777 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2778 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002779 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002780
2781 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002782 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002783 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002784 /* We do not probe if an immediate close was asked */
2785 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002786 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002787
2788 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002789 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002790
2791 /* Leave room for the datagram header */
2792 pos += dg_headlen;
2793 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2794 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2795 }
2796 else {
2797 end = pos + qc->path->mtu;
2798 }
2799
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002800 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002801 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002802 switch (err) {
2803 case -2:
2804 goto err;
2805 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002806 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2807 * MTU, we are here because of the congestion control window. There is
2808 * no need to try to reuse this buffer.
2809 */
2810 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002811 default:
2812 break;
2813 }
2814
2815 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2816 if (!pkt)
2817 goto err;
2818
2819 total += pkt->len;
2820 /* Set the current datagram as prepared into <cbuf>. */
2821 qc_set_dg(cbuf, pkt->len, pkt);
2822 }
2823
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002824 /* Reset <wr> writer index if in front of <rd> index */
2825 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002826 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002827 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002828 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002829 }
2830
2831 out:
2832 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2833 return total;
2834
2835 err:
2836 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2837 return -1;
2838}
2839
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002840/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002841 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2842 * several packets in the same datagram. A header made of two fields is added
2843 * to each datagram: the datagram length followed by the address of the first
2844 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002845 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2846 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002847 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002848static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2849 enum quic_tls_enc_level tel,
2850 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002851{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002853 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002854 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002855 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2856 /* length of datagrams */
2857 uint16_t dglen;
2858 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002859 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002860 /* Each datagram is prepended with its length followed by the
2861 * address of the first packet in the datagram.
2862 */
2863 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002865 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2866
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002867 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002868 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002869 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002870 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002871 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002872 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002873 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002874 /* Leave at least <dglen> bytes at the end of this buffer
2875 * to ensure there is enough room to mark the end of prepared
2876 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002877 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002878 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2879 first_pkt = prv_pkt = NULL;
2880 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002881 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002882 enum quic_pkt_type pkt_type;
2883
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002884 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002885 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002886 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002887 /* We do not probe if an immediate close was asked */
2888 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002889 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002890
2891 if (!qc_may_build_pkt(qc, &qel->pktns->tx.frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002892 if (prv_pkt)
2893 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002894 /* Let's select the next encryption level */
2895 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2896 tel = next_tel;
2897 qel = &qc->els[tel];
2898 /* Build a new datagram */
2899 prv_pkt = NULL;
2900 continue;
2901 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002902 break;
2903 }
2904
2905 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002906 if (!prv_pkt) {
2907 /* Leave room for the datagram header */
2908 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002909 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002910 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2911 }
2912 else {
2913 end = pos + qc->path->mtu;
2914 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002915 }
2916
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002917 cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002918 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002919 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002920 case -2:
2921 goto err;
2922 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002923 /* If there was already a correct packet present, set the
2924 * current datagram as prepared into <cbuf>.
2925 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002926 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002927 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002928 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002929 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002930 break;
2931 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002932
Frédéric Lécaille63556772021-12-29 17:18:21 +01002933 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2934 if (!cur_pkt)
2935 goto err;
2936
2937 total += cur_pkt->len;
2938 /* keep trace of the first packet in the datagram */
2939 if (!first_pkt)
2940 first_pkt = cur_pkt;
2941 /* Attach the current one to the previous one */
2942 if (prv_pkt)
2943 prv_pkt->next = cur_pkt;
2944 /* Let's say we have to build a new dgram */
2945 prv_pkt = NULL;
2946 dglen += cur_pkt->len;
2947 /* Client: discard the Initial encryption keys as soon as
2948 * a handshake packet could be built.
2949 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002950 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01002951 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2952 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2953 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2954 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2955 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002956 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002957 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002958 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002959 }
2960 /* If the data for the current encryption level have all been sent,
2961 * select the next level.
2962 */
2963 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécailled6570e12022-03-29 17:41:57 +02002964 (LIST_ISEMPTY(&qel->pktns->tx.frms) && !qel->pktns->tx.pto_probe)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002965 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2966 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2967 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2968 tel = next_tel;
2969 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002970 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002971 /* If there is data for the next level, do not
2972 * consume a datagram.
2973 */
2974 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002975 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002976 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002977 /* If we have to build a new datagram, set the current datagram as
2978 * prepared into <cbuf>.
2979 */
2980 if (!prv_pkt) {
2981 qc_set_dg(cbuf, dglen, first_pkt);
2982 first_pkt = NULL;
2983 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002984 padding = 0;
2985 }
2986 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002987 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002988 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2989 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002990 }
2991 }
2992
2993 stop_build:
2994 /* Reset <wr> writer index if in front of <rd> index */
2995 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002996 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002997 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002998 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999 }
3000
3001 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003002 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003003 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004
3005 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003006 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003007 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003008}
3009
3010/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003011 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003013int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014{
3015 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003016 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003017
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003018 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003019 cbuf = qr->cbuf;
3020 while (cb_contig_data(cbuf)) {
3021 unsigned char *pos;
3022 struct buffer tmpbuf = { };
3023 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3024 uint16_t dglen;
3025 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026 unsigned int time_sent;
3027
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003028 pos = cb_rd(cbuf);
3029 dglen = read_u16(pos);
3030 /* End of prepared datagrams.
3031 * Reset the reader index only if in front of the writer index.
3032 */
3033 if (!dglen) {
3034 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3035
3036 if (wr && wr < cbuf->rd) {
3037 cb_rd_reset(cbuf);
3038 continue;
3039 }
3040 break;
3041 }
3042
3043 pos += sizeof dglen;
3044 first_pkt = read_ptr(pos);
3045 pos += sizeof first_pkt;
3046 tmpbuf.area = (char *)pos;
3047 tmpbuf.size = tmpbuf.data = dglen;
3048
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003049 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003050 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003051 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003052
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003053 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003054 qc->tx.bytes += tmpbuf.data;
3055 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003056
3057 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3058 pkt->time_sent = time_sent;
3059 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3060 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003061 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003062 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3063 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003064 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003065 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3066 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3067 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003068 qc_notify_close(qc);
3069
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003070 /* RFC 9000 10.2. Immediate Close:
3071 * The closing and draining connection states exist to ensure
3072 * that connections close cleanly and that delayed or reordered
3073 * packets are properly discarded. These states SHOULD persist
3074 * for at least three times the current PTO interval...
3075 *
3076 * Rearm the idle timeout only one time when entering closing
3077 * state.
3078 */
3079 qc_idle_timer_do_rearm(qc);
3080 if (qc->timer_task) {
3081 task_destroy(qc->timer_task);
3082 qc->timer_task = NULL;
3083 }
3084 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003085 qc->path->in_flight += pkt->in_flight_len;
3086 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3087 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003088 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003089 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003090 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003091 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003092 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003093 }
3094 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003095
3096 return 1;
3097}
3098
3099/* Build all the frames which must be sent just after the handshake have succeeded.
3100 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3101 * a HANDSHAKE_DONE frame.
3102 * Return 1 if succeeded, 0 if not.
3103 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003104static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003106 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003107 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003108 struct quic_frame *frm, *frmbak;
3109 struct list frm_list = LIST_HEAD_INIT(frm_list);
3110 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003111
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003112 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003113 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003114 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003115 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003116 if (!frm)
3117 return 0;
3118
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003119 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003120 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003121 }
3122
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003123 first = 1;
3124 max = qc->tx.params.active_connection_id_limit;
3125 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003126 struct quic_connection_id *cid;
3127
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003128 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003129 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 goto err;
3131
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003132 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003133 if (!cid)
3134 goto err;
3135
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003136 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003137 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003138
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003139 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003140 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003141 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003142
3143 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003144 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003145
3146 return 1;
3147
3148 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003149 /* free the frames */
3150 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3151 pool_free(pool_head_quic_frame, frm);
3152
3153 node = eb64_first(&qc->cids);
3154 while (node) {
3155 struct quic_connection_id *cid;
3156
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003157
3158 cid = eb64_entry(&node->node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003159 if (cid->seq_num.key >= max)
3160 break;
3161
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003162 if (cid->seq_num.key < first)
3163 continue;
3164
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003165 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003166 ebmb_delete(&cid->node);
3167 eb64_delete(&cid->seq_num);
3168 pool_free(pool_head_quic_connection_id, cid);
3169 }
3170
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003171 return 0;
3172}
3173
3174/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003175void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003176{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003177 struct eb64_node *n;
3178 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003180 n = eb64_first(&arngs->root);
3181 while (n) {
3182 struct eb64_node *next;
3183
3184 ar = eb64_entry(&n->node, struct quic_arng_node, first);
3185 next = eb64_next(n);
3186 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003187 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003188 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003189 }
3190}
3191
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003192/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3193 * descending order.
3194 */
3195static inline size_t sack_gap(struct quic_arng_node *p,
3196 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003197{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003198 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003199}
3200
3201
3202/* Remove the last elements of <ack_ranges> list of ack range updating its
3203 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003204 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003205 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003206static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003207{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003208 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003209
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003210 last = eb64_last(&arngs->root);
3211 while (last && arngs->enc_sz > limit) {
3212 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003214 prev = eb64_prev(last);
3215 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003216 return 0;
3217
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003218 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
3219 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
3220 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3221 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3222 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3223 --arngs->sz;
3224 eb64_delete(last);
3225 pool_free(pool_head_quic_arng, last);
3226 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003227 }
3228
3229 return 1;
3230}
3231
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003232/* Set the encoded size of <arngs> QUIC ack ranges. */
3233static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3234{
3235 struct eb64_node *node, *next;
3236 struct quic_arng_node *ar, *ar_next;
3237
3238 node = eb64_last(&arngs->root);
3239 if (!node)
3240 return;
3241
3242 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3243 arngs->enc_sz = quic_int_getsize(ar->last) +
3244 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3245
3246 while ((next = eb64_prev(node))) {
3247 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
3248 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3249 quic_int_getsize(ar_next->last - ar_next->first.key);
3250 node = next;
3251 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3252 }
3253}
3254
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003255/* Insert <ar> ack range into <argns> tree of ack ranges.
3256 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003257 */
3258static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003259struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3260 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003261{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003262 struct quic_arng_node *new_ar;
3263
3264 new_ar = pool_alloc(pool_head_quic_arng);
3265 if (new_ar) {
3266 new_ar->first.key = ar->first;
3267 new_ar->last = ar->last;
3268 eb64_insert(&arngs->root, &new_ar->first);
3269 arngs->sz++;
3270 }
3271
3272 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003273}
3274
3275/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003277 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003278 *
3279 * Descending order
3280 * ------------->
3281 * range1 range2
3282 * ..........|--------|..............|--------|
3283 * ^ ^ ^ ^
3284 * | | | |
3285 * last1 first1 last2 first2
3286 * ..........+--------+--------------+--------+......
3287 * diff1 gap12 diff2
3288 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003289 * To encode the previous list of ranges we must encode integers as follows in
3290 * descending order:
3291 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003292 * with diff1 = last1 - first1
3293 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003294 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003295 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003296 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003297int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3298 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003299{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003300 struct eb64_node *le;
3301 struct quic_arng_node *new_node;
3302 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003303
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003304 new = NULL;
3305 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003306 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003307 if (!new_node)
3308 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003309
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003310 goto out;
3311 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003312
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003313 le = eb64_lookup_le(&arngs->root, ar->first);
3314 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003315 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003316 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003317 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003318
3319 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003320 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003321 else {
3322 struct quic_arng_node *le_ar =
3323 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003324
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003325 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003326 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003327 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003328
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003329 if (le_ar->last + 1 >= ar->first) {
3330 le_ar->last = ar->last;
3331 new = le;
3332 new_node = le_ar;
3333 }
3334 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003335 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003336 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003337 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003338
3339 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003340 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003341 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003342
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003343 /* Verify that the new inserted node does not overlap the nodes
3344 * which follow it.
3345 */
3346 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003347 struct eb64_node *next;
3348 struct quic_arng_node *next_node;
3349
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003350 while ((next = eb64_next(new))) {
3351 next_node =
3352 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003353 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003354 break;
3355
3356 if (next_node->last > new_node->last)
3357 new_node->last = next_node->last;
3358 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003359 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003360 /* Decrement the size of these ranges. */
3361 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003362 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003363 }
3364
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003365 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003366 quic_arngs_set_enc_sz(arngs);
3367
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368 return 1;
3369}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003370/* Remove the header protection of packets at <el> encryption level.
3371 * Always succeeds.
3372 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003373static 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 +01003374{
3375 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003376 struct quic_rx_packet *pqpkt;
3377 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003378 struct quic_enc_level *app_qel;
3379
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003380 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3381 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003382 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003383 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003384 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003385 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 goto out;
3387 }
3388 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003389 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003390 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003391 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003392 pqpkt->data, pqpkt->data + pqpkt->len)) {
3393 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003394 /* XXX TO DO XXX */
3395 }
3396 else {
3397 /* The AAD includes the packet number field */
3398 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3399 /* Store the packet into the tree of packets to decrypt. */
3400 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003401 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003402 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3403 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003404 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003405 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003406 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003407 MT_LIST_DELETE_SAFE(pkttmp1);
3408 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003409 }
3410
3411 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003412 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003413}
3414
3415/* Process all the CRYPTO frame at <el> encryption level.
3416 * Return 1 if succeeded, 0 if not.
3417 */
3418static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003419 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420{
3421 struct eb64_node *node;
3422
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423 node = eb64_first(&el->rx.crypto.frms);
3424 while (node) {
3425 struct quic_rx_crypto_frm *cf;
3426
3427 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3428 if (cf->offset_node.key != el->rx.crypto.offset)
3429 break;
3430
3431 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3432 goto err;
3433
3434 node = eb64_next(node);
3435 quic_rx_packet_refdec(cf->pkt);
3436 eb64_delete(&cf->offset_node);
3437 pool_free(pool_head_quic_rx_crypto_frm, cf);
3438 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003439 return 1;
3440
3441 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003442 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003443 return 0;
3444}
3445
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003446/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003447 * 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 +02003448 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449 * Return 1 if succeeded, 0 if not.
3450 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003451int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3452 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003453{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003454 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003455 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003456 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003457 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003458
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003459 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003460 qel = cur_el;
3461 next_tel:
3462 if (!qel)
3463 goto out;
3464
3465 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3466 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003467 while (node) {
3468 struct quic_rx_packet *pkt;
3469
3470 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003471 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003472 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003473 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003474 /* Drop the packet */
3475 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003476 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003477 }
3478 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003479 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003480 /* Drop the packet */
3481 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003482 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483 }
3484 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003485 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3486
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003487 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3488 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3489 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003490 qc_idle_timer_rearm(qc, 1);
3491 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003492 if (pkt->pn > largest_pn)
3493 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003495 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003496 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003497 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003498 }
3499 }
3500 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003501 eb64_delete(&pkt->pn_node);
3502 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003503 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003504 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003505
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003506 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003507 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn)
3508 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003509 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510 goto err;
3511
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003512 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003513 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003514 qel = next_el;
3515 goto next_tel;
3516 }
3517
3518 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003519 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003520 return 1;
3521
3522 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003523 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 return 0;
3525}
3526
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003527/* Check if it's possible to remove header protection for packets related to
3528 * encryption level <qel>. If <qel> is NULL, assume it's false.
3529 *
3530 * Return true if the operation is possible else false.
3531 */
3532static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3533{
3534 enum quic_tls_enc_level tel;
3535
3536 if (!qel)
3537 return 0;
3538
3539 tel = ssl_to_quic_enc_level(qel->level);
3540
3541 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003542 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003543 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003544 return 0;
3545 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003546
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003547 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003548 return 0;
3549
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003550 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003551 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003552 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003553 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003554
3555 return 1;
3556}
3557
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003558/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaillec2f561c2022-02-25 17:46:07 +01003559int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003560{
3561 int ret;
3562 struct qring *qr;
3563
3564 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3565 if (!qr)
3566 /* Never happens */
3567 return 1;
3568
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003569 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003570 if (ret == -1)
3571 goto err;
3572 else if (ret == 0)
3573 goto out;
3574
3575 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3576 goto err;
3577
3578 out:
3579 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3580 return 1;
3581
3582 err:
3583 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3584 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3585 return 0;
3586}
3587
3588/* QUIC connection packet handler task (post handshake) */
3589static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3590{
3591 struct ssl_sock_ctx *ctx;
3592 struct quic_conn *qc;
3593 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003594
3595
3596 ctx = context;
3597 qc = ctx->qc;
3598 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003599
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003600 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003601
3602 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3603 qc_rm_hp_pkts(qc, qel);
3604
3605 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3606 goto err;
3607
Frédéric Lécaille47756802022-03-25 09:12:16 +01003608 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3609 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3610 goto out;
3611
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003612 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003613 goto err;
3614
Frédéric Lécaille47756802022-03-25 09:12:16 +01003615out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003616 return t;
3617
3618 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003619 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003620 return t;
3621}
3622
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003623/* QUIC connection packet handler task. */
3624struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003626 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003627 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003628 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003629 enum quic_tls_enc_level tel, next_tel;
3630 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003631 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003632 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003633
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003634 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003635 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003636 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003637 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003638 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003639 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003640 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3641 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003642 /* The I/O handler has been woken up by the dgram listener
3643 * after the anti-amplification was reached.
3644 */
3645 qc_set_timer(qc);
3646 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3647 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3648 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003649 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003650 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3651 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3652 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003653 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003654 if (st >= QUIC_HS_ST_COMPLETE &&
3655 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3656 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3657 /* There may be remaining Handshake packets to treat and acknowledge. */
3658 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3659 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3660 }
3661 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662 goto err;
3663
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003664 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003665 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666
3667 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003668 /* Treat packets waiting for header packet protection decryption */
3669 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003670 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003671
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003672 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3673 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3674 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003675 goto err;
3676
Frédéric Lécaille47756802022-03-25 09:12:16 +01003677 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3678 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3679 goto out;
3680
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003681 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003682 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003683 qel = next_qel;
3684 next_qel = NULL;
3685 goto next_level;
3686 }
3687
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003688 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003689 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003690 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003691 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003692 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003693
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003694 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003695 QUIC_FL_TLS_SECRETS_DCD)) {
3696 /* Discard the Handshake keys. */
3697 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3698 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3699 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3700 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003701 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003702 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003703 }
3704
3705 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3706 /* There may be remaining handshake to build (acks) */
3707 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3708 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003709 }
3710
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003711 if (!qr)
3712 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003713 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3714 * be considered.
3715 */
3716 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003717 goto err;
3718 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003719 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003720 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003721 else if (ret == 0)
3722 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003723
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003724 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725 goto err;
3726
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003727 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003728 /* Check if there is something to do for the next level.
3729 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003730 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003731 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003732 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003733 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003734 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003735 goto next_level;
3736 }
3737
Frédéric Lécaille47756802022-03-25 09:12:16 +01003738 out:
3739 if (qr)
3740 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003741 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003742 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003743
3744 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003745 if (qr)
3746 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003747 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003748 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003749}
3750
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003751/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003752static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3753{
3754 int i;
3755
3756 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3757 if (qel->tx.crypto.bufs[i]) {
3758 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3759 qel->tx.crypto.bufs[i] = NULL;
3760 }
3761 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003762 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763}
3764
3765/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003766 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767 * Returns 1 if succeeded, 0 if not.
3768 */
3769static int quic_conn_enc_level_init(struct quic_conn *qc,
3770 enum quic_tls_enc_level level)
3771{
3772 struct quic_enc_level *qel;
3773
3774 qel = &qc->els[level];
3775 qel->level = quic_to_ssl_enc_level(level);
3776 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3777 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3778 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003779 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003780
3781 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003782 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003783 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003784 qel->rx.crypto.offset = 0;
3785 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003786
3787 /* Allocate only one buffer. */
3788 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3789 if (!qel->tx.crypto.bufs)
3790 goto err;
3791
3792 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3793 if (!qel->tx.crypto.bufs[0])
3794 goto err;
3795
3796 qel->tx.crypto.bufs[0]->sz = 0;
3797 qel->tx.crypto.nb_buf = 1;
3798
3799 qel->tx.crypto.sz = 0;
3800 qel->tx.crypto.offset = 0;
3801
3802 return 1;
3803
3804 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003805 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003806 return 0;
3807}
3808
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003809/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
3810 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003811 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003812 * This function must only be called by the thread responsible of the quic_conn
3813 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003814 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003815static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816{
3817 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003818 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003819 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003820 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003821
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02003822 /* We must not free the quic-conn if the MUX is still allocated. */
3823 BUG_ON(qc->mux_state == QC_MUX_READY);
3824
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003825 /* free remaining stream descriptors */
3826 node = eb64_first(&qc->streams_by_id);
3827 while (node) {
3828 struct qc_stream_desc *stream;
3829
3830 stream = eb64_entry(node, struct qc_stream_desc, by_id);
3831 node = eb64_next(node);
3832
Amaury Denoyellec9acc312022-04-01 16:41:21 +02003833 /* all streams attached to the quic-conn are released, so
3834 * qc_stream_desc_free will liberate the stream instance.
3835 */
3836 BUG_ON(!stream->release);
3837 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003838 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003839
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003840 if (qc->idle_timer_task) {
3841 task_destroy(qc->idle_timer_task);
3842 qc->idle_timer_task = NULL;
3843 }
3844
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003845 if (qc->timer_task) {
3846 task_destroy(qc->timer_task);
3847 qc->timer_task = NULL;
3848 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003849
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003850 /* remove the connection from receiver cids trees */
3851 ebmb_delete(&qc->odcid_node);
3852 ebmb_delete(&qc->scid_node);
3853 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003854
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003855 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003856 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003857 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003858 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003859 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003860 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003861
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003862 quic_tls_ku_free(qc);
3863 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3864 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003865 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003866 }
3867
3868 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
3869 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
3870 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003871
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02003872 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
3873 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02003874 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02003875 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02003876
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003877 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3878 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003879 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003880}
3881
Amaury Denoyellee0be5732022-04-05 17:34:18 +02003882static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003883{
3884 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003885 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003886
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003887 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003888
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003889 /* Next application data can be dropped. */
3890 qc->mux_state = QC_MUX_RELEASED;
3891
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02003892 /* If the quic-conn timer has already expired free the quic-conn. */
3893 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
3894 quic_conn_release(qc);
3895 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
3896 return;
3897 }
3898
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003899 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003900}
3901
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003902/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003903static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003904{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003905 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003906 struct quic_conn *qc;
3907 struct quic_pktns *pktns;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003908 int i;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003909
3910 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003911 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003912 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003913 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003914 task->expire = TICK_ETERNITY;
3915 pktns = quic_loss_pktns(qc);
3916 if (tick_isset(pktns->tx.loss_time)) {
3917 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3918
3919 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3920 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003921 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3922 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003923 goto out;
3924 }
3925
3926 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003927 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003928 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3929 pktns->tx.pto_probe = 1;
3930 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3931 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3932 }
3933 else {
3934 pktns->tx.pto_probe = 2;
3935 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003936 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003937 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003938 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3939 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3940
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003941 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003942 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003943 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003944 iel->pktns->tx.pto_probe = 1;
3945 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003946
3947 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003948 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3949 continue;
3950
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003951 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003952 }
3953
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3955 qc->path->loss.pto_count++;
3956
3957 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003958 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959
3960 return task;
3961}
3962
3963/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3964 * connections used to identify the first Initial packets of client connecting
3965 * to listeners. This parameter must be NULL for QUIC connections attached
3966 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3967 * <scid> is the source connection ID with <scid_len> as length.
3968 * Returns 1 if succeeded, 0 if not.
3969 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003970static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003971 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003972 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003973{
3974 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003975 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003976 /* Initial CID. */
3977 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003978 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003979 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003980
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003981 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003982 qc = pool_zalloc(pool_head_quic_conn);
3983 if (!qc) {
3984 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3985 goto err;
3986 }
3987
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003988 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3989 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003990 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003991 goto err;
3992 }
3993
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003994 qc->cids = EB_ROOT;
3995 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003996 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003997 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003998
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003999 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004000 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004001 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004002 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004003 qc->odcid.addrlen = dcid_addr_len;
4004 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004005
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004006 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004007 if (scid_len)
4008 memcpy(qc->dcid.data, scid, scid_len);
4009 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02004010 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004011 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004012 }
4013 /* QUIC Client (outgoing connection to servers) */
4014 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004015 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004016 if (dcid_len)
4017 memcpy(qc->dcid.data, dcid, dcid_len);
4018 qc->dcid.len = dcid_len;
4019 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004020 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021
4022 /* Initialize the output buffer */
4023 qc->obuf.pos = qc->obuf.data;
4024
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004025 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004026 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004027 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004028 goto err;
4029 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004030
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004031 /* insert the allocated CID in the receiver datagram handler tree */
4032 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004033 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004034
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004035 /* Select our SCID which is the first CID with 0 as sequence number. */
4036 qc->scid = icid->cid;
4037
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004038 /* Packet number spaces initialization. */
4039 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4040 quic_pktns_init(&qc->pktns[i]);
4041 /* QUIC encryption level context initialization. */
4042 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004043 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004044 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004045 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004046 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004047 /* Initialize the packet number space. */
4048 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4049 }
4050
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02004051 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004052 qc->tps_tls_ext = qc->version & 0xff000000 ?
4053 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4054 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004055 /* TX part. */
4056 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004057 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4058 qc->tx.wbuf = qc->tx.rbuf = 0;
4059 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004060 /* RX part. */
4061 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004062 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004063
4064 qc->nb_pkt_for_cc = 1;
4065 qc->nb_pkt_since_cc = 0;
4066
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004067 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004068 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004069 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004070 goto err;
4071 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004072
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004073 /* XXX TO DO: Only one path at this time. */
4074 qc->path = &qc->paths[0];
4075 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4076
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004077 /* required to use MTLIST_IN_LIST */
4078 MT_LIST_INIT(&qc->accept_list);
4079
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004080 qc->streams_by_id = EB_ROOT_UNIQUE;
4081
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004082 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004083
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004084 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004085
4086 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004087 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004088 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4089 if (qc)
4090 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004091 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004092 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004093}
4094
4095/* Initialize the timer task of <qc> QUIC connection.
4096 * Returns 1 if succeeded, 0 if not.
4097 */
4098static int quic_conn_init_timer(struct quic_conn *qc)
4099{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004100 /* Attach this task to the same thread ID used for the connection */
4101 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004102 if (!qc->timer_task)
4103 return 0;
4104
4105 qc->timer = TICK_ETERNITY;
4106 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004107 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004108
4109 return 1;
4110}
4111
Frédéric Lécaille47756802022-03-25 09:12:16 +01004112/* Rearm the idle timer for <qc> QUIC connection. */
4113static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4114{
4115 unsigned int expire;
4116
4117 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4118 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4119}
4120
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004121/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4122 * which is set to 1 when receiving a packet , and 0 when sending packet
4123 */
4124static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4125{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004126 if (read) {
4127 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4128 }
4129 else {
4130 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4131 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004132 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004133}
4134
4135/* The task handling the idle timeout */
4136static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4137{
4138 struct quic_conn *qc = ctx;
4139
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004140 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4141 * might free the quic-conn too early via quic_close().
4142 */
4143 qc_notify_close(qc);
4144
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004145 /* If the MUX is still alive, keep the quic-conn. The MUX is
4146 * responsible to call quic_close to release it.
4147 */
4148 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4149 if (qc->mux_state != QC_MUX_READY)
4150 quic_conn_release(qc);
4151
4152 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4153 * least clean some parts of it such as the tasklet.
4154 */
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004155
4156 return NULL;
4157}
4158
4159/* Initialize the idle timeout task for <qc>.
4160 * Returns 1 if succeeded, 0 if not.
4161 */
4162static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4163{
4164 qc->idle_timer_task = task_new_here();
4165 if (!qc->idle_timer_task)
4166 return 0;
4167
4168 qc->idle_timer_task->process = qc_idle_timer_task;
4169 qc->idle_timer_task->context = qc;
4170 qc_idle_timer_rearm(qc, 1);
4171 task_queue(qc->idle_timer_task);
4172
4173 return 1;
4174}
4175
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004176/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4177 * past one byte of this buffer.
4178 */
4179static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4180 struct quic_rx_packet *pkt)
4181{
4182 unsigned char dcid_len, scid_len;
4183
4184 /* Version */
4185 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4186 return 0;
4187
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188 /* Destination Connection ID Length */
4189 dcid_len = *(*buf)++;
4190 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4191 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4192 /* XXX MUST BE DROPPED */
4193 return 0;
4194
4195 if (dcid_len) {
4196 /* Check that the length of this received DCID matches the CID lengths
4197 * of our implementation for non Initials packets only.
4198 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004199 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4200 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004201 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004202 return 0;
4203
4204 memcpy(pkt->dcid.data, *buf, dcid_len);
4205 }
4206
4207 pkt->dcid.len = dcid_len;
4208 *buf += dcid_len;
4209
4210 /* Source Connection ID Length */
4211 scid_len = *(*buf)++;
4212 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4213 /* XXX MUST BE DROPPED */
4214 return 0;
4215
4216 if (scid_len)
4217 memcpy(pkt->scid.data, *buf, scid_len);
4218 pkt->scid.len = scid_len;
4219 *buf += scid_len;
4220
4221 return 1;
4222}
4223
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004224/* Insert <pkt> RX packet in its <qel> RX packets tree */
4225static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4226{
4227 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004228 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004229 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4230 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4231 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004232}
4233
4234/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004235 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4236 * byte past the end of the buffer containing this packet and <beg> the address of
4237 * the packet first byte.
4238 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4239 * Returns 1 if succeeded, 0 if not.
4240 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004241static inline int qc_try_rm_hp(struct quic_conn *qc,
4242 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004243 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004244 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004245 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004246{
4247 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004248 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004249 struct quic_enc_level *qel;
4250 /* Only for traces. */
4251 struct quic_rx_packet *qpkt_trace;
4252
4253 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004254 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004255 /* The packet number is here. This is also the start minus
4256 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4257 * protection.
4258 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004259 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004260
4261 tel = quic_packet_type_enc_level(pkt->type);
4262 qel = &qc->els[tel];
4263
4264 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004265 /* Note that the following function enables us to unprotect the packet
4266 * number and its length subsequently used to decrypt the entire
4267 * packets.
4268 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004269 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4270 qel->pktns->rx.largest_pn, pn, beg, end)) {
4271 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004272 goto err;
4273 }
4274
4275 /* The AAD includes the packet number field found at <pn>. */
4276 pkt->aad_len = pn - beg + pkt->pnl;
4277 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004278 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004279 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004280 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004281 /* If the packet number space has been discarded, this packet
4282 * will be not parsed.
4283 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004284 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004285 goto out;
4286 }
4287
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004288 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004289 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004290 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4291 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004292 }
4293
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004294 *el = qel;
4295 /* No reference counter incrementation here!!! */
4296 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4297 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4298 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4299 b_add(&qc->rx.buf, pkt->len);
4300 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004301 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004302 return 1;
4303
4304 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004305 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004306 return 0;
4307}
4308
4309/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4310 * Also set <*long_header> to 1 if this form is long, 0 if not.
4311 */
4312static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4313 unsigned char byte0, int *long_header)
4314{
4315 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4316 pkt->type =
4317 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4318 *long_header = 1;
4319 }
4320 else {
4321 pkt->type = QUIC_PACKET_TYPE_SHORT;
4322 *long_header = 0;
4323 }
4324}
4325
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004326/*
4327 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4328 */
4329static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4330{
4331 int j = 0, version;
4332
4333 do {
4334 version = quic_supported_version[j];
4335 if (version == pkt->version)
4336 return 1;
4337
4338 version = quic_supported_version[++j];
4339 } while(version);
4340
4341 return 0;
4342}
4343
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004344/*
4345 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4346 * address <addr>.
4347 * Implementation of RFC9000 6. Version Negotiation
4348 *
4349 * TODO implement a rate-limiting sending of Version Negotiation packets
4350 *
4351 * Returns 0 on success else non-zero
4352 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004353static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4354 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004355{
4356 char buf[256];
4357 int i = 0, j, version;
4358 const socklen_t addrlen = get_addr_len(addr);
4359
4360 /*
4361 * header form
4362 * long header, fixed bit to 0 for Version Negotiation
4363 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004364 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4365 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004366
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004367 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004368 /* null version for Version Negotiation */
4369 buf[i++] = '\x00';
4370 buf[i++] = '\x00';
4371 buf[i++] = '\x00';
4372 buf[i++] = '\x00';
4373
4374 /* source connection id */
4375 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004376 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004377 i += pkt->scid.len;
4378
4379 /* destination connection id */
4380 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004381 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004382 i += pkt->dcid.len;
4383
4384 /* supported version */
4385 j = 0;
4386 do {
4387 version = htonl(quic_supported_version[j]);
4388 memcpy(&buf[i], &version, sizeof(version));
4389 i += sizeof(version);
4390
4391 version = quic_supported_version[++j];
4392 } while (version);
4393
4394 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4395 return 1;
4396
4397 return 0;
4398}
4399
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004400/* Generate the token to be used in Retry packets. The token is written to
4401 * <buf> which is expected to be <len> bytes.
4402 *
4403 * Various parameters are expected to be encoded in the token. For now, only
4404 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4405 * as this CID must be repeated by the server in the transport parameters.
4406 *
4407 * TODO add the client address to validate the token origin.
4408 *
4409 * Returns the length of the encoded token or 0 on error.
4410 */
4411static int generate_retry_token(unsigned char *buf, unsigned char len,
4412 struct quic_rx_packet *pkt)
4413{
4414 const size_t token_len = 1 + pkt->dcid.len;
4415 unsigned char i = 0;
4416
4417 if (token_len > len)
4418 return 0;
4419
4420 buf[i++] = pkt->dcid.len;
4421 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4422 i += pkt->dcid.len;
4423
4424 return i;
4425}
4426
4427/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4428 * the Initial <pkt> packet.
4429 *
4430 * Returns 0 on success else non-zero.
4431 */
4432static int send_retry(int fd, struct sockaddr_storage *addr,
4433 struct quic_rx_packet *pkt)
4434{
4435 unsigned char buf[128];
4436 int i = 0, token_len;
4437 const socklen_t addrlen = get_addr_len(addr);
4438 struct quic_cid scid;
4439
4440 /* long header + fixed bit + packet type 0x3 */
4441 buf[i++] = 0xf0;
4442 /* version */
4443 buf[i++] = 0x00;
4444 buf[i++] = 0x00;
4445 buf[i++] = 0x00;
4446 buf[i++] = 0x01;
4447
4448 /* Use the SCID from <pkt> for Retry DCID. */
4449 buf[i++] = pkt->scid.len;
4450 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4451 i += pkt->scid.len;
4452
4453 /* Generate a new CID to be used as SCID for the Retry packet. */
4454 scid.len = QUIC_HAP_CID_LEN;
4455 if (RAND_bytes(scid.data, scid.len) != 1)
4456 return 1;
4457
4458 buf[i++] = scid.len;
4459 memcpy(&buf[i], scid.data, scid.len);
4460 i += scid.len;
4461
4462 /* token */
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004463 if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004464 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004465
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004466 i += token_len;
4467
4468 /* token integrity tag */
4469 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4470 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4471 pkt->dcid.len, buf, i)) {
4472 return 1;
4473 }
4474
4475 i += QUIC_TLS_TAG_LEN;
4476
4477 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4478 return 1;
4479
4480 return 0;
4481}
4482
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004483/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4484 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4485 * concatenated to the <pkt> DCID field.
4486 *
4487 * Returns the instance or NULL if not found.
4488 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004489static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004490 struct listener *l,
4491 struct sockaddr_storage *saddr)
4492{
4493 struct quic_conn *qc = NULL;
4494 struct ebmb_node *node;
4495 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004496 /* set if the quic_conn is found in the second DCID tree */
4497 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004498
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004499 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4500 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4501 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4502 /* DCIDs of first packets coming from multiple clients may have
4503 * the same values. Let's distinguish them by concatenating the
4504 * socket addresses.
4505 */
4506 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004507 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004508 pkt->dcid.len + pkt->dcid.addrlen);
4509 if (node) {
4510 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4511 goto end;
4512 }
4513 }
4514
4515 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4516 * also for INITIAL/0-RTT non-first packets with the final DCID in
4517 * used.
4518 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004519 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004520 if (!node)
4521 goto end;
4522
4523 id = ebmb_entry(node, struct quic_connection_id, node);
4524 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004525 found_in_dcid = 1;
4526
4527 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004528 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4529 * If already done, this is a noop.
4530 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004531 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004532 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004533
4534 return qc;
4535}
4536
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004537/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4538 * will extract the parameters stored in the token : <odcid>.
4539 *
4540 * Returns 0 on success else non-zero.
4541 */
4542static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4543 struct quic_cid *odcid)
4544{
4545 uint64_t odcid_len;
4546
4547 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4548 return 1;
4549
Frédéric Lécaillef1f812b2022-03-17 16:22:02 +01004550 if (odcid_len > QUIC_CID_MAXLEN)
4551 return 1;
4552
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004553 memcpy(odcid->data, token, odcid_len);
4554 odcid->len = odcid_len;
4555
4556 return 0;
4557}
4558
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004559/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4560 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4561 * parameters of this session.
4562 * This is the responsibility of the caller to check the validity of all the
4563 * pointers passed as parameter to this function.
4564 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4565 * CO_ER_SSL_NO_MEM.
4566 */
4567static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4568 unsigned char *params, size_t params_len)
4569{
4570 int retry;
4571
4572 retry = 1;
4573 retry:
4574 *ssl = SSL_new(ssl_ctx);
4575 if (!*ssl) {
4576 if (!retry--)
4577 goto err;
4578
4579 pool_gc(NULL);
4580 goto retry;
4581 }
4582
4583 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4584 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4585 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004586 SSL_free(*ssl);
4587 *ssl = NULL;
4588 if (!retry--)
4589 goto err;
4590
4591 pool_gc(NULL);
4592 goto retry;
4593 }
4594
4595 return 0;
4596
4597 err:
4598 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4599 return -1;
4600}
4601
4602/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4603 * used to process <qc> received packets. The allocated context is stored in
4604 * <qc.xprt_ctx>.
4605 *
4606 * Returns 0 on success else non-zero.
4607 */
4608int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4609{
4610 struct bind_conf *bc = qc->li->bind_conf;
4611 struct ssl_sock_ctx *ctx = NULL;
4612
4613 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4614 if (!ctx)
4615 goto err;
4616
4617 ctx->wait_event.tasklet = tasklet_new();
4618 if (!ctx->wait_event.tasklet)
4619 goto err;
4620
4621 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4622 ctx->wait_event.tasklet->context = ctx;
4623 ctx->wait_event.events = 0;
4624 ctx->subs = NULL;
4625 ctx->xprt_ctx = NULL;
4626 ctx->qc = qc;
4627
4628 /* Set tasklet tid based on the SCID selected by us for this
4629 * connection. The upper layer will also be binded on the same thread.
4630 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004631 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004632
4633 if (qc_is_listener(qc)) {
4634 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4635 qc->enc_params, qc->enc_params_len) == -1) {
4636 goto err;
4637 }
4638
4639 /* Enabling 0-RTT */
4640 if (bc->ssl_conf.early_data)
4641 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4642
4643 SSL_set_accept_state(ctx->ssl);
4644 }
4645
4646 ctx->xprt = xprt_get(XPRT_QUIC);
4647
4648 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004649 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004650
4651 return 0;
4652
4653 err:
4654 if (ctx && ctx->wait_event.tasklet)
4655 tasklet_free(ctx->wait_event.tasklet);
4656 pool_free(pool_head_quic_conn_ctx, ctx);
4657
4658 return 1;
4659}
4660
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004661static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004662 struct quic_rx_packet *pkt, int first_pkt,
4663 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004664{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004665 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004666 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004667 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004668 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004669 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004670 size_t b_cspace;
4671 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004672
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004673 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004674 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004675 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004676 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004677 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4678 /* This ist only to please to traces and distinguish the
4679 * packet with parsed packet number from others.
4680 */
4681 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004682 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004683 goto err;
4684
4685 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004686 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004687 /* XXX TO BE DISCARDED */
4688 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4689 goto err;
4690 }
4691
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004692 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004693 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004694 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004695 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004696 uint64_t len;
4697
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004698 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004699 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4700 goto err;
4701 }
4702
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004703 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4704 * they must have the same DCID.
4705 */
4706 if (!first_pkt &&
4707 (pkt->dcid.len != dgram->dcid_len ||
4708 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4709 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4710 goto err;
4711 }
4712
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004713 /* Retry of Version Negotiation packets are only sent by servers */
4714 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4715 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4716 goto err;
4717 }
4718
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004719 /* RFC9000 6. Version Negotiation */
4720 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004721 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004722 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004723 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4724 goto err;
4725 }
4726
4727 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004728 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004729 }
4730
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004731 /* For Initial packets, and for servers (QUIC clients connections),
4732 * there is no Initial connection IDs storage.
4733 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004734 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004735 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004736
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004737 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4738 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004739 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4740 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004741 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004742
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004743 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4744 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004745 */
4746 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004747
4748 /* TODO Retry should be automatically activated if
4749 * suspect network usage is detected.
4750 */
4751 if (!token_len && l->bind_conf->quic_force_retry) {
4752 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004753 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004754 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4755 goto err;
4756 }
4757
4758 goto err;
4759 }
4760 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004761 pkt->token = buf;
4762 buf += pkt->token_len;
4763 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004764 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004765 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004766 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004767 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4768 goto err;
4769 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004770 }
4771
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004772 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4773 end - buf < len) {
4774 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4775 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004776 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004777
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004778 payload = buf;
4779 pkt->len = len + payload - beg;
4780
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004781 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004782 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004783 int ipv4;
4784 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004785 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004786 const unsigned char *salt = initial_salt_v1;
4787 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004788
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004789 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004790 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004791 goto err;
4792 }
4793
Frédéric Lécailledc364042022-01-27 16:51:54 +01004794 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4795 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4796 goto err;
4797 }
4798
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004799 pkt->saddr = dgram->saddr;
4800 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004801 qc = qc_new_conn(pkt->version, ipv4,
4802 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004803 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004804 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004805 goto err;
4806
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004807 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4808
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004809 odcid = &qc->rx.params.original_destination_connection_id;
4810 /* Copy the transport parameters. */
4811 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004812
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004813 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004814 if (pkt->token_len) {
4815 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4816 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4817 goto err;
4818 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004819 /* Copy retry_source_connection_id transport parameter. */
4820 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4821 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004822 }
4823 else {
4824 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4825 odcid->len = pkt->dcid.len;
4826 }
4827
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004828 /* Copy the initial source connection ID. */
4829 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4830 qc->enc_params_len =
4831 quic_transport_params_encode(qc->enc_params,
4832 qc->enc_params + sizeof qc->enc_params,
4833 &qc->rx.params, 1);
4834 if (!qc->enc_params_len)
4835 goto err;
4836
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004837 if (qc_conn_alloc_ssl_ctx(qc))
4838 goto err;
4839
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004840 if (!quic_conn_init_timer(qc))
4841 goto err;
4842
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004843 if (!quic_conn_init_idle_timer_task(qc))
4844 goto err;
4845
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004846 /* NOTE: the socket address has been concatenated to the destination ID
4847 * chosen by the client for Initial packets.
4848 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004849 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4850 salt = initial_salt_draft_29;
4851 salt_len = sizeof initial_salt_draft_29;
4852 }
4853 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004854 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004855 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004856 goto err;
4857 }
4858
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004859 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004860 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004861 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004862
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004863 /* If the insertion failed, it means that another
4864 * thread has already allocated a QUIC connection for
4865 * the same CID. Liberate our allocated connection.
4866 */
4867 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004868 qc_to_purge = qc;
4869
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004870 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4871 pkt->qc = qc;
4872 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004873
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004874 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004875 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004876 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004877 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004878 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004879 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004880 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004881 }
4882 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004883 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004884 }
4885 }
4886 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004887 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004888 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4889 goto err;
4890 }
4891
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004892 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004893 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004894
4895 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4896 * they must have the same DCID.
4897 */
4898 if (!first_pkt &&
4899 (pkt->dcid.len != dgram->dcid_len ||
4900 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4901 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4902 goto err;
4903 }
4904
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004905 buf += QUIC_HAP_CID_LEN;
4906
4907 /* A short packet is the last one of a UDP datagram. */
4908 payload = buf;
4909 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004910
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004911 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004912 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004913 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004914 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4915 goto err;
4916 }
4917
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004918 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004919 }
4920
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004921 if (qc->flags & QUIC_FL_CONN_CLOSING) {
4922 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
4923 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
4924 qc->nb_pkt_for_cc++;
4925 qc->nb_pkt_since_cc = 0;
4926 }
4927 /* Skip the entire datagram */
4928 pkt->len = end - beg;
4929 TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc);
4930 goto out;
4931 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004932
4933 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4934 * they must have the same DCID.
4935 *
4936 * This check must be done after the final update to pkt.len to
4937 * properly drop the packet on failure.
4938 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004939 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004940 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004941 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4942 QUIC_EV_CONN_LPKT, qc);
4943 /* Reset the anti-amplification bit. It will be set again
4944 * when sending the next packet if reached again.
4945 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004946 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
4947 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004948 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004949 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004950
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004951 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004952
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004953 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004954 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004955 goto out;
4956 }
4957
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004958 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004959 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004960 b_cspace = b_contig_space(&qc->rx.buf);
4961 if (b_cspace < pkt->len) {
4962 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004963 if (b_cspace) {
4964 b_putchr(&qc->rx.buf, 0x00);
4965 b_cspace--;
4966 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004967 b_add(&qc->rx.buf, b_cspace);
4968 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004969 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004970 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004971 goto err;
4972 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004973 }
4974
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004975 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004976 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004977 goto err;
4978 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004979
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004980 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004981 if (pkt->aad_len)
4982 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004983 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004984 /* Wake up the connection packet handler task from here only if all
4985 * the contexts have been initialized, especially the mux context
4986 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4987 * will start it if these contexts for the connection are not already
4988 * initialized.
4989 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004990 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004991 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004992 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004993
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004994 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004995
4996 return pkt->len;
4997
4998 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004999 /* Wakeup the I/O handler callback if the PTO timer must be armed.
5000 * This cannot be done by this thread.
5001 */
5002 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005003 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005004 if (conn_ctx && conn_ctx->wait_event.tasklet)
5005 tasklet_wakeup(conn_ctx->wait_event.tasklet);
5006 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01005007 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005008 if (!pkt->len)
5009 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005010 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005011 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005012
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005013 return -1;
5014}
5015
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005016/* This function builds into <buf> buffer a QUIC long packet header.
5017 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005018 */
5019static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
5020 int type, size_t pn_len, struct quic_conn *conn)
5021{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005022 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005023 return 0;
5024
5025 /* #0 byte flags */
5026 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5027 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5028 /* Version */
5029 quic_write_uint32(buf, end, conn->version);
5030 *(*buf)++ = conn->dcid.len;
5031 /* Destination connection ID */
5032 if (conn->dcid.len) {
5033 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5034 *buf += conn->dcid.len;
5035 }
5036 /* Source connection ID */
5037 *(*buf)++ = conn->scid.len;
5038 if (conn->scid.len) {
5039 memcpy(*buf, conn->scid.data, conn->scid.len);
5040 *buf += conn->scid.len;
5041 }
5042
5043 return 1;
5044}
5045
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005046/* This function builds into <buf> buffer a QUIC short packet header.
5047 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005048 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005049static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5050 size_t pn_len, struct quic_conn *conn,
5051 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005052{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005053 if (end - *buf < 1 + conn->dcid.len)
5054 return 0;
5055
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005056 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005057 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5058 ((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 +01005059 /* Destination connection ID */
5060 if (conn->dcid.len) {
5061 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5062 *buf += conn->dcid.len;
5063 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005064
5065 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005066}
5067
5068/* Apply QUIC header protection to the packet with <buf> as first byte address,
5069 * <pn> as address of the Packet number field, <pnlen> being this field length
5070 * with <aead> as AEAD cipher and <key> as secret key.
5071 * Returns 1 if succeeded or 0 if failed.
5072 */
5073static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5074 const EVP_CIPHER *aead, const unsigned char *key)
5075{
5076 int i, ret, outlen;
5077 EVP_CIPHER_CTX *ctx;
5078 /* We need an IV of at least 5 bytes: one byte for bytes #0
5079 * and at most 4 bytes for the packet number
5080 */
5081 unsigned char mask[5] = {0};
5082
5083 ret = 0;
5084 ctx = EVP_CIPHER_CTX_new();
5085 if (!ctx)
5086 return 0;
5087
5088 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5089 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5090 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5091 goto out;
5092
5093 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5094 for (i = 0; i < pnlen; i++)
5095 pn[i] ^= mask[i + 1];
5096
5097 ret = 1;
5098
5099 out:
5100 EVP_CIPHER_CTX_free(ctx);
5101
5102 return ret;
5103}
5104
5105/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5106 * ACK ranges if needed to a value below <limit> in bytes.
5107 * Return 1 if succeeded, 0 if not.
5108 */
5109static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5110{
5111 size_t room, ack_delay_sz;
5112
5113 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5114 /* A frame is made of 1 byte for the frame type. */
5115 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005116 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005117 return 0;
5118
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005119 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005120}
5121
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005122/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5123 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5124 * with <room> as available room, and <*len> the packet Length field initialized
5125 * with the number of bytes already present in this buffer which must be taken
5126 * into an account for the Length packet field value. <headlen> is the number of
5127 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005128 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005129 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005130 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005131 * Return 1 if succeeded, 0 if not.
5132 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005133static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005134 size_t room, size_t *len, size_t headlen,
5135 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005136 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005137{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005138 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005139 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005140
Frédéric Lécailleea604992020-12-24 13:01:37 +01005141 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005142 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005143 return 0;
5144
Frédéric Lécailleea604992020-12-24 13:01:37 +01005145 /* If we are not probing we must take into an account the congestion
5146 * control window.
5147 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005148 if (!qel->pktns->tx.pto_probe) {
5149 size_t remain = quic_path_prep_data(qc->path);
5150
5151 if (headlen > remain)
5152 return 0;
5153
5154 room = QUIC_MIN(room, remain - headlen);
5155 }
5156
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005157 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005158 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005159 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005160 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005161 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005162
Frédéric Lécailleea604992020-12-24 13:01:37 +01005163 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005164 break;
5165
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005166 switch (cf->type) {
5167 case QUIC_FT_CRYPTO:
5168 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005169 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005170 /* Compute the length of this CRYPTO frame header */
5171 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5172 /* Compute the data length of this CRyPTO frame. */
5173 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5174 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005175 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005176 if (!dlen)
5177 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005178
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005179 /* CRYPTO frame length. */
5180 flen = hlen + quic_int_getsize(dlen) + dlen;
5181 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005182 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005183 /* Add the CRYPTO data length and its encoded length to the packet
5184 * length and the length of this length.
5185 */
5186 *len += flen;
5187 room -= flen;
5188 if (dlen == cf->crypto.len) {
5189 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005190 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005191 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005192 }
5193 else {
5194 struct quic_frame *new_cf;
5195
5196 new_cf = pool_alloc(pool_head_quic_frame);
5197 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005198 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005199 return 0;
5200 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005201
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005202 new_cf->type = QUIC_FT_CRYPTO;
5203 new_cf->crypto.len = dlen;
5204 new_cf->crypto.offset = cf->crypto.offset;
5205 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005206 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005207 /* Consume <dlen> bytes of the current frame. */
5208 cf->crypto.len -= dlen;
5209 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005210 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005211 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005212
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005213 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005214 /* Note that these frames are accepted in short packets only without
5215 * "Length" packet field. Here, <*len> is used only to compute the
5216 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005217 *
5218 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005219 * excepting the variable ones. Note that +1 is for the type of this frame.
5220 */
5221 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005222 ((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 +02005223 /* Compute the data length of this STREAM frame. */
5224 avail_room = room - hlen - *len;
5225 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005226 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005227
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005228 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005229 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005230 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5231 dlen = max_available_room(avail_room, &dlen_sz);
5232 if (dlen > cf->stream.len) {
5233 dlen = cf->stream.len;
5234 }
5235 dlen_sz = quic_int_getsize(dlen);
5236 flen = hlen + dlen_sz + dlen;
5237 }
5238 else {
5239 dlen = QUIC_MIN(avail_room, cf->stream.len);
5240 flen = hlen + dlen;
5241 }
5242 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005243 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005244 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005245 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005246 /* Add the STREAM data length and its encoded length to the packet
5247 * length and the length of this length.
5248 */
5249 *len += flen;
5250 room -= flen;
5251 if (dlen == cf->stream.len) {
5252 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005253 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005254 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005255
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005256 /* The MUX stream might be released at this
5257 * stage. This can most notably happen on
5258 * retransmission.
5259 */
5260 if (qc->mux_state == QC_MUX_READY &&
5261 !cf->stream.stream->release) {
5262 qcc_streams_sent_done(cf->stream.stream->ctx,
5263 cf->stream.len,
5264 cf->stream.offset.key);
5265 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005266 }
5267 else {
5268 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005269 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005270
5271 new_cf = pool_zalloc(pool_head_quic_frame);
5272 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005273 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005274 return 0;
5275 }
5276
5277 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005278 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005279 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005280 new_cf->stream.id = cf->stream.id;
5281 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5282 new_cf->stream.offset = cf->stream.offset;
5283 new_cf->stream.len = dlen;
5284 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5285 /* FIN bit reset */
5286 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5287 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005288 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005289 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5290 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005291 cf_buf = b_make(b_orig(cf->stream.buf),
5292 b_size(cf->stream.buf),
5293 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005294 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005295 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005296 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005297
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005298 /* The MUX stream might be released at this
5299 * stage. This can most notably happen on
5300 * retransmission.
5301 */
5302 if (qc->mux_state == QC_MUX_READY &&
5303 !cf->stream.stream->release) {
5304 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5305 new_cf->stream.len,
5306 new_cf->stream.offset.key);
5307 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005308 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005309
5310 /* TODO the MUX is notified about the frame sending via
5311 * previous qcc_streams_sent_done call. However, the
5312 * sending can fail later, for example if the sendto
5313 * system call returns an error. As the MUX has been
5314 * notified, the transport layer is responsible to
5315 * bufferize and resent the announced data later.
5316 */
5317
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005318 break;
5319
5320 default:
5321 flen = qc_frm_len(cf);
5322 BUG_ON(!flen);
5323 if (flen > room)
5324 continue;
5325
5326 *len += flen;
5327 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005328 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005329 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005330 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005331 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01005332 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005333 }
5334
Frédéric Lécailleea604992020-12-24 13:01:37 +01005335 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005336}
5337
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005338/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005339 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5340 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005341 * filling the buffer with as much frames as possible from <frms> list of
5342 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005343 * 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 +02005344 * 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 +02005345 * having returned from this function.
5346 * 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 +01005347 * number field in this packet. <pn_len> will also have the packet number
5348 * length as value.
5349 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005350 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005351 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005352static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5353 size_t dglen, struct quic_tx_packet *pkt,
5354 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005355 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005356 struct quic_enc_level *qel, struct quic_conn *qc,
5357 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005358{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005359 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005360 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005361 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5362 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005363 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005364 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005365 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005366 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005367 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005368 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005369
Frédéric Lécailleea604992020-12-24 13:01:37 +01005370 /* Length field value with CRYPTO frames if present. */
5371 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005372 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005373 /* When not probing, and no immediate close is required, reduce the size of this
5374 * buffer to respect the congestion controller window.
5375 * 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 +01005376 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005377 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005378 size_t path_room;
5379
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005380 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005381 if (end - beg > path_room)
5382 end = beg + path_room;
5383 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005384
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005385 /* Ensure there is enough room for the TLS encryption tag and a zero token
5386 * length field if any.
5387 */
5388 if (end - pos < QUIC_TLS_TAG_LEN +
5389 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5390 goto no_room;
5391
5392 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005393 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005394 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005395 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005396 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005397 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005398 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005399 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005400 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005401 goto no_room;
5402
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005403 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5404 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005405 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005406 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005407 /* Build an ACK frame if required. */
5408 ack_frm_len = 0;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005409 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
5410 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005411 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005412 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005413 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5414 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5415 * that from here, we do not know if we will have to send a PING frame.
5416 * This will be decided after having computed the ack-eliciting frames
5417 * to be added to this packet.
5418 */
5419 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 +01005420 if (!ack_frm_len)
5421 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005422 }
5423
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005424 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005425 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005426 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005427 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005428 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005429
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005430 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005431 /* Initialize the length of the frames built below to <len>.
5432 * If any frame could be successfully built by qc_build_frms(),
5433 * we will have len_frms > len.
5434 */
5435 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005436 if (!qc_build_frms(&frm_list, frms,
5437 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005438 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005439 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005440 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005441 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005442
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005443 /* Length (of the remaining data). Must not fail because, the buffer size
5444 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5445 * for the encryption tag. It must be taken into an account for the length
5446 * of this packet.
5447 */
5448 if (len_frms)
5449 len = len_frms + QUIC_TLS_TAG_LEN;
5450 else
5451 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005452 /* CONNECTION_CLOSE frame */
5453 if (cc) {
5454 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005455
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005456 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005457 len += qc_frm_len(&cc_frm);
5458 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005459 add_ping_frm = 0;
5460 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005461 len_sz = quic_int_getsize(len);
5462 /* Add this packet size to <dglen> */
5463 dglen += head_len + len_sz + len;
5464 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5465 /* This is a maximum padding size */
5466 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5467 /* The length field value is of this packet is <len> + <padding_len>
5468 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005469 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005470 * sizes from <padding_len>.
5471 */
5472 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5473 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005474 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005475 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005476 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005477 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005478 add_ping_frm = 1;
5479 len += 1;
5480 }
5481 /* 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 +01005482 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005483 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5484 }
5485
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005486 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5487 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005488
5489 /* Packet number field address. */
5490 *buf_pn = pos;
5491
5492 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005493 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5494 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005495
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005496 if (ack_frm_len) {
5497 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
5498 goto no_room;
5499
5500 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005501 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005502 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005503
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005504 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005505 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005506 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005507 unsigned char *spos = pos;
5508
5509 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005510 ssize_t room = end - pos;
5511 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005512 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005513 /* TODO: this should not have happened if qc_build_frms()
5514 * had correctly computed and sized the frames to be
5515 * added to this packet. Note that <cf> was added
5516 * from <frm_list> to <frms> list by qc_build_frms().
5517 */
5518 LIST_DELETE(&cf->list);
5519 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005520 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005521 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005522
5523 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005524 quic_tx_packet_refinc(pkt);
5525 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005526 }
5527 }
5528
5529 /* Build a PING frame if needed. */
5530 if (add_ping_frm) {
5531 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005532 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005533 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005534 }
5535
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005536 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005537 if (cc) {
5538 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
5539 goto no_room;
5540
5541 pkt->flags |= QUIC_FL_TX_PACKET_CC;
5542 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005543
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005544 /* Build a PADDING frame if needed. */
5545 if (padding_len) {
5546 frm.type = QUIC_FT_PADDING;
5547 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005548 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005549 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005550 }
5551
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005552 /* If this packet is ack-eliciting and we are probing let's
5553 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005554 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005555 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5556 qel->pktns->tx.pto_probe)
5557 qel->pktns->tx.pto_probe--;
5558
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005559 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005560 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005561 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005562
5563 return 1;
5564
5565 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005566 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005567 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005568 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
5569
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005570 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005571}
5572
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005573static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005574{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005575 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005576 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005577 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005578 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005579 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005580 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005581 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005582 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005583 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005584 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005585}
5586
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005587/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005588 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
5589 * of prebuilt frames.
5590 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005591 * Return -2 if the packet could not be allocated or encrypted for any reason,
5592 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01005593 * XXX NOTE XXX
5594 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
5595 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
5596 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005597 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005598static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5599 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005600 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005601 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005602 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005603{
5604 /* The pointer to the packet number field. */
5605 unsigned char *buf_pn;
5606 unsigned char *beg, *end, *payload;
5607 int64_t pn;
5608 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005609 struct quic_tls_ctx *tls_ctx;
5610 struct quic_tx_packet *pkt;
5611
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005612 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005613 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005614 pkt = pool_alloc(pool_head_quic_tx_packet);
5615 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005616 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005617 *err = -2;
5618 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005619 }
5620
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005621 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005622 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005623 pn_len = 0;
5624 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005625
5626 pn = qel->pktns->tx.next_pn + 1;
5627 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 +02005628 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005629 *err = -1;
5630 goto err;
5631 }
5632
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005633 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005634 payload = buf_pn + pn_len;
5635 payload_len = end - payload;
5636 aad_len = payload - beg;
5637
5638 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005639 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005640 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005641 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005642 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005643
5644 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005645 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005646 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5647 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005648 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005649 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005650 goto err;
5651 }
5652
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005653 /* Consume a packet number */
5654 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005655 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01005656 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005657 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005658 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5659 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005660 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005661 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005662 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005663 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005664 pkt->in_flight_len = pkt->len;
5665 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005666 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01005667 /* Always reset this flags */
5668 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005669 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
5670 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
5671 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
5672 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005673
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005674 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005675 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005676
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005677 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005678
5679 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005680 /* TODO: what about the frames which have been built
5681 * for this packet.
5682 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005683 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005684 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005685 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005686}
5687
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005688
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005689/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5690 * event subscriber <es> is not allowed to change from a previous call as long
5691 * as at least one event is still subscribed. The <event_type> must only be a
5692 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5693 */
5694static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5695{
Willy Tarreau784b8682022-04-11 14:18:10 +02005696 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005697
5698 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5699 BUG_ON(qcc->subs && qcc->subs != es);
5700
5701 es->events |= event_type;
5702 qcc->subs = es;
5703
5704 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02005705 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005706
5707 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02005708 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005709
5710 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005711}
5712
5713/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5714 * The <es> pointer is not allowed to differ from the one passed to the
5715 * subscribe() call. It always returns zero.
5716 */
5717static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5718{
5719 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5720}
5721
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005722/* Store in <xprt_ctx> the context attached to <conn>.
5723 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005724 */
5725static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5726{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005727 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005728
5729 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5730
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005731 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005732 if (*xprt_ctx)
5733 goto out;
5734
Willy Tarreau784b8682022-04-11 14:18:10 +02005735 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005736
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005737 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005738 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005739
5740 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005741}
5742
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005743/* Start the QUIC transport layer */
5744static int qc_xprt_start(struct connection *conn, void *ctx)
5745{
5746 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005747 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005748
Willy Tarreau784b8682022-04-11 14:18:10 +02005749 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005750 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5751 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5752 return 0;
5753 }
5754
5755 /* mux-quic can now be considered ready. */
5756 qc->mux_state = QC_MUX_READY;
5757
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005758 tasklet_wakeup(qctx->wait_event.tasklet);
5759 return 1;
5760}
5761
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005762static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
5763{
Willy Tarreau784b8682022-04-11 14:18:10 +02005764 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005765 return NULL;
5766
Willy Tarreau784b8682022-04-11 14:18:10 +02005767 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005768}
5769
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005770/* transport-layer operations for QUIC connections. */
5771static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005772 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005773 .subscribe = quic_conn_subscribe,
5774 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005775 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005776 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005777 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5778 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005779 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02005780 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005781 .name = "QUIC",
5782};
5783
5784__attribute__((constructor))
5785static void __quic_conn_init(void)
5786{
5787 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5788 xprt_register(XPRT_QUIC, &ssl_quic);
5789}
5790
5791__attribute__((destructor))
5792static void __quic_conn_deinit(void)
5793{
5794 BIO_meth_free(ha_quic_meth);
5795}
5796
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005797/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5798 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005799 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005800 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005801struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005802{
5803 unsigned char *pos;
5804 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005805 struct quic_dghdlr *dghdlr = ctx;
5806 struct quic_dgram *dgram;
5807 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005808
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005809 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005810 pos = dgram->buf;
5811 end = pos + dgram->len;
5812 do {
5813 int ret;
5814 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005815
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005816 pkt = pool_zalloc(pool_head_quic_rx_packet);
5817 if (!pkt)
5818 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005819
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005820 quic_rx_packet_refinc(pkt);
5821 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5822 first_pkt = 0;
5823 pos += pkt->len;
5824 quic_rx_packet_refdec(pkt);
5825 if (ret == -1)
5826 /* If the packet length could not be found, we cannot continue. */
5827 break;
5828 } while (pos < end);
5829
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005830 /* Increasing the received bytes counter by the UDP datagram length
5831 * if this datagram could be associated to a connection.
5832 */
5833 if (dgram->qc)
5834 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005835
5836 /* Mark this datagram as consumed */
5837 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005838 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005839
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005840 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005841
5842 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005843 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005844}
5845
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005846/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5847 * Returns 1 if succeeded, 0 if not.
5848 */
5849static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5850 unsigned char **dcid, size_t *dcid_len)
5851{
5852 int long_header;
5853 size_t minlen, skip;
5854
5855 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5856 goto err;
5857
5858 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5859 minlen = long_header ?
5860 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5861 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005862 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005863 goto err;
5864
5865 buf += skip;
5866 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5867 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5868 goto err;
5869
5870 *dcid = buf;
5871
5872 return 1;
5873
5874 err:
5875 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5876 return 0;
5877}
5878
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005879/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5880 * correct datagram handler.
5881 * Return 1 if a correct datagram could be found, 0 if not.
5882 */
5883int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5884 struct sockaddr_storage *saddr,
5885 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005886{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005887 struct quic_dgram *dgram;
5888 unsigned char *dcid;
5889 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005890 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005891
5892 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005893 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005894
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005895 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005896 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005897 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005898
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005899 cid_tid = quic_get_cid_tid(dcid);
5900
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005901 /* All the members must be initialized! */
5902 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005903 dgram->buf = buf;
5904 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005905 dgram->dcid = dcid;
5906 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005907 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005908 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005909 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005910 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005911
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005912 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005913
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005914 return 1;
5915
5916 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005917 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005918}
5919
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005920/* Allocate a new stream descriptor with id <id>. The caller is responsible to
5921 * store the stream in the appropriate tree.
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005922 *
5923 * Returns the newly allocated instance on success or else NULL.
5924 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005925struct qc_stream_desc *qc_stream_desc_new(uint64_t id, void *ctx)
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005926{
5927 struct qc_stream_desc *stream;
5928
5929 stream = pool_alloc(pool_head_quic_conn_stream);
5930 if (!stream)
5931 return NULL;
5932
5933 stream->by_id.key = id;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005934 stream->by_id.node.leaf_p = NULL;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005935
5936 stream->buf = BUF_NULL;
5937 stream->acked_frms = EB_ROOT;
5938 stream->ack_offset = 0;
5939 stream->release = 0;
5940 stream->ctx = ctx;
5941
5942 return stream;
5943}
5944
5945/* Mark the stream descriptor <stream> as released by the upper layer. It will
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005946 * be freed as soon as all its buffered data are acknowledged. In the meantime,
5947 * the stream is stored in the <qc> tree : thus it must have been removed from
5948 * any other tree before calling this function.
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005949 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005950void qc_stream_desc_release(struct qc_stream_desc *stream,
5951 struct quic_conn *qc)
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005952{
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005953 BUG_ON(stream->by_id.node.leaf_p);
5954
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005955 stream->release = 1;
5956 stream->ctx = NULL;
5957
5958 if (!b_data(&stream->buf))
5959 qc_stream_desc_free(stream);
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005960 else
5961 eb64_insert(&qc->streams_by_id, &stream->by_id);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005962}
5963
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02005964/* Notify the MUX layer if alive about an imminent close of <qc>. */
5965void qc_notify_close(struct quic_conn *qc)
5966{
5967 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
5968 return;
5969
5970 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
5971
5972 /* wake up the MUX */
5973 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
5974 qc->conn->mux->wake(qc->conn);
5975}
5976
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005977/* Function to automatically activate QUIC traces on stdout.
5978 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5979 * Main use for now is in the docker image for QUIC interop testing.
5980 */
5981static void quic_init_stdout_traces(void)
5982{
5983#ifdef ENABLE_QUIC_STDOUT_TRACES
5984 trace_quic.sink = sink_find("stdout");
5985 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005986 trace_quic.state = TRACE_STATE_RUNNING;
5987#endif
5988}
5989INITCALL0(STG_INIT, quic_init_stdout_traces);
5990
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005991/*
5992 * Local variables:
5993 * c-indent-level: 8
5994 * c-basic-offset: 8
5995 * End:
5996 */