blob: 9dd280a18696e1656f2f9d4e25e0a0966353af51 [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écaillec5e72b92020-12-02 16:11:40 +0100351 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100352 pkt, quic_packet_type_enc_level_char(pkt->type));
353 if (pkt->pnl)
354 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
355 (unsigned long long)pkt->pn);
356 if (pkt->token_len)
357 chunk_appendf(&trace_buf, " toklen=%llu",
358 (unsigned long long)pkt->token_len);
359 if (pkt->aad_len)
360 chunk_appendf(&trace_buf, " aadlen=%llu",
361 (unsigned long long)pkt->aad_len);
362 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
363 pkt->flags, (unsigned long long)pkt->len);
364 }
365 if (pktlen)
366 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
367 if (ssl) {
368 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
369 chunk_appendf(&trace_buf, " el=%c",
370 quic_enc_level_char(ssl_to_quic_enc_level(level)));
371 }
372 }
373
374 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
375 const struct quic_rx_packet *pkt = a2;
376 const struct quic_rx_crypto_frm *cf = a3;
377 const SSL *ssl = a4;
378
379 if (pkt)
380 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
381 quic_packet_type_enc_level_char(pkt->type),
382 (unsigned long long)pkt->pn);
383 if (cf)
384 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
385 (unsigned long long)cf->offset_node.key,
386 (unsigned long long)cf->len);
387 if (ssl) {
388 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200389 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100390 quic_enc_level_char(ssl_to_quic_enc_level(level)));
391 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100392
393 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100394 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100395 }
396
397 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
398 const struct quic_frame *frm = a2;
399
400 if (frm)
401 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
402 }
403
404 if (mask & QUIC_EV_CONN_PHPKTS) {
405 const struct quic_enc_level *qel = a2;
406
407 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100408 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100409 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100410 " 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 +0100411 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200412 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200413 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100414 (unsigned long long)qc->path->cwnd,
415 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100416 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100417 (unsigned long long)pktns->tx.in_flight,
418 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100419 }
420 }
421
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100422 if (mask & QUIC_EV_CONN_ENCPKT) {
423 const struct enc_debug_info *edi = a2;
424
425 if (edi)
426 chunk_appendf(&trace_buf,
427 " payload=@%p payload_len=%llu"
428 " aad=@%p aad_len=%llu pn=%llu",
429 edi->payload, (unsigned long long)edi->payload_len,
430 edi->aad, (unsigned long long)edi->aad_len,
431 (unsigned long long)edi->pn);
432 }
433
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100434 if (mask & QUIC_EV_CONN_RMHP) {
435 const struct quic_rx_packet *pkt = a2;
436
437 if (pkt) {
438 const int *ret = a3;
439
440 chunk_appendf(&trace_buf, " pkt@%p", pkt);
441 if (ret && *ret)
442 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
443 pkt->pnl, (unsigned long long)pkt->pn);
444 }
445 }
446
447 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200448 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100449 const unsigned long *val1 = a3;
450 const unsigned long *val2 = a4;
451
452 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100453 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100454 if (val1)
455 chunk_appendf(&trace_buf, " %lu", *val1);
456 if (val2)
457 chunk_appendf(&trace_buf, "..%lu", *val2);
458 }
459
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100460 if (mask & QUIC_EV_CONN_ACKSTRM) {
461 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200462 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100463
464 if (s)
465 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200466 if (stream)
467 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100468 }
469
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100470 if (mask & QUIC_EV_CONN_RTTUPDT) {
471 const unsigned int *rtt_sample = a2;
472 const unsigned int *ack_delay = a3;
473 const struct quic_loss *ql = a4;
474
475 if (rtt_sample)
476 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
477 if (ack_delay)
478 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
479 if (ql)
480 chunk_appendf(&trace_buf,
481 " srtt=%ums rttvar=%ums min_rtt=%ums",
482 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
483 }
484 if (mask & QUIC_EV_CONN_CC) {
485 const struct quic_cc_event *ev = a2;
486 const struct quic_cc *cc = a3;
487
488 if (a2)
489 quic_cc_event_trace(&trace_buf, ev);
490 if (a3)
491 quic_cc_state_trace(&trace_buf, cc);
492 }
493
494 if (mask & QUIC_EV_CONN_PKTLOSS) {
495 const struct quic_pktns *pktns = a2;
496 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497
498 if (pktns) {
499 chunk_appendf(&trace_buf, " pktns=%s",
500 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
501 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
502 if (pktns->tx.loss_time)
503 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100504 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100505 }
506 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
507 struct quic_tx_packet *pkt;
508
509 chunk_appendf(&trace_buf, " lost_pkts:");
510 list_for_each_entry(pkt, lost_pkts, list)
511 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
512 }
513 }
514
515 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100516 const struct quic_pktns *pktns = a2;
517 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100518 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100519
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100520 if (ifae_pkts)
521 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
522 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100523 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100524 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100525 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100526 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
527 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100528 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
529 if (pktns->tx.in_flight)
530 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100531 if (pktns->tx.loss_time)
532 chunk_appendf(&trace_buf, " loss_time=%dms",
533 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
534 }
535 if (mask & QUIC_EV_CONN_SPTO) {
536 if (pktns->tx.time_of_last_eliciting)
537 chunk_appendf(&trace_buf, " tole=%dms",
538 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
539 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100540 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100541 }
542 }
543
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200544 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100545 chunk_appendf(&trace_buf,
546 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
547 }
548 }
549
550 if (mask & QUIC_EV_CONN_SPPKTS) {
551 const struct quic_tx_packet *pkt = a2;
552
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100553 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
554 (unsigned long long)qc->path->cwnd,
555 (unsigned long long)qc->path->prep_in_flight,
556 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100557 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100558 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100559 (unsigned long)pkt->pn_node.key,
560 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
561 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100562 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100563 }
564 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100565
566 if (mask & QUIC_EV_CONN_SSLALERT) {
567 const uint8_t *alert = a2;
568 const enum ssl_encryption_level_t *level = a3;
569
570 if (alert)
571 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
572 if (level)
573 chunk_appendf(&trace_buf, " el=%c",
574 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
575 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100576
577 if (mask & QUIC_EV_CONN_BCFRMS) {
578 const size_t *sz1 = a2;
579 const size_t *sz2 = a3;
580 const size_t *sz3 = a4;
581
582 if (sz1)
583 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
584 if (sz2)
585 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
586 if (sz3)
587 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
588 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100589
590 if (mask & QUIC_EV_CONN_PSTRM) {
591 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100592
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100593 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100594 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100595 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596 }
597 if (mask & QUIC_EV_CONN_LPKT) {
598 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200599 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100600
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100601 if (pkt) {
602 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
603 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
604 if (pkt->pn_node.key != (uint64_t)-1)
605 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
606 }
607
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200608 if (len)
609 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100610 }
611
612}
613
614/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100615static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100616{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200617 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100618
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100619 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620 return 1;
621
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200622 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
623 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200624 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
625 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200626 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100627 return 1;
628
629 return 0;
630}
631
632/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
633 * both loss detection and PTO and schedule the task assiated to this timer if needed.
634 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100635static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100636{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637 struct quic_pktns *pktns;
638 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200639 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100640
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100641 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100642 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100643
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100644 pktns = quic_loss_pktns(qc);
645 if (tick_isset(pktns->tx.loss_time)) {
646 qc->timer = pktns->tx.loss_time;
647 goto out;
648 }
649
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100650 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100651 * cancelled for a server which reached the anti-amplification limit.
652 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100653 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200654 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100655 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100656 qc->timer = TICK_ETERNITY;
657 goto out;
658 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100659
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100660 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100661 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100662 /* Timer cancellation. */
663 qc->timer = TICK_ETERNITY;
664 goto out;
665 }
666
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200667 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200668 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100669 if (tick_isset(pto))
670 qc->timer = pto;
671 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100672 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100673 if (tick_is_expired(qc->timer, now_ms)) {
674 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100675 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100676 }
677 else {
678 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100679 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100680 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100681 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100682 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100683}
684
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100685/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
686 * connection.
687 * Return 1 if succeeded, 0 if not.
688 */
689static int quic_tls_key_update(struct quic_conn *qc)
690{
691 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
692 struct quic_tls_secrets *rx, *tx;
693 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
694 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
695
696 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
697 rx = &tls_ctx->rx;
698 tx = &tls_ctx->tx;
699 nxt_rx = &qc->ku.nxt_rx;
700 nxt_tx = &qc->ku.nxt_tx;
701
702 /* Prepare new RX secrets */
703 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
704 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100705 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100706 return 0;
707 }
708
709 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
710 nxt_rx->key, nxt_rx->keylen,
711 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
712 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100713 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100714 return 0;
715 }
716
717 /* Prepare new TX secrets */
718 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
719 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100720 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100721 return 0;
722 }
723
724 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
725 nxt_tx->key, nxt_tx->keylen,
726 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
727 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100728 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100729 return 0;
730 }
731
732 return 1;
733}
734
735/* Rotate the Key Update information for <qc> QUIC connection.
736 * Must be used after having updated them.
737 * Always succeeds.
738 */
739static void quic_tls_rotate_keys(struct quic_conn *qc)
740{
741 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
742 unsigned char *curr_secret, *curr_iv, *curr_key;
743
744 /* Rotate the RX secrets */
745 curr_secret = tls_ctx->rx.secret;
746 curr_iv = tls_ctx->rx.iv;
747 curr_key = tls_ctx->rx.key;
748
749 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
750 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
751 tls_ctx->rx.key = qc->ku.nxt_rx.key;
752
753 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
754 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
755 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
756
757 qc->ku.prv_rx.secret = curr_secret;
758 qc->ku.prv_rx.iv = curr_iv;
759 qc->ku.prv_rx.key = curr_key;
760 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
761
762 /* Update the TX secrets */
763 curr_secret = tls_ctx->tx.secret;
764 curr_iv = tls_ctx->tx.iv;
765 curr_key = tls_ctx->tx.key;
766
767 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
768 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
769 tls_ctx->tx.key = qc->ku.nxt_tx.key;
770
771 qc->ku.nxt_tx.secret = curr_secret;
772 qc->ku.nxt_tx.iv = curr_iv;
773 qc->ku.nxt_tx.key = curr_key;
774}
775
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100776#ifndef OPENSSL_IS_BORINGSSL
777int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
778 const uint8_t *read_secret,
779 const uint8_t *write_secret, size_t secret_len)
780{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100781 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
782 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 +0100783 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100784 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100785
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100786 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100787 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200788 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100789 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100790 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100791 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100792
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100793 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100794 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100795 goto err;
796 }
797
798 rx = &tls_ctx->rx;
799 tx = &tls_ctx->tx;
800
801 rx->aead = tx->aead = tls_aead(cipher);
802 rx->md = tx->md = tls_md(cipher);
803 rx->hp = tx->hp = tls_hp(cipher);
804
805 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
806 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100807 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100808 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100809 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100810 }
811
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100812 /* Enqueue this connection asap if we could derive O-RTT secrets as
813 * listener. Note that a listener derives only RX secrets for this
814 * level.
815 */
816 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
817 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100818
819 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100820 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100821
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100822 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
823 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100824 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100825 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100826 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100827 }
828
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100829 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100830 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
831 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
832 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
833
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200834 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100835 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
836 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100837 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100838 goto err;
839 }
840
841 memcpy(rx->secret, read_secret, secret_len);
842 rx->secretlen = secret_len;
843 memcpy(tx->secret, write_secret, secret_len);
844 tx->secretlen = secret_len;
845 /* Initialize all the secret keys lengths */
846 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
847 /* Prepare the next key update */
848 if (!quic_tls_key_update(qc))
849 goto err;
850 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100851
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100852 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100853 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100854 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100855 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100856 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100857
858 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100859 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100860 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100861}
862#else
863/* ->set_read_secret callback to derive the RX secrets at <level> encryption
864 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500865 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100866 */
867int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
868 const SSL_CIPHER *cipher,
869 const uint8_t *secret, size_t secret_len)
870{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100871 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100872 struct quic_tls_ctx *tls_ctx =
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100873 &qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100874
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100875 TRACE_ENTER(QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200876 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100877 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100878 goto out;
879 }
880
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100881 tls_ctx->rx.aead = tls_aead(cipher);
882 tls_ctx->rx.md = tls_md(cipher);
883 tls_ctx->rx.hp = tls_hp(cipher);
884
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100885 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
886 goto err;
887
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100888 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 +0100889 tls_ctx->rx.key, tls_ctx->rx.keylen,
890 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
892 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100893 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100894 goto err;
895 }
896
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100897 if (!qc_is_listener(qc) && level == ssl_encryption_application) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100898 const unsigned char *buf;
899 size_t buflen;
900
901 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
902 if (!buflen)
903 goto err;
904
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100905 if (!quic_transport_params_store(qc, 1, buf, buf + buflen))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100906 goto err;
907 }
908
909 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100910 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100911 TRACE_LEAVE(QUIC_EV_CONN_RSEC, qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100912
913 return 1;
914
915 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100916 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100917 return 0;
918}
919
920/* ->set_write_secret callback to derive the TX secrets at <level>
921 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500922 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100923 */
924int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
925 const SSL_CIPHER *cipher,
926 const uint8_t *secret, size_t secret_len)
927{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100928 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
929 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 +0100930
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100931 TRACE_ENTER(QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200932 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100933 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100934 goto out;
935 }
936
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100937 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
938 goto err;
939
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100940 tls_ctx->tx.aead = tls_aead(cipher);
941 tls_ctx->tx.md = tls_md(cipher);
942 tls_ctx->tx.hp = tls_hp(cipher);
943
944 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 +0100945 tls_ctx->tx.key, tls_ctx->tx.keylen,
946 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100947 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
948 secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100949 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100950 goto err;
951 }
952
953 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100954 TRACE_LEAVE(QUIC_EV_CONN_WSEC, qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100955 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100956 return 1;
957
958 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100959 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100960 return 0;
961}
962#endif
963
964/* This function copies the CRYPTO data provided by the TLS stack found at <data>
965 * with <len> as size in CRYPTO buffers dedicated to store the information about
966 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
967 * It fails only if it could not managed to allocate enough CRYPTO buffers to
968 * store all the data.
969 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
970 */
971static int quic_crypto_data_cpy(struct quic_enc_level *qel,
972 const unsigned char *data, size_t len)
973{
974 struct quic_crypto_buf **qcb;
975 /* The remaining byte to store in CRYPTO buffers. */
976 size_t cf_offset, cf_len, *nb_buf;
977 unsigned char *pos;
978
979 nb_buf = &qel->tx.crypto.nb_buf;
980 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
981 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
982 cf_len = len;
983
984 while (len) {
985 size_t to_copy, room;
986
987 pos = (*qcb)->data + (*qcb)->sz;
988 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
989 to_copy = len > room ? room : len;
990 if (to_copy) {
991 memcpy(pos, data, to_copy);
992 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
993 qel->tx.crypto.sz += to_copy;
994 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100995 len -= to_copy;
996 data += to_copy;
997 }
998 else {
999 struct quic_crypto_buf **tmp;
1000
1001 tmp = realloc(qel->tx.crypto.bufs,
1002 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1003 if (tmp) {
1004 qel->tx.crypto.bufs = tmp;
1005 qcb = &qel->tx.crypto.bufs[*nb_buf];
1006 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1007 if (!*qcb)
1008 return 0;
1009
1010 (*qcb)->sz = 0;
1011 ++*nb_buf;
1012 }
1013 else {
1014 break;
1015 }
1016 }
1017 }
1018
1019 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1020 * have been buffered.
1021 */
1022 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001023 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001024 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001025
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001026 /* There is at most one CRYPTO frame in this packet number
1027 * space. Let's look for it.
1028 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001029 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001030 if (frm->type != QUIC_FT_CRYPTO)
1031 continue;
1032
1033 /* Found */
1034 found = frm;
1035 break;
1036 }
1037
1038 if (found) {
1039 found->crypto.len += cf_len;
1040 }
1041 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001042 frm = pool_alloc(pool_head_quic_frame);
1043 if (!frm)
1044 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001045
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001046 frm->type = QUIC_FT_CRYPTO;
1047 frm->crypto.offset = cf_offset;
1048 frm->crypto.len = cf_len;
1049 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001050 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001051 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001052 }
1053
1054 return len == 0;
1055}
1056
1057
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001058/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1059void quic_set_tls_alert(struct quic_conn *qc, int alert)
1060{
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001061 qc->err_code = QC_ERR_CRYPTO_ERROR | alert;
1062 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001063 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001064}
1065
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001066/* Set the application for <qc> QUIC connection.
1067 * Return 1 if succeeded, 0 if not.
1068 */
1069int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1070{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001071 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1072 qc->app_ops = &h3_ops;
1073 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1074 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001075 else
1076 return 0;
1077
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001078 return 1;
1079}
1080
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001081/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1082 * wants to provide the QUIC layer with CRYPTO data.
1083 * Returns 1 if succeeded, 0 if not.
1084 */
1085int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1086 const uint8_t *data, size_t len)
1087{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001088 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089 enum quic_tls_enc_level tel;
1090 struct quic_enc_level *qel;
1091
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001092 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1093 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001094 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001095 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001096 goto out;
1097 }
1098
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001100 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001101 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001102 goto err;
1103 }
1104
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001105 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001106 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001107 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001108 goto err;
1109 }
1110
1111 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001112 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001114 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001115 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116 return 1;
1117
1118 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001119 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001120 return 0;
1121}
1122
1123int ha_quic_flush_flight(SSL *ssl)
1124{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001125 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001126
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001127 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1128 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001129
1130 return 1;
1131}
1132
1133int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1134{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001135 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001136
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001137 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1138 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001139 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001140 return 1;
1141}
1142
1143/* QUIC TLS methods */
1144static SSL_QUIC_METHOD ha_quic_method = {
1145#ifdef OPENSSL_IS_BORINGSSL
1146 .set_read_secret = ha_set_rsec,
1147 .set_write_secret = ha_set_wsec,
1148#else
1149 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1150#endif
1151 .add_handshake_data = ha_quic_add_handshake_data,
1152 .flush_flight = ha_quic_flush_flight,
1153 .send_alert = ha_quic_send_alert,
1154};
1155
1156/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1157 * Returns an error count.
1158 */
1159int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1160{
1161 struct proxy *curproxy = bind_conf->frontend;
1162 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1163 int cfgerr = 0;
1164
1165#if 0
1166 /* XXX Did not manage to use this. */
1167 const char *ciphers =
1168 "TLS_AES_128_GCM_SHA256:"
1169 "TLS_AES_256_GCM_SHA384:"
1170 "TLS_CHACHA20_POLY1305_SHA256:"
1171 "TLS_AES_128_CCM_SHA256";
1172#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001173 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174 long options =
1175 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1176 SSL_OP_SINGLE_ECDH_USE |
1177 SSL_OP_CIPHER_SERVER_PREFERENCE;
1178 SSL_CTX *ctx;
1179
1180 ctx = SSL_CTX_new(TLS_server_method());
1181 bind_conf->initial_ctx = ctx;
1182
1183 SSL_CTX_set_options(ctx, options);
1184#if 0
1185 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1186 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1187 "for bind '%s' at [%s:%d].\n",
1188 curproxy->id, ciphers,
1189 bind_conf->arg, bind_conf->file, bind_conf->line);
1190 cfgerr++;
1191 }
1192#endif
1193
1194 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1195 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1196 "for bind '%s' at [%s:%d].\n",
1197 curproxy->id, groups,
1198 bind_conf->arg, bind_conf->file, bind_conf->line);
1199 cfgerr++;
1200 }
1201
1202 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1203 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1204 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001205
1206#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1207#ifdef OPENSSL_IS_BORINGSSL
1208 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1209 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1210#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1211 if (bind_conf->ssl_conf.early_data) {
1212 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001213 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001214 }
1215 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1216 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1217#else
1218 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1219#endif
1220 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1221#endif
1222 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1223
1224 return cfgerr;
1225}
1226
1227/* Decode an expected packet number from <truncated_on> its truncated value,
1228 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1229 * the number of bits used to encode this packet number (its length in bytes * 8).
1230 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1231 */
1232static uint64_t decode_packet_number(uint64_t largest_pn,
1233 uint32_t truncated_pn, unsigned int pn_nbits)
1234{
1235 uint64_t expected_pn = largest_pn + 1;
1236 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1237 uint64_t pn_hwin = pn_win / 2;
1238 uint64_t pn_mask = pn_win - 1;
1239 uint64_t candidate_pn;
1240
1241
1242 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1243 /* Note that <pn_win> > <pn_hwin>. */
1244 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1245 candidate_pn + pn_hwin <= expected_pn)
1246 return candidate_pn + pn_win;
1247
1248 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1249 return candidate_pn - pn_win;
1250
1251 return candidate_pn;
1252}
1253
1254/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1255 * cryptographic context.
1256 * <largest_pn> is the largest received packet number and <pn> the address of
1257 * the packet number field for this packet with <byte0> address of its first byte.
1258 * <end> points to one byte past the end of this packet.
1259 * Returns 1 if succeeded, 0 if not.
1260 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001261static int qc_do_rm_hp(struct quic_conn *qc,
1262 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001263 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001264 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265{
1266 int ret, outlen, i, pnlen;
1267 uint64_t packet_number;
1268 uint32_t truncated_pn = 0;
1269 unsigned char mask[5] = {0};
1270 unsigned char *sample;
1271 EVP_CIPHER_CTX *cctx;
1272 unsigned char *hp_key;
1273
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001274 /* Check there is enough data in this packet. */
1275 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001276 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001277 return 0;
1278 }
1279
1280 cctx = EVP_CIPHER_CTX_new();
1281 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001282 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001283 return 0;
1284 }
1285
1286 ret = 0;
1287 sample = pn + QUIC_PACKET_PN_MAXLEN;
1288
1289 hp_key = tls_ctx->rx.hp_key;
1290 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1291 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1292 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001293 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001294 goto out;
1295 }
1296
1297 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1298 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1299 for (i = 0; i < pnlen; i++) {
1300 pn[i] ^= mask[i + 1];
1301 truncated_pn = (truncated_pn << 8) | pn[i];
1302 }
1303
1304 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1305 /* Store remaining information for this unprotected header */
1306 pkt->pn = packet_number;
1307 pkt->pnl = pnlen;
1308
1309 ret = 1;
1310
1311 out:
1312 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313
1314 return ret;
1315}
1316
1317/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1318 * address, with <payload_len> as payload length, <aad> as address of
1319 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1320 * context.
1321 * Returns 1 if succeeded, 0 if not.
1322 */
1323static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1324 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001325 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001326{
1327 unsigned char iv[12];
1328 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001329 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001330 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001331
1332 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001333 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001334 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001335 }
1336
1337 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1338 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001339 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001340 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001341 }
1342
1343 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001344
1345 err:
1346 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001347 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001348 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001349}
1350
1351/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1352 * Returns 1 if succeeded, 0 if not.
1353 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001354static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001355{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001356 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001357 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001358 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001360 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1361 unsigned char *rx_key = tls_ctx->rx.key;
1362
1363 kp_changed = 0;
1364 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1365 /* The two tested bits are not at the same position,
1366 * this is why they are first both inversed.
1367 */
1368 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1369 if (pkt->pn < tls_ctx->rx.pn) {
1370 /* The lowest packet number of a previous key phase
1371 * cannot be null if it really stores previous key phase
1372 * secrets.
1373 */
1374 if (!pkt->qc->ku.prv_rx.pn)
1375 return 0;
1376
1377 rx_iv = pkt->qc->ku.prv_rx.iv;
1378 rx_key = pkt->qc->ku.prv_rx.key;
1379 }
1380 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1381 /* Next key phase */
1382 kp_changed = 1;
1383 rx_iv = pkt->qc->ku.nxt_rx.iv;
1384 rx_key = pkt->qc->ku.nxt_rx.key;
1385 }
1386 }
1387 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001388
1389 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1390 return 0;
1391
1392 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1393 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001394 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001395 if (!ret)
1396 return 0;
1397
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001398 /* Update the keys only if the packet decryption succeeded. */
1399 if (kp_changed) {
1400 quic_tls_rotate_keys(pkt->qc);
1401 /* Toggle the Key Phase bit */
1402 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1403 /* Store the lowest packet number received for the current key phase */
1404 tls_ctx->rx.pn = pkt->pn;
1405 /* Prepare the next key update */
1406 if (!quic_tls_key_update(pkt->qc))
1407 return 0;
1408 }
1409
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001410 /* Update the packet length (required to parse the frames). */
1411 pkt->len = pkt->aad_len + ret;
1412
1413 return 1;
1414}
1415
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02001416/* Free the stream descriptor <stream> buffer. This function should be used
1417 * when all its data have been acknowledged. If the stream was released by the
1418 * upper layer, the stream descriptor will be freed.
1419 *
1420 * Returns 0 if the stream was not freed else non-zero.
1421 */
1422static int qc_stream_desc_free(struct qc_stream_desc *stream)
1423{
1424 b_free(&stream->buf);
1425 offer_buffers(NULL, 1);
1426
1427 if (stream->release) {
1428 eb64_delete(&stream->by_id);
1429 pool_free(pool_head_quic_conn_stream, stream);
1430
1431 return 1;
1432 }
1433
1434 return 0;
1435}
1436
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001437/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001438 *
1439 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001440 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001441static int quic_stream_try_to_consume(struct quic_conn *qc,
1442 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001443{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001444 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001445 struct eb64_node *frm_node;
1446
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001447 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001448 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001449 while (frm_node) {
1450 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001451 struct quic_frame *frm;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001452
1453 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001454 if (strm->offset.key > stream->ack_offset)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001455 break;
1456
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001457 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001458 qc, strm, stream);
1459
1460 if (strm->offset.key + strm->len > stream->ack_offset) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001461 const size_t diff = strm->offset.key + strm->len -
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001462 stream->ack_offset;
1463 stream->ack_offset += diff;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001464 b_del(strm->buf, diff);
1465 ret = 1;
1466 }
1467
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001468 frm_node = eb64_next(frm_node);
1469 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001470
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001471 frm = container_of(strm, struct quic_frame, stream);
1472 LIST_DELETE(&frm->list);
1473 quic_tx_packet_refdec(frm->pkt);
1474 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001475 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001476
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001477 if (!b_data(&stream->buf))
1478 qc_stream_desc_free(stream);
1479
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001480 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001481}
1482
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001483/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001484static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1485 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001486{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001487 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001488
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001489 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001490 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001491 switch (frm->type) {
1492 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1493 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001494 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001495 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001496 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001497
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001498 /* do not use strm_frm->stream as the qc_stream_desc instance
1499 * might be freed at this stage. Use the id to do a proper
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001500 * lookup. First search in the MUX then in the released stream
1501 * list.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001502 *
1503 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001504 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001505 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02001506 if (qc->mux_state == QC_MUX_READY)
1507 stream = qcc_get_stream(qc->qcc, strm_frm->id);
1508 if (!stream) {
1509 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1510 stream = eb64_entry(node, struct qc_stream_desc, by_id);
1511 }
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001512
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001513 if (!stream) {
1514 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001515 LIST_DELETE(&frm->list);
1516 quic_tx_packet_refdec(frm->pkt);
1517 pool_free(pool_head_quic_frame, frm);
1518
1519 /* early return */
1520 return;
1521 }
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001522
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001523 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
1524 if (strm_frm->offset.key <= stream->ack_offset) {
1525 if (strm_frm->offset.key + strm_frm->len > stream->ack_offset) {
1526 const size_t diff = strm_frm->offset.key + strm_frm->len -
1527 stream->ack_offset;
1528 stream->ack_offset += diff;
1529 b_del(strm_frm->buf, diff);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001530 stream_acked = 1;
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001531
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001532 if (!b_data(strm_frm->buf)) {
1533 if (qc_stream_desc_free(stream)) {
1534 /* early return */
1535 return;
1536 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001537 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001538 }
1539
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +01001540 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001541 qc, strm_frm, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001542 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001543 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001544 pool_free(pool_head_quic_frame, frm);
1545 }
1546 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001547 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001548 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001549
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001550 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001551 }
1552 break;
1553 default:
1554 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001555 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001556 pool_free(pool_head_quic_frame, frm);
1557 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001558
1559 if (stream_acked) {
1560 struct qcc *qcc = qc->qcc;
1561
1562 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1563 tasklet_wakeup(qcc->subs->tasklet);
1564 qcc->subs->events &= ~SUB_RETRY_SEND;
1565 if (!qcc->subs->events)
1566 qcc->subs = NULL;
1567 }
1568 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569}
1570
1571/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1572 * deallocating them, and their TX frames.
1573 * Returns the last node reached to be used for the next range.
1574 * May be NULL if <largest> node could not be found.
1575 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001576static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1577 struct eb_root *pkts,
1578 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001579 struct list *newly_acked_pkts,
1580 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001581 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001582{
1583 struct eb64_node *node;
1584 struct quic_tx_packet *pkt;
1585
1586 if (largest_node)
1587 node = largest_node;
1588 else {
1589 node = eb64_lookup(pkts, largest);
1590 while (!node && largest > smallest) {
1591 node = eb64_lookup(pkts, --largest);
1592 }
1593 }
1594
1595 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001596 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001597
1598 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1599 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001600 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001601 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001602 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001603 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001604 node = eb64_prev(node);
1605 eb64_delete(&pkt->pn_node);
1606 }
1607
1608 return node;
1609}
1610
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001611/* Remove all frames from <pkt_frm_list> and reinsert them in the
1612 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001613 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001614static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1615 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001616 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001617{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001618 struct quic_frame *frm, *frmbak;
1619 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001621 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1622 LIST_DELETE(&frm->list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001623 quic_tx_packet_refdec(frm->pkt);
1624 /* This frame is not freed but removed from its packet */
1625 frm->pkt = NULL;
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001626 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001627 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001628 }
1629
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001630 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001631}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001632
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001633/* Free <pkt> TX packet and its attached frames.
1634 * This is the responsability of the caller to remove this packet of
1635 * any data structure it was possibly attached to.
1636 */
1637static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1638{
1639 struct quic_frame *frm, *frmbak;
1640
1641 if (!pkt)
1642 return;
1643
1644 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1645 LIST_DELETE(&frm->list);
1646 pool_free(pool_head_quic_frame, frm);
1647 }
1648 pool_free(pool_head_quic_tx_packet, pkt);
1649}
1650
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001651/* Free the TX packets of <pkts> list */
1652static inline void free_quic_tx_pkts(struct list *pkts)
1653{
1654 struct quic_tx_packet *pkt, *tmp;
1655
1656 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001657 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001658 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001659 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001660 }
1661}
1662
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001663/* Remove already sent ranges of acknowledged packet numbers from
1664 * <pktns> packet number space tree below <largest_acked_pn> possibly
1665 * updating the range which contains <largest_acked_pn>.
1666 * Never fails.
1667 */
1668static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1669 int64_t largest_acked_pn)
1670{
1671 struct eb64_node *ar, *next_ar;
1672 struct quic_arngs *arngs = &pktns->rx.arngs;
1673
1674 ar = eb64_first(&arngs->root);
1675 while (ar) {
1676 struct quic_arng_node *ar_node;
1677
1678 next_ar = eb64_next(ar);
1679 ar_node = eb64_entry(&ar->node, struct quic_arng_node, first);
1680 if ((int64_t)ar_node->first.key > largest_acked_pn)
1681 break;
1682
1683 if (largest_acked_pn < ar_node->last) {
1684 eb64_delete(ar);
1685 ar_node->first.key = largest_acked_pn + 1;
1686 eb64_insert(&arngs->root, ar);
1687 break;
1688 }
1689
1690 eb64_delete(ar);
1691 pool_free(pool_head_quic_arng, ar_node);
1692 arngs->sz--;
1693 ar = next_ar;
1694 }
1695}
1696
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001697/* Send a packet ack event nofication for each newly acked packet of
1698 * <newly_acked_pkts> list and free them.
1699 * Always succeeds.
1700 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001701static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001702 struct list *newly_acked_pkts)
1703{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001704 struct quic_tx_packet *pkt, *tmp;
1705 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1706
1707 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1708 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001709 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001710 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001712 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001713 /* If this packet contained an ACK frame, proceed to the
1714 * acknowledging of range of acks from the largest acknowledged
1715 * packet number which was sent in an ACK frame by this packet.
1716 */
1717 if (pkt->largest_acked_pn != -1)
1718 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001719 ev.ack.acked = pkt->in_flight_len;
1720 ev.ack.time_sent = pkt->time_sent;
1721 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001722 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001723 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001724 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001725 }
1726
1727}
1728
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001729/* Release all the frames attached to <pktns> packet number space */
1730static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1731{
1732 struct quic_frame *frm, *frmbak;
1733
1734 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1735 LIST_DELETE(&frm->list);
1736 pool_free(pool_head_quic_frame, frm);
1737 }
1738}
1739
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001740/* Handle <pkts> list of lost packets detected at <now_us> handling
1741 * their TX frames.
1742 * Send a packet loss event to the congestion controller if
1743 * in flight packet have been lost.
1744 * Also frees the packet in <pkts> list.
1745 * Never fails.
1746 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001747static inline void qc_release_lost_pkts(struct quic_conn *qc,
1748 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001749 struct list *pkts,
1750 uint64_t now_us)
1751{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001752 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001753 uint64_t lost_bytes;
1754
1755 lost_bytes = 0;
1756 oldest_lost = newest_lost = NULL;
1757 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001758 struct list tmp = LIST_HEAD_INIT(tmp);
1759
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001760 lost_bytes += pkt->in_flight_len;
1761 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001762 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001763 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001764 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001765 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001767 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001768 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001769 if (!oldest_lost) {
1770 oldest_lost = newest_lost = pkt;
1771 }
1772 else {
1773 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001774 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775 newest_lost = pkt;
1776 }
1777 }
1778
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001779 if (newest_lost) {
1780 /* Sent a congestion event to the controller */
1781 struct quic_cc_event ev = {
1782 .type = QUIC_CC_EVT_LOSS,
1783 .loss.time_sent = newest_lost->time_sent,
1784 };
1785
1786 quic_cc_event(&qc->path->cc, &ev);
1787 }
1788
1789 /* If an RTT have been already sampled, <rtt_min> has been set.
1790 * We must check if we are experiencing a persistent congestion.
1791 * If this is the case, the congestion controller must re-enter
1792 * slow start state.
1793 */
1794 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1795 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1796
1797 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1798 now_ms, qc->max_ack_delay))
1799 qc->path->cc.algo->slow_start(&qc->path->cc);
1800 }
1801
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001802 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001803 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001804 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001805 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001806 }
1807}
1808
1809/* Look for packet loss from sent packets for <qel> encryption level of a
1810 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1811 * their tree if deemed as lost or set the <loss_time> value the packet number
1812 * space if any not deemed lost.
1813 * Should be called after having received an ACK frame with newly acknowledged
1814 * packets or when the the loss detection timer has expired.
1815 * Always succeeds.
1816 */
1817static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1818 struct quic_conn *qc,
1819 struct list *lost_pkts)
1820{
1821 struct eb_root *pkts;
1822 struct eb64_node *node;
1823 struct quic_loss *ql;
1824 unsigned int loss_delay;
1825
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001826 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001827 pkts = &pktns->tx.pkts;
1828 pktns->tx.loss_time = TICK_ETERNITY;
1829 if (eb_is_empty(pkts))
1830 goto out;
1831
1832 ql = &qc->path->loss;
1833 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001834 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1835
1836 node = eb64_first(pkts);
1837 while (node) {
1838 struct quic_tx_packet *pkt;
1839 int64_t largest_acked_pn;
1840 unsigned int loss_time_limit, time_sent;
1841
1842 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001843 largest_acked_pn = pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001844 node = eb64_next(node);
1845 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1846 break;
1847
1848 time_sent = pkt->time_sent;
1849 loss_time_limit = tick_add(time_sent, loss_delay);
1850 if (tick_is_le(time_sent, now_ms) ||
1851 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1852 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001853 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001854 }
1855 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001856 if (tick_isset(pktns->tx.loss_time))
1857 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1858 else
1859 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001860 }
1861 }
1862
1863 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001864 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001865}
1866
1867/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1868 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001869 * 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 +01001870 * acked ack-eliciting packet.
1871 * Return 1, if succeeded, 0 if not.
1872 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001873static inline int qc_parse_ack_frm(struct quic_conn *qc,
1874 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001875 struct quic_enc_level *qel,
1876 unsigned int *rtt_sample,
1877 const unsigned char **pos, const unsigned char *end)
1878{
1879 struct quic_ack *ack = &frm->ack;
1880 uint64_t smallest, largest;
1881 struct eb_root *pkts;
1882 struct eb64_node *largest_node;
1883 unsigned int time_sent, pkt_flags;
1884 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1885 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1886
1887 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1888 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001889 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001890 goto err;
1891 }
1892
1893 if (ack->first_ack_range > ack->largest_ack) {
1894 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001895 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001896 goto err;
1897 }
1898
1899 largest = ack->largest_ack;
1900 smallest = largest - ack->first_ack_range;
1901 pkts = &qel->pktns->tx.pkts;
1902 pkt_flags = 0;
1903 largest_node = NULL;
1904 time_sent = 0;
1905
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001906 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001907 largest_node = eb64_lookup(pkts, largest);
1908 if (!largest_node) {
1909 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001910 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001911 }
1912 else {
1913 time_sent = eb64_entry(&largest_node->node,
1914 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001915 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001916 }
1917
1918 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001919 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001920 do {
1921 uint64_t gap, ack_range;
1922
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001923 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1924 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 if (!ack->ack_range_num--)
1926 break;
1927
1928 if (!quic_dec_int(&gap, pos, end))
1929 goto err;
1930
1931 if (smallest < gap + 2) {
1932 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001933 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001934 goto err;
1935 }
1936
1937 largest = smallest - gap - 2;
1938 if (!quic_dec_int(&ack_range, pos, end))
1939 goto err;
1940
1941 if (largest < ack_range) {
1942 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001943 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 goto err;
1945 }
1946
1947 /* Do not use this node anymore. */
1948 largest_node = NULL;
1949 /* Next range */
1950 smallest = largest - ack_range;
1951
1952 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001953 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001954 } while (1);
1955
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1957 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001958 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001959 }
1960
1961 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1962 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001963 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001964 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001965 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001966 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001967 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1968 if (quic_peer_validated_addr(qc))
1969 qc->path->loss.pto_count = 0;
1970 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001971 }
1972
1973
1974 return 1;
1975
1976 err:
1977 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001978 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001979 return 0;
1980}
1981
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001982/* This function gives the detail of the SSL error. It is used only
1983 * if the debug mode and the verbose mode are activated. It dump all
1984 * the SSL error until the stack was empty.
1985 */
1986static forceinline void qc_ssl_dump_errors(struct connection *conn)
1987{
1988 if (unlikely(global.mode & MODE_DEBUG)) {
1989 while (1) {
1990 unsigned long ret;
1991
1992 ret = ERR_get_error();
1993 if (!ret)
1994 return;
1995
1996 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1997 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1998 }
1999 }
2000}
2001
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002002int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2003 const char **str, int *len);
2004
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002005/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2006 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002007 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002008 * Return 1 if succeeded, 0 if not.
2009 */
2010static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002011 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002012 const unsigned char *data, size_t len,
2013 struct quic_rx_packet *pkt,
2014 struct quic_rx_crypto_frm *cf)
2015{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002016 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002017 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002019 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002020 qc = ctx->qc;
2021
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002022 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2023
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2025 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002026 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002027 goto err;
2028 }
2029
2030 el->rx.crypto.offset += len;
2031 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002032 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002033
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002034 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002035 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002036 ssl_err = SSL_do_handshake(ctx->ssl);
2037 if (ssl_err != 1) {
2038 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2039 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2040 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002041 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002042 goto out;
2043 }
2044
2045 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002046 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002047 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002048 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002049 goto err;
2050 }
2051
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002052 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
2053 /* I/O callback switch */
2054 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002055 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002056 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002057 /* The connection is ready to be accepted. */
2058 quic_accept_push_qc(qc);
2059 }
2060 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002061 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002062 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002063 } else {
2064 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2065 if (ssl_err != 1) {
2066 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2067 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2068 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002069 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002070 goto out;
2071 }
2072
2073 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002074 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002075 goto err;
2076 }
2077
2078 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002079 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002080 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002081
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002082 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002083 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002084 return 1;
2085
2086 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002087 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002088 return 0;
2089}
2090
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002091/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
2092 * <pkt> RX packet.
2093 * Return it if succeeded, NULL if not.
2094 */
2095static inline
2096struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
2097 struct quic_rx_packet *pkt)
2098{
2099 struct quic_rx_strm_frm *frm;
2100
2101 frm = pool_alloc(pool_head_quic_rx_strm_frm);
2102 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002103 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002104 frm->len = stream_frm->len;
2105 frm->data = stream_frm->data;
2106 frm->pkt = pkt;
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002107 frm->fin = stream_frm->fin;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002108 }
2109
2110 return frm;
2111}
2112
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002113/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01002114 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002115 */
2116static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
2117{
2118 size_t ret;
2119
Amaury Denoyelle2d2d0302022-02-28 10:00:54 +01002120 ret = b_putblk(buf, (char *)strm_frm->data, strm_frm->len);
2121 strm_frm->len -= ret;
2122 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002123
2124 return ret;
2125}
2126
2127/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2128 * streams may be open. The data are copied to the stream RX buffer if possible.
2129 * If not, the STREAM frame is stored to be treated again later.
2130 * We rely on the flow control so that not to store too much STREAM frames.
2131 * Return 1 if succeeded, 0 if not.
2132 */
2133static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2134 struct quic_stream *strm_frm,
2135 struct quic_conn *qc)
2136{
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002137 struct quic_rx_strm_frm *frm;
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002138 struct eb64_node *frm_node;
2139 struct qcs *qcs = NULL;
2140 int ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002141
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002142 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2143 strm_frm->offset.key, strm_frm->fin,
2144 (char *)strm_frm->data, &qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002145
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002146 /* invalid or already received frame */
2147 if (ret == 1)
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002148 return 1;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002149
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002150 if (ret == 2) {
2151 /* frame cannot be parsed at the moment and should be
2152 * buffered.
2153 */
2154 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2155 if (!frm) {
2156 TRACE_PROTO("Could not alloc RX STREAM frame",
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002157 QUIC_EV_CONN_PSTRM, qc);
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002158 return 0;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002159 }
2160
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002161 eb64_insert(&qcs->rx.frms, &frm->offset_node);
2162 quic_rx_packet_refinc(pkt);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002163
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002164 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002165 }
2166
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002167 /* Frame correctly received by the mux.
2168 * If there is buffered frame for next offset, it may be possible to
2169 * receive them now.
2170 */
2171 frm_node = eb64_first(&qcs->rx.frms);
2172 while (frm_node) {
2173 frm = eb64_entry(&frm_node->node,
2174 struct quic_rx_strm_frm, offset_node);
Amaury Denoyelle3c430392022-02-28 11:38:36 +01002175
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02002176 ret = qcc_recv(qc->qcc, qcs->id, frm->len,
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002177 frm->offset_node.key, frm->fin,
2178 (char *)frm->data, &qcs);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002179
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002180 /* interrupt the parsing if the frame cannot be handled for the
2181 * moment only by the MUX.
2182 */
2183 if (ret == 2)
2184 break;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002185
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002186 /* Remove a newly received frame or an invalid one. */
2187 frm_node = eb64_next(frm_node);
2188 eb64_delete(&frm->offset_node);
2189 quic_rx_packet_refdec(frm->pkt);
2190 pool_free(pool_head_quic_rx_strm_frm, frm);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002191 }
2192
Amaury Denoyelle0e3010b2022-02-28 11:37:48 +01002193 /* Decode the received data. */
Amaury Denoyelle20f89ca2022-03-08 10:48:35 +01002194 qcc_decode_qcs(qc->qcc, qcs);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002195
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002196 return 1;
2197}
2198
2199/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2200 * streams may be open. The data are copied to the stream RX buffer if possible.
2201 * If not, the STREAM frame is stored to be treated again later.
2202 * We rely on the flow control so that not to store too much STREAM frames.
2203 * Return 1 if succeeded, 0 if not.
2204 */
2205static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2206 struct quic_stream *strm_frm,
2207 struct quic_conn *qc)
2208{
2209 struct qcs *strm;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002210 struct quic_rx_strm_frm *frm;
2211 size_t strm_frm_len;
2212
Amaury Denoyelle50742292022-03-29 14:57:19 +02002213 strm = qcc_get_qcs(qc->qcc, strm_frm->id);
2214 if (!strm) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002215 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002216 return 0;
2217 }
2218
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002219 if (strm_frm->offset.key < strm->rx.offset) {
2220 size_t diff;
2221
2222 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2223 TRACE_PROTO("Already received STREAM data",
2224 QUIC_EV_CONN_PSTRM, qc);
2225 goto out;
2226 }
2227
2228 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2229 diff = strm->rx.offset - strm_frm->offset.key;
2230 strm_frm->offset.key = strm->rx.offset;
2231 strm_frm->len -= diff;
2232 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002233 }
2234
2235 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002236 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002237 int ret;
2238
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002239 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002240 goto store_frm;
2241
2242 /* qc_strm_cpy() will modify the offset, depending on the number
2243 * of bytes copied.
2244 */
2245 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2246 /* Inform the application of the arrival of this new stream */
2247 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002248 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002249 return 0;
2250 }
2251
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002252 if (ret)
2253 qcs_notify_recv(strm);
2254
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002255 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002256 }
2257 /* Take this frame into an account for the stream flow control */
2258 strm->rx.offset += strm_frm_len;
2259 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002260 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002261 */
2262 if (!strm_frm->len)
2263 goto out;
2264
2265 store_frm:
2266 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2267 if (!frm) {
2268 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002269 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002270 return 0;
2271 }
2272
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002273 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002274 quic_rx_packet_refinc(pkt);
2275
2276 out:
2277 return 1;
2278}
2279
2280static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2281 struct quic_stream *strm_frm,
2282 struct quic_conn *qc)
2283{
2284 if (strm_frm->id & QCS_ID_DIR_BIT)
2285 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2286 else
2287 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2288}
2289
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002290/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002291static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2292 struct quic_conn *qc)
2293{
2294 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002295 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002296 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002297
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002298 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002299 pkts = &qel->pktns->tx.pkts;
2300 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002301 /* Skip the empty packet (they have already been retransmitted) */
2302 while (node) {
2303 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2304 if (!LIST_ISEMPTY(&pkt->frms))
2305 break;
2306 node = eb64_next(node);
2307 }
2308
2309 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002310 return;
2311
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002312 /* When building a packet from another one, the field which may increase the
2313 * packet size is the packet number. And the maximum increase is 4 bytes.
2314 */
2315 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2316 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2317 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2318 return;
2319 }
2320
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002321 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002322}
2323
2324/* Prepare a fast retransmission during handshake after a client
2325 * has resent Initial packets. According to the RFC a server may retransmit
2326 * up to two datagrams of Initial packets if did not receive all Initial packets
2327 * and resend them coalescing with others (Handshake here).
2328 * (Listener only).
2329 */
2330static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2331{
2332 struct list itmp = LIST_HEAD_INIT(itmp);
2333 struct list htmp = LIST_HEAD_INIT(htmp);
2334 struct quic_frame *frm, *frmbak;
2335
2336 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2337 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2338 struct quic_enc_level *qel = iqel;
2339 struct eb_root *pkts;
2340 struct eb64_node *node;
2341 struct quic_tx_packet *pkt;
2342 struct list *tmp = &itmp;
2343
Frédéric Lécaille5cfb4ed2022-03-30 14:44:49 +02002344 /* Do not probe from a packet number space if some probing
2345 * was already asked.
2346 */
2347 if (qel->pktns->tx.pto_probe) {
2348 qel = hqel;
2349 if (qel->pktns->tx.pto_probe)
2350 return;
2351 }
2352
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002353 start:
2354 pkt = NULL;
2355 pkts = &qel->pktns->tx.pkts;
2356 node = eb64_first(pkts);
2357 /* Skip the empty packet (they have already been retransmitted) */
2358 while (node) {
2359 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2360 if (!LIST_ISEMPTY(&pkt->frms))
2361 break;
2362 node = eb64_next(node);
2363 }
2364
2365 if (!pkt)
2366 goto end;
2367
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002368 /* When building a packet from another one, the field which may increase the
2369 * packet size is the packet number. And the maximum increase is 4 bytes.
2370 */
2371 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2372 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2373 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2374 goto end;
2375 }
2376
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002377 qel->pktns->tx.pto_probe += 1;
2378 requeue:
2379 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Frédéric Lécaille009016c2022-03-30 14:58:55 +02002380 struct quic_frame *dup_frm;
2381
2382
2383 dup_frm = pool_alloc(pool_head_quic_frame);
2384 if (!dup_frm) {
2385 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2386 break;
2387 }
2388
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002389 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille009016c2022-03-30 14:58:55 +02002390 *dup_frm = *frm;
2391 LIST_APPEND(tmp, &dup_frm->list);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002392 }
2393
2394 if (qel == iqel) {
2395 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2396 pkt = pkt->next;
2397 tmp = &htmp;
2398 hqel->pktns->tx.pto_probe += 1;
2399 goto requeue;
2400 }
2401
2402 qel = hqel;
2403 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002404 goto start;
2405 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002406
2407 end:
2408 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2409 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002410}
2411
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2413 * as I/O handler context and <qel> as encryption level.
2414 * Returns 1 if succeeded, 0 if failed.
2415 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002416static 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 +01002417 struct quic_enc_level *qel)
2418{
2419 struct quic_frame frm;
2420 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002421 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002422 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002423
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002424 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002425 /* Skip the AAD */
2426 pos = pkt->data + pkt->aad_len;
2427 end = pkt->data + pkt->len;
2428
2429 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002430 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002431 goto err;
2432
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002433 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002434 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002435 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002436 break;
2437 case QUIC_FT_PING:
2438 break;
2439 case QUIC_FT_ACK:
2440 {
2441 unsigned int rtt_sample;
2442
2443 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002444 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002445 goto err;
2446
2447 if (rtt_sample) {
2448 unsigned int ack_delay;
2449
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002450 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002451 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002452 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2453 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002454 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002455 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002456 break;
2457 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002458 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002459 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002460 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002461 {
2462 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002464 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002465 /* XXX TO DO: <cfdebug> is used only for the traces. */
2466 struct quic_rx_crypto_frm cfdebug = { };
2467
2468 cfdebug.offset_node.key = frm.crypto.offset;
2469 cfdebug.len = frm.crypto.len;
2470 TRACE_PROTO("CRYPTO data discarded",
2471 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2472 break;
2473 }
2474
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002475 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2476 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2477 /* XXX TO DO: <cfdebug> is used only for the traces. */
2478 struct quic_rx_crypto_frm cfdebug = { };
2479
2480 cfdebug.offset_node.key = frm.crypto.offset;
2481 cfdebug.len = frm.crypto.len;
2482 /* Nothing to do */
2483 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002484 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002485 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002486 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2487 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002488 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002489 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002490 else {
2491 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2492 /* XXX TO DO: <cfdebug> is used only for the traces. */
2493 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002495 cfdebug.offset_node.key = frm.crypto.offset;
2496 cfdebug.len = frm.crypto.len;
2497 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002498 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002499 frm.crypto.len -= diff;
2500 frm.crypto.data += diff;
2501 frm.crypto.offset = qel->rx.crypto.offset;
2502 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002503 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002504
2505 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002506 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002507 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002508
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002509 cfdebug.offset_node.key = frm.crypto.offset;
2510 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002511 if (!qc_provide_cdata(qel, ctx,
2512 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002513 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002515
2516 break;
2517 }
2518
2519 /* frm.crypto.offset > qel->rx.crypto.offset */
2520 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2521 if (!cf) {
2522 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002523 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002524 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002526
2527 cf->offset_node.key = frm.crypto.offset;
2528 cf->len = frm.crypto.len;
2529 cf->data = frm.crypto.data;
2530 cf->pkt = pkt;
2531 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2532 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002534 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002535 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002536 {
2537 struct quic_stream *stream = &frm.stream;
2538
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002539 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002540 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2541 goto err;
2542 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2543 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002544
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002545 /* At the application layer the connection may have already been closed. */
2546 if (qc->mux_state != QC_MUX_READY)
2547 break;
2548
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002549 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002550 goto err;
2551
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002552 break;
2553 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002554 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002555 if (qc->mux_state == QC_MUX_READY) {
2556 struct quic_max_data *data = &frm.max_data;
2557 qcc_recv_max_data(qc->qcc, data->max_data);
2558 }
2559 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002560 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002561 if (qc->mux_state == QC_MUX_READY) {
2562 struct quic_max_stream_data *data = &frm.max_stream_data;
2563 qcc_recv_max_stream_data(qc->qcc, data->id,
2564 data->max_stream_data);
2565 }
2566 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002567 case QUIC_FT_MAX_STREAMS_BIDI:
2568 case QUIC_FT_MAX_STREAMS_UNI:
2569 case QUIC_FT_DATA_BLOCKED:
2570 case QUIC_FT_STREAM_DATA_BLOCKED:
2571 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2572 case QUIC_FT_STREAMS_BLOCKED_UNI:
2573 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002574 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002575 case QUIC_FT_RETIRE_CONNECTION_ID:
2576 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002577 break;
2578 case QUIC_FT_CONNECTION_CLOSE:
2579 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille47756802022-03-25 09:12:16 +01002580 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
2581 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2582 /* RFC 9000 10.2. Immediate Close:
2583 * The closing and draining connection states exist to ensure
2584 * that connections close cleanly and that delayed or reordered
2585 * packets are properly discarded. These states SHOULD persist
2586 * for at least three times the current PTO interval...
2587 *
2588 * Rearm the idle timeout only one time when entering draining
2589 * state.
2590 */
2591 qc_idle_timer_do_rearm(qc);
2592 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002593 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002594 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002596 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002597 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002598 goto err;
2599
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002600 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002601 break;
2602 default:
2603 goto err;
2604 }
2605 }
2606
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002607 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002608 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002609
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002610 if (fast_retrans)
2611 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002612
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002613 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2614 * has successfully parse a Handshake packet. The Initial encryption must also
2615 * be discarded.
2616 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002617 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002618 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002619 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2620 QUIC_FL_TLS_SECRETS_DCD)) {
2621 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2622 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2623 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2624 qc_set_timer(ctx->qc);
2625 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2626 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2627 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002628 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2629 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002630 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 }
2632
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002633 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002634 return 1;
2635
2636 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002637 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002638 return 0;
2639}
2640
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002641/* Must be called only by a <cbuf> writer (packet builder).
2642 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2643 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2644 * index after having marked the end of written data. This the responsability
2645 * of the caller to ensure there is enough room in <cbuf> to write the end of
2646 * data made of a uint16_t null field.
2647 *
2648 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2649 * ^ ^
2650 * r w
2651 *
2652 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2653 * ^ ^
2654 * r w
2655
2656 * +--------------------------------------+ (empty buffer, can be reused)
2657 * ^
2658 * (r = w)
2659 *
2660 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2661 * ^ ^
2662 * w r
2663 */
2664static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2665{
2666 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2667
2668 /* We can reset the writer index only if in front of the reader index and
2669 * if the reader index is not null. Resetting the writer when the reader
2670 * index is null would empty the buffer.
2671 * XXX Note than the writer index cannot reach the reader index.
2672 * Only the reader index can reach the writer index.
2673 */
2674 if (rd && rd <= cbuf->wr) {
2675 /* Mark the end of contiguous data for the reader */
2676 write_u16(cb_wr(cbuf), 0);
2677 cb_add(cbuf, sizeof(uint16_t));
2678 cb_wr_reset(cbuf);
2679 return 1;
2680 }
2681
2682 return 0;
2683}
2684
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002685/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002686 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002687 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2688 * This function must be called only after having built a correct datagram.
2689 * Always succeeds.
2690 */
2691static inline void qc_set_dg(struct cbuf *cbuf,
2692 uint16_t dglen, struct quic_tx_packet *pkt)
2693{
2694 write_u16(cb_wr(cbuf), dglen);
2695 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2696 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2697}
2698
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002699/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2700 * with <frms> as ack-eliciting frame list to send, 0 if not.
2701 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2702 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2703 */
2704static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2705 struct quic_enc_level *qel, int cc, int probe)
2706{
2707 unsigned int must_ack =
2708 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2709
2710 /* Do not build any more packet if the TX secrets are not available or
2711 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2712 * and if there is no more packets to send upon PTO expiration
2713 * and if there is no more ack-eliciting frames to send or in flight
2714 * congestion control limit is reached for prepared data
2715 */
2716 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2717 (!cc && !probe && !must_ack &&
2718 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2719 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2720 return 0;
2721 }
2722
2723 return 1;
2724}
2725
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002726/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002727 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2728 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002729 * A header made of two fields is added to each datagram: the datagram length followed
2730 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002731 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2732 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002733 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002734static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2735 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002736{
2737 struct quic_enc_level *qel;
2738 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002739 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002740 struct quic_tx_packet *pkt;
2741 size_t total;
2742 size_t dg_headlen;
2743
2744 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2745 /* Each datagram is prepended with its length followed by the
2746 * address of the first packet in the datagram.
2747 */
2748 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2749 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2750 total = 0;
2751 start:
2752 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002753 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002754 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2755 * to ensure there is enough room to mark the end of prepared
2756 * contiguous data with a zero length.
2757 */
2758 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2759 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002760 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002761
2762 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002763 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002764 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002765 /* We do not probe if an immediate close was asked */
2766 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002767 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002768
2769 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002770 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002771
2772 /* Leave room for the datagram header */
2773 pos += dg_headlen;
2774 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2775 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2776 }
2777 else {
2778 end = pos + qc->path->mtu;
2779 }
2780
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002781 pkt = qc_build_pkt(&pos, end, qel, frms, qc, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002782 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002783 switch (err) {
2784 case -2:
2785 goto err;
2786 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002787 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2788 * MTU, we are here because of the congestion control window. There is
2789 * no need to try to reuse this buffer.
2790 */
2791 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002792 default:
2793 break;
2794 }
2795
2796 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2797 if (!pkt)
2798 goto err;
2799
2800 total += pkt->len;
2801 /* Set the current datagram as prepared into <cbuf>. */
2802 qc_set_dg(cbuf, pkt->len, pkt);
2803 }
2804
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002805 /* Reset <wr> writer index if in front of <rd> index */
2806 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002807 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002808 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002809 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002810 }
2811
2812 out:
2813 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2814 return total;
2815
2816 err:
2817 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2818 return -1;
2819}
2820
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002821/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002822 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2823 * several packets in the same datagram. A header made of two fields is added
2824 * to each datagram: the datagram length followed by the address of the first
2825 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002826 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2827 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002828 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002829static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2830 enum quic_tls_enc_level tel,
2831 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002832{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002834 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002835 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002836 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2837 /* length of datagrams */
2838 uint16_t dglen;
2839 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002840 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002841 /* Each datagram is prepended with its length followed by the
2842 * address of the first packet in the datagram.
2843 */
2844 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002845
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002846 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2847
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002848 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002849 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002850 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002851 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002853 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002854 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002855 /* Leave at least <dglen> bytes at the end of this buffer
2856 * to ensure there is enough room to mark the end of prepared
2857 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002859 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2860 first_pkt = prv_pkt = NULL;
2861 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002862 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002863 enum quic_pkt_type pkt_type;
2864
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002865 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002866 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002867 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002868 /* We do not probe if an immediate close was asked */
2869 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002870 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002871
2872 if (!qc_may_build_pkt(qc, &qel->pktns->tx.frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002873 if (prv_pkt)
2874 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002875 /* Let's select the next encryption level */
2876 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2877 tel = next_tel;
2878 qel = &qc->els[tel];
2879 /* Build a new datagram */
2880 prv_pkt = NULL;
2881 continue;
2882 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883 break;
2884 }
2885
2886 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002887 if (!prv_pkt) {
2888 /* Leave room for the datagram header */
2889 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002890 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002891 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2892 }
2893 else {
2894 end = pos + qc->path->mtu;
2895 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002896 }
2897
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002898 cur_pkt = qc_build_pkt(&pos, end, qel, &qel->pktns->tx.frms,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002899 qc, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002900 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002901 case -2:
2902 goto err;
2903 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002904 /* If there was already a correct packet present, set the
2905 * current datagram as prepared into <cbuf>.
2906 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002907 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002908 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002909 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002910 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002911 break;
2912 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002913
Frédéric Lécaille63556772021-12-29 17:18:21 +01002914 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2915 if (!cur_pkt)
2916 goto err;
2917
2918 total += cur_pkt->len;
2919 /* keep trace of the first packet in the datagram */
2920 if (!first_pkt)
2921 first_pkt = cur_pkt;
2922 /* Attach the current one to the previous one */
2923 if (prv_pkt)
2924 prv_pkt->next = cur_pkt;
2925 /* Let's say we have to build a new dgram */
2926 prv_pkt = NULL;
2927 dglen += cur_pkt->len;
2928 /* Client: discard the Initial encryption keys as soon as
2929 * a handshake packet could be built.
2930 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002931 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01002932 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2933 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2934 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2935 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2936 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002937 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002938 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002939 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002940 }
2941 /* If the data for the current encryption level have all been sent,
2942 * select the next level.
2943 */
2944 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécailled6570e12022-03-29 17:41:57 +02002945 (LIST_ISEMPTY(&qel->pktns->tx.frms) && !qel->pktns->tx.pto_probe)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002946 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2947 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2948 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2949 tel = next_tel;
2950 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002951 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002952 /* If there is data for the next level, do not
2953 * consume a datagram.
2954 */
2955 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002956 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002957 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002958 /* If we have to build a new datagram, set the current datagram as
2959 * prepared into <cbuf>.
2960 */
2961 if (!prv_pkt) {
2962 qc_set_dg(cbuf, dglen, first_pkt);
2963 first_pkt = NULL;
2964 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002965 padding = 0;
2966 }
2967 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002968 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002969 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2970 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002971 }
2972 }
2973
2974 stop_build:
2975 /* Reset <wr> writer index if in front of <rd> index */
2976 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002977 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002978 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002979 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 }
2981
2982 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002983 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002984 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002985
2986 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002987 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002988 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989}
2990
2991/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002992 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002994int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995{
2996 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002997 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002999 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003000 cbuf = qr->cbuf;
3001 while (cb_contig_data(cbuf)) {
3002 unsigned char *pos;
3003 struct buffer tmpbuf = { };
3004 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3005 uint16_t dglen;
3006 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 unsigned int time_sent;
3008
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003009 pos = cb_rd(cbuf);
3010 dglen = read_u16(pos);
3011 /* End of prepared datagrams.
3012 * Reset the reader index only if in front of the writer index.
3013 */
3014 if (!dglen) {
3015 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3016
3017 if (wr && wr < cbuf->rd) {
3018 cb_rd_reset(cbuf);
3019 continue;
3020 }
3021 break;
3022 }
3023
3024 pos += sizeof dglen;
3025 first_pkt = read_ptr(pos);
3026 pos += sizeof first_pkt;
3027 tmpbuf.area = (char *)pos;
3028 tmpbuf.size = tmpbuf.data = dglen;
3029
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003030 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003031 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003032 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003033
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003034 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003035 qc->tx.bytes += tmpbuf.data;
3036 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003037
3038 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3039 pkt->time_sent = time_sent;
3040 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3041 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003042 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003043 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3044 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003045 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003046 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3047 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3048 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003049 qc_notify_close(qc);
3050
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003051 /* RFC 9000 10.2. Immediate Close:
3052 * The closing and draining connection states exist to ensure
3053 * that connections close cleanly and that delayed or reordered
3054 * packets are properly discarded. These states SHOULD persist
3055 * for at least three times the current PTO interval...
3056 *
3057 * Rearm the idle timeout only one time when entering closing
3058 * state.
3059 */
3060 qc_idle_timer_do_rearm(qc);
3061 if (qc->timer_task) {
3062 task_destroy(qc->timer_task);
3063 qc->timer_task = NULL;
3064 }
3065 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003066 qc->path->in_flight += pkt->in_flight_len;
3067 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3068 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003069 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003070 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003071 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003072 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003073 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074 }
3075 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003076
3077 return 1;
3078}
3079
3080/* Build all the frames which must be sent just after the handshake have succeeded.
3081 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3082 * a HANDSHAKE_DONE frame.
3083 * Return 1 if succeeded, 0 if not.
3084 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003085static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003087 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003088 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003089 struct quic_frame *frm, *frmbak;
3090 struct list frm_list = LIST_HEAD_INIT(frm_list);
3091 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003093 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003094 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003095 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003096 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003097 if (!frm)
3098 return 0;
3099
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003100 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003101 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003102 }
3103
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003104 first = 1;
3105 max = qc->tx.params.active_connection_id_limit;
3106 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003107 struct quic_connection_id *cid;
3108
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003109 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003110 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003111 goto err;
3112
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003113 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003114 if (!cid)
3115 goto err;
3116
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003117 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003118 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003119
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003120 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003121 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003122 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003123
3124 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003125 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003126
3127 return 1;
3128
3129 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003130 /* free the frames */
3131 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3132 pool_free(pool_head_quic_frame, frm);
3133
3134 node = eb64_first(&qc->cids);
3135 while (node) {
3136 struct quic_connection_id *cid;
3137
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003138
3139 cid = eb64_entry(&node->node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003140 if (cid->seq_num.key >= max)
3141 break;
3142
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003143 if (cid->seq_num.key < first)
3144 continue;
3145
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003146 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003147 ebmb_delete(&cid->node);
3148 eb64_delete(&cid->seq_num);
3149 pool_free(pool_head_quic_connection_id, cid);
3150 }
3151
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003152 return 0;
3153}
3154
3155/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003156void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003157{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003158 struct eb64_node *n;
3159 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003160
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003161 n = eb64_first(&arngs->root);
3162 while (n) {
3163 struct eb64_node *next;
3164
3165 ar = eb64_entry(&n->node, struct quic_arng_node, first);
3166 next = eb64_next(n);
3167 eb64_delete(n);
3168 free(ar);
3169 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003170 }
3171}
3172
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003173/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3174 * descending order.
3175 */
3176static inline size_t sack_gap(struct quic_arng_node *p,
3177 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003178{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003179 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003180}
3181
3182
3183/* Remove the last elements of <ack_ranges> list of ack range updating its
3184 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003185 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003186 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003187static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003188{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003189 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003190
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003191 last = eb64_last(&arngs->root);
3192 while (last && arngs->enc_sz > limit) {
3193 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003194
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003195 prev = eb64_prev(last);
3196 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003197 return 0;
3198
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003199 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
3200 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
3201 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3202 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3203 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3204 --arngs->sz;
3205 eb64_delete(last);
3206 pool_free(pool_head_quic_arng, last);
3207 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003208 }
3209
3210 return 1;
3211}
3212
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003213/* Set the encoded size of <arngs> QUIC ack ranges. */
3214static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3215{
3216 struct eb64_node *node, *next;
3217 struct quic_arng_node *ar, *ar_next;
3218
3219 node = eb64_last(&arngs->root);
3220 if (!node)
3221 return;
3222
3223 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3224 arngs->enc_sz = quic_int_getsize(ar->last) +
3225 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3226
3227 while ((next = eb64_prev(node))) {
3228 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
3229 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3230 quic_int_getsize(ar_next->last - ar_next->first.key);
3231 node = next;
3232 ar = eb64_entry(&node->node, struct quic_arng_node, first);
3233 }
3234}
3235
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003236/* Insert <ar> ack range into <argns> tree of ack ranges.
3237 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003238 */
3239static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003240struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3241 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003242{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003243 struct quic_arng_node *new_ar;
3244
3245 new_ar = pool_alloc(pool_head_quic_arng);
3246 if (new_ar) {
3247 new_ar->first.key = ar->first;
3248 new_ar->last = ar->last;
3249 eb64_insert(&arngs->root, &new_ar->first);
3250 arngs->sz++;
3251 }
3252
3253 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003254}
3255
3256/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003257 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003258 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003259 *
3260 * Descending order
3261 * ------------->
3262 * range1 range2
3263 * ..........|--------|..............|--------|
3264 * ^ ^ ^ ^
3265 * | | | |
3266 * last1 first1 last2 first2
3267 * ..........+--------+--------------+--------+......
3268 * diff1 gap12 diff2
3269 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003270 * To encode the previous list of ranges we must encode integers as follows in
3271 * descending order:
3272 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003273 * with diff1 = last1 - first1
3274 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003275 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003277 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003278int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3279 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003281 struct eb64_node *le;
3282 struct quic_arng_node *new_node;
3283 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003284
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003285 new = NULL;
3286 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003287 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003288 if (!new_node)
3289 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003291 goto out;
3292 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003293
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003294 le = eb64_lookup_le(&arngs->root, ar->first);
3295 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003296 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003297 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003298 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003299
3300 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003301 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003302 else {
3303 struct quic_arng_node *le_ar =
3304 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003305
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003306 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003307 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003308 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003309
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003310 if (le_ar->last + 1 >= ar->first) {
3311 le_ar->last = ar->last;
3312 new = le;
3313 new_node = le_ar;
3314 }
3315 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003316 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003317 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003318 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003319
3320 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003322 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003323
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003324 /* Verify that the new inserted node does not overlap the nodes
3325 * which follow it.
3326 */
3327 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003328 struct eb64_node *next;
3329 struct quic_arng_node *next_node;
3330
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003331 while ((next = eb64_next(new))) {
3332 next_node =
3333 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003334 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003335 break;
3336
3337 if (next_node->last > new_node->last)
3338 new_node->last = next_node->last;
3339 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003340 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003341 /* Decrement the size of these ranges. */
3342 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003344 }
3345
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003346 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003347 quic_arngs_set_enc_sz(arngs);
3348
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003349 return 1;
3350}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003351/* Remove the header protection of packets at <el> encryption level.
3352 * Always succeeds.
3353 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003354static 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 +01003355{
3356 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003357 struct quic_rx_packet *pqpkt;
3358 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359 struct quic_enc_level *app_qel;
3360
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003361 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3362 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003363 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003364 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003366 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003367 goto out;
3368 }
3369 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003370 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003371 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003372 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003373 pqpkt->data, pqpkt->data + pqpkt->len)) {
3374 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375 /* XXX TO DO XXX */
3376 }
3377 else {
3378 /* The AAD includes the packet number field */
3379 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3380 /* Store the packet into the tree of packets to decrypt. */
3381 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003382 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003383 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3384 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003385 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003386 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003387 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003388 MT_LIST_DELETE_SAFE(pkttmp1);
3389 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003390 }
3391
3392 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003393 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003394}
3395
3396/* Process all the CRYPTO frame at <el> encryption level.
3397 * Return 1 if succeeded, 0 if not.
3398 */
3399static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003400 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003401{
3402 struct eb64_node *node;
3403
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003404 node = eb64_first(&el->rx.crypto.frms);
3405 while (node) {
3406 struct quic_rx_crypto_frm *cf;
3407
3408 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3409 if (cf->offset_node.key != el->rx.crypto.offset)
3410 break;
3411
3412 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3413 goto err;
3414
3415 node = eb64_next(node);
3416 quic_rx_packet_refdec(cf->pkt);
3417 eb64_delete(&cf->offset_node);
3418 pool_free(pool_head_quic_rx_crypto_frm, cf);
3419 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420 return 1;
3421
3422 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003423 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003424 return 0;
3425}
3426
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003427/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003428 * 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 +02003429 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003430 * Return 1 if succeeded, 0 if not.
3431 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003432int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3433 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003434{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003435 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003436 int64_t largest_pn = -1;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003437 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003438 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003439
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003440 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003441 qel = cur_el;
3442 next_tel:
3443 if (!qel)
3444 goto out;
3445
3446 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3447 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003448 while (node) {
3449 struct quic_rx_packet *pkt;
3450
3451 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003452 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003453 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003454 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003455 /* Drop the packet */
3456 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003457 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003458 }
3459 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003460 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461 /* Drop the packet */
3462 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003463 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003464 }
3465 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003466 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3467
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003468 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3469 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3470 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003471 qc_idle_timer_rearm(qc, 1);
3472 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003473 if (pkt->pn > largest_pn)
3474 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003475 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003476 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003477 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003478 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003479 }
3480 }
3481 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003482 eb64_delete(&pkt->pn_node);
3483 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003484 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003485 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003487 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003488 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn)
3489 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003490 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003491 goto err;
3492
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003493 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003494 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003495 qel = next_el;
3496 goto next_tel;
3497 }
3498
3499 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003500 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003501 return 1;
3502
3503 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003504 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003505 return 0;
3506}
3507
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003508/* Check if it's possible to remove header protection for packets related to
3509 * encryption level <qel>. If <qel> is NULL, assume it's false.
3510 *
3511 * Return true if the operation is possible else false.
3512 */
3513static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3514{
3515 enum quic_tls_enc_level tel;
3516
3517 if (!qel)
3518 return 0;
3519
3520 tel = ssl_to_quic_enc_level(qel->level);
3521
3522 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003523 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003524 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003525 return 0;
3526 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003527
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003528 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003529 return 0;
3530
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003531 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003532 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003533 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003534 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003535
3536 return 1;
3537}
3538
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003539/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaillec2f561c2022-02-25 17:46:07 +01003540int qc_send_app_pkts(struct quic_conn *qc, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003541{
3542 int ret;
3543 struct qring *qr;
3544
3545 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3546 if (!qr)
3547 /* Never happens */
3548 return 1;
3549
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003550 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003551 if (ret == -1)
3552 goto err;
3553 else if (ret == 0)
3554 goto out;
3555
3556 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3557 goto err;
3558
3559 out:
3560 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3561 return 1;
3562
3563 err:
3564 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3565 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3566 return 0;
3567}
3568
3569/* QUIC connection packet handler task (post handshake) */
3570static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3571{
3572 struct ssl_sock_ctx *ctx;
3573 struct quic_conn *qc;
3574 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003575
3576
3577 ctx = context;
3578 qc = ctx->qc;
3579 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003580
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003581 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003582
3583 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3584 qc_rm_hp_pkts(qc, qel);
3585
3586 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3587 goto err;
3588
Frédéric Lécaille47756802022-03-25 09:12:16 +01003589 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3590 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3591 goto out;
3592
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003593 if (!qc_send_app_pkts(qc, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003594 goto err;
3595
Frédéric Lécaille47756802022-03-25 09:12:16 +01003596out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003597 return t;
3598
3599 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003600 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003601 return t;
3602}
3603
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003604/* QUIC connection packet handler task. */
3605struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003606{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003607 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003608 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003609 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610 enum quic_tls_enc_level tel, next_tel;
3611 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003612 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003613 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003614
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003615 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003616 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003617 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003618 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003619 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003620 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003621 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3622 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003623 /* The I/O handler has been woken up by the dgram listener
3624 * after the anti-amplification was reached.
3625 */
3626 qc_set_timer(qc);
3627 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3628 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3629 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003630 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003631 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3632 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3633 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003634 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003635 if (st >= QUIC_HS_ST_COMPLETE &&
3636 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3637 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3638 /* There may be remaining Handshake packets to treat and acknowledge. */
3639 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3640 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3641 }
3642 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003643 goto err;
3644
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003645 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003646 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647
3648 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003649 /* Treat packets waiting for header packet protection decryption */
3650 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003651 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003652
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003653 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3654 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3655 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003656 goto err;
3657
Frédéric Lécaille47756802022-03-25 09:12:16 +01003658 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3659 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3660 goto out;
3661
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003662 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003663 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003664 qel = next_qel;
3665 next_qel = NULL;
3666 goto next_level;
3667 }
3668
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003669 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003670 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003671 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003672 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003673 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003674
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003675 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003676 QUIC_FL_TLS_SECRETS_DCD)) {
3677 /* Discard the Handshake keys. */
3678 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3679 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3680 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3681 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003682 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003683 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003684 }
3685
3686 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3687 /* There may be remaining handshake to build (acks) */
3688 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3689 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003690 }
3691
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003692 if (!qr)
3693 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003694 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3695 * be considered.
3696 */
3697 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003698 goto err;
3699 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003700 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003701 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003702 else if (ret == 0)
3703 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003704
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003705 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003706 goto err;
3707
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003708 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003709 /* Check if there is something to do for the next level.
3710 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003711 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003712 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003713 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003714 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003715 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003716 goto next_level;
3717 }
3718
Frédéric Lécaille47756802022-03-25 09:12:16 +01003719 out:
3720 if (qr)
3721 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003722 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003723 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003724
3725 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003726 if (qr)
3727 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003728 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003729 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003730}
3731
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003732/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003733static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3734{
3735 int i;
3736
3737 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3738 if (qel->tx.crypto.bufs[i]) {
3739 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3740 qel->tx.crypto.bufs[i] = NULL;
3741 }
3742 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003743 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003744}
3745
3746/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003747 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748 * Returns 1 if succeeded, 0 if not.
3749 */
3750static int quic_conn_enc_level_init(struct quic_conn *qc,
3751 enum quic_tls_enc_level level)
3752{
3753 struct quic_enc_level *qel;
3754
3755 qel = &qc->els[level];
3756 qel->level = quic_to_ssl_enc_level(level);
3757 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3758 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3759 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003760 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003761
3762 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003763 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003764 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003765 qel->rx.crypto.offset = 0;
3766 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767
3768 /* Allocate only one buffer. */
3769 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3770 if (!qel->tx.crypto.bufs)
3771 goto err;
3772
3773 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3774 if (!qel->tx.crypto.bufs[0])
3775 goto err;
3776
3777 qel->tx.crypto.bufs[0]->sz = 0;
3778 qel->tx.crypto.nb_buf = 1;
3779
3780 qel->tx.crypto.sz = 0;
3781 qel->tx.crypto.offset = 0;
3782
3783 return 1;
3784
3785 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003786 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787 return 0;
3788}
3789
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003790/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
3791 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003792 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003793 * This function must only be called by the thread responsible of the quic_conn
3794 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003795 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003796static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797{
3798 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003799 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003800 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003801 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003802
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02003803 /* We must not free the quic-conn if the MUX is still allocated. */
3804 BUG_ON(qc->mux_state == QC_MUX_READY);
3805
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003806 /* free remaining stream descriptors */
3807 node = eb64_first(&qc->streams_by_id);
3808 while (node) {
3809 struct qc_stream_desc *stream;
3810
3811 stream = eb64_entry(node, struct qc_stream_desc, by_id);
3812 node = eb64_next(node);
3813
Amaury Denoyellec9acc312022-04-01 16:41:21 +02003814 /* all streams attached to the quic-conn are released, so
3815 * qc_stream_desc_free will liberate the stream instance.
3816 */
3817 BUG_ON(!stream->release);
3818 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02003819 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003820
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003821 if (qc->idle_timer_task) {
3822 task_destroy(qc->idle_timer_task);
3823 qc->idle_timer_task = NULL;
3824 }
3825
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003826 if (qc->timer_task) {
3827 task_destroy(qc->timer_task);
3828 qc->timer_task = NULL;
3829 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003830
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003831 /* remove the connection from receiver cids trees */
3832 ebmb_delete(&qc->odcid_node);
3833 ebmb_delete(&qc->scid_node);
3834 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003835
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003836 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003837 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01003838 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003839 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003840 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01003841 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003842
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003843 quic_tls_ku_free(qc);
3844 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3845 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003846 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02003847 }
3848
3849 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
3850 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
3851 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003852
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02003853 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
3854 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02003855 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02003856 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02003857
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003858 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3859 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003860 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003861}
3862
Amaury Denoyellee0be5732022-04-05 17:34:18 +02003863static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003864{
3865 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003866 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003867
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003868 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003869
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003870 /* Next application data can be dropped. */
3871 qc->mux_state = QC_MUX_RELEASED;
3872
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02003873 /* If the quic-conn timer has already expired free the quic-conn. */
3874 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
3875 quic_conn_release(qc);
3876 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
3877 return;
3878 }
3879
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003880 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003881}
3882
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003883/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003884static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003885{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003886 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003887 struct quic_conn *qc;
3888 struct quic_pktns *pktns;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003889 int i;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003890
3891 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003892 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003893 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003894 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003895 task->expire = TICK_ETERNITY;
3896 pktns = quic_loss_pktns(qc);
3897 if (tick_isset(pktns->tx.loss_time)) {
3898 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3899
3900 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3901 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003902 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3903 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003904 goto out;
3905 }
3906
3907 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003908 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003909 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3910 pktns->tx.pto_probe = 1;
3911 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3912 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3913 }
3914 else {
3915 pktns->tx.pto_probe = 2;
3916 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003917 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003918 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003919 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3920 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3921
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003922 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003923 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003924 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003925 iel->pktns->tx.pto_probe = 1;
3926 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003927
3928 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003929 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3930 continue;
3931
Frédéric Lécaille53c7d8d2022-02-15 12:00:55 +01003932 qc_prep_fast_retrans(&qc->els[i], qc);
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003933 }
3934
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003935 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3936 qc->path->loss.pto_count++;
3937
3938 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003939 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003940
3941 return task;
3942}
3943
3944/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3945 * connections used to identify the first Initial packets of client connecting
3946 * to listeners. This parameter must be NULL for QUIC connections attached
3947 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3948 * <scid> is the source connection ID with <scid_len> as length.
3949 * Returns 1 if succeeded, 0 if not.
3950 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003951static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003952 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003953 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954{
3955 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003956 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957 /* Initial CID. */
3958 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01003959 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003960 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003961
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003962 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003963 qc = pool_zalloc(pool_head_quic_conn);
3964 if (!qc) {
3965 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3966 goto err;
3967 }
3968
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003969 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3970 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003971 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003972 goto err;
3973 }
3974
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003975 qc->cids = EB_ROOT;
3976 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003977 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003978 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003979
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003980 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003981 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003982 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003983 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003984 qc->odcid.addrlen = dcid_addr_len;
3985 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003987 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003988 if (scid_len)
3989 memcpy(qc->dcid.data, scid, scid_len);
3990 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003991 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003992 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003993 }
3994 /* QUIC Client (outgoing connection to servers) */
3995 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003996 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003997 if (dcid_len)
3998 memcpy(qc->dcid.data, dcid, dcid_len);
3999 qc->dcid.len = dcid_len;
4000 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004001 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004002
4003 /* Initialize the output buffer */
4004 qc->obuf.pos = qc->obuf.data;
4005
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004006 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004007 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004008 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004009 goto err;
4010 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004011
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004012 /* insert the allocated CID in the receiver datagram handler tree */
4013 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004014 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004015
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004016 /* Select our SCID which is the first CID with 0 as sequence number. */
4017 qc->scid = icid->cid;
4018
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004019 /* Packet number spaces initialization. */
4020 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4021 quic_pktns_init(&qc->pktns[i]);
4022 /* QUIC encryption level context initialization. */
4023 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004024 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004025 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004026 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004027 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004028 /* Initialize the packet number space. */
4029 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4030 }
4031
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02004032 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004033 qc->tps_tls_ext = qc->version & 0xff000000 ?
4034 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4035 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004036 /* TX part. */
4037 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004038 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4039 qc->tx.wbuf = qc->tx.rbuf = 0;
4040 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004041 /* RX part. */
4042 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004043 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004044
4045 qc->nb_pkt_for_cc = 1;
4046 qc->nb_pkt_since_cc = 0;
4047
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004048 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004049 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004050 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004051 goto err;
4052 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004053
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004054 /* XXX TO DO: Only one path at this time. */
4055 qc->path = &qc->paths[0];
4056 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4057
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004058 /* required to use MTLIST_IN_LIST */
4059 MT_LIST_INIT(&qc->accept_list);
4060
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004061 qc->streams_by_id = EB_ROOT_UNIQUE;
4062
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004063 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004064
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004065 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004066
4067 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004068 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004069 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4070 if (qc)
4071 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004072 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004073 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004074}
4075
4076/* Initialize the timer task of <qc> QUIC connection.
4077 * Returns 1 if succeeded, 0 if not.
4078 */
4079static int quic_conn_init_timer(struct quic_conn *qc)
4080{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004081 /* Attach this task to the same thread ID used for the connection */
4082 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004083 if (!qc->timer_task)
4084 return 0;
4085
4086 qc->timer = TICK_ETERNITY;
4087 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004088 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004089
4090 return 1;
4091}
4092
Frédéric Lécaille47756802022-03-25 09:12:16 +01004093/* Rearm the idle timer for <qc> QUIC connection. */
4094static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4095{
4096 unsigned int expire;
4097
4098 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4099 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4100}
4101
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004102/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4103 * which is set to 1 when receiving a packet , and 0 when sending packet
4104 */
4105static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4106{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004107 if (read) {
4108 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4109 }
4110 else {
4111 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4112 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004113 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004114}
4115
4116/* The task handling the idle timeout */
4117static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4118{
4119 struct quic_conn *qc = ctx;
4120
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004121 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4122 * might free the quic-conn too early via quic_close().
4123 */
4124 qc_notify_close(qc);
4125
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004126 /* If the MUX is still alive, keep the quic-conn. The MUX is
4127 * responsible to call quic_close to release it.
4128 */
4129 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4130 if (qc->mux_state != QC_MUX_READY)
4131 quic_conn_release(qc);
4132
4133 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4134 * least clean some parts of it such as the tasklet.
4135 */
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004136
4137 return NULL;
4138}
4139
4140/* Initialize the idle timeout task for <qc>.
4141 * Returns 1 if succeeded, 0 if not.
4142 */
4143static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4144{
4145 qc->idle_timer_task = task_new_here();
4146 if (!qc->idle_timer_task)
4147 return 0;
4148
4149 qc->idle_timer_task->process = qc_idle_timer_task;
4150 qc->idle_timer_task->context = qc;
4151 qc_idle_timer_rearm(qc, 1);
4152 task_queue(qc->idle_timer_task);
4153
4154 return 1;
4155}
4156
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004157/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4158 * past one byte of this buffer.
4159 */
4160static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4161 struct quic_rx_packet *pkt)
4162{
4163 unsigned char dcid_len, scid_len;
4164
4165 /* Version */
4166 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
4167 return 0;
4168
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004169 /* Destination Connection ID Length */
4170 dcid_len = *(*buf)++;
4171 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4172 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4173 /* XXX MUST BE DROPPED */
4174 return 0;
4175
4176 if (dcid_len) {
4177 /* Check that the length of this received DCID matches the CID lengths
4178 * of our implementation for non Initials packets only.
4179 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004180 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4181 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004182 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183 return 0;
4184
4185 memcpy(pkt->dcid.data, *buf, dcid_len);
4186 }
4187
4188 pkt->dcid.len = dcid_len;
4189 *buf += dcid_len;
4190
4191 /* Source Connection ID Length */
4192 scid_len = *(*buf)++;
4193 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4194 /* XXX MUST BE DROPPED */
4195 return 0;
4196
4197 if (scid_len)
4198 memcpy(pkt->scid.data, *buf, scid_len);
4199 pkt->scid.len = scid_len;
4200 *buf += scid_len;
4201
4202 return 1;
4203}
4204
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004205/* Insert <pkt> RX packet in its <qel> RX packets tree */
4206static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4207{
4208 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004209 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004210 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4211 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4212 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004213}
4214
4215/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004216 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4217 * byte past the end of the buffer containing this packet and <beg> the address of
4218 * the packet first byte.
4219 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4220 * Returns 1 if succeeded, 0 if not.
4221 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004222static inline int qc_try_rm_hp(struct quic_conn *qc,
4223 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004224 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004226 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004227{
4228 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004229 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004230 struct quic_enc_level *qel;
4231 /* Only for traces. */
4232 struct quic_rx_packet *qpkt_trace;
4233
4234 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004235 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236 /* The packet number is here. This is also the start minus
4237 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4238 * protection.
4239 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004240 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004241
4242 tel = quic_packet_type_enc_level(pkt->type);
4243 qel = &qc->els[tel];
4244
4245 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004246 /* Note that the following function enables us to unprotect the packet
4247 * number and its length subsequently used to decrypt the entire
4248 * packets.
4249 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004250 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4251 qel->pktns->rx.largest_pn, pn, beg, end)) {
4252 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253 goto err;
4254 }
4255
4256 /* The AAD includes the packet number field found at <pn>. */
4257 pkt->aad_len = pn - beg + pkt->pnl;
4258 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004259 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004260 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004261 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004262 /* If the packet number space has been discarded, this packet
4263 * will be not parsed.
4264 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004265 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004266 goto out;
4267 }
4268
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004269 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004270 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004271 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4272 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004273 }
4274
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004275 *el = qel;
4276 /* No reference counter incrementation here!!! */
4277 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4278 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4279 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4280 b_add(&qc->rx.buf, pkt->len);
4281 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004282 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004283 return 1;
4284
4285 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004286 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004287 return 0;
4288}
4289
4290/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
4291 * Also set <*long_header> to 1 if this form is long, 0 if not.
4292 */
4293static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
4294 unsigned char byte0, int *long_header)
4295{
4296 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
4297 pkt->type =
4298 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
4299 *long_header = 1;
4300 }
4301 else {
4302 pkt->type = QUIC_PACKET_TYPE_SHORT;
4303 *long_header = 0;
4304 }
4305}
4306
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004307/*
4308 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
4309 */
4310static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
4311{
4312 int j = 0, version;
4313
4314 do {
4315 version = quic_supported_version[j];
4316 if (version == pkt->version)
4317 return 1;
4318
4319 version = quic_supported_version[++j];
4320 } while(version);
4321
4322 return 0;
4323}
4324
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004325/*
4326 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4327 * address <addr>.
4328 * Implementation of RFC9000 6. Version Negotiation
4329 *
4330 * TODO implement a rate-limiting sending of Version Negotiation packets
4331 *
4332 * Returns 0 on success else non-zero
4333 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004334static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4335 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004336{
4337 char buf[256];
4338 int i = 0, j, version;
4339 const socklen_t addrlen = get_addr_len(addr);
4340
4341 /*
4342 * header form
4343 * long header, fixed bit to 0 for Version Negotiation
4344 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004345 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4346 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004347
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004348 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004349 /* null version for Version Negotiation */
4350 buf[i++] = '\x00';
4351 buf[i++] = '\x00';
4352 buf[i++] = '\x00';
4353 buf[i++] = '\x00';
4354
4355 /* source connection id */
4356 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004357 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004358 i += pkt->scid.len;
4359
4360 /* destination connection id */
4361 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004362 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004363 i += pkt->dcid.len;
4364
4365 /* supported version */
4366 j = 0;
4367 do {
4368 version = htonl(quic_supported_version[j]);
4369 memcpy(&buf[i], &version, sizeof(version));
4370 i += sizeof(version);
4371
4372 version = quic_supported_version[++j];
4373 } while (version);
4374
4375 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4376 return 1;
4377
4378 return 0;
4379}
4380
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004381/* Generate the token to be used in Retry packets. The token is written to
4382 * <buf> which is expected to be <len> bytes.
4383 *
4384 * Various parameters are expected to be encoded in the token. For now, only
4385 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4386 * as this CID must be repeated by the server in the transport parameters.
4387 *
4388 * TODO add the client address to validate the token origin.
4389 *
4390 * Returns the length of the encoded token or 0 on error.
4391 */
4392static int generate_retry_token(unsigned char *buf, unsigned char len,
4393 struct quic_rx_packet *pkt)
4394{
4395 const size_t token_len = 1 + pkt->dcid.len;
4396 unsigned char i = 0;
4397
4398 if (token_len > len)
4399 return 0;
4400
4401 buf[i++] = pkt->dcid.len;
4402 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4403 i += pkt->dcid.len;
4404
4405 return i;
4406}
4407
4408/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4409 * the Initial <pkt> packet.
4410 *
4411 * Returns 0 on success else non-zero.
4412 */
4413static int send_retry(int fd, struct sockaddr_storage *addr,
4414 struct quic_rx_packet *pkt)
4415{
4416 unsigned char buf[128];
4417 int i = 0, token_len;
4418 const socklen_t addrlen = get_addr_len(addr);
4419 struct quic_cid scid;
4420
4421 /* long header + fixed bit + packet type 0x3 */
4422 buf[i++] = 0xf0;
4423 /* version */
4424 buf[i++] = 0x00;
4425 buf[i++] = 0x00;
4426 buf[i++] = 0x00;
4427 buf[i++] = 0x01;
4428
4429 /* Use the SCID from <pkt> for Retry DCID. */
4430 buf[i++] = pkt->scid.len;
4431 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4432 i += pkt->scid.len;
4433
4434 /* Generate a new CID to be used as SCID for the Retry packet. */
4435 scid.len = QUIC_HAP_CID_LEN;
4436 if (RAND_bytes(scid.data, scid.len) != 1)
4437 return 1;
4438
4439 buf[i++] = scid.len;
4440 memcpy(&buf[i], scid.data, scid.len);
4441 i += scid.len;
4442
4443 /* token */
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004444 if (!(token_len = generate_retry_token(&buf[i], sizeof(buf) - i, pkt)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004445 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004446
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004447 i += token_len;
4448
4449 /* token integrity tag */
4450 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4451 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4452 pkt->dcid.len, buf, i)) {
4453 return 1;
4454 }
4455
4456 i += QUIC_TLS_TAG_LEN;
4457
4458 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4459 return 1;
4460
4461 return 0;
4462}
4463
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004464/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4465 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4466 * concatenated to the <pkt> DCID field.
4467 *
4468 * Returns the instance or NULL if not found.
4469 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004470static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004471 struct listener *l,
4472 struct sockaddr_storage *saddr)
4473{
4474 struct quic_conn *qc = NULL;
4475 struct ebmb_node *node;
4476 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004477 /* set if the quic_conn is found in the second DCID tree */
4478 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004479
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004480 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4481 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4482 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4483 /* DCIDs of first packets coming from multiple clients may have
4484 * the same values. Let's distinguish them by concatenating the
4485 * socket addresses.
4486 */
4487 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004488 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004489 pkt->dcid.len + pkt->dcid.addrlen);
4490 if (node) {
4491 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4492 goto end;
4493 }
4494 }
4495
4496 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4497 * also for INITIAL/0-RTT non-first packets with the final DCID in
4498 * used.
4499 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004500 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004501 if (!node)
4502 goto end;
4503
4504 id = ebmb_entry(node, struct quic_connection_id, node);
4505 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004506 found_in_dcid = 1;
4507
4508 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004509 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4510 * If already done, this is a noop.
4511 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004512 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004513 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004514
4515 return qc;
4516}
4517
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004518/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4519 * will extract the parameters stored in the token : <odcid>.
4520 *
4521 * Returns 0 on success else non-zero.
4522 */
4523static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4524 struct quic_cid *odcid)
4525{
4526 uint64_t odcid_len;
4527
4528 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4529 return 1;
4530
Frédéric Lécaillef1f812b2022-03-17 16:22:02 +01004531 if (odcid_len > QUIC_CID_MAXLEN)
4532 return 1;
4533
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004534 memcpy(odcid->data, token, odcid_len);
4535 odcid->len = odcid_len;
4536
4537 return 0;
4538}
4539
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004540/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4541 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4542 * parameters of this session.
4543 * This is the responsibility of the caller to check the validity of all the
4544 * pointers passed as parameter to this function.
4545 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4546 * CO_ER_SSL_NO_MEM.
4547 */
4548static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4549 unsigned char *params, size_t params_len)
4550{
4551 int retry;
4552
4553 retry = 1;
4554 retry:
4555 *ssl = SSL_new(ssl_ctx);
4556 if (!*ssl) {
4557 if (!retry--)
4558 goto err;
4559
4560 pool_gc(NULL);
4561 goto retry;
4562 }
4563
4564 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4565 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc) ||
4566 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004567 SSL_free(*ssl);
4568 *ssl = NULL;
4569 if (!retry--)
4570 goto err;
4571
4572 pool_gc(NULL);
4573 goto retry;
4574 }
4575
4576 return 0;
4577
4578 err:
4579 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4580 return -1;
4581}
4582
4583/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
4584 * used to process <qc> received packets. The allocated context is stored in
4585 * <qc.xprt_ctx>.
4586 *
4587 * Returns 0 on success else non-zero.
4588 */
4589int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
4590{
4591 struct bind_conf *bc = qc->li->bind_conf;
4592 struct ssl_sock_ctx *ctx = NULL;
4593
4594 ctx = pool_zalloc(pool_head_quic_conn_ctx);
4595 if (!ctx)
4596 goto err;
4597
4598 ctx->wait_event.tasklet = tasklet_new();
4599 if (!ctx->wait_event.tasklet)
4600 goto err;
4601
4602 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4603 ctx->wait_event.tasklet->context = ctx;
4604 ctx->wait_event.events = 0;
4605 ctx->subs = NULL;
4606 ctx->xprt_ctx = NULL;
4607 ctx->qc = qc;
4608
4609 /* Set tasklet tid based on the SCID selected by us for this
4610 * connection. The upper layer will also be binded on the same thread.
4611 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01004612 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004613
4614 if (qc_is_listener(qc)) {
4615 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4616 qc->enc_params, qc->enc_params_len) == -1) {
4617 goto err;
4618 }
4619
4620 /* Enabling 0-RTT */
4621 if (bc->ssl_conf.early_data)
4622 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
4623
4624 SSL_set_accept_state(ctx->ssl);
4625 }
4626
4627 ctx->xprt = xprt_get(XPRT_QUIC);
4628
4629 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004630 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004631
4632 return 0;
4633
4634 err:
4635 if (ctx && ctx->wait_event.tasklet)
4636 tasklet_free(ctx->wait_event.tasklet);
4637 pool_free(pool_head_quic_conn_ctx, ctx);
4638
4639 return 1;
4640}
4641
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004642static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004643 struct quic_rx_packet *pkt, int first_pkt,
4644 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004645{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004646 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004647 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004648 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004649 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004650 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004651 size_t b_cspace;
4652 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004653
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004654 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004655 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004656 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004657 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004658 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4659 /* This ist only to please to traces and distinguish the
4660 * packet with parsed packet number from others.
4661 */
4662 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004663 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004664 goto err;
4665
4666 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004667 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004668 /* XXX TO BE DISCARDED */
4669 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4670 goto err;
4671 }
4672
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004673 l = dgram->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004674 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004675 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004676 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004677 uint64_t len;
4678
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004679 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004680 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4681 goto err;
4682 }
4683
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004684 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4685 * they must have the same DCID.
4686 */
4687 if (!first_pkt &&
4688 (pkt->dcid.len != dgram->dcid_len ||
4689 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4690 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4691 goto err;
4692 }
4693
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004694 /* Retry of Version Negotiation packets are only sent by servers */
4695 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4696 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4697 goto err;
4698 }
4699
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004700 /* RFC9000 6. Version Negotiation */
4701 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004702 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004703 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004704 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4705 goto err;
4706 }
4707
4708 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004709 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004710 }
4711
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004712 /* For Initial packets, and for servers (QUIC clients connections),
4713 * there is no Initial connection IDs storage.
4714 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004715 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004716 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004717
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004718 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4719 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004720 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4721 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004722 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004723
Frédéric Lécaille055ee6c2022-01-25 21:21:56 +01004724 /* The token may be provided in a Retry packet or NEW_TOKEN frame
4725 * only by the QUIC server.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004726 */
4727 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004728
4729 /* TODO Retry should be automatically activated if
4730 * suspect network usage is detected.
4731 */
4732 if (!token_len && l->bind_conf->quic_force_retry) {
4733 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004734 if (send_retry(l->rx.fd, &dgram->saddr, pkt)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004735 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4736 goto err;
4737 }
4738
4739 goto err;
4740 }
4741 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004742 pkt->token = buf;
4743 buf += pkt->token_len;
4744 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004745 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004746 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004747 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004748 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4749 goto err;
4750 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004751 }
4752
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004753 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4754 end - buf < len) {
4755 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4756 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004757 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004758
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004759 payload = buf;
4760 pkt->len = len + payload - beg;
4761
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004762 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004763 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004764 int ipv4;
4765 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004766 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004767 const unsigned char *salt = initial_salt_v1;
4768 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004769
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004770 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004771 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004772 goto err;
4773 }
4774
Frédéric Lécailledc364042022-01-27 16:51:54 +01004775 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
4776 TRACE_PROTO("dropped packet", QUIC_EV_CONN_LPKT);
4777 goto err;
4778 }
4779
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004780 pkt->saddr = dgram->saddr;
4781 ipv4 = dgram->saddr.ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004782 qc = qc_new_conn(pkt->version, ipv4,
4783 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004784 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004785 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004786 goto err;
4787
Amaury Denoyelle9fa15e52022-01-19 15:54:23 +01004788 memcpy(&qc->peer_addr, &pkt->saddr, sizeof(pkt->saddr));
4789
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004790 odcid = &qc->rx.params.original_destination_connection_id;
4791 /* Copy the transport parameters. */
4792 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004793
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004794 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004795 if (pkt->token_len) {
4796 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4797 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4798 goto err;
4799 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004800 /* Copy retry_source_connection_id transport parameter. */
4801 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4802 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004803 }
4804 else {
4805 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4806 odcid->len = pkt->dcid.len;
4807 }
4808
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004809 /* Copy the initial source connection ID. */
4810 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4811 qc->enc_params_len =
4812 quic_transport_params_encode(qc->enc_params,
4813 qc->enc_params + sizeof qc->enc_params,
4814 &qc->rx.params, 1);
4815 if (!qc->enc_params_len)
4816 goto err;
4817
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01004818 if (qc_conn_alloc_ssl_ctx(qc))
4819 goto err;
4820
Frédéric Lécaille789413c2022-01-31 10:16:18 +01004821 if (!quic_conn_init_timer(qc))
4822 goto err;
4823
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004824 if (!quic_conn_init_idle_timer_task(qc))
4825 goto err;
4826
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004827 /* NOTE: the socket address has been concatenated to the destination ID
4828 * chosen by the client for Initial packets.
4829 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004830 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4831 salt = initial_salt_draft_29;
4832 salt_len = sizeof initial_salt_draft_29;
4833 }
4834 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004835 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004836 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004837 goto err;
4838 }
4839
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004840 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004841 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004842 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004843
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004844 /* If the insertion failed, it means that another
4845 * thread has already allocated a QUIC connection for
4846 * the same CID. Liberate our allocated connection.
4847 */
4848 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004849 qc_to_purge = qc;
4850
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004851 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4852 pkt->qc = qc;
4853 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004854
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004855 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004856 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004857 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004858 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004859 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004860 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004861 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004862 }
4863 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004864 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004865 }
4866 }
4867 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004868 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004869 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4870 goto err;
4871 }
4872
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004873 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004874 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004875
4876 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4877 * they must have the same DCID.
4878 */
4879 if (!first_pkt &&
4880 (pkt->dcid.len != dgram->dcid_len ||
4881 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
4882 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
4883 goto err;
4884 }
4885
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004886 buf += QUIC_HAP_CID_LEN;
4887
4888 /* A short packet is the last one of a UDP datagram. */
4889 payload = buf;
4890 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004891
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004892 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004893 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004894 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004895 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4896 goto err;
4897 }
4898
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004899 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004900 }
4901
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004902 if (qc->flags & QUIC_FL_CONN_CLOSING) {
4903 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
4904 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
4905 qc->nb_pkt_for_cc++;
4906 qc->nb_pkt_since_cc = 0;
4907 }
4908 /* Skip the entire datagram */
4909 pkt->len = end - beg;
4910 TRACE_PROTO("Closing state connection", QUIC_EV_CONN_LPKT, pkt->qc);
4911 goto out;
4912 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004913
4914 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4915 * they must have the same DCID.
4916 *
4917 * This check must be done after the final update to pkt.len to
4918 * properly drop the packet on failure.
4919 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004920 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004921 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004922 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4923 QUIC_EV_CONN_LPKT, qc);
4924 /* Reset the anti-amplification bit. It will be set again
4925 * when sending the next packet if reached again.
4926 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004927 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
4928 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004929 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004930 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004931
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01004932 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004933
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004934 if (qc->err_code) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004935 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004936 goto out;
4937 }
4938
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004939 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004940 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004941 b_cspace = b_contig_space(&qc->rx.buf);
4942 if (b_cspace < pkt->len) {
4943 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004944 if (b_cspace) {
4945 b_putchr(&qc->rx.buf, 0x00);
4946 b_cspace--;
4947 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004948 b_add(&qc->rx.buf, b_cspace);
4949 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004950 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004951 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004952 goto err;
4953 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004954 }
4955
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004956 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004957 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004958 goto err;
4959 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004960
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004961 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004962 if (pkt->aad_len)
4963 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004964 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004965 /* Wake up the connection packet handler task from here only if all
4966 * the contexts have been initialized, especially the mux context
4967 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4968 * will start it if these contexts for the connection are not already
4969 * initialized.
4970 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004971 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004972 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004973 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004974
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004975 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004976
4977 return pkt->len;
4978
4979 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004980 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4981 * This cannot be done by this thread.
4982 */
4983 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004984 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004985 if (conn_ctx && conn_ctx->wait_event.tasklet)
4986 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4987 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004988 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004989 if (!pkt->len)
4990 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004991 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004992 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004993
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004994 return -1;
4995}
4996
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004997/* This function builds into <buf> buffer a QUIC long packet header.
4998 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004999 */
5000static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
5001 int type, size_t pn_len, struct quic_conn *conn)
5002{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005003 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005004 return 0;
5005
5006 /* #0 byte flags */
5007 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5008 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5009 /* Version */
5010 quic_write_uint32(buf, end, conn->version);
5011 *(*buf)++ = conn->dcid.len;
5012 /* Destination connection ID */
5013 if (conn->dcid.len) {
5014 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5015 *buf += conn->dcid.len;
5016 }
5017 /* Source connection ID */
5018 *(*buf)++ = conn->scid.len;
5019 if (conn->scid.len) {
5020 memcpy(*buf, conn->scid.data, conn->scid.len);
5021 *buf += conn->scid.len;
5022 }
5023
5024 return 1;
5025}
5026
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005027/* This function builds into <buf> buffer a QUIC short packet header.
5028 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005029 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005030static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5031 size_t pn_len, struct quic_conn *conn,
5032 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005033{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005034 if (end - *buf < 1 + conn->dcid.len)
5035 return 0;
5036
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005037 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005038 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5039 ((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 +01005040 /* Destination connection ID */
5041 if (conn->dcid.len) {
5042 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5043 *buf += conn->dcid.len;
5044 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005045
5046 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005047}
5048
5049/* Apply QUIC header protection to the packet with <buf> as first byte address,
5050 * <pn> as address of the Packet number field, <pnlen> being this field length
5051 * with <aead> as AEAD cipher and <key> as secret key.
5052 * Returns 1 if succeeded or 0 if failed.
5053 */
5054static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5055 const EVP_CIPHER *aead, const unsigned char *key)
5056{
5057 int i, ret, outlen;
5058 EVP_CIPHER_CTX *ctx;
5059 /* We need an IV of at least 5 bytes: one byte for bytes #0
5060 * and at most 4 bytes for the packet number
5061 */
5062 unsigned char mask[5] = {0};
5063
5064 ret = 0;
5065 ctx = EVP_CIPHER_CTX_new();
5066 if (!ctx)
5067 return 0;
5068
5069 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5070 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5071 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5072 goto out;
5073
5074 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5075 for (i = 0; i < pnlen; i++)
5076 pn[i] ^= mask[i + 1];
5077
5078 ret = 1;
5079
5080 out:
5081 EVP_CIPHER_CTX_free(ctx);
5082
5083 return ret;
5084}
5085
5086/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5087 * ACK ranges if needed to a value below <limit> in bytes.
5088 * Return 1 if succeeded, 0 if not.
5089 */
5090static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5091{
5092 size_t room, ack_delay_sz;
5093
5094 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5095 /* A frame is made of 1 byte for the frame type. */
5096 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005097 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005098 return 0;
5099
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005100 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005101}
5102
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005103/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5104 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5105 * with <room> as available room, and <*len> the packet Length field initialized
5106 * with the number of bytes already present in this buffer which must be taken
5107 * into an account for the Length packet field value. <headlen> is the number of
5108 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005109 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005110 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005111 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005112 * Return 1 if succeeded, 0 if not.
5113 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005114static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005115 size_t room, size_t *len, size_t headlen,
5116 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005117 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005118{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005119 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005120 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005121
Frédéric Lécailleea604992020-12-24 13:01:37 +01005122 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005123 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005124 return 0;
5125
Frédéric Lécailleea604992020-12-24 13:01:37 +01005126 /* If we are not probing we must take into an account the congestion
5127 * control window.
5128 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005129 if (!qel->pktns->tx.pto_probe) {
5130 size_t remain = quic_path_prep_data(qc->path);
5131
5132 if (headlen > remain)
5133 return 0;
5134
5135 room = QUIC_MIN(room, remain - headlen);
5136 }
5137
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005138 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005139 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005140 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005141 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005142 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005143
Frédéric Lécailleea604992020-12-24 13:01:37 +01005144 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005145 break;
5146
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005147 switch (cf->type) {
5148 case QUIC_FT_CRYPTO:
5149 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005150 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005151 /* Compute the length of this CRYPTO frame header */
5152 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5153 /* Compute the data length of this CRyPTO frame. */
5154 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5155 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005156 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005157 if (!dlen)
5158 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005159
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005160 /* CRYPTO frame length. */
5161 flen = hlen + quic_int_getsize(dlen) + dlen;
5162 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005163 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005164 /* Add the CRYPTO data length and its encoded length to the packet
5165 * length and the length of this length.
5166 */
5167 *len += flen;
5168 room -= flen;
5169 if (dlen == cf->crypto.len) {
5170 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005171 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005172 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005173 }
5174 else {
5175 struct quic_frame *new_cf;
5176
5177 new_cf = pool_alloc(pool_head_quic_frame);
5178 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005179 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005180 return 0;
5181 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005182
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005183 new_cf->type = QUIC_FT_CRYPTO;
5184 new_cf->crypto.len = dlen;
5185 new_cf->crypto.offset = cf->crypto.offset;
5186 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005187 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005188 /* Consume <dlen> bytes of the current frame. */
5189 cf->crypto.len -= dlen;
5190 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005191 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005192 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005193
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005194 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005195 /* Note that these frames are accepted in short packets only without
5196 * "Length" packet field. Here, <*len> is used only to compute the
5197 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005198 *
5199 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005200 * excepting the variable ones. Note that +1 is for the type of this frame.
5201 */
5202 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005203 ((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 +02005204 /* Compute the data length of this STREAM frame. */
5205 avail_room = room - hlen - *len;
5206 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005207 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005208
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005209 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005210 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005211 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5212 dlen = max_available_room(avail_room, &dlen_sz);
5213 if (dlen > cf->stream.len) {
5214 dlen = cf->stream.len;
5215 }
5216 dlen_sz = quic_int_getsize(dlen);
5217 flen = hlen + dlen_sz + dlen;
5218 }
5219 else {
5220 dlen = QUIC_MIN(avail_room, cf->stream.len);
5221 flen = hlen + dlen;
5222 }
5223 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005224 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005225 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005226 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005227 /* Add the STREAM data length and its encoded length to the packet
5228 * length and the length of this length.
5229 */
5230 *len += flen;
5231 room -= flen;
5232 if (dlen == cf->stream.len) {
5233 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005234 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005235 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005236
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005237 /* The MUX stream might be released at this
5238 * stage. This can most notably happen on
5239 * retransmission.
5240 */
5241 if (qc->mux_state == QC_MUX_READY &&
5242 !cf->stream.stream->release) {
5243 qcc_streams_sent_done(cf->stream.stream->ctx,
5244 cf->stream.len,
5245 cf->stream.offset.key);
5246 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005247 }
5248 else {
5249 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005250 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005251
5252 new_cf = pool_zalloc(pool_head_quic_frame);
5253 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005254 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005255 return 0;
5256 }
5257
5258 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005259 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005260 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005261 new_cf->stream.id = cf->stream.id;
5262 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5263 new_cf->stream.offset = cf->stream.offset;
5264 new_cf->stream.len = dlen;
5265 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5266 /* FIN bit reset */
5267 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5268 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005269 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005270 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5271 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005272 cf_buf = b_make(b_orig(cf->stream.buf),
5273 b_size(cf->stream.buf),
5274 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005275 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005276 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005277 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005278
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005279 /* The MUX stream might be released at this
5280 * stage. This can most notably happen on
5281 * retransmission.
5282 */
5283 if (qc->mux_state == QC_MUX_READY &&
5284 !cf->stream.stream->release) {
5285 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5286 new_cf->stream.len,
5287 new_cf->stream.offset.key);
5288 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005289 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005290
5291 /* TODO the MUX is notified about the frame sending via
5292 * previous qcc_streams_sent_done call. However, the
5293 * sending can fail later, for example if the sendto
5294 * system call returns an error. As the MUX has been
5295 * notified, the transport layer is responsible to
5296 * bufferize and resent the announced data later.
5297 */
5298
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005299 break;
5300
5301 default:
5302 flen = qc_frm_len(cf);
5303 BUG_ON(!flen);
5304 if (flen > room)
5305 continue;
5306
5307 *len += flen;
5308 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005309 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005310 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005311 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005312 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01005313 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005314 }
5315
Frédéric Lécailleea604992020-12-24 13:01:37 +01005316 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005317}
5318
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005319/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005320 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5321 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005322 * filling the buffer with as much frames as possible from <frms> list of
5323 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005324 * 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 +02005325 * 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 +02005326 * having returned from this function.
5327 * 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 +01005328 * number field in this packet. <pn_len> will also have the packet number
5329 * length as value.
5330 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005331 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005332 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005333static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
5334 size_t dglen, struct quic_tx_packet *pkt,
5335 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005336 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005337 struct quic_enc_level *qel, struct quic_conn *qc,
5338 struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005339{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005340 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005341 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005342 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
5343 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005344 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005345 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005346 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005347 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005348 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005349 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005350
Frédéric Lécailleea604992020-12-24 13:01:37 +01005351 /* Length field value with CRYPTO frames if present. */
5352 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005353 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005354 /* When not probing, and no immediate close is required, reduce the size of this
5355 * buffer to respect the congestion controller window.
5356 * 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 +01005357 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005358 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005359 size_t path_room;
5360
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005361 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005362 if (end - beg > path_room)
5363 end = beg + path_room;
5364 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005365
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005366 /* Ensure there is enough room for the TLS encryption tag and a zero token
5367 * length field if any.
5368 */
5369 if (end - pos < QUIC_TLS_TAG_LEN +
5370 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
5371 goto no_room;
5372
5373 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005374 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005375 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005376 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005377 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005378 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005379 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005380 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005381 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005382 goto no_room;
5383
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005384 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
5385 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005386 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005387 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005388 /* Build an ACK frame if required. */
5389 ack_frm_len = 0;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005390 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED)) {
5391 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005392 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005393 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005394 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
5395 * smallest frame (PING) and <*pn_len> more for the packet number. Note
5396 * that from here, we do not know if we will have to send a PING frame.
5397 * This will be decided after having computed the ack-eliciting frames
5398 * to be added to this packet.
5399 */
5400 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 +01005401 if (!ack_frm_len)
5402 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005403 }
5404
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005405 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005406 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01005407 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005408 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005409 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01005410
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005411 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005412 /* Initialize the length of the frames built below to <len>.
5413 * If any frame could be successfully built by qc_build_frms(),
5414 * we will have len_frms > len.
5415 */
5416 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005417 if (!qc_build_frms(&frm_list, frms,
5418 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01005419 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005420 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005421 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005422 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005423
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005424 /* Length (of the remaining data). Must not fail because, the buffer size
5425 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
5426 * for the encryption tag. It must be taken into an account for the length
5427 * of this packet.
5428 */
5429 if (len_frms)
5430 len = len_frms + QUIC_TLS_TAG_LEN;
5431 else
5432 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005433 /* CONNECTION_CLOSE frame */
5434 if (cc) {
5435 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005436
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005437 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005438 len += qc_frm_len(&cc_frm);
5439 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005440 add_ping_frm = 0;
5441 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005442 len_sz = quic_int_getsize(len);
5443 /* Add this packet size to <dglen> */
5444 dglen += head_len + len_sz + len;
5445 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
5446 /* This is a maximum padding size */
5447 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
5448 /* The length field value is of this packet is <len> + <padding_len>
5449 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05005450 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005451 * sizes from <padding_len>.
5452 */
5453 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
5454 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005455 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005456 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005457 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005458 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005459 add_ping_frm = 1;
5460 len += 1;
5461 }
5462 /* 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 +01005463 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005464 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5465 }
5466
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005467 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5468 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005469
5470 /* Packet number field address. */
5471 *buf_pn = pos;
5472
5473 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005474 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5475 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005476
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005477 if (ack_frm_len) {
5478 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
5479 goto no_room;
5480
5481 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005482 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005483 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005484
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005485 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005486 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005487 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005488 unsigned char *spos = pos;
5489
5490 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005491 ssize_t room = end - pos;
5492 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005493 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005494 /* TODO: this should not have happened if qc_build_frms()
5495 * had correctly computed and sized the frames to be
5496 * added to this packet. Note that <cf> was added
5497 * from <frm_list> to <frms> list by qc_build_frms().
5498 */
5499 LIST_DELETE(&cf->list);
5500 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005501 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005502 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01005503
5504 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005505 quic_tx_packet_refinc(pkt);
5506 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005507 }
5508 }
5509
5510 /* Build a PING frame if needed. */
5511 if (add_ping_frm) {
5512 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005513 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005514 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005515 }
5516
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005517 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005518 if (cc) {
5519 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
5520 goto no_room;
5521
5522 pkt->flags |= QUIC_FL_TX_PACKET_CC;
5523 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005524
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005525 /* Build a PADDING frame if needed. */
5526 if (padding_len) {
5527 frm.type = QUIC_FT_PADDING;
5528 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005529 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005530 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005531 }
5532
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005533 /* If this packet is ack-eliciting and we are probing let's
5534 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005535 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005536 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5537 qel->pktns->tx.pto_probe)
5538 qel->pktns->tx.pto_probe--;
5539
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005540 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005541 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005542 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005543
5544 return 1;
5545
5546 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005547 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005548 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02005549 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
5550
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005551 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005552}
5553
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005554static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005555{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005556 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005557 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005558 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005559 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005560 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005561 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005562 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01005563 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01005564 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005565 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005566}
5567
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005568/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005569 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
5570 * of prebuilt frames.
5571 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005572 * Return -2 if the packet could not be allocated or encrypted for any reason,
5573 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01005574 * XXX NOTE XXX
5575 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
5576 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
5577 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005578 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005579static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5580 const unsigned char *buf_end,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005581 struct quic_enc_level *qel, struct list *frms,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005582 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005583 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005584{
5585 /* The pointer to the packet number field. */
5586 unsigned char *buf_pn;
5587 unsigned char *beg, *end, *payload;
5588 int64_t pn;
5589 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005590 struct quic_tls_ctx *tls_ctx;
5591 struct quic_tx_packet *pkt;
5592
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005593 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005594 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005595 pkt = pool_alloc(pool_head_quic_tx_packet);
5596 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005597 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005598 *err = -2;
5599 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005600 }
5601
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005602 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005603 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005604 pn_len = 0;
5605 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005606
5607 pn = qel->pktns->tx.next_pn + 1;
5608 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 +02005609 padding, cc, probe, qel, qc, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005610 *err = -1;
5611 goto err;
5612 }
5613
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005614 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005615 payload = buf_pn + pn_len;
5616 payload_len = end - payload;
5617 aad_len = payload - beg;
5618
5619 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005620 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005621 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005622 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005623 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005624
5625 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005626 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005627 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5628 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005629 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005630 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005631 goto err;
5632 }
5633
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005634 /* Consume a packet number */
5635 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005636 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01005637 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005638 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005639 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5640 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005641 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005642 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005643 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005644 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005645 pkt->in_flight_len = pkt->len;
5646 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005647 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01005648 /* Always reset this flags */
5649 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02005650 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
5651 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
5652 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
5653 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02005654
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005655 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005656 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005657
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005658 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005659
5660 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01005661 /* TODO: what about the frames which have been built
5662 * for this packet.
5663 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005664 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005665 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005666 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005667}
5668
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005669
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005670/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5671 * event subscriber <es> is not allowed to change from a previous call as long
5672 * as at least one event is still subscribed. The <event_type> must only be a
5673 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5674 */
5675static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5676{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005677 struct qcc *qcc = conn->qc->qcc;
5678
5679 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5680 BUG_ON(qcc->subs && qcc->subs != es);
5681
5682 es->events |= event_type;
5683 qcc->subs = es;
5684
5685 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005686 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005687
5688 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005689 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005690
5691 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005692}
5693
5694/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5695 * The <es> pointer is not allowed to differ from the one passed to the
5696 * subscribe() call. It always returns zero.
5697 */
5698static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5699{
5700 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5701}
5702
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005703/* Store in <xprt_ctx> the context attached to <conn>.
5704 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005705 */
5706static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5707{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005708 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005709
5710 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5711
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005712 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005713 if (*xprt_ctx)
5714 goto out;
5715
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005716 *xprt_ctx = conn->qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005717
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005718 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005719 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005720
5721 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005722}
5723
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005724/* Start the QUIC transport layer */
5725static int qc_xprt_start(struct connection *conn, void *ctx)
5726{
5727 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005728 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005729
5730 qc = conn->qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005731 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
5732 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
5733 return 0;
5734 }
5735
5736 /* mux-quic can now be considered ready. */
5737 qc->mux_state = QC_MUX_READY;
5738
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005739 tasklet_wakeup(qctx->wait_event.tasklet);
5740 return 1;
5741}
5742
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005743/* transport-layer operations for QUIC connections. */
5744static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005745 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005746 .subscribe = quic_conn_subscribe,
5747 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005748 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005749 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005750 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5751 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005752 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005753 .name = "QUIC",
5754};
5755
5756__attribute__((constructor))
5757static void __quic_conn_init(void)
5758{
5759 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5760 xprt_register(XPRT_QUIC, &ssl_quic);
5761}
5762
5763__attribute__((destructor))
5764static void __quic_conn_deinit(void)
5765{
5766 BIO_meth_free(ha_quic_meth);
5767}
5768
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005769/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5770 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005771 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005772 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005773struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005774{
5775 unsigned char *pos;
5776 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005777 struct quic_dghdlr *dghdlr = ctx;
5778 struct quic_dgram *dgram;
5779 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005780
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005781 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005782 pos = dgram->buf;
5783 end = pos + dgram->len;
5784 do {
5785 int ret;
5786 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005787
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005788 pkt = pool_zalloc(pool_head_quic_rx_packet);
5789 if (!pkt)
5790 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005791
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005792 quic_rx_packet_refinc(pkt);
5793 ret = qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
5794 first_pkt = 0;
5795 pos += pkt->len;
5796 quic_rx_packet_refdec(pkt);
5797 if (ret == -1)
5798 /* If the packet length could not be found, we cannot continue. */
5799 break;
5800 } while (pos < end);
5801
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005802 /* Increasing the received bytes counter by the UDP datagram length
5803 * if this datagram could be associated to a connection.
5804 */
5805 if (dgram->qc)
5806 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01005807
5808 /* Mark this datagram as consumed */
5809 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01005810 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005811
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005812 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005813
5814 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005815 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005816}
5817
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005818/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
5819 * Returns 1 if succeeded, 0 if not.
5820 */
5821static int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
5822 unsigned char **dcid, size_t *dcid_len)
5823{
5824 int long_header;
5825 size_t minlen, skip;
5826
5827 if (!(*buf & QUIC_PACKET_FIXED_BIT))
5828 goto err;
5829
5830 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
5831 minlen = long_header ?
5832 QUIC_LONG_PACKET_MINLEN : QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN;
5833 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01005834 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005835 goto err;
5836
5837 buf += skip;
5838 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
5839 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
5840 goto err;
5841
5842 *dcid = buf;
5843
5844 return 1;
5845
5846 err:
5847 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
5848 return 0;
5849}
5850
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005851/* Retrieve the DCID from the datagram found in <buf> and deliver it to the
5852 * correct datagram handler.
5853 * Return 1 if a correct datagram could be found, 0 if not.
5854 */
5855int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner,
5856 struct sockaddr_storage *saddr,
5857 struct quic_dgram *new_dgram, struct list *dgrams)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005858{
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005859 struct quic_dgram *dgram;
5860 unsigned char *dcid;
5861 size_t dcid_len;
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005862 int cid_tid;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005863
5864 if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len))
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005865 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005866
Frédéric Lécaille37ae5052022-01-27 11:31:50 +01005867 dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005868 if (!dgram)
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005869 goto err;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005870
Amaury Denoyellef6dcbce2022-02-08 15:05:58 +01005871 cid_tid = quic_get_cid_tid(dcid);
5872
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005873 /* All the members must be initialized! */
5874 dgram->owner = owner;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005875 dgram->buf = buf;
5876 dgram->len = len;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005877 dgram->dcid = dcid;
5878 dgram->dcid_len = dcid_len;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005879 dgram->saddr = *saddr;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005880 dgram->qc = NULL;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005881 LIST_APPEND(dgrams, &dgram->list);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005882 MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005883
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005884 tasklet_wakeup(quic_dghdlrs[cid_tid].task);
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005885
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005886 return 1;
5887
5888 err:
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01005889 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005890}
5891
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005892/* Allocate a new stream descriptor with id <id>. The caller is responsible to
5893 * store the stream in the appropriate tree.
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005894 *
5895 * Returns the newly allocated instance on success or else NULL.
5896 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005897struct qc_stream_desc *qc_stream_desc_new(uint64_t id, void *ctx)
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005898{
5899 struct qc_stream_desc *stream;
5900
5901 stream = pool_alloc(pool_head_quic_conn_stream);
5902 if (!stream)
5903 return NULL;
5904
5905 stream->by_id.key = id;
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005906 stream->by_id.node.leaf_p = NULL;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005907
5908 stream->buf = BUF_NULL;
5909 stream->acked_frms = EB_ROOT;
5910 stream->ack_offset = 0;
5911 stream->release = 0;
5912 stream->ctx = ctx;
5913
5914 return stream;
5915}
5916
5917/* Mark the stream descriptor <stream> as released by the upper layer. It will
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005918 * be freed as soon as all its buffered data are acknowledged. In the meantime,
5919 * the stream is stored in the <qc> tree : thus it must have been removed from
5920 * any other tree before calling this function.
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005921 */
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005922void qc_stream_desc_release(struct qc_stream_desc *stream,
5923 struct quic_conn *qc)
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005924{
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005925 BUG_ON(stream->by_id.node.leaf_p);
5926
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005927 stream->release = 1;
5928 stream->ctx = NULL;
5929
5930 if (!b_data(&stream->buf))
5931 qc_stream_desc_free(stream);
Amaury Denoyelled8e680c2022-03-29 15:18:44 +02005932 else
5933 eb64_insert(&qc->streams_by_id, &stream->by_id);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02005934}
5935
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02005936/* Notify the MUX layer if alive about an imminent close of <qc>. */
5937void qc_notify_close(struct quic_conn *qc)
5938{
5939 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
5940 return;
5941
5942 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
5943
5944 /* wake up the MUX */
5945 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
5946 qc->conn->mux->wake(qc->conn);
5947}
5948
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005949/* Function to automatically activate QUIC traces on stdout.
5950 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5951 * Main use for now is in the docker image for QUIC interop testing.
5952 */
5953static void quic_init_stdout_traces(void)
5954{
5955#ifdef ENABLE_QUIC_STDOUT_TRACES
5956 trace_quic.sink = sink_find("stdout");
5957 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005958 trace_quic.state = TRACE_STATE_RUNNING;
5959#endif
5960}
5961INITCALL0(STG_INIT, quic_init_stdout_traces);
5962
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005963/*
5964 * Local variables:
5965 * c-indent-level: 8
5966 * c-basic-offset: 8
5967 * End:
5968 */