blob: 9e2b066bdff265ea586497770ddb5174bc1c1236 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010015#include <stdio.h>
16#include <stdlib.h>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <netinet/tcp.h>
23
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020024#include <import/ebmbtree.h>
25
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010026#include <haproxy/buf-t.h>
27#include <haproxy/compat.h>
28#include <haproxy/api.h>
29#include <haproxy/debug.h>
30#include <haproxy/tools.h>
31#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010032
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010038#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010039#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010040#include <haproxy/mux_quic.h>
Amaury Denoyelle3db98e92022-05-13 15:41:04 +020041#include <haproxy/ncbuf.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écaille3ccea6d2022-05-23 22:54:54 +020048#include <haproxy/quic_stats.h>
Amaury Denoyelle0cc02a32022-04-19 17:21:11 +020049#include <haproxy/quic_stream.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020050#include <haproxy/quic_tp.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020051#include <haproxy/cbuf.h>
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +010052#include <haproxy/proto_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010053#include <haproxy/quic_tls.h>
54#include <haproxy/ssl_sock.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010055#include <haproxy/task.h>
56#include <haproxy/trace.h>
57#include <haproxy/xprt_quic.h>
58
Amaury Denoyellea22d8602021-11-10 15:17:56 +010059/* list of supported QUIC versions by this implementation */
Frédéric Lécaille301425b2022-06-14 17:40:39 +020060const struct quic_version quic_versions[] = {
Frédéric Lécaille86845c52022-06-08 19:28:36 +020061 {
62 .num = QUIC_PROTOCOL_VERSION_DRAFT_29,
63 .initial_salt = initial_salt_draft_29,
64 .initial_salt_len = sizeof initial_salt_draft_29,
65 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
66 .key_label_len = strlen(QUIC_HKDF_KEY_LABEL_V1),
67 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
68 .iv_label_len = strlen(QUIC_HKDF_IV_LABEL_V1),
69 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
70 .hp_label_len = strlen(QUIC_HKDF_HP_LABEL_V1),
71 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
72 .ku_label_len = strlen(QUIC_HKDF_KU_LABEL_V1),
73 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_DRAFT,
74 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_DRAFT,
75 },
76 {
77 .num = QUIC_PROTOCOL_VERSION_1,
78 .initial_salt = initial_salt_v1,
79 .initial_salt_len = sizeof initial_salt_v1,
80 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V1,
81 .key_label_len = strlen(QUIC_HKDF_KEY_LABEL_V1),
82 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
83 .iv_label_len = strlen(QUIC_HKDF_IV_LABEL_V1),
84 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
85 .hp_label_len = strlen(QUIC_HKDF_HP_LABEL_V1),
86 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
87 .ku_label_len = strlen(QUIC_HKDF_KU_LABEL_V1),
88 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V1,
89 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V1,
90 },
91 {
92 .num = QUIC_PROTOCOL_VERSION_2_DRAFT,
93 .initial_salt = initial_salt_v2_draft,
94 .initial_salt_len = sizeof initial_salt_v2_draft,
95 .key_label = (const unsigned char *)QUIC_HKDF_KEY_LABEL_V2,
96 .key_label_len = strlen(QUIC_HKDF_KEY_LABEL_V2),
97 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
98 .iv_label_len = strlen(QUIC_HKDF_IV_LABEL_V2),
99 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
100 .hp_label_len = strlen(QUIC_HKDF_HP_LABEL_V2),
101 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
102 .ku_label_len = strlen(QUIC_HKDF_KU_LABEL_V2),
103 .retry_tag_key = (const unsigned char *)QUIC_TLS_RETRY_KEY_V2_DRAFT,
104 .retry_tag_nonce = (const unsigned char *)QUIC_TLS_RETRY_NONCE_V2_DRAFT,
105 },
Amaury Denoyellea22d8602021-11-10 15:17:56 +0100106};
107
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200108/* The total number of supported versions */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200109const size_t quic_versions_nb = sizeof quic_versions / sizeof *quic_versions;
110/* Listener only preferred version */
111const struct quic_version *preferred_version;
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200112
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100113/* trace source and events */
114static void quic_trace(enum trace_level level, uint64_t mask, \
115 const struct trace_source *src,
116 const struct ist where, const struct ist func,
117 const void *a1, const void *a2, const void *a3, const void *a4);
118
119static const struct trace_event quic_trace_events[] = {
120 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
121 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
122 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
123 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
124 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
125 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
126 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500127 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100128 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
129 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
130 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100131 { .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 +0100132 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
133 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
134 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
135 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
136 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
137 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
138 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
139 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
140 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
141 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
142 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
143 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
144 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
145 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
146 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100147 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
148 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
149 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
150 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
151 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
152 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100153 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200154 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
155 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécailleba85acd2022-01-11 14:43:50 +0100156 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
157 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100158 { .mask = QUIC_EV_CONN_ACKSTRM, .name = "ack_strm", .desc = "STREAM ack."},
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200159 { .mask = QUIC_EV_CONN_FRMLIST, .name = "frm_list", .desc = "frame list"},
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200160 { .mask = QUIC_EV_STATELESS_RST, .name = "stateless_reset", .desc = "stateless reset sent"},
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200161 { .mask = QUIC_EV_TRANSP_PARAMS, .name = "transport_params", .desc = "transport parameters"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162 { /* end */ }
163};
164
165static const struct name_desc quic_trace_lockon_args[4] = {
166 /* arg1 */ { /* already used by the connection */ },
167 /* arg2 */ { .name="quic", .desc="QUIC transport" },
168 /* arg3 */ { },
169 /* arg4 */ { }
170};
171
172static const struct name_desc quic_trace_decoding[] = {
173#define QUIC_VERB_CLEAN 1
174 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
175 { /* end */ }
176};
177
178
179struct trace_source trace_quic = {
180 .name = IST("quic"),
181 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100182 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100183 .default_cb = quic_trace,
184 .known_events = quic_trace_events,
185 .lockon_args = quic_trace_lockon_args,
186 .decoding = quic_trace_decoding,
187 .report_events = ~0, /* report everything by default */
188};
189
190#define TRACE_SOURCE &trace_quic
191INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
192
193static BIO_METHOD *ha_quic_meth;
194
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200195DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100196DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100197DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200198 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100199DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100200DECLARE_POOL(pool_head_quic_connection_id,
201 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +0100202DECLARE_POOL(pool_head_quic_dgram, "quic_dgram", sizeof(struct quic_dgram));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100203DECLARE_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 +0100204DECLARE_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 +0100205DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100206DECLARE_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 +0200207DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100208DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200210static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200211 struct quic_enc_level *qel, struct quic_tls_ctx *ctx,
212 struct list *frms, struct quic_conn *qc,
213 const struct quic_version *ver, size_t dglen, int pkt_type,
Frédéric Lécailleb0021452022-03-29 11:42:03 +0200214 int padding, int probe, int cc, int *err);
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100215static 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 +0100216static void qc_idle_timer_do_rearm(struct quic_conn *qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +0100217static void qc_idle_timer_rearm(struct quic_conn *qc, int read);
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +0200218static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc);
219static int quic_conn_init_timer(struct quic_conn *qc);
220static int quic_conn_init_idle_timer_task(struct quic_conn *qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100221
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100222/* Only for debug purpose */
223struct enc_debug_info {
224 unsigned char *payload;
225 size_t payload_len;
226 unsigned char *aad;
227 size_t aad_len;
228 uint64_t pn;
229};
230
231/* Initializes a enc_debug_info struct (only for debug purpose) */
232static inline void enc_debug_info_init(struct enc_debug_info *edi,
233 unsigned char *payload, size_t payload_len,
234 unsigned char *aad, size_t aad_len, uint64_t pn)
235{
236 edi->payload = payload;
237 edi->payload_len = payload_len;
238 edi->aad = aad;
239 edi->aad_len = aad_len;
240 edi->pn = pn;
241}
242
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100243/* Trace callback for QUIC.
244 * These traces always expect that arg1, if non-null, is of type connection.
245 */
246static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
247 const struct ist where, const struct ist func,
248 const void *a1, const void *a2, const void *a3, const void *a4)
249{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100250 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100251
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100252 if (qc) {
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100253 const struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100254
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100255 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100256 if ((mask & QUIC_EV_CONN_INIT) && qc) {
257 chunk_appendf(&trace_buf, "\n odcid");
258 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100259 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100260 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100261 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100262 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100263 }
264
Frédéric Lécaillec7785b52022-05-23 09:08:54 +0200265 if (mask & QUIC_EV_TRANSP_PARAMS) {
266 const struct quic_transport_params *p = a2;
267 quic_transport_params_dump(&trace_buf, p);
268 }
269
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100270 if (mask & QUIC_EV_CONN_ADDDATA) {
271 const enum ssl_encryption_level_t *level = a2;
272 const size_t *len = a3;
273
274 if (level) {
275 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
276
277 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
278 }
279 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100280 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100281 }
282 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
283 /* Initial read & write secrets. */
284 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
285 const unsigned char *rx_sec = a2;
286 const unsigned char *tx_sec = a3;
287
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100288 tls_ctx = &qc->els[level].tls_ctx;
289 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
291 if (rx_sec)
292 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100293 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100294 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
295 if (tx_sec)
296 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100297 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100298 }
299 }
300 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
301 const enum ssl_encryption_level_t *level = a2;
302 const unsigned char *secret = a3;
303 const size_t *secret_len = a4;
304
305 if (level) {
306 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
307
308 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
309 if (secret && secret_len)
310 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100311 tls_ctx = &qc->els[lvl].tls_ctx;
312 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
313 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->rx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100314 }
315 }
316
317 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
318 const enum ssl_encryption_level_t *level = a2;
319 const unsigned char *secret = a3;
320 const size_t *secret_len = a4;
321
322 if (level) {
323 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
324
325 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
326 if (secret && secret_len)
327 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100328 tls_ctx = &qc->els[lvl].tls_ctx;
329 if (tls_ctx->flags & QUIC_FL_TLS_SECRETS_SET)
330 quic_tls_keys_hexdump(&trace_buf, &tls_ctx->tx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 }
332
333 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100334
Frédéric Lécailleb823bb72022-03-31 20:26:18 +0200335 if (mask & QUIC_EV_CONN_FRMLIST) {
336 const struct list *l = a2;
337
338 if (l) {
339 const struct quic_frame *frm;
340 list_for_each_entry(frm, l, list)
341 chunk_frm_appendf(&trace_buf, frm);
342 }
343 }
344
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100345 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100346 const struct quic_tx_packet *pkt = a2;
347 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100348 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100349
350 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100351 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100352 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100353 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100354 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100355 (unsigned long long)qc->path->cwnd,
356 (unsigned long long)qc->path->prep_in_flight,
357 (unsigned long long)qc->path->in_flight,
358 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100359 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100360 }
361 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200362 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100363 if (pkt->pn_node.key != (uint64_t)-1)
364 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100365 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100366 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100367 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
368 (unsigned long long)qc->rx.bytes,
369 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100370 }
371
372 if (room) {
373 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
374 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
375 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100376 }
377 }
378
Frédéric Lécaille00e24002022-02-18 17:13:45 +0100379 if (mask & QUIC_EV_CONN_IO_CB) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100380 const enum quic_handshake_state *state = a2;
381 const int *err = a3;
382
383 if (state)
384 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
385 if (err)
386 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
387 }
388
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100389 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100390 const struct quic_rx_packet *pkt = a2;
391 const unsigned long *pktlen = a3;
392 const SSL *ssl = a4;
393
394 if (pkt) {
Frédéric Lécaille3dfd4c42022-04-05 15:29:14 +0200395 chunk_appendf(&trace_buf, " pkt@%p", pkt);
396 if (pkt->type == QUIC_PACKET_TYPE_SHORT && pkt->data)
397 chunk_appendf(&trace_buf, " kp=%d",
398 !!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT));
399 chunk_appendf(&trace_buf, " el=%c",
400 quic_packet_type_enc_level_char(pkt->type));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401 if (pkt->pnl)
402 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
403 (unsigned long long)pkt->pn);
404 if (pkt->token_len)
405 chunk_appendf(&trace_buf, " toklen=%llu",
406 (unsigned long long)pkt->token_len);
407 if (pkt->aad_len)
408 chunk_appendf(&trace_buf, " aadlen=%llu",
409 (unsigned long long)pkt->aad_len);
410 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
411 pkt->flags, (unsigned long long)pkt->len);
412 }
413 if (pktlen)
414 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
415 if (ssl) {
416 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
417 chunk_appendf(&trace_buf, " el=%c",
418 quic_enc_level_char(ssl_to_quic_enc_level(level)));
419 }
420 }
421
422 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
423 const struct quic_rx_packet *pkt = a2;
424 const struct quic_rx_crypto_frm *cf = a3;
425 const SSL *ssl = a4;
426
427 if (pkt)
428 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
429 quic_packet_type_enc_level_char(pkt->type),
430 (unsigned long long)pkt->pn);
431 if (cf)
432 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
433 (unsigned long long)cf->offset_node.key,
434 (unsigned long long)cf->len);
435 if (ssl) {
436 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200437 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100438 quic_enc_level_char(ssl_to_quic_enc_level(level)));
439 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100440
441 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100442 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100443 }
444
445 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
446 const struct quic_frame *frm = a2;
447
448 if (frm)
449 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
450 }
451
452 if (mask & QUIC_EV_CONN_PHPKTS) {
453 const struct quic_enc_level *qel = a2;
454
455 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100456 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100457 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100458 " 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 +0100459 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200460 quic_hdshk_state_str(qc->state),
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200461 !!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100462 (unsigned long long)qc->path->cwnd,
463 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100464 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100465 (unsigned long long)pktns->tx.in_flight,
466 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100467 }
468 }
469
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100470 if (mask & QUIC_EV_CONN_ENCPKT) {
471 const struct enc_debug_info *edi = a2;
472
473 if (edi)
474 chunk_appendf(&trace_buf,
475 " payload=@%p payload_len=%llu"
476 " aad=@%p aad_len=%llu pn=%llu",
477 edi->payload, (unsigned long long)edi->payload_len,
478 edi->aad, (unsigned long long)edi->aad_len,
479 (unsigned long long)edi->pn);
480 }
481
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100482 if (mask & QUIC_EV_CONN_RMHP) {
483 const struct quic_rx_packet *pkt = a2;
484
485 if (pkt) {
486 const int *ret = a3;
487
488 chunk_appendf(&trace_buf, " pkt@%p", pkt);
489 if (ret && *ret)
490 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
491 pkt->pnl, (unsigned long long)pkt->pn);
492 }
493 }
494
495 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200496 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497 const unsigned long *val1 = a3;
498 const unsigned long *val2 = a4;
499
500 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100501 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100502 if (val1)
503 chunk_appendf(&trace_buf, " %lu", *val1);
504 if (val2)
505 chunk_appendf(&trace_buf, "..%lu", *val2);
506 }
507
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100508 if (mask & QUIC_EV_CONN_ACKSTRM) {
509 const struct quic_stream *s = a2;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200510 const struct qc_stream_desc *stream = a3;
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100511
512 if (s)
513 chunk_appendf(&trace_buf, " off=%llu len=%llu", (ull)s->offset.key, (ull)s->len);
Amaury Denoyelle7272cd72022-03-29 15:15:54 +0200514 if (stream)
515 chunk_appendf(&trace_buf, " ack_offset=%llu", (ull)stream->ack_offset);
Frédéric Lécaillece69cbc2022-03-22 12:45:33 +0100516 }
517
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100518 if (mask & QUIC_EV_CONN_RTTUPDT) {
519 const unsigned int *rtt_sample = a2;
520 const unsigned int *ack_delay = a3;
521 const struct quic_loss *ql = a4;
522
523 if (rtt_sample)
524 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
525 if (ack_delay)
526 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
527 if (ql)
528 chunk_appendf(&trace_buf,
529 " srtt=%ums rttvar=%ums min_rtt=%ums",
530 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
531 }
532 if (mask & QUIC_EV_CONN_CC) {
533 const struct quic_cc_event *ev = a2;
534 const struct quic_cc *cc = a3;
535
536 if (a2)
537 quic_cc_event_trace(&trace_buf, ev);
538 if (a3)
539 quic_cc_state_trace(&trace_buf, cc);
540 }
541
542 if (mask & QUIC_EV_CONN_PKTLOSS) {
543 const struct quic_pktns *pktns = a2;
544 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100545
546 if (pktns) {
547 chunk_appendf(&trace_buf, " pktns=%s",
548 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
549 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
550 if (pktns->tx.loss_time)
551 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100552 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100553 }
554 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
555 struct quic_tx_packet *pkt;
556
557 chunk_appendf(&trace_buf, " lost_pkts:");
558 list_for_each_entry(pkt, lost_pkts, list)
559 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
560 }
561 }
562
563 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100564 const struct quic_pktns *pktns = a2;
565 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100566 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100567
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100568 if (ifae_pkts)
569 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
570 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100571 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100572 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100573 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100574 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
575 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100576 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
577 if (pktns->tx.in_flight)
578 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100579 if (pktns->tx.loss_time)
580 chunk_appendf(&trace_buf, " loss_time=%dms",
581 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
582 }
583 if (mask & QUIC_EV_CONN_SPTO) {
584 if (pktns->tx.time_of_last_eliciting)
585 chunk_appendf(&trace_buf, " tole=%dms",
586 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
587 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100588 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100589 }
590 }
591
Frédéric Lécaille03235d72022-03-30 14:36:40 +0200592 if (!(mask & (QUIC_EV_CONN_SPTO|QUIC_EV_CONN_PTIMER)) && qc->timer_task) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593 chunk_appendf(&trace_buf,
594 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
595 }
596 }
597
598 if (mask & QUIC_EV_CONN_SPPKTS) {
599 const struct quic_tx_packet *pkt = a2;
600
Frédéric Lécaille8726d632022-05-03 10:32:21 +0200601 chunk_appendf(&trace_buf, " err=%u cwnd=%llu ppif=%llu pif=%llu", qc->sendto_err,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100602 (unsigned long long)qc->path->cwnd,
603 (unsigned long long)qc->path->prep_in_flight,
604 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100605 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100606 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100607 (unsigned long)pkt->pn_node.key,
608 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
609 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100610 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611 }
612 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100613
614 if (mask & QUIC_EV_CONN_SSLALERT) {
615 const uint8_t *alert = a2;
616 const enum ssl_encryption_level_t *level = a3;
617
618 if (alert)
619 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
620 if (level)
621 chunk_appendf(&trace_buf, " el=%c",
622 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
623 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100624
625 if (mask & QUIC_EV_CONN_BCFRMS) {
626 const size_t *sz1 = a2;
627 const size_t *sz2 = a3;
628 const size_t *sz3 = a4;
629
630 if (sz1)
631 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
632 if (sz2)
633 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
634 if (sz3)
635 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
636 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100637
638 if (mask & QUIC_EV_CONN_PSTRM) {
639 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100640
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100641 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100642 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100643 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100644 }
645 if (mask & QUIC_EV_CONN_LPKT) {
646 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200647 const uint64_t *len = a3;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200648 const struct quic_version *ver = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100649
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100650 if (pkt) {
651 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
652 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
653 if (pkt->pn_node.key != (uint64_t)-1)
654 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
655 }
656
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200657 if (len)
658 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200659
660 if (ver)
661 chunk_appendf(&trace_buf, " ver=0x%08x", ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100662 }
663
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +0200664 if (mask & QUIC_EV_STATELESS_RST) {
665 const struct quic_cid *cid = a2;
666
667 if (cid)
668 quic_cid_dump(&trace_buf, cid);
669 }
670
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100671}
672
673/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100674static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100675{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200676 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100677
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100678 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100679 return 1;
680
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200681 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
682 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +0200683 if ((hdshk_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
684 (app_pktns->flags & QUIC_FL_PKTNS_PKT_RECEIVED) ||
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200685 qc->state >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100686 return 1;
687
688 return 0;
689}
690
691/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
692 * both loss detection and PTO and schedule the task assiated to this timer if needed.
693 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100694static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100695{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100696 struct quic_pktns *pktns;
697 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200698 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100699
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100700 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100701 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100702
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100703 pktns = quic_loss_pktns(qc);
704 if (tick_isset(pktns->tx.loss_time)) {
705 qc->timer = pktns->tx.loss_time;
706 goto out;
707 }
708
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100709 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100710 * cancelled for a server which reached the anti-amplification limit.
711 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100712 if (!quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200713 (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100714 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100715 qc->timer = TICK_ETERNITY;
716 goto out;
717 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100718
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100719 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100720 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100721 /* Timer cancellation. */
722 qc->timer = TICK_ETERNITY;
723 goto out;
724 }
725
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200726 handshake_complete = qc->state >= QUIC_HS_ST_COMPLETE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200727 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100728 if (tick_isset(pto))
729 qc->timer = pto;
730 out:
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100731 if (qc->timer_task && qc->timer != TICK_ETERNITY) {
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100732 if (tick_is_expired(qc->timer, now_ms)) {
733 TRACE_PROTO("wakeup asap timer task", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100734 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100735 }
736 else {
737 TRACE_PROTO("timer task scheduling", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100738 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécailleaaf1f192022-03-22 15:37:41 +0100739 }
Frédéric Lécaille5757b4a2022-02-16 14:46:17 +0100740 }
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100741 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100742}
743
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100744/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
745 * connection.
746 * Return 1 if succeeded, 0 if not.
747 */
748static int quic_tls_key_update(struct quic_conn *qc)
749{
750 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
751 struct quic_tls_secrets *rx, *tx;
752 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
753 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200754 const struct quic_version *ver =
755 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100756
757 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
758 rx = &tls_ctx->rx;
759 tx = &tls_ctx->tx;
760 nxt_rx = &qc->ku.nxt_rx;
761 nxt_tx = &qc->ku.nxt_tx;
762
763 /* Prepare new RX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200764 if (!quic_tls_sec_update(rx->md, ver, nxt_rx->secret, nxt_rx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100765 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100766 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100767 return 0;
768 }
769
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200770 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100771 nxt_rx->key, nxt_rx->keylen,
772 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
773 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100774 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100775 return 0;
776 }
777
778 /* Prepare new TX secrets */
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200779 if (!quic_tls_sec_update(tx->md, ver, nxt_tx->secret, nxt_tx->secretlen,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100780 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100781 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100782 return 0;
783 }
784
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200785 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md, ver,
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100786 nxt_tx->key, nxt_tx->keylen,
787 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
788 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100789 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100790 return 0;
791 }
792
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200793 if (nxt_rx->ctx) {
794 EVP_CIPHER_CTX_free(nxt_rx->ctx);
795 nxt_rx->ctx = NULL;
796 }
797
798 if (!quic_tls_rx_ctx_init(&nxt_rx->ctx, tls_ctx->rx.aead, nxt_rx->key)) {
799 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
800 return 0;
801 }
802
803 if (nxt_tx->ctx) {
804 EVP_CIPHER_CTX_free(nxt_tx->ctx);
805 nxt_tx->ctx = NULL;
806 }
807
808 if (!quic_tls_rx_ctx_init(&nxt_tx->ctx, tls_ctx->tx.aead, nxt_tx->key)) {
809 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
810 return 0;
811 }
812
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100813 return 1;
814}
815
816/* Rotate the Key Update information for <qc> QUIC connection.
817 * Must be used after having updated them.
818 * Always succeeds.
819 */
820static void quic_tls_rotate_keys(struct quic_conn *qc)
821{
822 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
823 unsigned char *curr_secret, *curr_iv, *curr_key;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200824 EVP_CIPHER_CTX *curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100825
826 /* Rotate the RX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200827 curr_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100828 curr_secret = tls_ctx->rx.secret;
829 curr_iv = tls_ctx->rx.iv;
830 curr_key = tls_ctx->rx.key;
831
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200832 tls_ctx->rx.ctx = qc->ku.nxt_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100833 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
834 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
835 tls_ctx->rx.key = qc->ku.nxt_rx.key;
836
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200837 qc->ku.nxt_rx.ctx = qc->ku.prv_rx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100838 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
839 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
840 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
841
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200842 qc->ku.prv_rx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100843 qc->ku.prv_rx.secret = curr_secret;
844 qc->ku.prv_rx.iv = curr_iv;
845 qc->ku.prv_rx.key = curr_key;
846 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
847
848 /* Update the TX secrets */
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200849 curr_ctx = tls_ctx->tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100850 curr_secret = tls_ctx->tx.secret;
851 curr_iv = tls_ctx->tx.iv;
852 curr_key = tls_ctx->tx.key;
853
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200854 tls_ctx->tx.ctx = qc->ku.nxt_tx.ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100855 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
856 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
857 tls_ctx->tx.key = qc->ku.nxt_tx.key;
858
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +0200859 qc->ku.nxt_tx.ctx = curr_ctx;
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100860 qc->ku.nxt_tx.secret = curr_secret;
861 qc->ku.nxt_tx.iv = curr_iv;
862 qc->ku.nxt_tx.key = curr_key;
863}
864
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100865int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
866 const uint8_t *read_secret,
867 const uint8_t *write_secret, size_t secret_len)
868{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100869 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
870 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 +0100871 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100872 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200873 const struct quic_version *ver =
874 qc->negotiated_version ? qc->negotiated_version : qc->original_version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100875
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100876 TRACE_ENTER(QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100877 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaillefc790062022-03-28 17:10:31 +0200878 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100879 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100880 goto no_secret;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100881 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100882
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100883 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100884 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100885 goto err;
886 }
887
888 rx = &tls_ctx->rx;
889 tx = &tls_ctx->tx;
890
891 rx->aead = tx->aead = tls_aead(cipher);
892 rx->md = tx->md = tls_md(cipher);
893 rx->hp = tx->hp = tls_hp(cipher);
894
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200895 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, ver, rx->key, rx->keylen,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100896 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897 read_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100898 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100899 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100900 }
901
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200902 if (!quic_tls_rx_ctx_init(&rx->ctx, rx->aead, rx->key)) {
903 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
904 goto err;
905 }
906
Frédéric Lécaille61b851d2022-01-28 21:38:45 +0100907 /* Enqueue this connection asap if we could derive O-RTT secrets as
908 * listener. Note that a listener derives only RX secrets for this
909 * level.
910 */
911 if (qc_is_listener(qc) && level == ssl_encryption_early_data)
912 quic_accept_push_qc(qc);
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100913
914 if (!write_secret)
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100915 goto out;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100916
Frédéric Lécaille301425b2022-06-14 17:40:39 +0200917 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, ver, tx->key, tx->keylen,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100918 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100919 write_secret, secret_len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100920 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100921 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922 }
923
Frédéric Lécaillef4605742022-04-05 10:28:29 +0200924 if (!quic_tls_tx_ctx_init(&tx->ctx, tx->aead, tx->key)) {
925 TRACE_DEVEL("could not initial RX TLS cipher context", QUIC_EV_CONN_RWSEC, qc);
926 goto err;
927 }
928
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100929 if (level == ssl_encryption_application) {
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100930 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
931 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
932 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
933
Frédéric Lécaille96fd1632022-04-01 11:21:47 +0200934 /* These secrets must be stored only for Application encryption level */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100935 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
936 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100937 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100938 goto err;
939 }
940
941 memcpy(rx->secret, read_secret, secret_len);
942 rx->secretlen = secret_len;
943 memcpy(tx->secret, write_secret, secret_len);
944 tx->secretlen = secret_len;
945 /* Initialize all the secret keys lengths */
946 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
947 /* Prepare the next key update */
948 if (!quic_tls_key_update(qc))
949 goto err;
950 }
Frédéric Lécailleee4508d2022-02-14 17:54:04 +0100951
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100952 out:
Frédéric Lécaillebd242082022-02-25 17:17:59 +0100953 tls_ctx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaillef44d19e2022-03-26 12:22:41 +0100954 no_secret:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100955 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100956 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100957
958 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100959 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100960 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100961}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100962
963/* This function copies the CRYPTO data provided by the TLS stack found at <data>
964 * with <len> as size in CRYPTO buffers dedicated to store the information about
965 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
966 * It fails only if it could not managed to allocate enough CRYPTO buffers to
967 * store all the data.
968 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
969 */
970static int quic_crypto_data_cpy(struct quic_enc_level *qel,
971 const unsigned char *data, size_t len)
972{
973 struct quic_crypto_buf **qcb;
974 /* The remaining byte to store in CRYPTO buffers. */
975 size_t cf_offset, cf_len, *nb_buf;
976 unsigned char *pos;
977
978 nb_buf = &qel->tx.crypto.nb_buf;
979 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
980 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
981 cf_len = len;
982
983 while (len) {
984 size_t to_copy, room;
985
986 pos = (*qcb)->data + (*qcb)->sz;
987 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
988 to_copy = len > room ? room : len;
989 if (to_copy) {
990 memcpy(pos, data, to_copy);
991 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
992 qel->tx.crypto.sz += to_copy;
993 (*qcb)->sz += to_copy;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100994 len -= to_copy;
995 data += to_copy;
996 }
997 else {
998 struct quic_crypto_buf **tmp;
999
1000 tmp = realloc(qel->tx.crypto.bufs,
1001 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
1002 if (tmp) {
1003 qel->tx.crypto.bufs = tmp;
1004 qcb = &qel->tx.crypto.bufs[*nb_buf];
1005 *qcb = pool_alloc(pool_head_quic_crypto_buf);
1006 if (!*qcb)
1007 return 0;
1008
1009 (*qcb)->sz = 0;
1010 ++*nb_buf;
1011 }
1012 else {
1013 break;
1014 }
1015 }
1016 }
1017
1018 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
1019 * have been buffered.
1020 */
1021 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001022 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001023 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001024
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001025 /* There is at most one CRYPTO frame in this packet number
1026 * space. Let's look for it.
1027 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001028 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001029 if (frm->type != QUIC_FT_CRYPTO)
1030 continue;
1031
1032 /* Found */
1033 found = frm;
1034 break;
1035 }
1036
1037 if (found) {
1038 found->crypto.len += cf_len;
1039 }
1040 else {
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001041 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001042 if (!frm)
1043 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001044
Frédéric Lécailleb9171912022-04-21 17:32:10 +02001045 LIST_INIT(&frm->reflist);
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
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001057/* Prepare the emission of CONNECTION_CLOSE with error <err>. All send/receive
1058 * activity for <qc> will be interrupted.
1059 */
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02001060void quic_set_connection_close(struct quic_conn *qc, int err, int app)
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001061{
1062 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE)
1063 return;
1064
1065 qc->err_code = err;
1066 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02001067
1068 if (app)
1069 qc->flags |= QUIC_FL_CONN_APP_ALERT;
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001070}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001071
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001072/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1073void quic_set_tls_alert(struct quic_conn *qc, int alert)
1074{
Frédéric Lécailleeb791452022-05-24 16:01:39 +02001075 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02001076 quic_set_connection_close(qc, QC_ERR_CRYPTO_ERROR | alert, 0);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001077 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001078 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001079}
1080
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001081/* Set the application for <qc> QUIC connection.
1082 * Return 1 if succeeded, 0 if not.
1083 */
1084int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1085{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001086 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1087 qc->app_ops = &h3_ops;
1088 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1089 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001090 else
1091 return 0;
1092
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001093 return 1;
1094}
1095
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1097 * wants to provide the QUIC layer with CRYPTO data.
1098 * Returns 1 if succeeded, 0 if not.
1099 */
1100int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1101 const uint8_t *data, size_t len)
1102{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001103 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001104 enum quic_tls_enc_level tel;
1105 struct quic_enc_level *qel;
1106
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001107 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1108 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001109 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001110 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001111 goto out;
1112 }
1113
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001114 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001115 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001116 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117 goto err;
1118 }
1119
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001120 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001121 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001122 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001123 goto err;
1124 }
1125
1126 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001127 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001128
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001129 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001130 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001131 return 1;
1132
1133 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001134 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001135 return 0;
1136}
1137
1138int ha_quic_flush_flight(SSL *ssl)
1139{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001140 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001141
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001142 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1143 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144
1145 return 1;
1146}
1147
1148int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1149{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001150 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001151
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001152 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1153 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001154 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155 return 1;
1156}
1157
1158/* QUIC TLS methods */
1159static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001160 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001161 .add_handshake_data = ha_quic_add_handshake_data,
1162 .flush_flight = ha_quic_flush_flight,
1163 .send_alert = ha_quic_send_alert,
1164};
1165
1166/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1167 * Returns an error count.
1168 */
1169int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1170{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1172 int cfgerr = 0;
1173
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);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001184 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1185 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1186 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001187
1188#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaillee06f7452022-06-16 15:06:44 +02001189#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001190 if (bind_conf->ssl_conf.early_data) {
1191 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001192 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001193 }
1194 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1195 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1196#else
1197 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1198#endif
1199 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1200#endif
1201 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1202
1203 return cfgerr;
1204}
1205
1206/* Decode an expected packet number from <truncated_on> its truncated value,
1207 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1208 * the number of bits used to encode this packet number (its length in bytes * 8).
1209 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1210 */
1211static uint64_t decode_packet_number(uint64_t largest_pn,
1212 uint32_t truncated_pn, unsigned int pn_nbits)
1213{
1214 uint64_t expected_pn = largest_pn + 1;
1215 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1216 uint64_t pn_hwin = pn_win / 2;
1217 uint64_t pn_mask = pn_win - 1;
1218 uint64_t candidate_pn;
1219
1220
1221 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1222 /* Note that <pn_win> > <pn_hwin>. */
1223 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1224 candidate_pn + pn_hwin <= expected_pn)
1225 return candidate_pn + pn_win;
1226
1227 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1228 return candidate_pn - pn_win;
1229
1230 return candidate_pn;
1231}
1232
1233/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1234 * cryptographic context.
1235 * <largest_pn> is the largest received packet number and <pn> the address of
1236 * the packet number field for this packet with <byte0> address of its first byte.
1237 * <end> points to one byte past the end of this packet.
1238 * Returns 1 if succeeded, 0 if not.
1239 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001240static int qc_do_rm_hp(struct quic_conn *qc,
1241 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001242 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001243 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001244{
1245 int ret, outlen, i, pnlen;
1246 uint64_t packet_number;
1247 uint32_t truncated_pn = 0;
1248 unsigned char mask[5] = {0};
1249 unsigned char *sample;
1250 EVP_CIPHER_CTX *cctx;
1251 unsigned char *hp_key;
1252
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253 /* Check there is enough data in this packet. */
1254 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001255 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001256 return 0;
1257 }
1258
1259 cctx = EVP_CIPHER_CTX_new();
1260 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001261 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001262 return 0;
1263 }
1264
1265 ret = 0;
1266 sample = pn + QUIC_PACKET_PN_MAXLEN;
1267
1268 hp_key = tls_ctx->rx.hp_key;
1269 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1270 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1271 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001272 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001273 goto out;
1274 }
1275
1276 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1277 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1278 for (i = 0; i < pnlen; i++) {
1279 pn[i] ^= mask[i + 1];
1280 truncated_pn = (truncated_pn << 8) | pn[i];
1281 }
1282
1283 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1284 /* Store remaining information for this unprotected header */
1285 pkt->pn = packet_number;
1286 pkt->pnl = pnlen;
1287
1288 ret = 1;
1289
1290 out:
1291 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001292
1293 return ret;
1294}
1295
1296/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1297 * address, with <payload_len> as payload length, <aad> as address of
1298 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1299 * context.
1300 * Returns 1 if succeeded, 0 if not.
1301 */
1302static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1303 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001304 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001305{
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001306 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001308 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001309 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001310
1311 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001312 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001313 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314 }
1315
1316 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001317 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001318 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001319 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001320 }
1321
1322 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001323
1324 err:
1325 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001326 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001327 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001328}
1329
1330/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1331 * Returns 1 if succeeded, 0 if not.
1332 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001333static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001334{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001335 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001336 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001337 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001338 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001339 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001340 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1341 unsigned char *rx_key = tls_ctx->rx.key;
1342
1343 kp_changed = 0;
1344 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1345 /* The two tested bits are not at the same position,
1346 * this is why they are first both inversed.
1347 */
1348 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1349 if (pkt->pn < tls_ctx->rx.pn) {
1350 /* The lowest packet number of a previous key phase
1351 * cannot be null if it really stores previous key phase
1352 * secrets.
1353 */
1354 if (!pkt->qc->ku.prv_rx.pn)
1355 return 0;
1356
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001357 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1358 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001359 rx_key = pkt->qc->ku.prv_rx.key;
1360 }
1361 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1362 /* Next key phase */
1363 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001364 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1365 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001366 rx_key = pkt->qc->ku.nxt_rx.key;
1367 }
1368 }
1369 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001370
1371 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1372 return 0;
1373
1374 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1375 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001376 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001377 if (!ret)
1378 return 0;
1379
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001380 /* Update the keys only if the packet decryption succeeded. */
1381 if (kp_changed) {
1382 quic_tls_rotate_keys(pkt->qc);
1383 /* Toggle the Key Phase bit */
1384 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1385 /* Store the lowest packet number received for the current key phase */
1386 tls_ctx->rx.pn = pkt->pn;
1387 /* Prepare the next key update */
1388 if (!quic_tls_key_update(pkt->qc))
1389 return 0;
1390 }
1391
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001392 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001393 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001394
1395 return 1;
1396}
1397
Frédéric Lécaille96367152022-04-25 09:40:19 +02001398
1399/* Remove references to <frm> frame */
1400static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1401{
1402 uint64_t pn;
1403 struct quic_frame *f, *tmp;
1404
1405 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1406 pn = f->pkt->pn_node.key;
1407 f->origin = NULL;
1408 LIST_DELETE(&f->ref);
1409 TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1410 }
1411}
1412
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001413/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001414void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001415{
1416 uint64_t pn;
1417 struct quic_frame *origin, *f, *tmp;
1418
1419 /* Identify this frame: a frame copy or one of its copies */
1420 origin = frm->origin ? frm->origin : frm;
1421 /* Ensure the source of the copies is flagged as acked, <frm> being
1422 * possibly a copy of <origin>
1423 */
1424 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1425 /* Mark all the copy of <origin> as acknowledged. We must
1426 * not release the packets (releasing the frames) at this time as
1427 * they are possibly also to be acknowledged alongside the
1428 * the current one.
1429 */
1430 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1431 pn = f->pkt->pn_node.key;
1432 TRACE_PROTO("mark frame as acked from packet",
1433 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1434 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1435 f->origin = NULL;
1436 LIST_DELETE(&f->ref);
1437 }
1438 LIST_DELETE(&frm->list);
1439 pn = frm->pkt->pn_node.key;
1440 quic_tx_packet_refdec(frm->pkt);
1441 TRACE_PROTO("freeing frame from packet",
1442 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1443 pool_free(pool_head_quic_frame, frm);
1444}
1445
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001446/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001447 *
1448 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001449 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001450static int quic_stream_try_to_consume(struct quic_conn *qc,
1451 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001452{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001453 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001454 struct eb64_node *frm_node;
1455
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001456 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001457 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001458 while (frm_node) {
1459 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001460 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001461 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001462
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001463 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001464 offset = strm->offset.key;
1465 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001466
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001467 if (offset > stream->ack_offset)
1468 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001469
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001470 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001471 /* cf. next comment : frame may be freed at this stage. */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001472 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001473 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001474 ret = 1;
1475 }
1476
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001477 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1478 * has been freed. with the stream frames tree. Nothing to do
1479 * anymore in here.
1480 */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001481 if (!stream)
1482 return 1;
1483
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001484 frm_node = eb64_next(frm_node);
1485 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001486
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001487 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001488 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001489 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001490
1491 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001492}
1493
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001494/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001495static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1496 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001497{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001498 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001499
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001500 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001501 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001502 switch (frm->type) {
1503 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1504 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001505 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001506 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001507 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001508 const size_t offset = strm_frm->offset.key;
1509 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001510
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001511 /* do not use strm_frm->stream as the qc_stream_desc instance
1512 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001513 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001514 *
1515 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001516 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001517 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001518 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1519 if (!node) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001520 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001521 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001522 /* early return */
1523 return;
1524 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001525 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001526
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001527 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001528 if (offset <= stream->ack_offset) {
1529 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001530 stream_acked = 1;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001531 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1532 qc, strm_frm, stream);
1533 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001534
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001535 if (!stream) {
1536 /* no need to continue if stream freed. */
1537 TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001538 qc_release_frm(qc, frm);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001539 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001540 }
1541
Frédéric Lécailleda342552022-04-25 10:28:49 +02001542 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1543 qc, strm_frm, stream);
1544 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001545 }
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:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001554 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001555 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001556
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001557 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001558 struct qcc *qcc = qc->qcc;
1559
1560 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1561 tasklet_wakeup(qcc->subs->tasklet);
1562 qcc->subs->events &= ~SUB_RETRY_SEND;
1563 if (!qcc->subs->events)
1564 qcc->subs = NULL;
1565 }
1566 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001567}
1568
1569/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1570 * deallocating them, and their TX frames.
1571 * Returns the last node reached to be used for the next range.
1572 * May be NULL if <largest> node could not be found.
1573 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001574static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1575 struct eb_root *pkts,
1576 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001577 struct list *newly_acked_pkts,
1578 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001579 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580{
1581 struct eb64_node *node;
1582 struct quic_tx_packet *pkt;
1583
1584 if (largest_node)
1585 node = largest_node;
1586 else {
1587 node = eb64_lookup(pkts, largest);
1588 while (!node && largest > smallest) {
1589 node = eb64_lookup(pkts, --largest);
1590 }
1591 }
1592
1593 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001594 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001595
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001596 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001597 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001598 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001599 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001601 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001602 node = eb64_prev(node);
1603 eb64_delete(&pkt->pn_node);
1604 }
1605
1606 return node;
1607}
1608
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001609/* Remove all frames from <pkt_frm_list> and reinsert them in the
1610 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001611 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001612static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001613 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001614 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001615{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001616 struct quic_frame *frm, *frmbak;
1617 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001618 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001619
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001620 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001621 /* Only for debug */
1622 uint64_t pn;
1623
1624 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001625 LIST_DELETE(&frm->list);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001626 pn = frm->pkt->pn_node.key;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001627 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001628 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001629 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001630 /* Remove any reference to this frame */
1631 qc_frm_unref(qc, frm);
1632 switch (frm->type) {
1633 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1634 {
1635 struct quic_stream *strm_frm = &frm->stream;
1636 struct eb64_node *node = NULL;
1637 struct qc_stream_desc *stream_desc;
1638
1639 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1640 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02001641 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001642 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1643 qc, frm, &pn);
1644 pool_free(pool_head_quic_frame, frm);
1645 continue;
1646 }
1647
1648 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1649 /* Do not resend this frame if in the "already acked range" */
1650 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1651 TRACE_PROTO("ignored frame in already acked range",
1652 QUIC_EV_CONN_PRSAFRM, qc, frm);
1653 continue;
1654 }
1655 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1656 strm_frm->offset.key = stream_desc->ack_offset;
1657 TRACE_PROTO("updated partially acked frame",
1658 QUIC_EV_CONN_PRSAFRM, qc, frm);
1659 }
1660
1661 break;
1662 }
1663
1664 default:
1665 break;
1666 }
1667
1668 /* Do not resend probing packet with old data */
1669 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1670 TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1671 qc, frm, &pn);
1672 if (frm->origin)
1673 LIST_DELETE(&frm->ref);
1674 pool_free(pool_head_quic_frame, frm);
1675 continue;
1676 }
1677
1678 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1679 TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1680 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1681 qc, frm, &pn);
1682 pool_free(pool_head_quic_frame, frm);
1683 }
1684 else {
1685 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001686 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1687 /* Mark this STREAM frame as lost. A look up their stream descriptor
1688 * will be performed to check the stream is not consumed or released.
1689 */
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001690 frm->flags = QUIC_FL_TX_FRAME_LOST;
1691 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001692 LIST_APPEND(&tmp, &frm->list);
1693 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001694 }
1695
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001696 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001697}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001698
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001699/* Free <pkt> TX packet and its attached frames.
1700 * This is the responsability of the caller to remove this packet of
1701 * any data structure it was possibly attached to.
1702 */
1703static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1704{
1705 struct quic_frame *frm, *frmbak;
1706
1707 if (!pkt)
1708 return;
1709
1710 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1711 LIST_DELETE(&frm->list);
1712 pool_free(pool_head_quic_frame, frm);
1713 }
1714 pool_free(pool_head_quic_tx_packet, pkt);
1715}
1716
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001717/* Free the TX packets of <pkts> list */
1718static inline void free_quic_tx_pkts(struct list *pkts)
1719{
1720 struct quic_tx_packet *pkt, *tmp;
1721
1722 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001723 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001724 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001725 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001726 }
1727}
1728
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001729/* Remove already sent ranges of acknowledged packet numbers from
1730 * <pktns> packet number space tree below <largest_acked_pn> possibly
1731 * updating the range which contains <largest_acked_pn>.
1732 * Never fails.
1733 */
1734static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1735 int64_t largest_acked_pn)
1736{
1737 struct eb64_node *ar, *next_ar;
1738 struct quic_arngs *arngs = &pktns->rx.arngs;
1739
1740 ar = eb64_first(&arngs->root);
1741 while (ar) {
1742 struct quic_arng_node *ar_node;
1743
1744 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001745 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001746 if ((int64_t)ar_node->first.key > largest_acked_pn)
1747 break;
1748
1749 if (largest_acked_pn < ar_node->last) {
1750 eb64_delete(ar);
1751 ar_node->first.key = largest_acked_pn + 1;
1752 eb64_insert(&arngs->root, ar);
1753 break;
1754 }
1755
1756 eb64_delete(ar);
1757 pool_free(pool_head_quic_arng, ar_node);
1758 arngs->sz--;
1759 ar = next_ar;
1760 }
1761}
1762
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001763/* Send a packet ack event nofication for each newly acked packet of
1764 * <newly_acked_pkts> list and free them.
1765 * Always succeeds.
1766 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001767static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001768 struct list *newly_acked_pkts)
1769{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001770 struct quic_tx_packet *pkt, *tmp;
1771 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1772
1773 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1774 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001775 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001776 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001777 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001778 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001779 /* If this packet contained an ACK frame, proceed to the
1780 * acknowledging of range of acks from the largest acknowledged
1781 * packet number which was sent in an ACK frame by this packet.
1782 */
1783 if (pkt->largest_acked_pn != -1)
1784 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001785 ev.ack.acked = pkt->in_flight_len;
1786 ev.ack.time_sent = pkt->time_sent;
1787 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001788 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001789 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001790 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001791 }
1792
1793}
1794
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001795/* Release all the frames attached to <pktns> packet number space */
1796static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1797{
1798 struct quic_frame *frm, *frmbak;
1799
1800 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1801 LIST_DELETE(&frm->list);
1802 pool_free(pool_head_quic_frame, frm);
1803 }
1804}
1805
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001806/* Handle <pkts> list of lost packets detected at <now_us> handling
1807 * their TX frames.
1808 * Send a packet loss event to the congestion controller if
1809 * in flight packet have been lost.
1810 * Also frees the packet in <pkts> list.
1811 * Never fails.
1812 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001813static inline void qc_release_lost_pkts(struct quic_conn *qc,
1814 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001815 struct list *pkts,
1816 uint64_t now_us)
1817{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001818 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001819 uint64_t lost_bytes;
1820
1821 lost_bytes = 0;
1822 oldest_lost = newest_lost = NULL;
1823 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001824 struct list tmp = LIST_HEAD_INIT(tmp);
1825
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001826 lost_bytes += pkt->in_flight_len;
1827 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001828 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001829 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001830 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001831 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001832 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001833 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001834 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001835 if (!oldest_lost) {
1836 oldest_lost = newest_lost = pkt;
1837 }
1838 else {
1839 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001840 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001841 newest_lost = pkt;
1842 }
1843 }
1844
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001845 if (newest_lost) {
1846 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02001847 struct quic_cc_event ev = { };
1848
1849 ev.type = QUIC_CC_EVT_LOSS;
1850 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001851
1852 quic_cc_event(&qc->path->cc, &ev);
1853 }
1854
1855 /* If an RTT have been already sampled, <rtt_min> has been set.
1856 * We must check if we are experiencing a persistent congestion.
1857 * If this is the case, the congestion controller must re-enter
1858 * slow start state.
1859 */
1860 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1861 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1862
1863 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1864 now_ms, qc->max_ack_delay))
1865 qc->path->cc.algo->slow_start(&qc->path->cc);
1866 }
1867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001869 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001870 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001871 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001872 }
1873}
1874
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001875/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1876 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001877 * 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 +01001878 * acked ack-eliciting packet.
1879 * Return 1, if succeeded, 0 if not.
1880 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001881static inline int qc_parse_ack_frm(struct quic_conn *qc,
1882 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001883 struct quic_enc_level *qel,
1884 unsigned int *rtt_sample,
1885 const unsigned char **pos, const unsigned char *end)
1886{
1887 struct quic_ack *ack = &frm->ack;
1888 uint64_t smallest, largest;
1889 struct eb_root *pkts;
1890 struct eb64_node *largest_node;
1891 unsigned int time_sent, pkt_flags;
1892 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1893 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1894
1895 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1896 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001897 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 goto err;
1899 }
1900
1901 if (ack->first_ack_range > ack->largest_ack) {
1902 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001903 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 goto err;
1905 }
1906
1907 largest = ack->largest_ack;
1908 smallest = largest - ack->first_ack_range;
1909 pkts = &qel->pktns->tx.pkts;
1910 pkt_flags = 0;
1911 largest_node = NULL;
1912 time_sent = 0;
1913
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001914 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001915 largest_node = eb64_lookup(pkts, largest);
1916 if (!largest_node) {
1917 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001918 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001919 }
1920 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001921 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001922 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001924 }
1925
1926 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001927 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001928 do {
1929 uint64_t gap, ack_range;
1930
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001931 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1932 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001933 if (!ack->ack_range_num--)
1934 break;
1935
1936 if (!quic_dec_int(&gap, pos, end))
1937 goto err;
1938
1939 if (smallest < gap + 2) {
1940 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001941 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001942 goto err;
1943 }
1944
1945 largest = smallest - gap - 2;
1946 if (!quic_dec_int(&ack_range, pos, end))
1947 goto err;
1948
1949 if (largest < ack_range) {
1950 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001951 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001952 goto err;
1953 }
1954
1955 /* Do not use this node anymore. */
1956 largest_node = NULL;
1957 /* Next range */
1958 smallest = largest - ack_range;
1959
1960 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001961 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001962 } while (1);
1963
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001964 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1965 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001966 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001967 }
1968
1969 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1970 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001971 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001973 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001974 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001975 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1976 if (quic_peer_validated_addr(qc))
1977 qc->path->loss.pto_count = 0;
1978 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001979 }
1980
1981
1982 return 1;
1983
1984 err:
1985 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001986 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001987 return 0;
1988}
1989
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001990/* This function gives the detail of the SSL error. It is used only
1991 * if the debug mode and the verbose mode are activated. It dump all
1992 * the SSL error until the stack was empty.
1993 */
1994static forceinline void qc_ssl_dump_errors(struct connection *conn)
1995{
1996 if (unlikely(global.mode & MODE_DEBUG)) {
1997 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02001998 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001999 unsigned long ret;
2000
Willy Tarreau325fc632022-04-11 18:47:38 +02002001 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002002 ret = ERR_get_error();
2003 if (!ret)
2004 return;
2005
2006 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002007 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002008 }
2009 }
2010}
2011
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002012int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2013 const char **str, int *len);
2014
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002015/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2016 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002017 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018 * Return 1 if succeeded, 0 if not.
2019 */
2020static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002021 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002022 const unsigned char *data, size_t len,
2023 struct quic_rx_packet *pkt,
2024 struct quic_rx_crypto_frm *cf)
2025{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002026 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002027 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002028
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002029 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002030 qc = ctx->qc;
2031
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002032 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2033
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002034 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2035 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002036 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 goto err;
2038 }
2039
2040 el->rx.crypto.offset += len;
2041 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002042 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002043
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002044 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002045 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002046 ssl_err = SSL_do_handshake(ctx->ssl);
2047 if (ssl_err != 1) {
2048 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2049 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2050 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002051 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002052 goto out;
2053 }
2054
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002055 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002056 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002057 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002058 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002059 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002060 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002061 goto err;
2062 }
2063
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002064 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002065
2066 /* Check the alpn could be negotiated */
2067 if (!qc->app_ops) {
2068 TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2069 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2070 goto err;
2071 }
2072
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002073 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002074 /* I/O callback switch */
2075 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002076 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002077 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002078 /* The connection is ready to be accepted. */
2079 quic_accept_push_qc(qc);
2080 }
2081 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002082 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002083 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002084 } else {
2085 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2086 if (ssl_err != 1) {
2087 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2088 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2089 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002090 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002091 goto out;
2092 }
2093
2094 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002095 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002096 goto err;
2097 }
2098
2099 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002100 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002102
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002104 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002105 return 1;
2106
2107 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002108 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002109 return 0;
2110}
2111
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002112/* Parse a STREAM frame <strm_frm>
2113 *
2114 * Return 1 on success. On error, 0 is returned. In this case, the packet
2115 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002116 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002117static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2118 struct quic_stream *strm_frm,
2119 struct quic_conn *qc)
2120{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002121 int ret;
2122
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002123 /* RFC9000 13.1. Packet Processing
2124 *
2125 * A packet MUST NOT be acknowledged until packet protection has been
2126 * successfully removed and all frames contained in the packet have
2127 * been processed. For STREAM frames, this means the data has been
2128 * enqueued in preparation to be received by the application protocol,
2129 * but it does not require that data be delivered and consumed.
2130 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002131 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2132 strm_frm->offset.key, strm_frm->fin,
2133 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002134
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002135 /* frame rejected - packet must not be acknowledeged */
2136 if (ret)
2137 return 0;
2138
2139 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002140}
2141
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002142/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2143 * for <qc> QUIC connection.
2144 * This is a best effort function which never fails even if no memory could be
2145 * allocated to duplicate these frames.
2146 */
2147static void qc_dup_pkt_frms(struct quic_conn *qc,
2148 struct list *pkt_frm_list, struct list *out_frm_list)
2149{
2150 struct quic_frame *frm, *frmbak;
2151 struct list tmp = LIST_HEAD_INIT(tmp);
2152
2153 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2154 struct quic_frame *dup_frm, *origin;
2155
2156 switch (frm->type) {
2157 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2158 {
2159 struct quic_stream *strm_frm = &frm->stream;
2160 struct eb64_node *node = NULL;
2161 struct qc_stream_desc *stream_desc;
2162
2163 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2164 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02002165 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002166 continue;
2167 }
2168
2169 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2170 /* Do not resend this frame if in the "already acked range" */
2171 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2172 TRACE_PROTO("ignored frame frame in already acked range",
2173 QUIC_EV_CONN_PRSAFRM, qc, frm);
2174 continue;
2175 }
2176 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2177 strm_frm->offset.key = stream_desc->ack_offset;
2178 TRACE_PROTO("updated partially acked frame",
2179 QUIC_EV_CONN_PRSAFRM, qc, frm);
2180 }
2181
2182 break;
2183 }
2184
2185 default:
2186 break;
2187 }
2188
2189 dup_frm = pool_zalloc(pool_head_quic_frame);
2190 if (!dup_frm) {
2191 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2192 break;
2193 }
2194
2195 /* If <frm> is already a copy of another frame, we must take
2196 * its original frame as source for the copy.
2197 */
2198 origin = frm->origin ? frm->origin : frm;
2199 TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
2200 *dup_frm = *origin;
2201 LIST_INIT(&dup_frm->reflist);
2202 TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM,
2203 qc, NULL, &origin->pkt->pn_node.key);
2204 dup_frm->origin = origin;
2205 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2206 LIST_APPEND(&tmp, &dup_frm->list);
2207 }
2208
2209 LIST_SPLICE(out_frm_list, &tmp);
2210}
2211
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002212/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002213static void qc_prep_fast_retrans(struct quic_conn *qc,
2214 struct quic_enc_level *qel,
2215 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002216{
2217 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002218 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002219 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002220 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002221
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002222 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002223 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002224 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002225 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002226 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2227 node = eb64_next(node);
2228 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002229 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002230 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002231 }
2232
2233 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002234 return;
2235
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002236 /* When building a packet from another one, the field which may increase the
2237 * packet size is the packet number. And the maximum increase is 4 bytes.
2238 */
2239 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2240 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2241 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2242 return;
2243 }
2244
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002245 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2246 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2247 if (frms == frms1 && frms2) {
2248 frms = frms2;
2249 goto start;
2250 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002251}
2252
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002253/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002254 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002255 * Initial packets send them coalescing with others (Handshake here).
2256 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002257 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002258static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2259 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002260{
2261 struct list itmp = LIST_HEAD_INIT(itmp);
2262 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002263
2264 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2265 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2266 struct quic_enc_level *qel = iqel;
2267 struct eb_root *pkts;
2268 struct eb64_node *node;
2269 struct quic_tx_packet *pkt;
2270 struct list *tmp = &itmp;
2271
2272 start:
2273 pkt = NULL;
2274 pkts = &qel->pktns->tx.pkts;
2275 node = eb64_first(pkts);
2276 /* Skip the empty packet (they have already been retransmitted) */
2277 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002278 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002279 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002280 break;
2281 node = eb64_next(node);
2282 }
2283
2284 if (!pkt)
2285 goto end;
2286
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002287 /* When building a packet from another one, the field which may increase the
2288 * packet size is the packet number. And the maximum increase is 4 bytes.
2289 */
2290 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2291 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2292 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2293 goto end;
2294 }
2295
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002296 qel->pktns->tx.pto_probe += 1;
2297 requeue:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002298 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2299 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002300 if (qel == iqel) {
2301 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2302 pkt = pkt->next;
2303 tmp = &htmp;
2304 hqel->pktns->tx.pto_probe += 1;
2305 goto requeue;
2306 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002307 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002308
2309 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002310 LIST_SPLICE(ifrms, &itmp);
2311 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002312}
2313
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002314static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002315{
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002316 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2317 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2318 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2319 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2320 return;
2321
2322 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2323 }
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002324}
2325
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002326/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2327 * frame list to send.
2328 * Return 1 if succeeded, 0 if not.
2329 */
2330static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2331{
2332 struct quic_frame *frm;
2333 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2334 uint64_t app_error_code;
2335
2336 /* TODO: the mux may be released, we cannot have more
2337 * information about the application error code to send
2338 * at this time.
2339 */
2340 app_error_code = H3_REQUEST_REJECTED;
2341
2342 frm = pool_zalloc(pool_head_quic_frame);
2343 if (!frm)
2344 return 0;
2345
2346 frm->type = QUIC_FT_STOP_SENDING;
2347 frm->stop_sending.id = id;
2348 frm->stop_sending.app_error_code = app_error_code;
2349 LIST_INIT(&frm->reflist);
2350 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
2351
2352 return 1;
2353}
2354
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002355/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2356 * as I/O handler context and <qel> as encryption level.
2357 * Returns 1 if succeeded, 0 if failed.
2358 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002359static 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 +01002360 struct quic_enc_level *qel)
2361{
2362 struct quic_frame frm;
2363 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002364 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002365 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002366
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002367 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002368 /* Skip the AAD */
2369 pos = pkt->data + pkt->aad_len;
2370 end = pkt->data + pkt->len;
2371
2372 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002373 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002374 goto err;
2375
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002376 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002377 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002378 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002379 break;
2380 case QUIC_FT_PING:
2381 break;
2382 case QUIC_FT_ACK:
2383 {
2384 unsigned int rtt_sample;
2385
2386 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002387 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002388 goto err;
2389
2390 if (rtt_sample) {
2391 unsigned int ack_delay;
2392
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002393 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002394 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002395 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2396 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002397 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002398 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002399 break;
2400 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002401 case QUIC_FT_RESET_STREAM:
2402 /* TODO: handle this frame at STREAM level */
2403 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002404 case QUIC_FT_STOP_SENDING:
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002405 /* TODO: handle this frame at STREAM level */
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002406 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002407 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002408 {
2409 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002410
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002411 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002412 /* XXX TO DO: <cfdebug> is used only for the traces. */
2413 struct quic_rx_crypto_frm cfdebug = { };
2414
2415 cfdebug.offset_node.key = frm.crypto.offset;
2416 cfdebug.len = frm.crypto.len;
2417 TRACE_PROTO("CRYPTO data discarded",
2418 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2419 break;
2420 }
2421
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002422 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2423 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2424 /* XXX TO DO: <cfdebug> is used only for the traces. */
2425 struct quic_rx_crypto_frm cfdebug = { };
2426
2427 cfdebug.offset_node.key = frm.crypto.offset;
2428 cfdebug.len = frm.crypto.len;
2429 /* Nothing to do */
2430 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002431 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002432 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002433 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2434 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002435 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002436 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002437 else {
2438 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2439 /* XXX TO DO: <cfdebug> is used only for the traces. */
2440 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002441
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002442 cfdebug.offset_node.key = frm.crypto.offset;
2443 cfdebug.len = frm.crypto.len;
2444 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002445 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002446 frm.crypto.len -= diff;
2447 frm.crypto.data += diff;
2448 frm.crypto.offset = qel->rx.crypto.offset;
2449 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002451
2452 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002453 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002454 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002455
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002456 cfdebug.offset_node.key = frm.crypto.offset;
2457 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002458 if (!qc_provide_cdata(qel, ctx,
2459 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002460 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002461 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002462
2463 break;
2464 }
2465
2466 /* frm.crypto.offset > qel->rx.crypto.offset */
2467 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2468 if (!cf) {
2469 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002470 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002471 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002473
2474 cf->offset_node.key = frm.crypto.offset;
2475 cf->len = frm.crypto.len;
2476 cf->data = frm.crypto.data;
2477 cf->pkt = pkt;
2478 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2479 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002480 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002481 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002482 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002483 {
2484 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002485 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002486
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002487 /* The upper layer may not be allocated. */
2488 if (qc->mux_state != QC_MUX_READY) {
2489 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
2490 TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
2491 break;
2492 }
2493 else {
2494 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002495 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
2496 TRACE_PROTO("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2497
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002498 goto err;
2499 }
2500 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002501
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002502 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002503 goto err;
2504
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002505 break;
2506 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002507 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002508 if (qc->mux_state == QC_MUX_READY) {
2509 struct quic_max_data *data = &frm.max_data;
2510 qcc_recv_max_data(qc->qcc, data->max_data);
2511 }
2512 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002513 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002514 if (qc->mux_state == QC_MUX_READY) {
2515 struct quic_max_stream_data *data = &frm.max_stream_data;
2516 qcc_recv_max_stream_data(qc->qcc, data->id,
2517 data->max_stream_data);
2518 }
2519 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002520 case QUIC_FT_MAX_STREAMS_BIDI:
2521 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002522 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002523 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002524 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2525 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002526 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002527 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2528 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002529 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002530 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2531 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002532 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002533 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002534 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002535 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002536 case QUIC_FT_RETIRE_CONNECTION_ID:
2537 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002538 break;
2539 case QUIC_FT_CONNECTION_CLOSE:
2540 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002541 /* Increment the error counters */
2542 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002543 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille07968dc2022-05-20 20:53:20 +02002544 /* If the connection did not reached the handshake complete state,
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002545 * the <half_open_conn> counter was not decremented. Note that if
Frédéric Lécaille07968dc2022-05-20 20:53:20 +02002546 * a TLS alert was received from the TLS stack, this counter
2547 * has already been decremented.
2548 */
2549 if (qc->state < QUIC_HS_ST_COMPLETE && !(qc->flags & QUIC_FL_CONN_TLS_ALERT))
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002550 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002551 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2552 /* RFC 9000 10.2. Immediate Close:
2553 * The closing and draining connection states exist to ensure
2554 * that connections close cleanly and that delayed or reordered
2555 * packets are properly discarded. These states SHOULD persist
2556 * for at least three times the current PTO interval...
2557 *
2558 * Rearm the idle timeout only one time when entering draining
2559 * state.
2560 */
2561 qc_idle_timer_do_rearm(qc);
2562 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002563 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002564 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002565 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002566 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002567 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002568 goto err;
2569
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002570 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002571 break;
2572 default:
2573 goto err;
2574 }
2575 }
2576
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002577 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002578 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002579
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002580 if (fast_retrans) {
2581 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2582 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2583
2584 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2585 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002586
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2588 * has successfully parse a Handshake packet. The Initial encryption must also
2589 * be discarded.
2590 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002591 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002592 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002593 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2594 QUIC_FL_TLS_SECRETS_DCD)) {
2595 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2596 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2597 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2598 qc_set_timer(ctx->qc);
2599 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2600 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2601 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002602 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2603 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002604 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002605 }
2606
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002607 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 return 1;
2609
2610 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002611 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612 return 0;
2613}
2614
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002615/* Must be called only by a <cbuf> writer (packet builder).
2616 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2617 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2618 * index after having marked the end of written data. This the responsability
2619 * of the caller to ensure there is enough room in <cbuf> to write the end of
2620 * data made of a uint16_t null field.
2621 *
2622 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2623 * ^ ^
2624 * r w
2625 *
2626 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2627 * ^ ^
2628 * r w
2629
2630 * +--------------------------------------+ (empty buffer, can be reused)
2631 * ^
2632 * (r = w)
2633 *
2634 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2635 * ^ ^
2636 * w r
2637 */
2638static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2639{
2640 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2641
2642 /* We can reset the writer index only if in front of the reader index and
2643 * if the reader index is not null. Resetting the writer when the reader
2644 * index is null would empty the buffer.
2645 * XXX Note than the writer index cannot reach the reader index.
2646 * Only the reader index can reach the writer index.
2647 */
2648 if (rd && rd <= cbuf->wr) {
2649 /* Mark the end of contiguous data for the reader */
2650 write_u16(cb_wr(cbuf), 0);
2651 cb_add(cbuf, sizeof(uint16_t));
2652 cb_wr_reset(cbuf);
2653 return 1;
2654 }
2655
2656 return 0;
2657}
2658
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002659/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002660 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002661 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2662 * This function must be called only after having built a correct datagram.
2663 * Always succeeds.
2664 */
2665static inline void qc_set_dg(struct cbuf *cbuf,
2666 uint16_t dglen, struct quic_tx_packet *pkt)
2667{
2668 write_u16(cb_wr(cbuf), dglen);
2669 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2670 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2671}
2672
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002673/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2674 * with <frms> as ack-eliciting frame list to send, 0 if not.
2675 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2676 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
2677 */
2678static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
2679 struct quic_enc_level *qel, int cc, int probe)
2680{
2681 unsigned int must_ack =
2682 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2683
2684 /* Do not build any more packet if the TX secrets are not available or
2685 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2686 * and if there is no more packets to send upon PTO expiration
2687 * and if there is no more ack-eliciting frames to send or in flight
2688 * congestion control limit is reached for prepared data
2689 */
2690 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2691 (!cc && !probe && !must_ack &&
2692 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2693 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2694 return 0;
2695 }
2696
2697 return 1;
2698}
2699
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002700/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002701 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2702 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002703 * A header made of two fields is added to each datagram: the datagram length followed
2704 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002705 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2706 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002707 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002708static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2709 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002710{
2711 struct quic_enc_level *qel;
2712 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002713 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002714 struct quic_tx_packet *pkt;
2715 size_t total;
2716 size_t dg_headlen;
2717
2718 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2719 /* Each datagram is prepended with its length followed by the
2720 * address of the first packet in the datagram.
2721 */
2722 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2723 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2724 total = 0;
2725 start:
2726 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002727 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002728 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2729 * to ensure there is enough room to mark the end of prepared
2730 * contiguous data with a zero length.
2731 */
2732 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2733 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002734 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002735
2736 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002737 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002738 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002739 /* We do not probe if an immediate close was asked */
2740 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002741 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002742
2743 if (!qc_may_build_pkt(qc, frms, qel, cc, probe))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002744 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002745
2746 /* Leave room for the datagram header */
2747 pos += dg_headlen;
2748 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2749 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2750 }
2751 else {
2752 end = pos + qc->path->mtu;
2753 }
2754
Frédéric Lécaille301425b2022-06-14 17:40:39 +02002755 pkt = qc_build_pkt(&pos, end, qel, &qel->tls_ctx, frms, qc, NULL, 0, 0,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002756 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002757 switch (err) {
2758 case -2:
2759 goto err;
2760 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002761 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2762 * MTU, we are here because of the congestion control window. There is
2763 * no need to try to reuse this buffer.
2764 */
2765 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002766 default:
2767 break;
2768 }
2769
2770 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2771 if (!pkt)
2772 goto err;
2773
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002774 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2775 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2776
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002777 total += pkt->len;
2778 /* Set the current datagram as prepared into <cbuf>. */
2779 qc_set_dg(cbuf, pkt->len, pkt);
2780 }
2781
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002782 /* Reset <wr> writer index if in front of <rd> index */
2783 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002784 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002785 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002786 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002787 }
2788
2789 out:
2790 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2791 return total;
2792
2793 err:
2794 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2795 return -1;
2796}
2797
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002798/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002799 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2800 * several packets in the same datagram. A header made of two fields is added
2801 * to each datagram: the datagram length followed by the address of the first
2802 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002803 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2804 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002805 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002806static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002807 enum quic_tls_enc_level tel, struct list *tel_frms,
2808 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002809{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002811 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002812 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002813 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2814 /* length of datagrams */
2815 uint16_t dglen;
2816 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002817 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002818 /* Each datagram is prepended with its length followed by the
2819 * address of the first packet in the datagram.
2820 */
2821 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002822 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002824 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2825
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002826 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002827 qel = &qc->els[tel];
2828 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002829 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002830 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002831 padding = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002832 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002833 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002834 /* Leave at least <dglen> bytes at the end of this buffer
2835 * to ensure there is enough room to mark the end of prepared
2836 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002837 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002838 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2839 first_pkt = prv_pkt = NULL;
2840 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002841 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002842 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02002843 struct quic_tls_ctx *tls_ctx;
2844 const struct quic_version *ver;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002845
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002846 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002847 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002848 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002849 /* We do not probe if an immediate close was asked */
2850 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002851 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002852
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002853 if (!qc_may_build_pkt(qc, frms, qel, cc, probe)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002854 if (prv_pkt)
2855 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002856 /* Let's select the next encryption level */
2857 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2858 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002859 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002860 qel = &qc->els[tel];
2861 /* Build a new datagram */
2862 prv_pkt = NULL;
2863 continue;
2864 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865 break;
2866 }
2867
2868 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002869 if (!prv_pkt) {
2870 /* Leave room for the datagram header */
2871 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002872 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002873 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2874 }
2875 else {
2876 end = pos + qc->path->mtu;
2877 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002878 }
2879
Frédéric Lécaille301425b2022-06-14 17:40:39 +02002880 if (qc->negotiated_version) {
2881 ver = qc->negotiated_version;
2882 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2883 tls_ctx = &qc->negotiated_ictx;
2884 else
2885 tls_ctx = &qel->tls_ctx;
2886 }
2887 else {
2888 ver = qc->original_version;
2889 tls_ctx = &qel->tls_ctx;
2890 }
2891
2892 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
2893 qc, ver, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002894 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895 case -2:
2896 goto err;
2897 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002898 /* If there was already a correct packet present, set the
2899 * current datagram as prepared into <cbuf>.
2900 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002901 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002902 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002903 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002904 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002905 break;
2906 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002907
Frédéric Lécaille63556772021-12-29 17:18:21 +01002908 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2909 if (!cur_pkt)
2910 goto err;
2911
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002912 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2913 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2914
Frédéric Lécaille63556772021-12-29 17:18:21 +01002915 total += cur_pkt->len;
2916 /* keep trace of the first packet in the datagram */
2917 if (!first_pkt)
2918 first_pkt = cur_pkt;
2919 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002920 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002921 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002922 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
2923 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01002924 /* Let's say we have to build a new dgram */
2925 prv_pkt = NULL;
2926 dglen += cur_pkt->len;
2927 /* Client: discard the Initial encryption keys as soon as
2928 * a handshake packet could be built.
2929 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002930 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01002931 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2932 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2933 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2934 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2935 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002936 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002937 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002938 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002939 }
2940 /* If the data for the current encryption level have all been sent,
2941 * select the next level.
2942 */
2943 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02002944 next_tel != QUIC_TLS_ENC_LEVEL_NONE && (LIST_ISEMPTY(frms) && !qel->pktns->tx.pto_probe)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002945 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2946 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2947 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2948 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002949 if (tel == QUIC_TLS_ENC_LEVEL_APP)
2950 frms = &qc->els[tel].pktns->tx.frms;
2951 else
2952 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002953 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002954 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002955 /* If there is data for the next level, do not
2956 * consume a datagram.
2957 */
2958 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002959 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002960 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002961 /* If we have to build a new datagram, set the current datagram as
2962 * prepared into <cbuf>.
2963 */
2964 if (!prv_pkt) {
2965 qc_set_dg(cbuf, dglen, first_pkt);
2966 first_pkt = NULL;
2967 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002968 padding = 0;
2969 }
2970 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002971 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002972 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2973 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002974 }
2975 }
2976
2977 stop_build:
2978 /* Reset <wr> writer index if in front of <rd> index */
2979 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002980 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002981 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002982 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002983 }
2984
2985 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002986 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002987 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002988
2989 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002990 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002991 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992}
2993
2994/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002995 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002997int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998{
2999 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003000 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003001
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003002 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003003 cbuf = qr->cbuf;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003004 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003005 while (cb_contig_data(cbuf)) {
3006 unsigned char *pos;
3007 struct buffer tmpbuf = { };
3008 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3009 uint16_t dglen;
3010 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003011 unsigned int time_sent;
3012
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003013 pos = cb_rd(cbuf);
3014 dglen = read_u16(pos);
3015 /* End of prepared datagrams.
3016 * Reset the reader index only if in front of the writer index.
3017 */
3018 if (!dglen) {
3019 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3020
3021 if (wr && wr < cbuf->rd) {
3022 cb_rd_reset(cbuf);
3023 continue;
3024 }
3025 break;
3026 }
3027
3028 pos += sizeof dglen;
3029 first_pkt = read_ptr(pos);
3030 pos += sizeof first_pkt;
3031 tmpbuf.area = (char *)pos;
3032 tmpbuf.size = tmpbuf.data = dglen;
3033
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003034 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003035 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003036 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003037
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003038 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039 qc->tx.bytes += tmpbuf.data;
3040 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003041
3042 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3043 pkt->time_sent = time_sent;
3044 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3045 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003046 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003047 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3048 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003049 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003050 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3051 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3052 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003053 qc_notify_close(qc);
3054
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003055 /* RFC 9000 10.2. Immediate Close:
3056 * The closing and draining connection states exist to ensure
3057 * that connections close cleanly and that delayed or reordered
3058 * packets are properly discarded. These states SHOULD persist
3059 * for at least three times the current PTO interval...
3060 *
3061 * Rearm the idle timeout only one time when entering closing
3062 * state.
3063 */
3064 qc_idle_timer_do_rearm(qc);
3065 if (qc->timer_task) {
3066 task_destroy(qc->timer_task);
3067 qc->timer_task = NULL;
3068 }
3069 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003070 qc->path->in_flight += pkt->in_flight_len;
3071 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3072 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003073 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003074 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003075 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003076 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003077 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003078 }
3079 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003081 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3082
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003083 return 1;
3084}
3085
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003086/* Copy into <buf> buffer a stateless reset token depending on the
3087 * <salt> salt input. This is the cluster secret which will be derived
3088 * as HKDF input secret to generate this token.
3089 * Return 1 if succeeded, 0 if not.
3090 */
3091static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len,
3092 const unsigned char *salt, size_t saltlen)
3093{
3094 /* Input secret */
3095 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3096 size_t keylen = strlen(global.cluster_secret);
3097 /* Info */
3098 const unsigned char label[] = "stateless token";
3099 size_t labellen = sizeof label - 1;
3100
3101 return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3102 key, keylen, salt, saltlen, label, labellen);
3103}
3104
3105/* Initialize the stateless reset token attached to <cid> connection ID.
3106 * Returns 1 if succeeded, 0 if not.
3107 */
3108static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid)
3109{
3110 if (global.cluster_secret) {
3111 /* Output secret */
3112 unsigned char *token = quic_cid->stateless_reset_token;
3113 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3114 /* Salt */
3115 const unsigned char *cid = quic_cid->cid.data;
3116 size_t cidlen = quic_cid->cid.len;
3117
3118 return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
3119 }
3120 else {
3121 return RAND_bytes(quic_cid->stateless_reset_token,
3122 sizeof quic_cid->stateless_reset_token) == 1;
3123 }
3124}
3125
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003126/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3127 * ebtree.
3128 *
3129 * The CID is randomly generated in part with the result altered to be
3130 * associated with the current thread ID. This means this function must only
3131 * be called by the quic_conn thread.
3132 *
3133 * Returns the new CID if succeeded, NULL if not.
3134 */
3135static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3136 struct quic_conn *qc,
3137 int seq_num)
3138{
3139 struct quic_connection_id *cid;
3140
3141 cid = pool_alloc(pool_head_quic_connection_id);
3142 if (!cid)
3143 return NULL;
3144
3145 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003146 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1)
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003147 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003148
3149 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003150 if (quic_stateless_reset_token_init(cid) != 1)
3151 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003152
3153 cid->qc = qc;
3154
3155 cid->seq_num.key = seq_num;
3156 cid->retire_prior_to = 0;
3157 /* insert the allocated CID in the quic_conn tree */
3158 eb64_insert(root, &cid->seq_num);
3159
3160 return cid;
3161
3162 err:
3163 pool_free(pool_head_quic_connection_id, cid);
3164 return NULL;
3165}
3166
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003167/* Build all the frames which must be sent just after the handshake have succeeded.
3168 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3169 * a HANDSHAKE_DONE frame.
3170 * Return 1 if succeeded, 0 if not.
3171 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003172static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003173{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003174 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003175 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003176 struct quic_frame *frm, *frmbak;
3177 struct list frm_list = LIST_HEAD_INIT(frm_list);
3178 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003179
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003180 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003181 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003182 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003183 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003184 if (!frm)
3185 return 0;
3186
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003187 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003188 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003189 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003190 }
3191
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003192 first = 1;
3193 max = qc->tx.params.active_connection_id_limit;
3194 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003195 struct quic_connection_id *cid;
3196
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003197 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003198 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003199 goto err;
3200
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003201 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003202 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003203 if (!cid)
3204 goto err;
3205
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003206 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003207 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003208
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003209 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003210 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003211 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003212
3213 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003214 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215
3216 return 1;
3217
3218 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003219 /* free the frames */
3220 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3221 pool_free(pool_head_quic_frame, frm);
3222
3223 node = eb64_first(&qc->cids);
3224 while (node) {
3225 struct quic_connection_id *cid;
3226
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003227
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003228 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003229 if (cid->seq_num.key >= max)
3230 break;
3231
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003232 if (cid->seq_num.key < first)
3233 continue;
3234
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003235 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003236 ebmb_delete(&cid->node);
3237 eb64_delete(&cid->seq_num);
3238 pool_free(pool_head_quic_connection_id, cid);
3239 }
3240
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241 return 0;
3242}
3243
3244/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003245void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003246{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003247 struct eb64_node *n;
3248 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003249
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003250 n = eb64_first(&arngs->root);
3251 while (n) {
3252 struct eb64_node *next;
3253
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003254 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003255 next = eb64_next(n);
3256 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003257 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003258 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003259 }
3260}
3261
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003262/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3263 * descending order.
3264 */
3265static inline size_t sack_gap(struct quic_arng_node *p,
3266 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003267{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003268 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003269}
3270
3271
3272/* Remove the last elements of <ack_ranges> list of ack range updating its
3273 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003274 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003275 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003276static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003277{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003278 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003279
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003280 last = eb64_last(&arngs->root);
3281 while (last && arngs->enc_sz > limit) {
3282 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003283
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003284 prev = eb64_prev(last);
3285 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286 return 0;
3287
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003288 last_node = eb64_entry(last, struct quic_arng_node, first);
3289 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003290 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3291 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3292 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3293 --arngs->sz;
3294 eb64_delete(last);
3295 pool_free(pool_head_quic_arng, last);
3296 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003297 }
3298
3299 return 1;
3300}
3301
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003302/* Set the encoded size of <arngs> QUIC ack ranges. */
3303static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3304{
3305 struct eb64_node *node, *next;
3306 struct quic_arng_node *ar, *ar_next;
3307
3308 node = eb64_last(&arngs->root);
3309 if (!node)
3310 return;
3311
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003312 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003313 arngs->enc_sz = quic_int_getsize(ar->last) +
3314 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3315
3316 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003317 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003318 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3319 quic_int_getsize(ar_next->last - ar_next->first.key);
3320 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003321 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003322 }
3323}
3324
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003325/* Insert <ar> ack range into <argns> tree of ack ranges.
3326 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003327 */
3328static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003329struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3330 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003331{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003332 struct quic_arng_node *new_ar;
3333
3334 new_ar = pool_alloc(pool_head_quic_arng);
3335 if (new_ar) {
3336 new_ar->first.key = ar->first;
3337 new_ar->last = ar->last;
3338 eb64_insert(&arngs->root, &new_ar->first);
3339 arngs->sz++;
3340 }
3341
3342 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003343}
3344
3345/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003346 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003347 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348 *
3349 * Descending order
3350 * ------------->
3351 * range1 range2
3352 * ..........|--------|..............|--------|
3353 * ^ ^ ^ ^
3354 * | | | |
3355 * last1 first1 last2 first2
3356 * ..........+--------+--------------+--------+......
3357 * diff1 gap12 diff2
3358 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003359 * To encode the previous list of ranges we must encode integers as follows in
3360 * descending order:
3361 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003362 * with diff1 = last1 - first1
3363 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003364 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003366 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003367int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3368 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003369{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003370 struct eb64_node *le;
3371 struct quic_arng_node *new_node;
3372 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003373
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003374 new = NULL;
3375 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003376 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003377 if (!new_node)
3378 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003379
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003380 goto out;
3381 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003382
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003383 le = eb64_lookup_le(&arngs->root, ar->first);
3384 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003385 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003386 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003387 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003388
3389 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003390 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003391 else {
3392 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003393 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003394
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003395 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003396 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003397 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003398
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003399 if (le_ar->last + 1 >= ar->first) {
3400 le_ar->last = ar->last;
3401 new = le;
3402 new_node = le_ar;
3403 }
3404 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003405 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003406 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003408
3409 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003410 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003411 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003413 /* Verify that the new inserted node does not overlap the nodes
3414 * which follow it.
3415 */
3416 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003417 struct eb64_node *next;
3418 struct quic_arng_node *next_node;
3419
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003420 while ((next = eb64_next(new))) {
3421 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003422 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003423 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003424 break;
3425
3426 if (next_node->last > new_node->last)
3427 new_node->last = next_node->last;
3428 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003429 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003430 /* Decrement the size of these ranges. */
3431 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003432 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433 }
3434
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003435 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003436 quic_arngs_set_enc_sz(arngs);
3437
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003438 return 1;
3439}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003440/* Remove the header protection of packets at <el> encryption level.
3441 * Always succeeds.
3442 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003443static 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 +01003444{
3445 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003446 struct quic_rx_packet *pqpkt;
3447 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003448 struct quic_enc_level *app_qel;
3449
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003450 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3451 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003452 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003453 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003454 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003455 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003456 goto out;
3457 }
3458 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003459 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003460 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003462 pqpkt->data, pqpkt->data + pqpkt->len)) {
3463 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003464 /* XXX TO DO XXX */
3465 }
3466 else {
3467 /* The AAD includes the packet number field */
3468 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3469 /* Store the packet into the tree of packets to decrypt. */
3470 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003471 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003472 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3473 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003474 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003475 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003477 MT_LIST_DELETE_SAFE(pkttmp1);
3478 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003479 }
3480
3481 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003482 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003483}
3484
3485/* Process all the CRYPTO frame at <el> encryption level.
3486 * Return 1 if succeeded, 0 if not.
3487 */
3488static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003489 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490{
3491 struct eb64_node *node;
3492
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003493 node = eb64_first(&el->rx.crypto.frms);
3494 while (node) {
3495 struct quic_rx_crypto_frm *cf;
3496
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003497 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003498 if (cf->offset_node.key != el->rx.crypto.offset)
3499 break;
3500
3501 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3502 goto err;
3503
3504 node = eb64_next(node);
3505 quic_rx_packet_refdec(cf->pkt);
3506 eb64_delete(&cf->offset_node);
3507 pool_free(pool_head_quic_rx_crypto_frm, cf);
3508 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003509 return 1;
3510
3511 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003512 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003513 return 0;
3514}
3515
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003516/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003517 * 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 +02003518 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003519 * Return 1 if succeeded, 0 if not.
3520 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003521int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3522 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003523{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003525 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003526 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003527 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003528 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003529
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003530 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003531 qel = cur_el;
3532 next_tel:
3533 if (!qel)
3534 goto out;
3535
3536 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3537 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003538 while (node) {
3539 struct quic_rx_packet *pkt;
3540
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003541 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003542 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003543 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003544 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003545 /* Drop the packet */
3546 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003547 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548 }
3549 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003550 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003551 /* Drop the packet */
3552 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003553 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003554 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003555 }
3556 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003557 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3558
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003559 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3560 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3561 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003562 qc_idle_timer_rearm(qc, 1);
3563 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003564 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003565 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003566 largest_pn_time_received = pkt->time_received;
3567 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003568 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003569 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003570 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003571 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572 }
3573 }
3574 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003575 eb64_delete(&pkt->pn_node);
3576 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003577 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003578 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003579
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003580 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3581 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003582 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003583 /* Update the largest acknowledged packet timestamps */
3584 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3585 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3586 }
3587
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003588 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 goto err;
3590
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003591 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003592 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003593 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003594 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003595 goto next_tel;
3596 }
3597
3598 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003599 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600 return 1;
3601
3602 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003603 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003604 return 0;
3605}
3606
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003607/* Check if it's possible to remove header protection for packets related to
3608 * encryption level <qel>. If <qel> is NULL, assume it's false.
3609 *
3610 * Return true if the operation is possible else false.
3611 */
3612static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3613{
3614 enum quic_tls_enc_level tel;
3615
3616 if (!qel)
3617 return 0;
3618
3619 tel = ssl_to_quic_enc_level(qel->level);
3620
3621 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003622 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003623 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003624 return 0;
3625 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003626
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003627 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003628 return 0;
3629
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003630 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003631 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003632 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003633 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003634
3635 return 1;
3636}
3637
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003638/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003639int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003640{
3641 int ret;
3642 struct qring *qr;
3643
3644 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3645 if (!qr)
3646 /* Never happens */
3647 return 1;
3648
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003649 if (old_data)
3650 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003651 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003652 if (ret == -1)
3653 goto err;
3654 else if (ret == 0)
3655 goto out;
3656
3657 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3658 goto err;
3659
3660 out:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003661 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003662 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3663 return 1;
3664
3665 err:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003666 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003667 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3668 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3669 return 0;
3670}
3671
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003672/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3673 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
3674 * QUIC connection
3675 * Returns 1 if succeeded, 0 if not.
3676 */
3677int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
3678 enum quic_tls_enc_level tel, struct list *tel_frms,
3679 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3680{
3681 int ret;
3682 struct qring *qr;
3683
3684 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3685 if (!qr)
3686 /* Never happens */
3687 return 1;
3688
3689 if (old_data)
3690 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
3691 ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms);
3692 if (ret == -1)
3693 goto err;
3694 else if (ret == 0)
3695 goto out;
3696
3697 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3698 goto err;
3699
3700 out:
3701 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3702 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3703 return 1;
3704
3705 err:
3706 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3707 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3708 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3709 return 0;
3710}
3711
3712/* Retransmit up to two datagrams depending on packet number space */
3713static void qc_dgrams_retransmit(struct quic_conn *qc)
3714{
3715 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3716 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3717 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3718
3719 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3720 struct list ifrms = LIST_HEAD_INIT(ifrms);
3721 struct list hfrms = LIST_HEAD_INIT(hfrms);
3722
3723 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
3724 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
3725 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3726 if (!LIST_ISEMPTY(&ifrms)) {
3727 iqel->pktns->tx.pto_probe = 1;
3728 if (!LIST_ISEMPTY(&hfrms)) {
3729 hqel->pktns->tx.pto_probe = 1;
3730 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
3731 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
3732 }
3733 }
3734 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3735 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
3736 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3737 if (!LIST_ISEMPTY(&hfrms)) {
3738 hqel->pktns->tx.pto_probe = 1;
3739 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
3740 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3741 }
3742 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3743 }
3744 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3745 }
3746 else {
3747 int i;
3748 struct list frms1 = LIST_HEAD_INIT(frms1);
3749 struct list frms2 = LIST_HEAD_INIT(frms2);
3750
3751 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3752 hqel->pktns->tx.pto_probe = 0;
3753 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
3754 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
3755 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3756 if (!LIST_ISEMPTY(&frms1)) {
3757 hqel->pktns->tx.pto_probe = 1;
3758 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
3759 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3760 }
3761 }
3762 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3763 }
3764 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3765 aqel->pktns->tx.pto_probe = 0;
3766 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
3767 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3768 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
3769 if (!LIST_ISEMPTY(&frms1)) {
3770 aqel->pktns->tx.pto_probe = 1;
3771 qc_send_app_pkts(qc, 1, &frms1);
3772 }
3773 if (!LIST_ISEMPTY(&frms2)) {
3774 aqel->pktns->tx.pto_probe = 1;
3775 qc_send_app_pkts(qc, 1, &frms2);
3776 }
3777 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3778 }
3779 }
3780}
3781
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003782/* QUIC connection packet handler task (post handshake) */
3783static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3784{
3785 struct ssl_sock_ctx *ctx;
3786 struct quic_conn *qc;
3787 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003788
3789
3790 ctx = context;
3791 qc = ctx->qc;
3792 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003793
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003794 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003795
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003796 /* Retranmissions */
3797 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3798 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
3799 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3800 qc_dgrams_retransmit(qc);
3801 }
3802
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003803 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3804 qc_rm_hp_pkts(qc, qel);
3805
3806 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3807 goto err;
3808
Frédéric Lécaille47756802022-03-25 09:12:16 +01003809 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3810 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3811 goto out;
3812
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003813 if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003814 goto err;
3815
Frédéric Lécaille47756802022-03-25 09:12:16 +01003816out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003817 return t;
3818
3819 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003820 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003821 return t;
3822}
3823
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003824/* QUIC connection packet handler task. */
3825struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003826{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003827 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003828 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003829 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003830 enum quic_tls_enc_level tel, next_tel;
3831 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003832 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003833 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003834
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003835 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003836 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003837 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003838 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003839 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003840 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003841
3842 /* Retranmissions */
3843 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3844 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
3845 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3846 qc_dgrams_retransmit(qc);
3847 }
3848
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003849 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3850 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003851 /* The I/O handler has been woken up by the dgram listener
3852 * after the anti-amplification was reached.
3853 */
3854 qc_set_timer(qc);
3855 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3856 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3857 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003858 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003859 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3860 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3861 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003862 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003863 if (st >= QUIC_HS_ST_COMPLETE &&
3864 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3865 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3866 /* There may be remaining Handshake packets to treat and acknowledge. */
3867 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3868 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3869 }
3870 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003871 goto err;
3872
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003873 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003874 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003875
3876 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003877 /* Treat packets waiting for header packet protection decryption */
3878 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003879 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003880
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003881 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3882 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3883 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003884 goto err;
3885
Frédéric Lécaille47756802022-03-25 09:12:16 +01003886 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3887 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3888 goto out;
3889
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02003890 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
3891 !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
3892 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3893 qel = next_qel;
3894 next_qel = NULL;
3895 goto next_level;
3896 }
3897 else {
3898 struct quic_rx_packet *pkt;
3899 struct mt_list *elt1, elt2;
3900 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
3901
3902 /* Drop these 0-RTT packets */
3903 TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
3904 mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) {
3905 MT_LIST_DELETE_SAFE(elt1);
3906 quic_rx_packet_refdec(pkt);
3907 }
3908 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003909 }
3910
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003911 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003912 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003913 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003914 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003915 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003916
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003917 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003918 QUIC_FL_TLS_SECRETS_DCD)) {
3919 /* Discard the Handshake keys. */
3920 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3921 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3922 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3923 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003924 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003925 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003926 }
3927
3928 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3929 /* There may be remaining handshake to build (acks) */
3930 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3931 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003932 }
3933
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003934 if (!qr)
3935 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003936 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3937 * be considered.
3938 */
3939 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003940 goto err;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003941 ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms,
3942 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003943 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003944 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003945 else if (ret == 0)
3946 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003947
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003948 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003949 goto err;
3950
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003951 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003952 /* Check if there is something to do for the next level.
3953 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003954 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003955 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003956 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003958 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959 goto next_level;
3960 }
3961
Frédéric Lécaille47756802022-03-25 09:12:16 +01003962 out:
3963 if (qr)
3964 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003965 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003966 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003967
3968 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003969 if (qr)
3970 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003971 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003972 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003973}
3974
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003975/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003976static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3977{
3978 int i;
3979
3980 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3981 if (qel->tx.crypto.bufs[i]) {
3982 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3983 qel->tx.crypto.bufs[i] = NULL;
3984 }
3985 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003986 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003987}
3988
3989/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003990 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003991 * Returns 1 if succeeded, 0 if not.
3992 */
3993static int quic_conn_enc_level_init(struct quic_conn *qc,
3994 enum quic_tls_enc_level level)
3995{
3996 struct quic_enc_level *qel;
3997
3998 qel = &qc->els[level];
3999 qel->level = quic_to_ssl_enc_level(level);
4000 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4001 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4002 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004003 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004004
4005 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02004006 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004007 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004008 qel->rx.crypto.offset = 0;
4009 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004010
4011 /* Allocate only one buffer. */
4012 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4013 if (!qel->tx.crypto.bufs)
4014 goto err;
4015
4016 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4017 if (!qel->tx.crypto.bufs[0])
4018 goto err;
4019
4020 qel->tx.crypto.bufs[0]->sz = 0;
4021 qel->tx.crypto.nb_buf = 1;
4022
4023 qel->tx.crypto.sz = 0;
4024 qel->tx.crypto.offset = 0;
4025
4026 return 1;
4027
4028 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004029 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004030 return 0;
4031}
4032
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004033/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4034 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004035 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004036 * This function must only be called by the thread responsible of the quic_conn
4037 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004038 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004039static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004040{
4041 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004042 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004043 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004044 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004045
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004046 /* We must not free the quic-conn if the MUX is still allocated. */
4047 BUG_ON(qc->mux_state == QC_MUX_READY);
4048
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004049 /* free remaining stream descriptors */
4050 node = eb64_first(&qc->streams_by_id);
4051 while (node) {
4052 struct qc_stream_desc *stream;
4053
4054 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4055 node = eb64_next(node);
4056
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004057 /* all streams attached to the quic-conn are released, so
4058 * qc_stream_desc_free will liberate the stream instance.
4059 */
4060 BUG_ON(!stream->release);
4061 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004062 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004063
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004064 if (qc->idle_timer_task) {
4065 task_destroy(qc->idle_timer_task);
4066 qc->idle_timer_task = NULL;
4067 }
4068
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004069 if (qc->timer_task) {
4070 task_destroy(qc->timer_task);
4071 qc->timer_task = NULL;
4072 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004073
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004074 /* remove the connection from receiver cids trees */
4075 ebmb_delete(&qc->odcid_node);
4076 ebmb_delete(&qc->scid_node);
4077 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004078
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004079 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004080 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004081 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004082 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004083 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004084 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004085
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004086 quic_tls_ku_free(qc);
4087 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4088 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004089 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004090 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004091 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004092
4093 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4094 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4095 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004096
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004097 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
4098 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02004099 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004100 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004101
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004102 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4103 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004104 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004105}
4106
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004107static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004108{
4109 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004110 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004111
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004112 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004113
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004114 /* Next application data can be dropped. */
4115 qc->mux_state = QC_MUX_RELEASED;
4116
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004117 /* If the quic-conn timer has already expired free the quic-conn. */
4118 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4119 quic_conn_release(qc);
4120 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
4121 return;
4122 }
4123
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004124 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004125}
4126
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004127/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004128static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004129{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004130 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004131 struct quic_conn *qc;
4132 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004133
4134 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004135 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004136 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004137 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004138 task->expire = TICK_ETERNITY;
4139 pktns = quic_loss_pktns(qc);
4140 if (tick_isset(pktns->tx.loss_time)) {
4141 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4142
4143 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4144 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004145 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4146 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004147 goto out;
4148 }
4149
4150 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004151 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004152 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4153 qc->qcc->subs->events & SUB_RETRY_SEND) {
4154 struct qcc *qcc = qc->qcc;
4155
4156 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4157 tasklet_wakeup(qcc->subs->tasklet);
4158 qcc->subs->events &= ~SUB_RETRY_SEND;
4159 if (!qcc->subs->events)
4160 qcc->subs = NULL;
4161 }
4162 else {
4163 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4164 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4165 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
4166 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
4167 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4168 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004169 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004170 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004171 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004172 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4173 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4174
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004175 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004176 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004177 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004178 iel->pktns->tx.pto_probe = 1;
4179 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004180
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004181 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4182 qc->path->loss.pto_count++;
4183
4184 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004185 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004186
4187 return task;
4188}
4189
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004190/* Parse the Retry token from buffer <token> with <end> a pointer to
4191 * one byte past the end of this buffer. This will extract the ODCID
4192 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004193 *
4194 * Returns 0 on success else non-zero.
4195 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004196static int parse_retry_token(const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004197 struct quic_cid *odcid)
4198{
4199 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004200 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004201
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004202 if (!quic_dec_int(&odcid_len, &token, end))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004203 return 1;
4204
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004205 /* RFC 9000 7.2. Negotiating Connection IDs:
4206 * When an Initial packet is sent by a client that has not previously
4207 * received an Initial or Retry packet from the server, the client
4208 * populates the Destination Connection ID field with an unpredictable
4209 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4210 */
4211 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN)
4212 return 1;
4213
4214 if (end - token < odcid_len + sizeof timestamp)
4215 return 1;
4216
4217 timestamp = ntohl(read_u32(token + odcid_len));
4218 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms)
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004219 return 1;
4220
4221 memcpy(odcid->data, token, odcid_len);
4222 odcid->len = odcid_len;
4223
4224 return 0;
4225}
4226
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004227/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4228 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4229 * for QUIC servers (or haproxy listeners).
4230 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4231 * <token> the token found to be used for this connection with <token_len> as
4232 * length. <saddr> is the source address.
4233 * Returns the connection if succeeded, NULL if not.
4234 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004235static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004236 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004237 const struct quic_cid *odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004238 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004239 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240{
4241 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004242 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004243 /* Initial CID. */
4244 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004245 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004246 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004247
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004248 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004249 qc = pool_zalloc(pool_head_quic_conn);
4250 if (!qc) {
4251 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
4252 goto err;
4253 }
4254
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004255 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4256 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004257 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004258 goto err;
4259 }
4260
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004261 qc->cids = EB_ROOT;
4262 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004263 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004264 struct proxy *prx;
4265
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004266 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004267 prx = l->bind_conf->frontend;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004268
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004269 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4270 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004271 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004272 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004273 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004274 qc->odcid.len = dcid->len;
4275 qc->odcid.addrlen = dcid->addrlen;
4276 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004277
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004278 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004279 if (scid->len)
4280 memcpy(qc->dcid.data, scid->data, scid->len);
4281 qc->dcid.len = scid->len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02004282 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004283 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004284 }
4285 /* QUIC Client (outgoing connection to servers) */
4286 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004287 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004288 if (dcid->len)
4289 memcpy(qc->dcid.data, dcid->data, dcid->len);
4290 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004291 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004292 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004293
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004294 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004295 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004296 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004297 goto err;
4298 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004299
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004300 /* insert the allocated CID in the receiver datagram handler tree */
4301 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004302 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004303
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004304 /* Select our SCID which is the first CID with 0 as sequence number. */
4305 qc->scid = icid->cid;
4306
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004307 /* Packet number spaces initialization. */
4308 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4309 quic_pktns_init(&qc->pktns[i]);
4310 /* QUIC encryption level context initialization. */
4311 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004312 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004313 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004314 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004315 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004316 /* Initialize the packet number space. */
4317 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4318 }
4319
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004320 qc->original_version = qv;
4321 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004322 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4323 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004324 /* TX part. */
4325 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004326 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4327 qc->tx.wbuf = qc->tx.rbuf = 0;
4328 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004329 /* RX part. */
4330 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004331 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004332 for (i = 0; i < QCS_MAX_TYPES; i++)
4333 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004334
4335 qc->nb_pkt_for_cc = 1;
4336 qc->nb_pkt_since_cc = 0;
4337
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004338 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004339 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004340 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004341 goto err;
4342 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004344 /* XXX TO DO: Only one path at this time. */
4345 qc->path = &qc->paths[0];
4346 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4347
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004348 /* required to use MTLIST_IN_LIST */
4349 MT_LIST_INIT(&qc->accept_list);
4350
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004351 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004352 qc->stream_buf_count = 0;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02004353 qc->sendto_err = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004354 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4355
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004356 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4357 icid->stateless_reset_token,
4358 dcid->data, dcid->len,
4359 qc->scid.data, qc->scid.len,
4360 odcid->data, odcid->len, token))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004361 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004362
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004363 if (qc_conn_alloc_ssl_ctx(qc) ||
4364 !quic_conn_init_timer(qc) ||
4365 !quic_conn_init_idle_timer_task(qc))
4366 goto err;
4367
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004368 if (!qc_new_isecs(qc, &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx,
4369 qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004370 goto err;
4371
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004372 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004373
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004374 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004375
4376 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004377 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004378 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4379 if (qc)
4380 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004381 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004382 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004383}
4384
4385/* Initialize the timer task of <qc> QUIC connection.
4386 * Returns 1 if succeeded, 0 if not.
4387 */
4388static int quic_conn_init_timer(struct quic_conn *qc)
4389{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004390 /* Attach this task to the same thread ID used for the connection */
Willy Tarreau87168752022-06-13 16:31:53 +02004391 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004392 if (!qc->timer_task)
4393 return 0;
4394
4395 qc->timer = TICK_ETERNITY;
4396 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004397 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004398
4399 return 1;
4400}
4401
Frédéric Lécaille47756802022-03-25 09:12:16 +01004402/* Rearm the idle timer for <qc> QUIC connection. */
4403static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4404{
4405 unsigned int expire;
4406
4407 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4408 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4409}
4410
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004411/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4412 * which is set to 1 when receiving a packet , and 0 when sending packet
4413 */
4414static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4415{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004416 if (read) {
4417 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4418 }
4419 else {
4420 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4421 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004422 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004423}
4424
4425/* The task handling the idle timeout */
4426static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4427{
4428 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004429 struct quic_counters *prx_counters = qc->prx_counters;
4430 int qc_state = qc->state;
4431 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004432
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004433 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4434 * might free the quic-conn too early via quic_close().
4435 */
4436 qc_notify_close(qc);
4437
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004438 /* If the MUX is still alive, keep the quic-conn. The MUX is
4439 * responsible to call quic_close to release it.
4440 */
4441 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4442 if (qc->mux_state != QC_MUX_READY)
4443 quic_conn_release(qc);
4444
4445 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4446 * least clean some parts of it such as the tasklet.
4447 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004448
4449 /* If the connection did not reached the handshake complete state,
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004450 * the <half_open_conn> counter was not decremented. Note that if
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004451 * a TLS alert was received from the TLS stack, this counter
4452 * has already been decremented.
4453 */
Frédéric Lécaillead9895d2022-05-20 20:50:59 +02004454 if (qc_state < QUIC_HS_ST_COMPLETE && !(qc_flags & QUIC_FL_CONN_TLS_ALERT))
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004455 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004456
4457 return NULL;
4458}
4459
4460/* Initialize the idle timeout task for <qc>.
4461 * Returns 1 if succeeded, 0 if not.
4462 */
4463static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4464{
4465 qc->idle_timer_task = task_new_here();
4466 if (!qc->idle_timer_task)
4467 return 0;
4468
4469 qc->idle_timer_task->process = qc_idle_timer_task;
4470 qc->idle_timer_task->context = qc;
4471 qc_idle_timer_rearm(qc, 1);
4472 task_queue(qc->idle_timer_task);
4473
4474 return 1;
4475}
4476
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4478 * past one byte of this buffer.
4479 */
4480static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4481 struct quic_rx_packet *pkt)
4482{
4483 unsigned char dcid_len, scid_len;
4484
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004485 if (end == *buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004486 return 0;
4487
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004488 /* Destination Connection ID Length */
4489 dcid_len = *(*buf)++;
4490 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4491 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4492 /* XXX MUST BE DROPPED */
4493 return 0;
4494
4495 if (dcid_len) {
4496 /* Check that the length of this received DCID matches the CID lengths
4497 * of our implementation for non Initials packets only.
4498 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004499 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4500 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004501 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502 return 0;
4503
4504 memcpy(pkt->dcid.data, *buf, dcid_len);
4505 }
4506
4507 pkt->dcid.len = dcid_len;
4508 *buf += dcid_len;
4509
4510 /* Source Connection ID Length */
4511 scid_len = *(*buf)++;
4512 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4513 /* XXX MUST BE DROPPED */
4514 return 0;
4515
4516 if (scid_len)
4517 memcpy(pkt->scid.data, *buf, scid_len);
4518 pkt->scid.len = scid_len;
4519 *buf += scid_len;
4520
4521 return 1;
4522}
4523
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004524/* Insert <pkt> RX packet in its <qel> RX packets tree */
4525static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4526{
4527 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004528 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004529 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4530 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4531 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004532}
4533
4534/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004535 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4536 * byte past the end of the buffer containing this packet and <beg> the address of
4537 * the packet first byte.
4538 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4539 * Returns 1 if succeeded, 0 if not.
4540 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004541static inline int qc_try_rm_hp(struct quic_conn *qc,
4542 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004543 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004544 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004545 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004546{
4547 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004548 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004549 struct quic_enc_level *qel;
4550 /* Only for traces. */
4551 struct quic_rx_packet *qpkt_trace;
4552
4553 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004554 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004555 /* The packet number is here. This is also the start minus
4556 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4557 * protection.
4558 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004559 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004560
4561 tel = quic_packet_type_enc_level(pkt->type);
4562 qel = &qc->els[tel];
4563
4564 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004565 /* Note that the following function enables us to unprotect the packet
4566 * number and its length subsequently used to decrypt the entire
4567 * packets.
4568 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004569 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4570 qel->pktns->rx.largest_pn, pn, beg, end)) {
4571 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004572 goto err;
4573 }
4574
4575 /* The AAD includes the packet number field found at <pn>. */
4576 pkt->aad_len = pn - beg + pkt->pnl;
4577 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004578 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004579 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004580 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004581 /* If the packet number space has been discarded, this packet
4582 * will be not parsed.
4583 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004584 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004585 goto out;
4586 }
4587
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004588 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004589 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004590 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4591 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004592 }
4593
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004594 *el = qel;
4595 /* No reference counter incrementation here!!! */
4596 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4597 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4598 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4599 b_add(&qc->rx.buf, pkt->len);
4600 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004601 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004602 return 1;
4603
4604 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004605 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004606 return 0;
4607}
4608
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004609/* Parse the header form from <byte0> first byte of <pkt> packet to set its type.
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004610 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
4611 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004612 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004613static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
4614 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004615 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004616{
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004617 const unsigned char byte0 = **buf;
4618
4619 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004620 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004621 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004622 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004623
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004624 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004625 /* Version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004626 if (!quic_read_uint32(version, (const unsigned char **)buf, end))
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004627 return 0;
4628
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004629 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004630 pkt->type = type;
4631 }
4632 else {
4633 switch (type) {
4634 case 0:
4635 pkt->type = QUIC_PACKET_TYPE_RETRY;
4636 break;
4637 case 1:
4638 pkt->type = QUIC_PACKET_TYPE_INITIAL;
4639 break;
4640 case 2:
4641 pkt->type = QUIC_PACKET_TYPE_0RTT;
4642 break;
4643 case 3:
4644 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
4645 break;
4646 }
4647 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004648 }
4649 else {
4650 pkt->type = QUIC_PACKET_TYPE_SHORT;
4651 *long_header = 0;
4652 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004653
4654 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004655}
4656
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004657/* Return the QUIC version (quic_version struct) with <version> as version number
4658 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004659 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004660static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004661{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004662 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004663
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004664 for (i = 0; i < quic_versions_nb; i++)
4665 if (quic_versions[i].num == version)
4666 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004667
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004668 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004669}
4670
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004671/*
4672 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4673 * address <addr>.
4674 * Implementation of RFC9000 6. Version Negotiation
4675 *
4676 * TODO implement a rate-limiting sending of Version Negotiation packets
4677 *
4678 * Returns 0 on success else non-zero
4679 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004680static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4681 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004682{
4683 char buf[256];
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004684 int i = 0, j;
4685 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004686 const socklen_t addrlen = get_addr_len(addr);
4687
4688 /*
4689 * header form
4690 * long header, fixed bit to 0 for Version Negotiation
4691 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004692 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4693 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004694
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004695 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004696 /* null version for Version Negotiation */
4697 buf[i++] = '\x00';
4698 buf[i++] = '\x00';
4699 buf[i++] = '\x00';
4700 buf[i++] = '\x00';
4701
4702 /* source connection id */
4703 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004704 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004705 i += pkt->scid.len;
4706
4707 /* destination connection id */
4708 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004709 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004710 i += pkt->dcid.len;
4711
4712 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004713 for (j = 0; j < quic_versions_nb; j++) {
4714 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004715 memcpy(&buf[i], &version, sizeof(version));
4716 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004717 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004718
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004719
4720 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4721 return 1;
4722
4723 return 0;
4724}
4725
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004726/* Send a stateless reset packet depending on <pkt> RX packet information
4727 * from <fd> UDP socket to <dst>
4728 * Return 1 if succeeded, 0 if not.
4729 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004730static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004731 struct quic_rx_packet *rxpkt)
4732{
4733 int pktlen, rndlen;
4734 unsigned char pkt[64];
4735 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004736 struct proxy *prx;
4737 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004738
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004739 prx = l->bind_conf->frontend;
4740 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004741 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
4742 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
4743 * Reset is difficult to distinguish from other packets if the recipient requires
4744 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
4745 * that all packets it sends are at least 22 bytes longer than the minimum
4746 * connection ID length that it requests the peer to include in its packets,
4747 * adding PADDING frames as necessary. This ensures that any Stateless Reset
4748 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
4749 * An endpoint that sends a Stateless Reset in response to a packet that is
4750 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
4751 * than the packet it responds to.
4752 */
4753
4754 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
4755 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
4756 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
4757 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
4758 /* Put a header of random bytes */
4759 if (RAND_bytes(pkt, rndlen) != 1)
4760 return 0;
4761
4762 /* Clear the most significant bit, and set the second one */
4763 *pkt = (*pkt & ~0x80) | 0x40;
4764 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
4765 rxpkt->dcid.data, rxpkt->dcid.len))
4766 return 0;
4767
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004768 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004769 return 0;
4770
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004771 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004772 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
4773 return 1;
4774}
4775
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004776/* QUIC server only function.
4777 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
4778 * This is the responsability of the caller to check <aad> size is big enough
4779 * to contain these data.
4780 * Return the number of bytes copied to <aad>.
4781 */
4782static int quic_generate_retry_token_aad(unsigned char *aad,
4783 uint32_t version,
4784 const struct quic_cid *cid,
4785 const struct sockaddr_storage *addr)
4786{
4787 unsigned char *p;
4788
4789 p = aad;
4790 memcpy(p, &version, sizeof version);
4791 p += sizeof version;
4792 p += quic_saddr_cpy(p, addr);
4793 memcpy(p, cid->data, cid->len);
4794 p += cid->len;
4795
4796 return p - aad;
4797}
4798
4799/* QUIC server only function.
4800 * Generate the token to be used in Retry packets. The token is written to
4801 * <buf> whith <len> as length. <odcid> is the original destination connection
4802 * ID and <dcid> is our side destination connection ID (or client source
4803 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004804 * Returns the length of the encoded token or 0 on error.
4805 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004806static int quic_generate_retry_token(unsigned char *buf, size_t len,
4807 const uint32_t version,
4808 const struct quic_cid *odcid,
4809 const struct quic_cid *dcid,
4810 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004811{
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004812 unsigned char *p;
4813 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4814 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4815 size_t aadlen;
4816 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
4817 unsigned char key[QUIC_TLS_KEY_LEN];
4818 unsigned char iv[QUIC_TLS_IV_LEN];
4819 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4820 size_t seclen = strlen(global.cluster_secret);
4821 EVP_CIPHER_CTX *ctx = NULL;
4822 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4823 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004824
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004825 /* We copy the odcid into the token, prefixed by its one byte
4826 * length, the format token byte. It is followed by an AEAD TAG, and finally
4827 * the random bytes used to derive the secret to encrypt the token.
4828 */
4829 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004830 return 0;
4831
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004832 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
4833 if (RAND_bytes(salt, sizeof salt) != 1)
4834 goto err;
4835
4836 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
4837 salt, sizeof salt, sec, seclen))
4838 goto err;
4839
4840 if (!quic_tls_tx_ctx_init(&ctx, aead, key))
4841 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004842
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004843 /* Token build */
4844 p = buf;
4845 *p++ = QUIC_TOKEN_FMT_RETRY,
4846 *p++ = odcid->len;
4847 memcpy(p, odcid->data, odcid->len);
4848 p += odcid->len;
4849 write_u32(p, htonl(timestamp));
4850 p += sizeof timestamp;
4851
4852 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
4853 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv))
4854 goto err;
4855
4856 p += QUIC_TLS_TAG_LEN;
4857 memcpy(p, salt, sizeof salt);
4858 p += sizeof salt;
4859 EVP_CIPHER_CTX_free(ctx);
4860
4861 return p - buf;
4862
4863 err:
4864 if (ctx)
4865 EVP_CIPHER_CTX_free(ctx);
4866 return 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004867}
4868
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004869/* QUIC server only function.
4870 * Check the validity of the Retry token from <token> buffer with <tokenlen>
4871 * as length. If valid, the ODCID of <qc> QUIC connection will be put
4872 * into <odcid> connection ID. <dcid> is our side destination connection ID
4873 * of client source connection ID.
4874 * Return 1 if succeeded, 0 if not.
4875 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004876static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
4877 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004878 struct quic_cid *odcid,
4879 const struct quic_cid *dcid,
4880 struct quic_conn *qc,
4881 struct sockaddr_storage *addr)
4882{
4883 unsigned char buf[128];
4884 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4885 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4886 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004887 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004888 unsigned char key[QUIC_TLS_KEY_LEN];
4889 unsigned char iv[QUIC_TLS_IV_LEN];
4890 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4891 size_t seclen = strlen(global.cluster_secret);
4892 EVP_CIPHER_CTX *ctx = NULL;
4893 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4894
4895 if (sizeof buf < tokenlen)
4896 return 0;
4897
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004898 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004899 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
4900 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02004901 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
4902 TRACE_PROTO("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004903 return 0;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02004904 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004905
4906 if (!quic_tls_rx_ctx_init(&ctx, aead, key))
4907 goto err;
4908
4909 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
4910 if (!quic_tls_decrypt2(buf, token + 1, tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, aad, aadlen,
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02004911 ctx, aead, key, iv)) {
4912 TRACE_PROTO("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004913 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02004914 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004915
4916 if (parse_retry_token(buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
4917 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4918 goto err;
4919 }
4920
4921 EVP_CIPHER_CTX_free(ctx);
4922 return 1;
4923
4924 err:
4925 if (ctx)
4926 EVP_CIPHER_CTX_free(ctx);
4927 return 0;
4928}
4929
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004930/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4931 * the Initial <pkt> packet.
4932 *
4933 * Returns 0 on success else non-zero.
4934 */
4935static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004936 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004937{
4938 unsigned char buf[128];
4939 int i = 0, token_len;
4940 const socklen_t addrlen = get_addr_len(addr);
4941 struct quic_cid scid;
4942
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004943 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
4944 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
4945 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004946 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004947 buf[i++] = *((unsigned char *)&qv->num + 3);
4948 buf[i++] = *((unsigned char *)&qv->num + 2);
4949 buf[i++] = *((unsigned char *)&qv->num + 1);
4950 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004951
4952 /* Use the SCID from <pkt> for Retry DCID. */
4953 buf[i++] = pkt->scid.len;
4954 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4955 i += pkt->scid.len;
4956
4957 /* Generate a new CID to be used as SCID for the Retry packet. */
4958 scid.len = QUIC_HAP_CID_LEN;
4959 if (RAND_bytes(scid.data, scid.len) != 1)
4960 return 1;
4961
4962 buf[i++] = scid.len;
4963 memcpy(&buf[i], scid.data, scid.len);
4964 i += scid.len;
4965
4966 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004967 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004968 &pkt->dcid, &pkt->scid, addr)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004969 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004970
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004971 i += token_len;
4972
4973 /* token integrity tag */
4974 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4975 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004976 pkt->dcid.len, buf, i, qv)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004977 return 1;
4978 }
4979
4980 i += QUIC_TLS_TAG_LEN;
4981
4982 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4983 return 1;
4984
4985 return 0;
4986}
4987
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004988/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4989 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4990 * concatenated to the <pkt> DCID field.
4991 *
4992 * Returns the instance or NULL if not found.
4993 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004994static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004995 struct listener *l,
4996 struct sockaddr_storage *saddr)
4997{
4998 struct quic_conn *qc = NULL;
4999 struct ebmb_node *node;
5000 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005001 /* set if the quic_conn is found in the second DCID tree */
5002 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005003
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005004 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5005 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5006 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5007 /* DCIDs of first packets coming from multiple clients may have
5008 * the same values. Let's distinguish them by concatenating the
5009 * socket addresses.
5010 */
5011 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005012 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005013 pkt->dcid.len + pkt->dcid.addrlen);
5014 if (node) {
5015 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5016 goto end;
5017 }
5018 }
5019
5020 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5021 * also for INITIAL/0-RTT non-first packets with the final DCID in
5022 * used.
5023 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005024 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005025 if (!node)
5026 goto end;
5027
5028 id = ebmb_entry(node, struct quic_connection_id, node);
5029 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005030 found_in_dcid = 1;
5031
5032 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005033 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5034 * If already done, this is a noop.
5035 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01005036 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005037 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005038
5039 return qc;
5040}
5041
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005042/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5043 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5044 * parameters of this session.
5045 * This is the responsibility of the caller to check the validity of all the
5046 * pointers passed as parameter to this function.
5047 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5048 * CO_ER_SSL_NO_MEM.
5049 */
5050static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5051 unsigned char *params, size_t params_len)
5052{
5053 int retry;
5054
5055 retry = 1;
5056 retry:
5057 *ssl = SSL_new(ssl_ctx);
5058 if (!*ssl) {
5059 if (!retry--)
5060 goto err;
5061
5062 pool_gc(NULL);
5063 goto retry;
5064 }
5065
5066 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005067 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005068 SSL_free(*ssl);
5069 *ssl = NULL;
5070 if (!retry--)
5071 goto err;
5072
5073 pool_gc(NULL);
5074 goto retry;
5075 }
5076
5077 return 0;
5078
5079 err:
5080 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5081 return -1;
5082}
5083
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005084/* Finalize <qc> QUIC connection:
5085 * - initialize the Initial QUIC TLS context for negotiated version,
5086 * - derive the secrets for this context,
5087 * - encode the transport parameters to be sent,
5088 * - set them into the TLS stack,
5089 * - initialize ->max_ack_delay and max_idle_timeout,
5090 *
5091 * MUST be called after having received the remote transport parameters.
5092 * Return 1 if succeeded, 0 if not.
5093 */
5094int qc_conn_finalize(struct quic_conn *qc, int server)
5095{
5096 struct quic_transport_params *tx_tp = &qc->tx.params;
5097 struct quic_transport_params *rx_tp = &qc->rx.params;
5098 const struct quic_version *ver;
5099
5100 if (tx_tp->version_information.negotiated_version &&
5101 tx_tp->version_information.negotiated_version != qc->original_version) {
5102 qc->negotiated_version =
5103 qc->tx.params.version_information.negotiated_version;
5104 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5105 qc->odcid.data, qc->odcid.len, !server))
5106 return 0;
5107
5108 ver = qc->negotiated_version;
5109 }
5110 else {
5111 ver = qc->original_version;
5112 }
5113
5114 qc->enc_params_len =
5115 quic_transport_params_encode(qc->enc_params,
5116 qc->enc_params + sizeof qc->enc_params,
5117 &qc->rx.params, ver, 1);
5118 if (!qc->enc_params_len)
5119 return 0;
5120
5121 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len))
5122 return 0;
5123
5124 if (tx_tp->max_ack_delay)
5125 qc->max_ack_delay = tx_tp->max_ack_delay;
5126
5127 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5128 qc->max_idle_timeout =
5129 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5130 else
5131 qc->max_idle_timeout =
5132 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5133
5134 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5135
5136 return 1;
5137}
5138
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005139/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5140 * used to process <qc> received packets. The allocated context is stored in
5141 * <qc.xprt_ctx>.
5142 *
5143 * Returns 0 on success else non-zero.
5144 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005145static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005146{
5147 struct bind_conf *bc = qc->li->bind_conf;
5148 struct ssl_sock_ctx *ctx = NULL;
5149
5150 ctx = pool_zalloc(pool_head_quic_conn_ctx);
5151 if (!ctx)
5152 goto err;
5153
5154 ctx->wait_event.tasklet = tasklet_new();
5155 if (!ctx->wait_event.tasklet)
5156 goto err;
5157
5158 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5159 ctx->wait_event.tasklet->context = ctx;
5160 ctx->wait_event.events = 0;
5161 ctx->subs = NULL;
5162 ctx->xprt_ctx = NULL;
5163 ctx->qc = qc;
5164
5165 /* Set tasklet tid based on the SCID selected by us for this
5166 * connection. The upper layer will also be binded on the same thread.
5167 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005168 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005169
5170 if (qc_is_listener(qc)) {
5171 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5172 qc->enc_params, qc->enc_params_len) == -1) {
5173 goto err;
5174 }
5175
5176 /* Enabling 0-RTT */
5177 if (bc->ssl_conf.early_data)
5178 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5179
5180 SSL_set_accept_state(ctx->ssl);
5181 }
5182
5183 ctx->xprt = xprt_get(XPRT_QUIC);
5184
5185 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005186 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005187
5188 return 0;
5189
5190 err:
5191 if (ctx && ctx->wait_event.tasklet)
5192 tasklet_free(ctx->wait_event.tasklet);
5193 pool_free(pool_head_quic_conn_ctx, ctx);
5194
5195 return 1;
5196}
5197
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005198/* Check that all the bytes between <buf> included and <end> address
5199 * excluded are null. This is the responsability of the caller to
5200 * check that there is at least one byte between <buf> end <end>.
5201 * Return 1 if this all the bytes are null, 0 if not.
5202 */
5203static inline int quic_padding_check(const unsigned char *buf,
5204 const unsigned char *end)
5205{
5206 while (buf < end && !*buf)
5207 buf++;
5208
5209 return buf == end;
5210}
5211
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005212/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5213 * end of this buffer past one byte and populate <pkt> RX packet structure
5214 * with the information collected from the packet.
5215 * This function sets ->len <pkt> field value to the end of the packet past one
5216 * byte to enable the caller to run this function again to continue to parse
5217 * the remaing QUIC packets carried by the datagram.
5218 * Note that this function always sets this ->len value. If a paquet could
5219 * not be correctly found, ->len value will be set to the remaining number
5220 * bytes in the datagram to entirely consume this latter.
5221 */
5222static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5223 struct quic_rx_packet *pkt, int first_pkt,
5224 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005225{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005226 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005227 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005228 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005229 struct proxy *prx;
5230 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005231 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005232 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005233 size_t b_cspace;
5234 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005235 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005236 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005237
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005238 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005239 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005240 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005241 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005242 TRACE_ENTER(QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005243 l = dgram->owner;
5244 prx = l->bind_conf->frontend;
5245 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005246 /* This ist only to please to traces and distinguish the
5247 * packet with parsed packet number from others.
5248 */
5249 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005250 if (end <= buf) {
5251 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5252 goto drop;
5253 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005254
5255 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005256 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005257 if (!first_pkt && quic_padding_check(buf, end)) {
5258 /* Some browsers may pad the remaining datagram space with null bytes.
5259 * That is what we called add padding out of QUIC packets. Such
5260 * datagrams must be considered as valid. But we can only consume
5261 * the remaining space.
5262 */
5263 pkt->len = end - buf;
5264 goto drop_no_conn;
5265 }
5266
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005267 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005268 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005269 }
5270
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005271 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005272 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005273 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5274 goto drop;
5275 }
5276
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005277 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005278 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005279 struct quic_cid odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005280 int check_token = 0;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005281
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005282 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005283 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005284 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005285 }
5286
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005287 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5288 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005289 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005290 }
5291 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5292 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005293 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005294 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005295 }
5296
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005297 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5298 * they must have the same DCID.
5299 */
5300 if (!first_pkt &&
5301 (pkt->dcid.len != dgram->dcid_len ||
5302 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5303 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005304 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005305 }
5306
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005307 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005308 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005309 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005310 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005311 }
5312
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005313 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005314 qv = qc_supported_version(version);
5315 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005316 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005317 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005318 TRACE_PROTO("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005319 goto err;
5320 }
5321
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005322 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005323 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005324 }
5325
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005326 /* For Initial packets, and for servers (QUIC clients connections),
5327 * there is no Initial connection IDs storage.
5328 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005329 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005330 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005331
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005332 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5333 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005334 TRACE_PROTO("Packet dropped",
5335 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005336 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005337 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005338
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005339 /* TODO Retry should be automatically activated if
5340 * suspect network usage is detected.
5341 */
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005342 if (global.cluster_secret) {
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005343 if (!token_len) {
Willy Tarreau787e92a2022-05-20 16:06:01 +02005344 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005345 TRACE_PROTO("Initial without token, sending retry",
5346 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005347 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005348 TRACE_PROTO("Error during Retry generation",
5349 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005350 goto err;
5351 }
5352
5353 HA_ATOMIC_INC(&prx_counters->retry_sent);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005354 goto err;
5355 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005356 }
5357 else {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005358 check_token = 1;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005359 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005360 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005361
5362 pkt->token = buf;
5363 pkt->token_len = token_len;
5364 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005365 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005366 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005367 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005368 TRACE_PROTO("Packet dropped",
5369 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005370 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005371 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005372 }
5373
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005374 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5375 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005376 TRACE_PROTO("Packet dropped",
5377 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005378 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005379 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005380
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005381 payload = buf;
5382 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005383 if (drop_no_conn)
5384 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005385
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005386 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005387 if (check_token && pkt->token) {
5388 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5389 const struct quic_version *ver = qc ? qc->original_version : qv;
5390 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5391 &pkt->scid, qc, &dgram->saddr)) {
5392 HA_ATOMIC_INC(&prx_counters->retry_error);
5393 TRACE_PROTO("Wrong retry token",
5394 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5395 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5396 * with an INVALID_TOKEN error.
5397 */
5398 goto drop;
5399 }
5400
5401 HA_ATOMIC_INC(&prx_counters->retry_validated);
5402 }
5403 else {
5404 /* TODO: New token check */
5405 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5406 goto drop;
5407 }
5408 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005409 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005410 int ipv4;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005411 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005412
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005413 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005414 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005415 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005416 }
5417
Willy Tarreau787e92a2022-05-20 16:06:01 +02005418 if (global.cluster_secret && !pkt->token_len && !(l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) &&
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005419 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005420 TRACE_PROTO("Initial without token, sending retry",
5421 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005422 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005423 TRACE_PROTO("Error during Retry generation",
5424 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005425 goto err;
5426 }
5427
5428 HA_ATOMIC_INC(&prx_counters->retry_sent);
5429 goto err;
5430 }
5431
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005432 /* RFC 9000 7.2. Negotiating Connection IDs:
5433 * When an Initial packet is sent by a client that has not previously
5434 * received an Initial or Retry packet from the server, the client
5435 * populates the Destination Connection ID field with an unpredictable
5436 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5437 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01005438 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005439 TRACE_PROTO("dropped packet",
5440 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005441 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01005442 }
5443
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005444 pkt->saddr = dgram->saddr;
5445 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005446 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, &odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02005447 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005448 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005449 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005450
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005451 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005452 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005453 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005454 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005455
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005456 /* If the insertion failed, it means that another
5457 * thread has already allocated a QUIC connection for
5458 * the same CID. Liberate our allocated connection.
5459 */
5460 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005461 qc_to_purge = qc;
5462
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005463 qc = ebmb_entry(n, struct quic_conn, odcid_node);
5464 pkt->qc = qc;
5465 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005466
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005467 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005468 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01005469 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005470 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005471 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01005472 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005473 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005474 }
5475 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005476 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005477 }
5478 }
5479 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005480 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005481 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005482 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005483 }
5484
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005485 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005486 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005487
5488 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5489 * they must have the same DCID.
5490 */
5491 if (!first_pkt &&
5492 (pkt->dcid.len != dgram->dcid_len ||
5493 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5494 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005495 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005496 }
5497
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005498 buf += QUIC_HAP_CID_LEN;
5499
5500 /* A short packet is the last one of a UDP datagram. */
5501 payload = buf;
5502 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005503
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005504 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005505 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005506 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005507 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005508 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005509 TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005510 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005511 }
5512
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005513 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005514 }
5515
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005516 if (qc->flags & QUIC_FL_CONN_CLOSING) {
5517 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
5518 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
5519 qc->nb_pkt_for_cc++;
5520 qc->nb_pkt_since_cc = 0;
5521 }
5522 /* Skip the entire datagram */
5523 pkt->len = end - beg;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005524 TRACE_PROTO("Closing state connection",
5525 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005526 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005527 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005528
5529 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5530 * they must have the same DCID.
5531 *
5532 * This check must be done after the final update to pkt.len to
5533 * properly drop the packet on failure.
5534 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005535 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005536 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005537 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005538 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005539 /* Reset the anti-amplification bit. It will be set again
5540 * when sending the next packet if reached again.
5541 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005542 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
5543 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005544 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005545 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005546
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005547 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005548
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005549 if (qc->err_code) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005550 TRACE_PROTO("Connection error",
5551 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005552 goto out;
5553 }
5554
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005555 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005556 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005557 b_cspace = b_contig_space(&qc->rx.buf);
5558 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02005559 /* Do not consume buf if space not at the end. */
5560 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005561 TRACE_PROTO("Packet dropped",
5562 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle80d05722022-05-16 18:13:56 +02005563 goto err;
5564 }
5565
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005566 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005567 if (b_cspace) {
5568 b_putchr(&qc->rx.buf, 0x00);
5569 b_cspace--;
5570 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005571 b_add(&qc->rx.buf, b_cspace);
5572 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005573 TRACE_PROTO("Too big packet",
5574 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01005575 qc_list_all_rx_pkts(qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005576 goto drop;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005577 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005578 }
5579
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005580 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005581 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005582 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02005583 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005584
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005585 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005586 if (pkt->aad_len)
5587 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005588 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02005589 /* Wake up the connection packet handler task from here only if all
5590 * the contexts have been initialized, especially the mux context
5591 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
5592 * will start it if these contexts for the connection are not already
5593 * initialized.
5594 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005595 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005596 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005597 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005598
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005599 drop_no_conn:
5600 if (drop_no_conn)
5601 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005602 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005603
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005604 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005605
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005606 drop:
5607 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005608 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005609 /* Wakeup the I/O handler callback if the PTO timer must be armed.
5610 * This cannot be done by this thread.
5611 */
5612 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005613 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005614 if (conn_ctx && conn_ctx->wait_event.tasklet)
5615 tasklet_wakeup(conn_ctx->wait_event.tasklet);
5616 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01005617 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005618 if (!pkt->len)
5619 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005620 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005621 qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005622}
5623
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005624/* This function builds into <buf> buffer a QUIC long packet header.
5625 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005626 */
5627static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005628 int type, size_t pn_len,
5629 struct quic_conn *conn, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005630{
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005631 if (end - *buf < sizeof ver->num + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005632 return 0;
5633
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005634 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005635 /* #0 byte flags */
5636 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5637 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5638 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005639 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005640 *(*buf)++ = conn->dcid.len;
5641 /* Destination connection ID */
5642 if (conn->dcid.len) {
5643 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5644 *buf += conn->dcid.len;
5645 }
5646 /* Source connection ID */
5647 *(*buf)++ = conn->scid.len;
5648 if (conn->scid.len) {
5649 memcpy(*buf, conn->scid.data, conn->scid.len);
5650 *buf += conn->scid.len;
5651 }
5652
5653 return 1;
5654}
5655
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005656/* This function builds into <buf> buffer a QUIC short packet header.
5657 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005658 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005659static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5660 size_t pn_len, struct quic_conn *conn,
5661 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005662{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005663 if (end - *buf < 1 + conn->dcid.len)
5664 return 0;
5665
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005666 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005667 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5668 ((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 +01005669 /* Destination connection ID */
5670 if (conn->dcid.len) {
5671 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5672 *buf += conn->dcid.len;
5673 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005674
5675 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005676}
5677
5678/* Apply QUIC header protection to the packet with <buf> as first byte address,
5679 * <pn> as address of the Packet number field, <pnlen> being this field length
5680 * with <aead> as AEAD cipher and <key> as secret key.
5681 * Returns 1 if succeeded or 0 if failed.
5682 */
5683static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5684 const EVP_CIPHER *aead, const unsigned char *key)
5685{
5686 int i, ret, outlen;
5687 EVP_CIPHER_CTX *ctx;
5688 /* We need an IV of at least 5 bytes: one byte for bytes #0
5689 * and at most 4 bytes for the packet number
5690 */
5691 unsigned char mask[5] = {0};
5692
5693 ret = 0;
5694 ctx = EVP_CIPHER_CTX_new();
5695 if (!ctx)
5696 return 0;
5697
5698 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5699 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5700 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5701 goto out;
5702
5703 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5704 for (i = 0; i < pnlen; i++)
5705 pn[i] ^= mask[i + 1];
5706
5707 ret = 1;
5708
5709 out:
5710 EVP_CIPHER_CTX_free(ctx);
5711
5712 return ret;
5713}
5714
5715/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5716 * ACK ranges if needed to a value below <limit> in bytes.
5717 * Return 1 if succeeded, 0 if not.
5718 */
5719static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5720{
5721 size_t room, ack_delay_sz;
5722
5723 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5724 /* A frame is made of 1 byte for the frame type. */
5725 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005726 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005727 return 0;
5728
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005729 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005730}
5731
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005732/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5733 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5734 * with <room> as available room, and <*len> the packet Length field initialized
5735 * with the number of bytes already present in this buffer which must be taken
5736 * into an account for the Length packet field value. <headlen> is the number of
5737 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005738 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005739 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005740 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005741 * Return 1 if at least one ack-eleciting frame could be built, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005742 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005743static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005744 size_t room, size_t *len, size_t headlen,
5745 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005746 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005747{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005748 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005749 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005750
Frédéric Lécailleea604992020-12-24 13:01:37 +01005751 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005752 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005753 return 0;
5754
Frédéric Lécailleea604992020-12-24 13:01:37 +01005755 /* If we are not probing we must take into an account the congestion
5756 * control window.
5757 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005758 if (!qel->pktns->tx.pto_probe) {
5759 size_t remain = quic_path_prep_data(qc->path);
5760
5761 if (headlen > remain)
5762 return 0;
5763
5764 room = QUIC_MIN(room, remain - headlen);
5765 }
5766
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005767 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005768 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005769
5770 /* NOTE: switch/case block inside a loop, a successful status must be
5771 * returned by this function only if at least one frame could be built
5772 * in the switch/case block.
5773 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005774 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005775 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005776 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005777
Frédéric Lécailleea604992020-12-24 13:01:37 +01005778 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005779 break;
5780
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005781 switch (cf->type) {
5782 case QUIC_FT_CRYPTO:
5783 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005784 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005785 /* Compute the length of this CRYPTO frame header */
5786 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5787 /* Compute the data length of this CRyPTO frame. */
5788 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5789 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005790 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005791 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005792 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005793
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005794 /* CRYPTO frame length. */
5795 flen = hlen + quic_int_getsize(dlen) + dlen;
5796 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005797 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005798 /* Add the CRYPTO data length and its encoded length to the packet
5799 * length and the length of this length.
5800 */
5801 *len += flen;
5802 room -= flen;
5803 if (dlen == cf->crypto.len) {
5804 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005805 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005806 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005807 }
5808 else {
5809 struct quic_frame *new_cf;
5810
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005811 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005812 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005813 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005814 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005815 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005816
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005817 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005818 new_cf->type = QUIC_FT_CRYPTO;
5819 new_cf->crypto.len = dlen;
5820 new_cf->crypto.offset = cf->crypto.offset;
5821 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005822 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005823 /* Consume <dlen> bytes of the current frame. */
5824 cf->crypto.len -= dlen;
5825 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005826 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005827 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005828
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005829 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005830 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
5831 struct eb64_node *node = NULL;
5832 struct qc_stream_desc *stream_desc = NULL;
5833 struct quic_stream *strm = &cf->stream;
5834
5835 /* As this frame has been already lost, ensure the stream is always
5836 * available or the range of this frame is not consumed before
5837 * resending it.
5838 */
5839 node = eb64_lookup(&qc->streams_by_id, strm->id);
5840 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005841 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005842 LIST_DELETE(&cf->list);
5843 pool_free(pool_head_quic_frame, cf);
5844 continue;
5845 }
5846
5847 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
5848 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
5849 TRACE_PROTO("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005850 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005851 LIST_DELETE(&cf->list);
5852 pool_free(pool_head_quic_frame, cf);
5853 continue;
5854 }
5855 else if (strm->offset.key < stream_desc->ack_offset) {
5856 strm->offset.key = stream_desc->ack_offset;
5857 TRACE_PROTO("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005858 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005859 }
5860 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005861 /* Note that these frames are accepted in short packets only without
5862 * "Length" packet field. Here, <*len> is used only to compute the
5863 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005864 *
5865 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005866 * excepting the variable ones. Note that +1 is for the type of this frame.
5867 */
5868 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005869 ((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 +02005870 /* Compute the data length of this STREAM frame. */
5871 avail_room = room - hlen - *len;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005872 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005873 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005874 if ((ssize_t)avail_room <= 0)
5875 continue;
5876
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005877 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5878 dlen = max_available_room(avail_room, &dlen_sz);
5879 if (dlen > cf->stream.len) {
5880 dlen = cf->stream.len;
5881 }
5882 dlen_sz = quic_int_getsize(dlen);
5883 flen = hlen + dlen_sz + dlen;
5884 }
5885 else {
5886 dlen = QUIC_MIN(avail_room, cf->stream.len);
5887 flen = hlen + dlen;
5888 }
5889 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005890 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005891 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005892 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005893 /* Add the STREAM data length and its encoded length to the packet
5894 * length and the length of this length.
5895 */
5896 *len += flen;
5897 room -= flen;
5898 if (dlen == cf->stream.len) {
5899 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005900 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005901 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005902
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005903 /* The MUX stream might be released at this
5904 * stage. This can most notably happen on
5905 * retransmission.
5906 */
5907 if (qc->mux_state == QC_MUX_READY &&
5908 !cf->stream.stream->release) {
5909 qcc_streams_sent_done(cf->stream.stream->ctx,
5910 cf->stream.len,
5911 cf->stream.offset.key);
5912 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005913 }
5914 else {
5915 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005916 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005917
5918 new_cf = pool_zalloc(pool_head_quic_frame);
5919 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005920 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005921 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005922 }
5923
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005924 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005925 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005926 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005927 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005928 new_cf->stream.id = cf->stream.id;
5929 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5930 new_cf->stream.offset = cf->stream.offset;
5931 new_cf->stream.len = dlen;
5932 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5933 /* FIN bit reset */
5934 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5935 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005936 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005937 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5938 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005939 cf_buf = b_make(b_orig(cf->stream.buf),
5940 b_size(cf->stream.buf),
5941 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005942 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005943 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005944 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005945
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005946 /* The MUX stream might be released at this
5947 * stage. This can most notably happen on
5948 * retransmission.
5949 */
5950 if (qc->mux_state == QC_MUX_READY &&
5951 !cf->stream.stream->release) {
5952 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5953 new_cf->stream.len,
5954 new_cf->stream.offset.key);
5955 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005956 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005957
5958 /* TODO the MUX is notified about the frame sending via
5959 * previous qcc_streams_sent_done call. However, the
5960 * sending can fail later, for example if the sendto
5961 * system call returns an error. As the MUX has been
5962 * notified, the transport layer is responsible to
5963 * bufferize and resent the announced data later.
5964 */
5965
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005966 break;
5967
5968 default:
5969 flen = qc_frm_len(cf);
5970 BUG_ON(!flen);
5971 if (flen > room)
5972 continue;
5973
5974 *len += flen;
5975 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005976 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005977 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005978 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005979 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005980
5981 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01005982 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005983 }
5984
Frédéric Lécailleea604992020-12-24 13:01:37 +01005985 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005986}
5987
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005988/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005989 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
5990 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01005991 * filling the buffer with as much frames as possible from <frms> list of
5992 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005993 * 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 +02005994 * 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 +02005995 * having returned from this function.
5996 * 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 +01005997 * number field in this packet. <pn_len> will also have the packet number
5998 * length as value.
5999 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006000 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006001 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006002static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6003 size_t dglen, struct quic_tx_packet *pkt,
6004 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006005 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006006 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006007 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006008{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006009 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006010 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006011 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6012 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006013 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006014 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006015 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006016 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006017 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006018 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006019
Frédéric Lécailleea604992020-12-24 13:01:37 +01006020 /* Length field value with CRYPTO frames if present. */
6021 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006022 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006023 /* When not probing, and no immediate close is required, reduce the size of this
6024 * buffer to respect the congestion controller window.
6025 * 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 +01006026 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006027 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006028 size_t path_room;
6029
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006030 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006031 if (end - beg > path_room)
6032 end = beg + path_room;
6033 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006034
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006035 /* Ensure there is enough room for the TLS encryption tag and a zero token
6036 * length field if any.
6037 */
6038 if (end - pos < QUIC_TLS_TAG_LEN +
6039 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6040 goto no_room;
6041
6042 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006043 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006044 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006045 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006046 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006047 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006048 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006049 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006050 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006051 goto no_room;
6052
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006053 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
6054 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006055 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006056 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006057 /* Build an ACK frame if required. */
6058 ack_frm_len = 0;
Frédéric Lécaille337108e2022-04-25 10:38:27 +02006059 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006060 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006061 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006062 ack_frm.tx_ack.arngs = arngs;
6063 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6064 qel->pktns->tx.ack_delay =
6065 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6066 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6067 }
6068 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006069 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6070 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6071 * that from here, we do not know if we will have to send a PING frame.
6072 * This will be decided after having computed the ack-eliciting frames
6073 * to be added to this packet.
6074 */
6075 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 +01006076 if (!ack_frm_len)
6077 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006078 }
6079
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006080 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006081 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006082 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006083 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006084 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006085
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006086 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006087 /* Initialize the length of the frames built below to <len>.
6088 * If any frame could be successfully built by qc_build_frms(),
6089 * we will have len_frms > len.
6090 */
6091 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006092 if (!qc_build_frms(&frm_list, frms,
6093 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01006094 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006095 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006096 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6097 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006098 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006099 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006100
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006101 /* Length (of the remaining data). Must not fail because, the buffer size
6102 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6103 * for the encryption tag. It must be taken into an account for the length
6104 * of this packet.
6105 */
6106 if (len_frms)
6107 len = len_frms + QUIC_TLS_TAG_LEN;
6108 else
6109 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006110 /* CONNECTION_CLOSE frame */
6111 if (cc) {
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006112 cc_frm.type = qc->flags & QUIC_FL_CONN_APP_ALERT ?
6113 QUIC_FT_CONNECTION_CLOSE_APP : QUIC_FT_CONNECTION_CLOSE;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006114
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006115 cc_frm.connection_close.error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006116 len += qc_frm_len(&cc_frm);
6117 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006118 add_ping_frm = 0;
6119 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006120 len_sz = quic_int_getsize(len);
6121 /* Add this packet size to <dglen> */
6122 dglen += head_len + len_sz + len;
6123 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6124 /* This is a maximum padding size */
6125 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6126 /* The length field value is of this packet is <len> + <padding_len>
6127 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006128 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006129 * sizes from <padding_len>.
6130 */
6131 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6132 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006133 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006134 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006135 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006136 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006137 add_ping_frm = 1;
6138 len += 1;
6139 }
6140 /* 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 +01006141 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006142 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6143 }
6144
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006145 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6146 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006147
6148 /* Packet number field address. */
6149 *buf_pn = pos;
6150
6151 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006152 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6153 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006154
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006155 if (ack_frm_len) {
6156 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6157 goto no_room;
6158
6159 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006160 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006161 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006162
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006163 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006164 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006165 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006166 unsigned char *spos = pos;
6167
6168 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006169 ssize_t room = end - pos;
6170 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006171 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006172 /* TODO: this should not have happened if qc_build_frms()
6173 * had correctly computed and sized the frames to be
6174 * added to this packet. Note that <cf> was added
6175 * from <frm_list> to <frms> list by qc_build_frms().
6176 */
6177 LIST_DELETE(&cf->list);
6178 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006179 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006180 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006181
6182 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006183 quic_tx_packet_refinc(pkt);
6184 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006185 }
6186 }
6187
6188 /* Build a PING frame if needed. */
6189 if (add_ping_frm) {
6190 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006191 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006192 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006193 }
6194
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006195 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006196 if (cc) {
6197 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6198 goto no_room;
6199
6200 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6201 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006202
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006203 /* Build a PADDING frame if needed. */
6204 if (padding_len) {
6205 frm.type = QUIC_FT_PADDING;
6206 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006207 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006208 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006209 }
6210
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006211 /* If this packet is ack-eliciting and we are probing let's
6212 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006213 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006214 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6215 qel->pktns->tx.pto_probe)
6216 qel->pktns->tx.pto_probe--;
6217
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006218 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006219 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006220 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006221
6222 return 1;
6223
6224 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006225 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006226 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailled8b798d2022-04-25 17:48:40 +02006227 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006228
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006229 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006230}
6231
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006232static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006233{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006234 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006235 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006236 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006237 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006238 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006239 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006240 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006241 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006242 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006243 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006244}
6245
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006246/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006247 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6248 * of prebuilt frames.
6249 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006250 * Return -2 if the packet could not be allocated or encrypted for any reason,
6251 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006252 * XXX NOTE XXX
6253 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6254 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6255 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006256 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006257static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6258 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006259 struct quic_enc_level *qel,
6260 struct quic_tls_ctx *tls_ctx, struct list *frms,
6261 struct quic_conn *qc, const struct quic_version *ver,
6262 size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006263 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006264{
6265 /* The pointer to the packet number field. */
6266 unsigned char *buf_pn;
6267 unsigned char *beg, *end, *payload;
6268 int64_t pn;
6269 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006270 struct quic_tx_packet *pkt;
6271
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006272 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006273 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006274 pkt = pool_alloc(pool_head_quic_tx_packet);
6275 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006276 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006277 *err = -2;
6278 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006279 }
6280
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006281 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006282 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006283 pn_len = 0;
6284 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006285
6286 pn = qel->pktns->tx.next_pn + 1;
6287 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006288 padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006289 *err = -1;
6290 goto err;
6291 }
6292
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006293 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006294 payload = buf_pn + pn_len;
6295 payload_len = end - payload;
6296 aad_len = payload - beg;
6297
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006298 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006299 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006300 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006301 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006302
6303 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006304 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006305 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
6306 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006307 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006308 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006309 goto err;
6310 }
6311
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006312 /* Consume a packet number */
6313 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006314 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01006315 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006316 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006317 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6318 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006319 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006320 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006321 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006322 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006323 pkt->in_flight_len = pkt->len;
6324 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006325 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006326 /* Always reset this flags */
6327 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006328 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6329 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6330 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6331 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006332
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006333 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006334 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006335
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006336 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006337
6338 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006339 /* TODO: what about the frames which have been built
6340 * for this packet.
6341 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006342 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006343 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006344 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006345}
6346
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006347
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006348/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6349 * event subscriber <es> is not allowed to change from a previous call as long
6350 * as at least one event is still subscribed. The <event_type> must only be a
6351 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6352 */
6353static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6354{
Willy Tarreau784b8682022-04-11 14:18:10 +02006355 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006356
6357 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6358 BUG_ON(qcc->subs && qcc->subs != es);
6359
6360 es->events |= event_type;
6361 qcc->subs = es;
6362
6363 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02006364 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006365
6366 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02006367 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006368
6369 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006370}
6371
6372/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6373 * The <es> pointer is not allowed to differ from the one passed to the
6374 * subscribe() call. It always returns zero.
6375 */
6376static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6377{
6378 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
6379}
6380
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006381/* Store in <xprt_ctx> the context attached to <conn>.
6382 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006383 */
6384static int qc_conn_init(struct connection *conn, void **xprt_ctx)
6385{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01006386 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006387
6388 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
6389
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006390 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006391 if (*xprt_ctx)
6392 goto out;
6393
Willy Tarreau784b8682022-04-11 14:18:10 +02006394 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006395
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006396 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01006397 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006398
6399 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006400}
6401
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006402/* Start the QUIC transport layer */
6403static int qc_xprt_start(struct connection *conn, void *ctx)
6404{
6405 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02006406 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006407
Willy Tarreau784b8682022-04-11 14:18:10 +02006408 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006409 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
6410 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02006411 /* prepare a CONNECTION_CLOSE frame */
6412 qc->err_code = QC_ERR_APPLICATION_ERROR;
6413 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle05d4ae62022-04-13 17:40:26 +02006414 return -1;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006415 }
6416
6417 /* mux-quic can now be considered ready. */
6418 qc->mux_state = QC_MUX_READY;
6419
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006420 tasklet_wakeup(qctx->wait_event.tasklet);
6421 return 1;
6422}
6423
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006424static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
6425{
Willy Tarreau784b8682022-04-11 14:18:10 +02006426 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006427 return NULL;
6428
Willy Tarreau784b8682022-04-11 14:18:10 +02006429 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006430}
6431
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006432/* transport-layer operations for QUIC connections. */
6433static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02006434 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006435 .subscribe = quic_conn_subscribe,
6436 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006437 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006438 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006439 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
6440 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01006441 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006442 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006443 .name = "QUIC",
6444};
6445
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006446static void __quic_conn_init(void)
6447{
6448 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
6449 xprt_register(XPRT_QUIC, &ssl_quic);
6450}
Willy Tarreau79367f92022-04-25 19:18:24 +02006451INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006452
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006453static void __quic_conn_deinit(void)
6454{
6455 BIO_meth_free(ha_quic_meth);
6456}
Willy Tarreau79367f92022-04-25 19:18:24 +02006457REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006458
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006459/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
6460 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05006461 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006462 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006463struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006464{
6465 unsigned char *pos;
6466 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006467 struct quic_dghdlr *dghdlr = ctx;
6468 struct quic_dgram *dgram;
6469 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006470
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006471 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006472 pos = dgram->buf;
6473 end = pos + dgram->len;
6474 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006475 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006476
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006477 pkt = pool_zalloc(pool_head_quic_rx_packet);
6478 if (!pkt)
6479 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006480
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006481 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006482 quic_rx_packet_refinc(pkt);
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006483 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006484 first_pkt = 0;
6485 pos += pkt->len;
6486 quic_rx_packet_refdec(pkt);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006487 } while (pos < end);
6488
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006489 /* Increasing the received bytes counter by the UDP datagram length
6490 * if this datagram could be associated to a connection.
6491 */
6492 if (dgram->qc)
6493 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01006494
6495 /* Mark this datagram as consumed */
6496 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006497 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006498
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006499 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006500
6501 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006502 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006503}
6504
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006505/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
6506 * Returns 1 if succeeded, 0 if not.
6507 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02006508int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
6509 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006510{
6511 int long_header;
6512 size_t minlen, skip;
6513
6514 if (!(*buf & QUIC_PACKET_FIXED_BIT))
6515 goto err;
6516
6517 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02006518 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
6519 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006520 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01006521 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006522 goto err;
6523
6524 buf += skip;
6525 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
6526 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
6527 goto err;
6528
6529 *dcid = buf;
6530
6531 return 1;
6532
6533 err:
6534 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
6535 return 0;
6536}
6537
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02006538/* Notify the MUX layer if alive about an imminent close of <qc>. */
6539void qc_notify_close(struct quic_conn *qc)
6540{
6541 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
6542 return;
6543
6544 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
6545
6546 /* wake up the MUX */
6547 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
6548 qc->conn->mux->wake(qc->conn);
6549}
6550
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006551/*
6552 * Local variables:
6553 * c-indent-level: 8
6554 * c-basic-offset: 8
6555 * End:
6556 */