blob: 7c314346058e10f933200772343aa001aeb48d1d [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,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020066 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020067 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020068 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020069 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020070 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020071 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020072 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020073 .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,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020081 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020082 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020083 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020084 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020085 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020086 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V1,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020087 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V1) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020088 .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,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020096 .key_label_len = sizeof(QUIC_HKDF_KEY_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020097 .iv_label = (const unsigned char *)QUIC_HKDF_IV_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +020098 .iv_label_len = sizeof(QUIC_HKDF_IV_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +020099 .hp_label = (const unsigned char *)QUIC_HKDF_HP_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +0200100 .hp_label_len = sizeof(QUIC_HKDF_HP_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200101 .ku_label = (const unsigned char *)QUIC_HKDF_KU_LABEL_V2,
Frédéric Lécailleb1cb9582022-06-16 17:53:46 +0200102 .ku_label_len = sizeof(QUIC_HKDF_KU_LABEL_V2) - 1,
Frédéric Lécaille86845c52022-06-08 19:28:36 +0200103 .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écaille2aebaa42022-06-17 15:11:32 +02001075 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
1076 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
1077 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
1078 }
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02001079 quic_set_connection_close(qc, QC_ERR_CRYPTO_ERROR | alert, 0);
Amaury Denoyelle9fab9fd2022-05-20 15:04:38 +02001080 qc->flags |= QUIC_FL_CONN_TLS_ALERT;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001081 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001082}
1083
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001084/* Set the application for <qc> QUIC connection.
1085 * Return 1 if succeeded, 0 if not.
1086 */
1087int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1088{
Amaury Denoyelle4b40f192022-01-19 11:29:25 +01001089 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0)
1090 qc->app_ops = &h3_ops;
1091 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0)
1092 qc->app_ops = &hq_interop_ops;
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001093 else
1094 return 0;
1095
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001096 return 1;
1097}
1098
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1100 * wants to provide the QUIC layer with CRYPTO data.
1101 * Returns 1 if succeeded, 0 if not.
1102 */
1103int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1104 const uint8_t *data, size_t len)
1105{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001106 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107 enum quic_tls_enc_level tel;
1108 struct quic_enc_level *qel;
1109
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001110 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1111 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001112 if (qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001113 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001114 goto out;
1115 }
1116
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117 tel = ssl_to_quic_enc_level(level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001118 if (tel == -1) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001119 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001120 goto err;
1121 }
1122
Frédéric Lécaille3916ca12022-02-02 14:09:05 +01001123 qel = &qc->els[tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001124 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001125 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001126 goto err;
1127 }
1128
1129 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001130 qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001131
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001132 out:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001133 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001134 return 1;
1135
1136 err:
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001137 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001138 return 0;
1139}
1140
1141int ha_quic_flush_flight(SSL *ssl)
1142{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001143 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001144
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001145 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, qc);
1146 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001147
1148 return 1;
1149}
1150
1151int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1152{
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001153 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001154
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001155 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, qc, &alert, &level);
1156 quic_set_tls_alert(qc, alert);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02001157 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001158 return 1;
1159}
1160
1161/* QUIC TLS methods */
1162static SSL_QUIC_METHOD ha_quic_method = {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001163 .set_encryption_secrets = ha_quic_set_encryption_secrets,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001164 .add_handshake_data = ha_quic_add_handshake_data,
1165 .flush_flight = ha_quic_flush_flight,
1166 .send_alert = ha_quic_send_alert,
1167};
1168
1169/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1170 * Returns an error count.
1171 */
1172int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1173{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1175 int cfgerr = 0;
1176
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001177 long options =
1178 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1179 SSL_OP_SINGLE_ECDH_USE |
1180 SSL_OP_CIPHER_SERVER_PREFERENCE;
1181 SSL_CTX *ctx;
1182
1183 ctx = SSL_CTX_new(TLS_server_method());
1184 bind_conf->initial_ctx = ctx;
1185
1186 SSL_CTX_set_options(ctx, options);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001187 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1188 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1189 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001190
1191#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaillee06f7452022-06-16 15:06:44 +02001192#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001193 if (bind_conf->ssl_conf.early_data) {
1194 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001195 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001196 }
1197 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1198 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1199#else
1200 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1201#endif
1202 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1203#endif
1204 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1205
1206 return cfgerr;
1207}
1208
1209/* Decode an expected packet number from <truncated_on> its truncated value,
1210 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1211 * the number of bits used to encode this packet number (its length in bytes * 8).
1212 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1213 */
1214static uint64_t decode_packet_number(uint64_t largest_pn,
1215 uint32_t truncated_pn, unsigned int pn_nbits)
1216{
1217 uint64_t expected_pn = largest_pn + 1;
1218 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1219 uint64_t pn_hwin = pn_win / 2;
1220 uint64_t pn_mask = pn_win - 1;
1221 uint64_t candidate_pn;
1222
1223
1224 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1225 /* Note that <pn_win> > <pn_hwin>. */
1226 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1227 candidate_pn + pn_hwin <= expected_pn)
1228 return candidate_pn + pn_win;
1229
1230 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1231 return candidate_pn - pn_win;
1232
1233 return candidate_pn;
1234}
1235
1236/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1237 * cryptographic context.
1238 * <largest_pn> is the largest received packet number and <pn> the address of
1239 * the packet number field for this packet with <byte0> address of its first byte.
1240 * <end> points to one byte past the end of this packet.
1241 * Returns 1 if succeeded, 0 if not.
1242 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001243static int qc_do_rm_hp(struct quic_conn *qc,
1244 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001245 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001246 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001247{
1248 int ret, outlen, i, pnlen;
1249 uint64_t packet_number;
1250 uint32_t truncated_pn = 0;
1251 unsigned char mask[5] = {0};
1252 unsigned char *sample;
1253 EVP_CIPHER_CTX *cctx;
1254 unsigned char *hp_key;
1255
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001256 /* Check there is enough data in this packet. */
1257 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001258 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001259 return 0;
1260 }
1261
1262 cctx = EVP_CIPHER_CTX_new();
1263 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001264 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 return 0;
1266 }
1267
1268 ret = 0;
1269 sample = pn + QUIC_PACKET_PN_MAXLEN;
1270
1271 hp_key = tls_ctx->rx.hp_key;
1272 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1273 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1274 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001275 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001276 goto out;
1277 }
1278
1279 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1280 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1281 for (i = 0; i < pnlen; i++) {
1282 pn[i] ^= mask[i + 1];
1283 truncated_pn = (truncated_pn << 8) | pn[i];
1284 }
1285
1286 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1287 /* Store remaining information for this unprotected header */
1288 pkt->pn = packet_number;
1289 pkt->pnl = pnlen;
1290
1291 ret = 1;
1292
1293 out:
1294 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295
1296 return ret;
1297}
1298
1299/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1300 * address, with <payload_len> as payload length, <aad> as address of
1301 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1302 * context.
1303 * Returns 1 if succeeded, 0 if not.
1304 */
1305static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1306 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001307 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308{
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001309 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001310 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001311 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001312 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313
1314 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001315 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001316 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001317 }
1318
1319 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001320 tls_ctx->tx.ctx, tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001321 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001322 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323 }
1324
1325 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001326
1327 err:
1328 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001329 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001330 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001331}
1332
1333/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1334 * Returns 1 if succeeded, 0 if not.
1335 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001336static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001337{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001338 int ret, kp_changed;
Frédéric Lécaillef2f4a4e2022-04-05 12:18:46 +02001339 unsigned char iv[QUIC_TLS_IV_LEN];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001340 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001341 EVP_CIPHER_CTX *rx_ctx = tls_ctx->rx.ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001342 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001343 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1344 unsigned char *rx_key = tls_ctx->rx.key;
1345
1346 kp_changed = 0;
1347 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1348 /* The two tested bits are not at the same position,
1349 * this is why they are first both inversed.
1350 */
1351 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1352 if (pkt->pn < tls_ctx->rx.pn) {
1353 /* The lowest packet number of a previous key phase
1354 * cannot be null if it really stores previous key phase
1355 * secrets.
1356 */
1357 if (!pkt->qc->ku.prv_rx.pn)
1358 return 0;
1359
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001360 rx_ctx = pkt->qc->ku.prv_rx.ctx;
1361 rx_iv = pkt->qc->ku.prv_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001362 rx_key = pkt->qc->ku.prv_rx.key;
1363 }
1364 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1365 /* Next key phase */
1366 kp_changed = 1;
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001367 rx_ctx = pkt->qc->ku.nxt_rx.ctx;
1368 rx_iv = pkt->qc->ku.nxt_rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001369 rx_key = pkt->qc->ku.nxt_rx.key;
1370 }
1371 }
1372 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001373
1374 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1375 return 0;
1376
1377 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1378 pkt->data, pkt->aad_len,
Frédéric Lécaille8c7927c2022-04-05 16:28:38 +02001379 rx_ctx, tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001380 if (!ret)
1381 return 0;
1382
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001383 /* Update the keys only if the packet decryption succeeded. */
1384 if (kp_changed) {
1385 quic_tls_rotate_keys(pkt->qc);
1386 /* Toggle the Key Phase bit */
1387 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1388 /* Store the lowest packet number received for the current key phase */
1389 tls_ctx->rx.pn = pkt->pn;
1390 /* Prepare the next key update */
1391 if (!quic_tls_key_update(pkt->qc))
1392 return 0;
1393 }
1394
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001395 /* Update the packet length (required to parse the frames). */
Frédéric Lécaillef4605742022-04-05 10:28:29 +02001396 pkt->len -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001397
1398 return 1;
1399}
1400
Frédéric Lécaille96367152022-04-25 09:40:19 +02001401
1402/* Remove references to <frm> frame */
1403static void qc_frm_unref(struct quic_conn *qc, struct quic_frame *frm)
1404{
1405 uint64_t pn;
1406 struct quic_frame *f, *tmp;
1407
1408 list_for_each_entry_safe(f, tmp, &frm->reflist, ref) {
1409 pn = f->pkt->pn_node.key;
1410 f->origin = NULL;
1411 LIST_DELETE(&f->ref);
1412 TRACE_PROTO("remove frame reference", QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1413 }
1414}
1415
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001416/* Release <frm> frame and mark its copies as acknowledged */
Frédéric Lécailleda342552022-04-25 10:28:49 +02001417void qc_release_frm(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaillea9568412022-04-25 08:58:04 +02001418{
1419 uint64_t pn;
1420 struct quic_frame *origin, *f, *tmp;
1421
1422 /* Identify this frame: a frame copy or one of its copies */
1423 origin = frm->origin ? frm->origin : frm;
1424 /* Ensure the source of the copies is flagged as acked, <frm> being
1425 * possibly a copy of <origin>
1426 */
1427 origin->flags |= QUIC_FL_TX_FRAME_ACKED;
1428 /* Mark all the copy of <origin> as acknowledged. We must
1429 * not release the packets (releasing the frames) at this time as
1430 * they are possibly also to be acknowledged alongside the
1431 * the current one.
1432 */
1433 list_for_each_entry_safe(f, tmp, &origin->reflist, ref) {
1434 pn = f->pkt->pn_node.key;
1435 TRACE_PROTO("mark frame as acked from packet",
1436 QUIC_EV_CONN_PRSAFRM, qc, f, &pn);
1437 f->flags |= QUIC_FL_TX_FRAME_ACKED;
1438 f->origin = NULL;
1439 LIST_DELETE(&f->ref);
1440 }
1441 LIST_DELETE(&frm->list);
1442 pn = frm->pkt->pn_node.key;
1443 quic_tx_packet_refdec(frm->pkt);
1444 TRACE_PROTO("freeing frame from packet",
1445 QUIC_EV_CONN_PRSAFRM, qc, frm, &pn);
1446 pool_free(pool_head_quic_frame, frm);
1447}
1448
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001449/* Remove from <stream> the acknowledged frames.
Amaury Denoyelle95e50fb2022-03-29 14:50:25 +02001450 *
1451 * Returns 1 if at least one frame was removed else 0.
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001452 */
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001453static int quic_stream_try_to_consume(struct quic_conn *qc,
1454 struct qc_stream_desc *stream)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001455{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001456 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001457 struct eb64_node *frm_node;
1458
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001459 ret = 0;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001460 frm_node = eb64_first(&stream->acked_frms);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001461 while (frm_node) {
1462 struct quic_stream *strm;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001463 struct quic_frame *frm;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001464 size_t offset, len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001465
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001466 strm = eb64_entry(frm_node, struct quic_stream, offset);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001467 offset = strm->offset.key;
1468 len = strm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001469
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001470 if (offset > stream->ack_offset)
1471 break;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001472
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001473 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001474 /* cf. next comment : frame may be freed at this stage. */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001475 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001476 qc, stream ? strm : NULL, stream);
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001477 ret = 1;
1478 }
1479
Amaury Denoyelle7586bef2022-04-25 14:26:54 +02001480 /* If stream is NULL after qc_stream_desc_ack(), it means frame
1481 * has been freed. with the stream frames tree. Nothing to do
1482 * anymore in here.
1483 */
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001484 if (!stream)
1485 return 1;
1486
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001487 frm_node = eb64_next(frm_node);
1488 eb64_delete(&strm->offset);
Amaury Denoyelle7b4c9d62022-02-24 10:50:58 +01001489
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001490 frm = container_of(strm, struct quic_frame, stream);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001491 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001492 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001493
1494 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001495}
1496
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001497/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001498static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1499 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001500{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001501 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001502
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001503 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001504 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001505 switch (frm->type) {
1506 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1507 {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001508 struct quic_stream *strm_frm = &frm->stream;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001509 struct eb64_node *node = NULL;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001510 struct qc_stream_desc *stream = NULL;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001511 const size_t offset = strm_frm->offset.key;
1512 const size_t len = strm_frm->len;
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001513
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001514 /* do not use strm_frm->stream as the qc_stream_desc instance
1515 * might be freed at this stage. Use the id to do a proper
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001516 * lookup.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001517 *
1518 * TODO if lookup operation impact on the perf is noticeable,
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001519 * implement a refcount on qc_stream_desc instances.
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001520 */
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001521 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1522 if (!node) {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001523 TRACE_PROTO("acked stream for released stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001524 qc_release_frm(qc, frm);
Amaury Denoyelle8d5def02022-03-29 11:51:17 +02001525 /* early return */
1526 return;
1527 }
Amaury Denoyellee4301da2022-04-19 17:59:50 +02001528 stream = eb64_entry(node, struct qc_stream_desc, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001529
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001530 TRACE_PROTO("acked stream", QUIC_EV_CONN_ACKSTRM, qc, strm_frm, stream);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001531 if (offset <= stream->ack_offset) {
1532 if (qc_stream_desc_ack(&stream, offset, len)) {
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001533 stream_acked = 1;
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001534 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1535 qc, strm_frm, stream);
1536 }
Amaury Denoyelle0c7679d2022-02-24 10:56:33 +01001537
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001538 if (!stream) {
1539 /* no need to continue if stream freed. */
1540 TRACE_PROTO("stream released and freed", QUIC_EV_CONN_ACKSTRM, qc);
Frédéric Lécailleda342552022-04-25 10:28:49 +02001541 qc_release_frm(qc, frm);
Amaury Denoyelle1b81dda2022-04-21 09:32:53 +02001542 break;
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001543 }
1544
Frédéric Lécailleda342552022-04-25 10:28:49 +02001545 TRACE_PROTO("stream consumed", QUIC_EV_CONN_ACKSTRM,
1546 qc, strm_frm, stream);
1547 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001548 }
1549 else {
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001550 eb64_insert(&stream->acked_frms, &strm_frm->offset);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001551 }
Amaury Denoyelle119965f2022-02-24 17:39:57 +01001552
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02001553 stream_acked |= quic_stream_try_to_consume(qc, stream);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001554 }
1555 break;
1556 default:
Frédéric Lécailleda342552022-04-25 10:28:49 +02001557 qc_release_frm(qc, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001558 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001559
Frédéric Lécaille89a2ceb2022-04-20 15:59:07 +02001560 if (stream_acked && qc->mux_state == QC_MUX_READY) {
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001561 struct qcc *qcc = qc->qcc;
1562
1563 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1564 tasklet_wakeup(qcc->subs->tasklet);
1565 qcc->subs->events &= ~SUB_RETRY_SEND;
1566 if (!qcc->subs->events)
1567 qcc->subs = NULL;
1568 }
1569 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001570}
1571
1572/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1573 * deallocating them, and their TX frames.
1574 * Returns the last node reached to be used for the next range.
1575 * May be NULL if <largest> node could not be found.
1576 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001577static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1578 struct eb_root *pkts,
1579 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 struct list *newly_acked_pkts,
1581 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001582 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001583{
1584 struct eb64_node *node;
1585 struct quic_tx_packet *pkt;
1586
1587 if (largest_node)
1588 node = largest_node;
1589 else {
1590 node = eb64_lookup(pkts, largest);
1591 while (!node && largest > smallest) {
1592 node = eb64_lookup(pkts, --largest);
1593 }
1594 }
1595
1596 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001597 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001598
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001599 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001601 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001602 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001603 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001604 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605 node = eb64_prev(node);
1606 eb64_delete(&pkt->pn_node);
1607 }
1608
1609 return node;
1610}
1611
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001612/* Remove all frames from <pkt_frm_list> and reinsert them in the
1613 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001614 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001615static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
Frédéric Lécaille96367152022-04-25 09:40:19 +02001616 struct quic_tx_packet *pkt,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001617 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001618{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001619 struct quic_frame *frm, *frmbak;
1620 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001621 struct list *pkt_frm_list = &pkt->frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001622
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001623 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
Frédéric Lécaille96367152022-04-25 09:40:19 +02001624 /* Only for debug */
1625 uint64_t pn;
1626
1627 /* First remove this frame from the packet it was attached to */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001628 LIST_DELETE(&frm->list);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001629 pn = frm->pkt->pn_node.key;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001630 quic_tx_packet_refdec(frm->pkt);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001631 /* At this time, this frame is not freed but removed from its packet */
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001632 frm->pkt = NULL;
Frédéric Lécaille96367152022-04-25 09:40:19 +02001633 /* Remove any reference to this frame */
1634 qc_frm_unref(qc, frm);
1635 switch (frm->type) {
1636 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1637 {
1638 struct quic_stream *strm_frm = &frm->stream;
1639 struct eb64_node *node = NULL;
1640 struct qc_stream_desc *stream_desc;
1641
1642 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
1643 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02001644 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille96367152022-04-25 09:40:19 +02001645 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1646 qc, frm, &pn);
1647 pool_free(pool_head_quic_frame, frm);
1648 continue;
1649 }
1650
1651 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
1652 /* Do not resend this frame if in the "already acked range" */
1653 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
1654 TRACE_PROTO("ignored frame in already acked range",
1655 QUIC_EV_CONN_PRSAFRM, qc, frm);
1656 continue;
1657 }
1658 else if (strm_frm->offset.key < stream_desc->ack_offset) {
1659 strm_frm->offset.key = stream_desc->ack_offset;
1660 TRACE_PROTO("updated partially acked frame",
1661 QUIC_EV_CONN_PRSAFRM, qc, frm);
1662 }
1663
1664 break;
1665 }
1666
1667 default:
1668 break;
1669 }
1670
1671 /* Do not resend probing packet with old data */
1672 if (pkt->flags & QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA) {
1673 TRACE_PROTO("ignored frame with old data from packet", QUIC_EV_CONN_PRSAFRM,
1674 qc, frm, &pn);
1675 if (frm->origin)
1676 LIST_DELETE(&frm->ref);
1677 pool_free(pool_head_quic_frame, frm);
1678 continue;
1679 }
1680
1681 if (frm->flags & QUIC_FL_TX_FRAME_ACKED) {
1682 TRACE_PROTO("already acked frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
1683 TRACE_PROTO("freeing frame from packet", QUIC_EV_CONN_PRSAFRM,
1684 qc, frm, &pn);
1685 pool_free(pool_head_quic_frame, frm);
1686 }
1687 else {
1688 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001689 if (QUIC_FT_STREAM_8 <= frm->type && frm->type <= QUIC_FT_STREAM_F) {
1690 /* Mark this STREAM frame as lost. A look up their stream descriptor
1691 * will be performed to check the stream is not consumed or released.
1692 */
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02001693 frm->flags = QUIC_FL_TX_FRAME_LOST;
1694 }
Frédéric Lécaille96367152022-04-25 09:40:19 +02001695 LIST_APPEND(&tmp, &frm->list);
1696 }
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001697 }
1698
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001699 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001700}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001701
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001702/* Free <pkt> TX packet and its attached frames.
1703 * This is the responsability of the caller to remove this packet of
1704 * any data structure it was possibly attached to.
1705 */
1706static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
1707{
1708 struct quic_frame *frm, *frmbak;
1709
1710 if (!pkt)
1711 return;
1712
1713 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
1714 LIST_DELETE(&frm->list);
1715 pool_free(pool_head_quic_frame, frm);
1716 }
1717 pool_free(pool_head_quic_tx_packet, pkt);
1718}
1719
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001720/* Free the TX packets of <pkts> list */
1721static inline void free_quic_tx_pkts(struct list *pkts)
1722{
1723 struct quic_tx_packet *pkt, *tmp;
1724
1725 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001726 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001727 eb64_delete(&pkt->pn_node);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01001728 free_quic_tx_packet(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001729 }
1730}
1731
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001732/* Remove already sent ranges of acknowledged packet numbers from
1733 * <pktns> packet number space tree below <largest_acked_pn> possibly
1734 * updating the range which contains <largest_acked_pn>.
1735 * Never fails.
1736 */
1737static void qc_treat_ack_of_ack(struct quic_pktns *pktns,
1738 int64_t largest_acked_pn)
1739{
1740 struct eb64_node *ar, *next_ar;
1741 struct quic_arngs *arngs = &pktns->rx.arngs;
1742
1743 ar = eb64_first(&arngs->root);
1744 while (ar) {
1745 struct quic_arng_node *ar_node;
1746
1747 next_ar = eb64_next(ar);
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001748 ar_node = eb64_entry(ar, struct quic_arng_node, first);
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001749 if ((int64_t)ar_node->first.key > largest_acked_pn)
1750 break;
1751
1752 if (largest_acked_pn < ar_node->last) {
1753 eb64_delete(ar);
1754 ar_node->first.key = largest_acked_pn + 1;
1755 eb64_insert(&arngs->root, ar);
1756 break;
1757 }
1758
1759 eb64_delete(ar);
1760 pool_free(pool_head_quic_arng, ar_node);
1761 arngs->sz--;
1762 ar = next_ar;
1763 }
1764}
1765
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766/* Send a packet ack event nofication for each newly acked packet of
1767 * <newly_acked_pkts> list and free them.
1768 * Always succeeds.
1769 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001770static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001771 struct list *newly_acked_pkts)
1772{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001773 struct quic_tx_packet *pkt, *tmp;
1774 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1775
1776 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1777 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001778 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001779 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001781 qc->path->ifae_pkts--;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01001782 /* If this packet contained an ACK frame, proceed to the
1783 * acknowledging of range of acks from the largest acknowledged
1784 * packet number which was sent in an ACK frame by this packet.
1785 */
1786 if (pkt->largest_acked_pn != -1)
1787 qc_treat_ack_of_ack(pkt->pktns, pkt->largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001788 ev.ack.acked = pkt->in_flight_len;
1789 ev.ack.time_sent = pkt->time_sent;
1790 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001791 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001792 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001793 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001794 }
1795
1796}
1797
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001798/* Release all the frames attached to <pktns> packet number space */
1799static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1800{
1801 struct quic_frame *frm, *frmbak;
1802
1803 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1804 LIST_DELETE(&frm->list);
1805 pool_free(pool_head_quic_frame, frm);
1806 }
1807}
1808
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001809/* Handle <pkts> list of lost packets detected at <now_us> handling
1810 * their TX frames.
1811 * Send a packet loss event to the congestion controller if
1812 * in flight packet have been lost.
1813 * Also frees the packet in <pkts> list.
1814 * Never fails.
1815 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001816static inline void qc_release_lost_pkts(struct quic_conn *qc,
1817 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001818 struct list *pkts,
1819 uint64_t now_us)
1820{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001821 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001822 uint64_t lost_bytes;
1823
1824 lost_bytes = 0;
1825 oldest_lost = newest_lost = NULL;
1826 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001827 struct list tmp = LIST_HEAD_INIT(tmp);
1828
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001829 lost_bytes += pkt->in_flight_len;
1830 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001831 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécailleba9db402022-03-01 17:06:50 +01001832 qc->path->in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001834 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001835 /* Treat the frames of this lost packet. */
Frédéric Lécaille96367152022-04-25 09:40:19 +02001836 qc_requeue_nacked_pkt_tx_frms(qc, pkt, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001837 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001838 if (!oldest_lost) {
1839 oldest_lost = newest_lost = pkt;
1840 }
1841 else {
1842 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001843 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001844 newest_lost = pkt;
1845 }
1846 }
1847
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001848 if (newest_lost) {
1849 /* Sent a congestion event to the controller */
Benoit DOLEZ69e3f052022-06-08 09:28:56 +02001850 struct quic_cc_event ev = { };
1851
1852 ev.type = QUIC_CC_EVT_LOSS;
1853 ev.loss.time_sent = newest_lost->time_sent;
Frédéric Lécaillea5ee0ae2022-03-02 14:52:56 +01001854
1855 quic_cc_event(&qc->path->cc, &ev);
1856 }
1857
1858 /* If an RTT have been already sampled, <rtt_min> has been set.
1859 * We must check if we are experiencing a persistent congestion.
1860 * If this is the case, the congestion controller must re-enter
1861 * slow start state.
1862 */
1863 if (qc->path->loss.rtt_min && newest_lost != oldest_lost) {
1864 unsigned int period = newest_lost->time_sent - oldest_lost->time_sent;
1865
1866 if (quic_loss_persistent_congestion(&qc->path->loss, period,
1867 now_ms, qc->max_ack_delay))
1868 qc->path->cc.algo->slow_start(&qc->path->cc);
1869 }
1870
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001871 if (lost_bytes) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001872 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001873 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001874 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001875 }
1876}
1877
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001878/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1879 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001880 * 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 +01001881 * acked ack-eliciting packet.
1882 * Return 1, if succeeded, 0 if not.
1883 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001884static inline int qc_parse_ack_frm(struct quic_conn *qc,
1885 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001886 struct quic_enc_level *qel,
1887 unsigned int *rtt_sample,
1888 const unsigned char **pos, const unsigned char *end)
1889{
1890 struct quic_ack *ack = &frm->ack;
1891 uint64_t smallest, largest;
1892 struct eb_root *pkts;
1893 struct eb64_node *largest_node;
1894 unsigned int time_sent, pkt_flags;
1895 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1896 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1897
1898 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1899 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001900 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001901 goto err;
1902 }
1903
1904 if (ack->first_ack_range > ack->largest_ack) {
1905 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001906 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001907 goto err;
1908 }
1909
1910 largest = ack->largest_ack;
1911 smallest = largest - ack->first_ack_range;
1912 pkts = &qel->pktns->tx.pkts;
1913 pkt_flags = 0;
1914 largest_node = NULL;
1915 time_sent = 0;
1916
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001917 if ((int64_t)ack->largest_ack > qel->pktns->rx.largest_acked_pn) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001918 largest_node = eb64_lookup(pkts, largest);
1919 if (!largest_node) {
1920 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001921 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001922 }
1923 else {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02001924 time_sent = eb64_entry(largest_node,
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001925 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001926 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001927 }
1928
1929 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001930 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001931 do {
1932 uint64_t gap, ack_range;
1933
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001934 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1935 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001936 if (!ack->ack_range_num--)
1937 break;
1938
1939 if (!quic_dec_int(&gap, pos, end))
1940 goto err;
1941
1942 if (smallest < gap + 2) {
1943 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001944 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001945 goto err;
1946 }
1947
1948 largest = smallest - gap - 2;
1949 if (!quic_dec_int(&ack_range, pos, end))
1950 goto err;
1951
1952 if (largest < ack_range) {
1953 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001954 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001955 goto err;
1956 }
1957
1958 /* Do not use this node anymore. */
1959 largest_node = NULL;
1960 /* Next range */
1961 smallest = largest - ack_range;
1962
1963 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001964 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001965 } while (1);
1966
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001967 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1968 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02001969 qel->pktns->rx.largest_acked_pn = ack->largest_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001970 }
1971
1972 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1973 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001974 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001975 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001976 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001977 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001978 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1979 if (quic_peer_validated_addr(qc))
1980 qc->path->loss.pto_count = 0;
1981 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001982 }
1983
1984
1985 return 1;
1986
1987 err:
1988 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001989 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001990 return 0;
1991}
1992
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001993/* This function gives the detail of the SSL error. It is used only
1994 * if the debug mode and the verbose mode are activated. It dump all
1995 * the SSL error until the stack was empty.
1996 */
1997static forceinline void qc_ssl_dump_errors(struct connection *conn)
1998{
1999 if (unlikely(global.mode & MODE_DEBUG)) {
2000 while (1) {
Willy Tarreau325fc632022-04-11 18:47:38 +02002001 const char *func = NULL;
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002002 unsigned long ret;
2003
Willy Tarreau325fc632022-04-11 18:47:38 +02002004 ERR_peek_error_func(&func);
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002005 ret = ERR_get_error();
2006 if (!ret)
2007 return;
2008
2009 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
Willy Tarreau325fc632022-04-11 18:47:38 +02002010 func, ERR_reason_error_string(ret));
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02002011 }
2012 }
2013}
2014
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01002015int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
2016 const char **str, int *len);
2017
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
2019 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002020 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002021 * Return 1 if succeeded, 0 if not.
2022 */
2023static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002024 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002025 const unsigned char *data, size_t len,
2026 struct quic_rx_packet *pkt,
2027 struct quic_rx_crypto_frm *cf)
2028{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002029 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002030 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002031
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002032 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002033 qc = ctx->qc;
2034
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002035 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
2036
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
2038 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002039 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002040 goto err;
2041 }
2042
2043 el->rx.crypto.offset += len;
2044 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01002045 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002046
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002047 state = qc->state;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002048 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002049 ssl_err = SSL_do_handshake(ctx->ssl);
2050 if (ssl_err != 1) {
2051 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2052 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2053 TRACE_PROTO("SSL handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002054 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002055 goto out;
2056 }
2057
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002058 /* TODO: Should close the connection asap */
2059 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2060 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2061 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2062 HA_ATOMIC_INC(&qc->prx_counters->hdshk_fail);
2063 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002064 TRACE_DEVEL("SSL handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002065 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02002066 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002067 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002068 goto err;
2069 }
2070
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002071 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillebc964bd2022-04-13 16:20:09 +02002072
2073 /* Check the alpn could be negotiated */
2074 if (!qc->app_ops) {
2075 TRACE_PROTO("No ALPN", QUIC_EV_CONN_IO_CB, qc, &state);
2076 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
2077 goto err;
2078 }
2079
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002080 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2081 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
2082 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
2083 }
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002084 /* I/O callback switch */
2085 ctx->wait_event.tasklet->process = quic_conn_app_io_cb;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002086 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002087 qc->state = QUIC_HS_ST_CONFIRMED;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002088 /* The connection is ready to be accepted. */
2089 quic_accept_push_qc(qc);
2090 }
2091 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002092 qc->state = QUIC_HS_ST_COMPLETE;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01002093 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002094 } else {
2095 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
2096 if (ssl_err != 1) {
2097 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
2098 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
2099 TRACE_DEVEL("SSL post handshake",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002100 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 goto out;
2102 }
2103
2104 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002105 QUIC_EV_CONN_IO_CB, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002106 goto err;
2107 }
2108
2109 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécaille00e24002022-02-18 17:13:45 +01002110 QUIC_EV_CONN_IO_CB, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002111 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01002112
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002113 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002114 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002115 return 1;
2116
2117 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002118 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002119 return 0;
2120}
2121
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002122/* Parse a STREAM frame <strm_frm>
2123 *
2124 * Return 1 on success. On error, 0 is returned. In this case, the packet
2125 * containing the frame must not be acknowledged.
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002126 */
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002127static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2128 struct quic_stream *strm_frm,
2129 struct quic_conn *qc)
2130{
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002131 int ret;
2132
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002133 /* RFC9000 13.1. Packet Processing
2134 *
2135 * A packet MUST NOT be acknowledged until packet protection has been
2136 * successfully removed and all frames contained in the packet have
2137 * been processed. For STREAM frames, this means the data has been
2138 * enqueued in preparation to be received by the application protocol,
2139 * but it does not require that data be delivered and consumed.
2140 */
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002141 ret = qcc_recv(qc->qcc, strm_frm->id, strm_frm->len,
2142 strm_frm->offset.key, strm_frm->fin,
2143 (char *)strm_frm->data);
Amaury Denoyelle74cf2372022-04-29 15:58:22 +02002144
Amaury Denoyellef8db5aa2022-05-24 15:26:07 +02002145 /* frame rejected - packet must not be acknowledeged */
2146 if (ret)
2147 return 0;
2148
2149 return 1;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002150}
2151
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002152/* Duplicate all frames from <pkt_frm_list> list into <out_frm_list> list
2153 * for <qc> QUIC connection.
2154 * This is a best effort function which never fails even if no memory could be
2155 * allocated to duplicate these frames.
2156 */
2157static void qc_dup_pkt_frms(struct quic_conn *qc,
2158 struct list *pkt_frm_list, struct list *out_frm_list)
2159{
2160 struct quic_frame *frm, *frmbak;
2161 struct list tmp = LIST_HEAD_INIT(tmp);
2162
2163 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
2164 struct quic_frame *dup_frm, *origin;
2165
2166 switch (frm->type) {
2167 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
2168 {
2169 struct quic_stream *strm_frm = &frm->stream;
2170 struct eb64_node *node = NULL;
2171 struct qc_stream_desc *stream_desc;
2172
2173 node = eb64_lookup(&qc->streams_by_id, strm_frm->id);
2174 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02002175 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaillea9568412022-04-25 08:58:04 +02002176 continue;
2177 }
2178
2179 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
2180 /* Do not resend this frame if in the "already acked range" */
2181 if (strm_frm->offset.key + strm_frm->len <= stream_desc->ack_offset) {
2182 TRACE_PROTO("ignored frame frame in already acked range",
2183 QUIC_EV_CONN_PRSAFRM, qc, frm);
2184 continue;
2185 }
2186 else if (strm_frm->offset.key < stream_desc->ack_offset) {
2187 strm_frm->offset.key = stream_desc->ack_offset;
2188 TRACE_PROTO("updated partially acked frame",
2189 QUIC_EV_CONN_PRSAFRM, qc, frm);
2190 }
2191
2192 break;
2193 }
2194
2195 default:
2196 break;
2197 }
2198
2199 dup_frm = pool_zalloc(pool_head_quic_frame);
2200 if (!dup_frm) {
2201 TRACE_PROTO("could not duplicate frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2202 break;
2203 }
2204
2205 /* If <frm> is already a copy of another frame, we must take
2206 * its original frame as source for the copy.
2207 */
2208 origin = frm->origin ? frm->origin : frm;
2209 TRACE_PROTO("probing frame", QUIC_EV_CONN_PRSAFRM, qc, origin);
2210 *dup_frm = *origin;
2211 LIST_INIT(&dup_frm->reflist);
2212 TRACE_PROTO("copied from packet", QUIC_EV_CONN_PRSAFRM,
2213 qc, NULL, &origin->pkt->pn_node.key);
2214 dup_frm->origin = origin;
2215 LIST_APPEND(&origin->reflist, &dup_frm->ref);
2216 LIST_APPEND(&tmp, &dup_frm->list);
2217 }
2218
2219 LIST_SPLICE(out_frm_list, &tmp);
2220}
2221
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002222/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002223static void qc_prep_fast_retrans(struct quic_conn *qc,
2224 struct quic_enc_level *qel,
2225 struct list *frms1, struct list *frms2)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002226{
2227 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002228 struct list *frms = frms1;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002229 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002230 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002231
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002232 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002233 node = eb64_first(pkts);
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002234 start:
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002235 while (node) {
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002236 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
2237 node = eb64_next(node);
2238 /* Skip the empty and coalesced packets */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002239 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002240 break;
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002241 }
2242
2243 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002244 return;
2245
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002246 /* When building a packet from another one, the field which may increase the
2247 * packet size is the packet number. And the maximum increase is 4 bytes.
2248 */
2249 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2250 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2251 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2252 return;
2253 }
2254
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002255 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2256 qc_dup_pkt_frms(qc, &pkt->frms, frms);
2257 if (frms == frms1 && frms2) {
2258 frms = frms2;
2259 goto start;
2260 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002261}
2262
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002263/* Prepare a fast retransmission during a handshake after a client
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002264 * has resent Initial packets. According to the RFC a server may retransmit
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002265 * Initial packets send them coalescing with others (Handshake here).
2266 * (Listener only function).
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002267 */
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002268static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc,
2269 struct list *ifrms, struct list *hfrms)
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002270{
2271 struct list itmp = LIST_HEAD_INIT(itmp);
2272 struct list htmp = LIST_HEAD_INIT(htmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002273
2274 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2275 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2276 struct quic_enc_level *qel = iqel;
2277 struct eb_root *pkts;
2278 struct eb64_node *node;
2279 struct quic_tx_packet *pkt;
2280 struct list *tmp = &itmp;
2281
2282 start:
2283 pkt = NULL;
2284 pkts = &qel->pktns->tx.pkts;
2285 node = eb64_first(pkts);
2286 /* Skip the empty packet (they have already been retransmitted) */
2287 while (node) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02002288 pkt = eb64_entry(node, struct quic_tx_packet, pn_node);
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002289 if (!LIST_ISEMPTY(&pkt->frms) && !(pkt->flags & QUIC_FL_TX_PACKET_COALESCED))
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002290 break;
2291 node = eb64_next(node);
2292 }
2293
2294 if (!pkt)
2295 goto end;
2296
Frédéric Lécaille12fd2592022-03-31 08:42:06 +02002297 /* When building a packet from another one, the field which may increase the
2298 * packet size is the packet number. And the maximum increase is 4 bytes.
2299 */
2300 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc) &&
2301 pkt->len + 4 > 3 * qc->rx.bytes - qc->tx.prep_bytes) {
2302 TRACE_PROTO("anti-amplification limit would be reached", QUIC_EV_CONN_PRSAFRM, qc);
2303 goto end;
2304 }
2305
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002306 qel->pktns->tx.pto_probe += 1;
2307 requeue:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002308 TRACE_PROTO("duplicating packet", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
2309 qc_dup_pkt_frms(qc, &pkt->frms, tmp);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002310 if (qel == iqel) {
2311 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2312 pkt = pkt->next;
2313 tmp = &htmp;
2314 hqel->pktns->tx.pto_probe += 1;
2315 goto requeue;
2316 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002317 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002318
2319 end:
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002320 LIST_SPLICE(ifrms, &itmp);
2321 LIST_SPLICE(hfrms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002322}
2323
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002324static void qc_cc_err_count_inc(struct quic_conn *qc, struct quic_frame *frm)
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002325{
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002326 if (frm->type == QUIC_FT_CONNECTION_CLOSE)
2327 quic_stats_transp_err_count_inc(qc->prx_counters, frm->connection_close.error_code);
2328 else if (frm->type == QUIC_FT_CONNECTION_CLOSE_APP) {
2329 if (qc->mux_state != QC_MUX_READY || !qc->qcc->app_ops->inc_err_cnt)
2330 return;
2331
2332 qc->qcc->app_ops->inc_err_cnt(qc->qcc->ctx, frm->connection_close_app.error_code);
2333 }
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02002334}
2335
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002336/* Enqueue a STOP_SENDING frame to send into 1RTT packet number space
2337 * frame list to send.
2338 * Return 1 if succeeded, 0 if not.
2339 */
2340static int qc_stop_sending_frm_enqueue(struct quic_conn *qc, uint64_t id)
2341{
2342 struct quic_frame *frm;
2343 struct quic_enc_level *qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2344 uint64_t app_error_code;
2345
2346 /* TODO: the mux may be released, we cannot have more
2347 * information about the application error code to send
2348 * at this time.
2349 */
2350 app_error_code = H3_REQUEST_REJECTED;
2351
2352 frm = pool_zalloc(pool_head_quic_frame);
2353 if (!frm)
2354 return 0;
2355
2356 frm->type = QUIC_FT_STOP_SENDING;
2357 frm->stop_sending.id = id;
2358 frm->stop_sending.app_error_code = app_error_code;
2359 LIST_INIT(&frm->reflist);
2360 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
2361
2362 return 1;
2363}
2364
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002365/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2366 * as I/O handler context and <qel> as encryption level.
2367 * Returns 1 if succeeded, 0 if failed.
2368 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002369static 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 +01002370 struct quic_enc_level *qel)
2371{
2372 struct quic_frame frm;
2373 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002374 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002375 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002377 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002378 /* Skip the AAD */
2379 pos = pkt->data + pkt->aad_len;
2380 end = pkt->data + pkt->len;
2381
2382 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002383 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002384 goto err;
2385
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002386 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002387 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002388 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002389 break;
2390 case QUIC_FT_PING:
2391 break;
2392 case QUIC_FT_ACK:
2393 {
2394 unsigned int rtt_sample;
2395
2396 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002397 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002398 goto err;
2399
2400 if (rtt_sample) {
2401 unsigned int ack_delay;
2402
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002403 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002404 qc->state >= QUIC_HS_ST_CONFIRMED ?
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002405 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2406 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002407 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002408 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002409 break;
2410 }
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002411 case QUIC_FT_RESET_STREAM:
2412 /* TODO: handle this frame at STREAM level */
2413 break;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002414 case QUIC_FT_STOP_SENDING:
Frédéric Lécaillee06ca652022-05-29 11:48:58 +02002415 /* TODO: handle this frame at STREAM level */
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002416 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002417 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002418 {
2419 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002420
Frédéric Lécaillebd242082022-02-25 17:17:59 +01002421 if (unlikely(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002422 /* XXX TO DO: <cfdebug> is used only for the traces. */
2423 struct quic_rx_crypto_frm cfdebug = { };
2424
2425 cfdebug.offset_node.key = frm.crypto.offset;
2426 cfdebug.len = frm.crypto.len;
2427 TRACE_PROTO("CRYPTO data discarded",
2428 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2429 break;
2430 }
2431
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002432 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2433 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2434 /* XXX TO DO: <cfdebug> is used only for the traces. */
2435 struct quic_rx_crypto_frm cfdebug = { };
2436
2437 cfdebug.offset_node.key = frm.crypto.offset;
2438 cfdebug.len = frm.crypto.len;
2439 /* Nothing to do */
2440 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002441 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002442 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002443 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2444 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002445 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002446 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002447 else {
2448 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2449 /* XXX TO DO: <cfdebug> is used only for the traces. */
2450 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002451
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002452 cfdebug.offset_node.key = frm.crypto.offset;
2453 cfdebug.len = frm.crypto.len;
2454 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002455 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002456 frm.crypto.len -= diff;
2457 frm.crypto.data += diff;
2458 frm.crypto.offset = qel->rx.crypto.offset;
2459 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002460 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002461
2462 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002464 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002465
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002466 cfdebug.offset_node.key = frm.crypto.offset;
2467 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468 if (!qc_provide_cdata(qel, ctx,
2469 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002470 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002471 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002472
2473 break;
2474 }
2475
2476 /* frm.crypto.offset > qel->rx.crypto.offset */
2477 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2478 if (!cf) {
2479 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002480 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002481 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002482 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002483
2484 cf->offset_node.key = frm.crypto.offset;
2485 cf->len = frm.crypto.len;
2486 cf->data = frm.crypto.data;
2487 cf->pkt = pkt;
2488 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2489 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002491 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002492 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002493 {
2494 struct quic_stream *stream = &frm.stream;
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002495 unsigned nb_streams = qc->rx.strms[qcs_id_type(stream->id)].nb_streams;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002496
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002497 /* The upper layer may not be allocated. */
2498 if (qc->mux_state != QC_MUX_READY) {
2499 if ((stream->id >> QCS_ID_TYPE_SHIFT) < nb_streams) {
2500 TRACE_PROTO("Already closed stream", QUIC_EV_CONN_PRSHPKT, qc);
2501 break;
2502 }
2503 else {
2504 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaille4df2fe92022-05-29 11:36:03 +02002505 if (!qc_stop_sending_frm_enqueue(qc, stream->id))
2506 TRACE_PROTO("could not enqueue STOP_SENDING frame", QUIC_EV_CONN_PRSHPKT, qc);
2507
Frédéric Lécailled62240c2022-05-02 18:52:58 +02002508 goto err;
2509 }
2510 }
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01002511
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002512 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002513 goto err;
2514
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002515 break;
2516 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002517 case QUIC_FT_MAX_DATA:
Amaury Denoyelle1e5e5132022-03-08 16:23:03 +01002518 if (qc->mux_state == QC_MUX_READY) {
2519 struct quic_max_data *data = &frm.max_data;
2520 qcc_recv_max_data(qc->qcc, data->max_data);
2521 }
2522 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002523 case QUIC_FT_MAX_STREAM_DATA:
Amaury Denoyelle8727ff42022-03-08 10:39:55 +01002524 if (qc->mux_state == QC_MUX_READY) {
2525 struct quic_max_stream_data *data = &frm.max_stream_data;
2526 qcc_recv_max_stream_data(qc->qcc, data->id,
2527 data->max_stream_data);
2528 }
2529 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002530 case QUIC_FT_MAX_STREAMS_BIDI:
2531 case QUIC_FT_MAX_STREAMS_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002532 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002533 case QUIC_FT_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002534 HA_ATOMIC_INC(&qc->prx_counters->data_blocked);
2535 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002536 case QUIC_FT_STREAM_DATA_BLOCKED:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002537 HA_ATOMIC_INC(&qc->prx_counters->stream_data_blocked);
2538 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002539 case QUIC_FT_STREAMS_BLOCKED_BIDI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002540 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_bidi);
2541 break;
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002542 case QUIC_FT_STREAMS_BLOCKED_UNI:
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02002543 HA_ATOMIC_INC(&qc->prx_counters->streams_data_blocked_uni);
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002544 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002545 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaille2cca2412022-01-21 13:55:03 +01002546 case QUIC_FT_RETIRE_CONNECTION_ID:
2547 /* XXX TO DO XXX */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002548 break;
2549 case QUIC_FT_CONNECTION_CLOSE:
2550 case QUIC_FT_CONNECTION_CLOSE_APP:
Frédéric Lécaille6f7607e2022-05-25 22:25:37 +02002551 /* Increment the error counters */
2552 qc_cc_err_count_inc(qc, &frm);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002553 if (!(qc->flags & QUIC_FL_CONN_DRAINING)) {
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002554 if (!(qc->flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
2555 qc->flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02002556 HA_ATOMIC_DEC(&qc->prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02002557 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01002558 TRACE_PROTO("Entering draining state", QUIC_EV_CONN_PRSHPKT, qc);
2559 /* RFC 9000 10.2. Immediate Close:
2560 * The closing and draining connection states exist to ensure
2561 * that connections close cleanly and that delayed or reordered
2562 * packets are properly discarded. These states SHOULD persist
2563 * for at least three times the current PTO interval...
2564 *
2565 * Rearm the idle timeout only one time when entering draining
2566 * state.
2567 */
2568 qc_idle_timer_do_rearm(qc);
2569 qc->flags |= QUIC_FL_CONN_DRAINING|QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02002570 qc_notify_close(qc);
Frédéric Lécaille47756802022-03-25 09:12:16 +01002571 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002572 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002573 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002574 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 goto err;
2576
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002577 qc->state = QUIC_HS_ST_CONFIRMED;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002578 break;
2579 default:
2580 goto err;
2581 }
2582 }
2583
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002584 /* Flag this packet number space as having received a packet. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02002585 qel->pktns->flags |= QUIC_FL_PKTNS_PKT_RECEIVED;
Frédéric Lécaille44ae7522022-03-21 12:18:00 +01002586
Frédéric Lécaillee248e372022-04-25 09:25:56 +02002587 if (fast_retrans) {
2588 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2589 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2590
2591 qc_prep_hdshk_fast_retrans(qc, &iqel->pktns->tx.frms, &hqel->pktns->tx.frms);
2592 }
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002593
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002594 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2595 * has successfully parse a Handshake packet. The Initial encryption must also
2596 * be discarded.
2597 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002598 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002599 if (qc->state >= QUIC_HS_ST_SERVER_INITIAL) {
Frédéric Lécaille05bd92b2022-03-29 19:09:46 +02002600 if (!(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx.flags &
2601 QUIC_FL_TLS_SECRETS_DCD)) {
2602 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2603 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
2604 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2605 qc_set_timer(ctx->qc);
2606 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2607 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
2608 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002609 if (qc->state < QUIC_HS_ST_SERVER_HANDSHAKE)
2610 qc->state = QUIC_HS_ST_SERVER_HANDSHAKE;
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002611 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612 }
2613
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002614 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002615 return 1;
2616
2617 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002618 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002619 return 0;
2620}
2621
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002622/* Must be called only by a <cbuf> writer (packet builder).
2623 * Return 1 if <cbuf> may be reused to build packets, depending on its <rd> and
2624 * <wr> internal indexes, 0 if not. When this is the case, reset <wr> writer
2625 * index after having marked the end of written data. This the responsability
2626 * of the caller to ensure there is enough room in <cbuf> to write the end of
2627 * data made of a uint16_t null field.
2628 *
2629 * +XXXXXXXXXXXXXXXXXXXXXXX---------------+ (cannot be reused)
2630 * ^ ^
2631 * r w
2632 *
2633 * +-------XXXXXXXXXXXXXXXX---------------+ (can be reused)
2634 * ^ ^
2635 * r w
2636
2637 * +--------------------------------------+ (empty buffer, can be reused)
2638 * ^
2639 * (r = w)
2640 *
2641 * +XXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX+ (full buffer, cannot be reused)
2642 * ^ ^
2643 * w r
2644 */
2645static int qc_may_reuse_cbuf(struct cbuf *cbuf)
2646{
2647 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2648
2649 /* We can reset the writer index only if in front of the reader index and
2650 * if the reader index is not null. Resetting the writer when the reader
2651 * index is null would empty the buffer.
2652 * XXX Note than the writer index cannot reach the reader index.
2653 * Only the reader index can reach the writer index.
2654 */
2655 if (rd && rd <= cbuf->wr) {
2656 /* Mark the end of contiguous data for the reader */
2657 write_u16(cb_wr(cbuf), 0);
2658 cb_add(cbuf, sizeof(uint16_t));
2659 cb_wr_reset(cbuf);
2660 return 1;
2661 }
2662
2663 return 0;
2664}
2665
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002666/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002667 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002668 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2669 * This function must be called only after having built a correct datagram.
2670 * Always succeeds.
2671 */
2672static inline void qc_set_dg(struct cbuf *cbuf,
2673 uint16_t dglen, struct quic_tx_packet *pkt)
2674{
2675 write_u16(cb_wr(cbuf), dglen);
2676 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2677 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2678}
2679
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002680/* Returns 1 if a packet may be built for <qc> from <qel> encryption level
2681 * with <frms> as ack-eliciting frame list to send, 0 if not.
2682 * <cc> must equal to 1 if an immediate close was asked, 0 if not.
2683 * <probe> must equalt to 1 if a probing packet is required, 0 if not.
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002684 * <force_ack> may be set to 1 if you want to force an ack.
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002685 */
2686static int qc_may_build_pkt(struct quic_conn *qc, struct list *frms,
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002687 struct quic_enc_level *qel, int cc, int probe, int force_ack)
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002688{
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002689 unsigned int must_ack = force_ack ||
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002690 qel->pktns->rx.nb_aepkts_since_last_ack >= QUIC_MAX_RX_AEPKTS_SINCE_LAST_ACK;
2691
2692 /* Do not build any more packet if the TX secrets are not available or
2693 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
2694 * and if there is no more packets to send upon PTO expiration
2695 * and if there is no more ack-eliciting frames to send or in flight
2696 * congestion control limit is reached for prepared data
2697 */
2698 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) ||
2699 (!cc && !probe && !must_ack &&
2700 (LIST_ISEMPTY(frms) || qc->path->prep_in_flight >= qc->path->cwnd))) {
2701 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
2702 return 0;
2703 }
2704
2705 return 1;
2706}
2707
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002708/* Prepare as much as possible short packets which are also datagrams into <qr>
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002709 * ring buffer for the QUIC connection with <ctx> as I/O handler context from
2710 * <frms> list of prebuilt frames.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002711 * A header made of two fields is added to each datagram: the datagram length followed
2712 * by the address of the first packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002713 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2714 * or -1 if something wrong happened.
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002715 */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01002716static int qc_prep_app_pkts(struct quic_conn *qc, struct qring *qr,
2717 struct list *frms)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002718{
2719 struct quic_enc_level *qel;
2720 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002721 unsigned char *end_buf, *end, *pos;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002722 struct quic_tx_packet *pkt;
2723 size_t total;
2724 size_t dg_headlen;
2725
2726 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2727 /* Each datagram is prepended with its length followed by the
2728 * address of the first packet in the datagram.
2729 */
2730 dg_headlen = sizeof(uint16_t) + sizeof pkt;
2731 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
2732 total = 0;
2733 start:
2734 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002735 pos = cb_wr(cbuf);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002736 /* Leave at least <sizeof(uint16_t)> bytes at the end of this buffer
2737 * to ensure there is enough room to mark the end of prepared
2738 * contiguous data with a zero length.
2739 */
2740 end_buf = pos + cb_contig_space(cbuf) - sizeof(uint16_t);
2741 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002742 int err, probe, cc;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002743
2744 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002745 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002746 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002747 /* We do not probe if an immediate close was asked */
2748 if (!cc)
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002749 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002750
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002751 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, 0))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002752 break;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002753
2754 /* Leave room for the datagram header */
2755 pos += dg_headlen;
2756 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
2757 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2758 }
2759 else {
2760 end = pos + qc->path->mtu;
2761 }
2762
Frédéric Lécaille301425b2022-06-14 17:40:39 +02002763 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 +02002764 QUIC_PACKET_TYPE_SHORT, probe, cc, &err);
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002765 switch (err) {
2766 case -2:
2767 goto err;
2768 case -1:
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01002769 /* As we provide qc_build_pkt() with an enough big buffer to fulfill an
2770 * MTU, we are here because of the congestion control window. There is
2771 * no need to try to reuse this buffer.
2772 */
2773 goto out;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002774 default:
2775 break;
2776 }
2777
2778 /* This is to please to GCC. We cannot have (err >= 0 && !pkt) */
2779 if (!pkt)
2780 goto err;
2781
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002782 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2783 pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2784
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002785 total += pkt->len;
2786 /* Set the current datagram as prepared into <cbuf>. */
2787 qc_set_dg(cbuf, pkt->len, pkt);
2788 }
2789
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002790 /* Reset <wr> writer index if in front of <rd> index */
2791 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002792 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002793 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002794 goto start;
Frédéric Lécaille1c5968b2022-02-25 17:15:21 +01002795 }
2796
2797 out:
2798 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
2799 return total;
2800
2801 err:
2802 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
2803 return -1;
2804}
2805
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002806/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002807 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2808 * several packets in the same datagram. A header made of two fields is added
2809 * to each datagram: the datagram length followed by the address of the first
2810 * packet in this datagram.
Frédéric Lécaille728b30d2022-03-10 17:42:58 +01002811 * Returns the number of bytes prepared in packets if succeeded (may be 0),
2812 * or -1 if something wrong happened.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002814static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002815 enum quic_tls_enc_level tel, struct list *tel_frms,
2816 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002818 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002819 struct cbuf *cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002820 unsigned char *end_buf, *end, *pos;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002821 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2822 /* length of datagrams */
2823 uint16_t dglen;
2824 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002825 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002826 /* Each datagram is prepended with its length followed by the
2827 * address of the first packet in the datagram.
2828 */
2829 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002830 struct list *frms;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002831
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002832 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2833
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002834 total = 0;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002835 qel = &qc->els[tel];
2836 frms = tel_frms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002837 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002838 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002839 padding = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002840 cbuf = qr->cbuf;
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002841 pos = cb_wr(cbuf);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002842 /* Leave at least <dglen> bytes at the end of this buffer
2843 * to ensure there is enough room to mark the end of prepared
2844 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002845 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002846 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2847 first_pkt = prv_pkt = NULL;
2848 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002849 int err, probe, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002850 enum quic_pkt_type pkt_type;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02002851 struct quic_tls_ctx *tls_ctx;
2852 const struct quic_version *ver;
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002853 int force_ack = (qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) &&
2854 (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2855 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002856
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002857 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002858 probe = 0;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002859 cc = qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002860 /* We do not probe if an immediate close was asked */
2861 if (!cc)
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002862 probe = qel->pktns->tx.pto_probe;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02002863
Frédéric Lécaille57bddbc2022-06-20 17:51:24 +02002864 if (!qc_may_build_pkt(qc, frms, qel, cc, probe, force_ack)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002865 if (prv_pkt)
2866 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002867 /* Let's select the next encryption level */
2868 if (tel != next_tel && next_tel != QUIC_TLS_ENC_LEVEL_NONE) {
2869 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002870 frms = next_tel_frms;
Frédéric Lécaille39ba1c32022-01-21 16:52:56 +01002871 qel = &qc->els[tel];
2872 /* Build a new datagram */
2873 prv_pkt = NULL;
2874 continue;
2875 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876 break;
2877 }
2878
2879 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002880 if (!prv_pkt) {
2881 /* Leave room for the datagram header */
2882 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002883 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002884 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2885 }
2886 else {
2887 end = pos + qc->path->mtu;
2888 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002889 }
2890
Frédéric Lécaille301425b2022-06-14 17:40:39 +02002891 if (qc->negotiated_version) {
2892 ver = qc->negotiated_version;
2893 if (qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2894 tls_ctx = &qc->negotiated_ictx;
2895 else
2896 tls_ctx = &qel->tls_ctx;
2897 }
2898 else {
2899 ver = qc->original_version;
2900 tls_ctx = &qel->tls_ctx;
2901 }
2902
2903 cur_pkt = qc_build_pkt(&pos, end, qel, tls_ctx, frms,
2904 qc, ver, dglen, padding, pkt_type, probe, cc, &err);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002905 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002906 case -2:
2907 goto err;
2908 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002909 /* If there was already a correct packet present, set the
2910 * current datagram as prepared into <cbuf>.
2911 */
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002912 if (prv_pkt)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002913 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaille05e30ee2022-02-28 16:55:32 +01002914 goto stop_build;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002915 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002916 break;
2917 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002918
Frédéric Lécaille63556772021-12-29 17:18:21 +01002919 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2920 if (!cur_pkt)
2921 goto err;
2922
Frédéric Lécaille1809c332022-04-25 10:24:12 +02002923 if (qc->flags & QUIC_FL_CONN_RETRANS_OLD_DATA)
2924 cur_pkt->flags |= QUIC_FL_TX_PACKET_PROBE_WITH_OLD_DATA;
2925
Frédéric Lécaille63556772021-12-29 17:18:21 +01002926 total += cur_pkt->len;
2927 /* keep trace of the first packet in the datagram */
2928 if (!first_pkt)
2929 first_pkt = cur_pkt;
2930 /* Attach the current one to the previous one */
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002931 if (prv_pkt) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002932 prv_pkt->next = cur_pkt;
Frédéric Lécailleb44cbc62022-04-21 17:58:46 +02002933 cur_pkt->flags |= QUIC_FL_TX_PACKET_COALESCED;
2934 }
Frédéric Lécaille63556772021-12-29 17:18:21 +01002935 /* Let's say we have to build a new dgram */
2936 prv_pkt = NULL;
2937 dglen += cur_pkt->len;
2938 /* Client: discard the Initial encryption keys as soon as
2939 * a handshake packet could be built.
2940 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002941 if (qc->state == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaille63556772021-12-29 17:18:21 +01002942 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2943 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2944 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2945 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2946 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002947 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002948 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02002949 qc->state = QUIC_HS_ST_CLIENT_HANDSHAKE;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002950 }
2951 /* If the data for the current encryption level have all been sent,
2952 * select the next level.
2953 */
2954 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02002955 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 +01002956 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2957 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2958 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2959 tel = next_tel;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002960 if (tel == QUIC_TLS_ENC_LEVEL_APP)
2961 frms = &qc->els[tel].pktns->tx.frms;
2962 else
2963 frms = next_tel_frms;
Frédéric Lécaille63556772021-12-29 17:18:21 +01002964 qel = &qc->els[tel];
Frédéric Lécaillefc888442022-04-11 15:39:34 +02002965 if (!LIST_ISEMPTY(frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002966 /* If there is data for the next level, do not
2967 * consume a datagram.
2968 */
2969 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002970 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002971 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002972 /* If we have to build a new datagram, set the current datagram as
2973 * prepared into <cbuf>.
2974 */
2975 if (!prv_pkt) {
2976 qc_set_dg(cbuf, dglen, first_pkt);
2977 first_pkt = NULL;
2978 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002979 padding = 0;
2980 }
2981 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002982 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002983 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2984 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002985 }
2986 }
2987
2988 stop_build:
2989 /* Reset <wr> writer index if in front of <rd> index */
2990 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002991 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille302c2b12022-03-14 12:21:03 +01002992 if (qc_may_reuse_cbuf(cbuf))
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002993 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002994 }
2995
2996 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002997 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002998 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002999
3000 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003001 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003002 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003003}
3004
3005/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003006 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003008int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003009{
3010 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003011 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003013 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003014 cbuf = qr->cbuf;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003015 TRACE_ENTER(QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003016 while (cb_contig_data(cbuf)) {
3017 unsigned char *pos;
3018 struct buffer tmpbuf = { };
3019 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
3020 uint16_t dglen;
3021 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022 unsigned int time_sent;
3023
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003024 pos = cb_rd(cbuf);
3025 dglen = read_u16(pos);
3026 /* End of prepared datagrams.
3027 * Reset the reader index only if in front of the writer index.
3028 */
3029 if (!dglen) {
3030 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
3031
3032 if (wr && wr < cbuf->rd) {
3033 cb_rd_reset(cbuf);
3034 continue;
3035 }
3036 break;
3037 }
3038
3039 pos += sizeof dglen;
3040 first_pkt = read_ptr(pos);
3041 pos += sizeof first_pkt;
3042 tmpbuf.area = (char *)pos;
3043 tmpbuf.size = tmpbuf.data = dglen;
3044
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003045 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003046 if(qc_snd_buf(qc, &tmpbuf, tmpbuf.data, 0) <= 0)
Amaury Denoyelle74f22922022-01-18 16:48:17 +01003047 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003048
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003049 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003050 qc->tx.bytes += tmpbuf.data;
3051 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003052
3053 for (pkt = first_pkt; pkt; pkt = next_pkt) {
3054 pkt->time_sent = time_sent;
3055 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
3056 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003057 qc->path->ifae_pkts++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003058 if (qc->flags & QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ)
3059 qc_idle_timer_rearm(qc, 0);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 }
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003061 if (!(qc->flags & QUIC_FL_CONN_CLOSING) &&
3062 (pkt->flags & QUIC_FL_TX_PACKET_CC)) {
3063 qc->flags |= QUIC_FL_CONN_CLOSING;
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02003064 qc_notify_close(qc);
3065
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02003066 /* RFC 9000 10.2. Immediate Close:
3067 * The closing and draining connection states exist to ensure
3068 * that connections close cleanly and that delayed or reordered
3069 * packets are properly discarded. These states SHOULD persist
3070 * for at least three times the current PTO interval...
3071 *
3072 * Rearm the idle timeout only one time when entering closing
3073 * state.
3074 */
3075 qc_idle_timer_do_rearm(qc);
3076 if (qc->timer_task) {
3077 task_destroy(qc->timer_task);
3078 qc->timer_task = NULL;
3079 }
3080 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003081 qc->path->in_flight += pkt->in_flight_len;
3082 pkt->pktns->tx.in_flight += pkt->in_flight_len;
3083 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003084 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003085 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003086 next_pkt = pkt->next;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01003087 quic_tx_packet_refinc(pkt);
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02003088 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003089 }
3090 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091
Frédéric Lécaille8726d632022-05-03 10:32:21 +02003092 TRACE_LEAVE(QUIC_EV_CONN_SPPKTS, qc);
3093
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003094 return 1;
3095}
3096
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003097/* Copy into <buf> buffer a stateless reset token depending on the
3098 * <salt> salt input. This is the cluster secret which will be derived
3099 * as HKDF input secret to generate this token.
3100 * Return 1 if succeeded, 0 if not.
3101 */
3102static int quic_stateless_reset_token_cpy(unsigned char *buf, size_t len,
3103 const unsigned char *salt, size_t saltlen)
3104{
3105 /* Input secret */
3106 const unsigned char *key = (const unsigned char *)global.cluster_secret;
3107 size_t keylen = strlen(global.cluster_secret);
3108 /* Info */
3109 const unsigned char label[] = "stateless token";
3110 size_t labellen = sizeof label - 1;
3111
3112 return quic_hkdf_extract_and_expand(EVP_sha256(), buf, len,
3113 key, keylen, salt, saltlen, label, labellen);
3114}
3115
3116/* Initialize the stateless reset token attached to <cid> connection ID.
3117 * Returns 1 if succeeded, 0 if not.
3118 */
3119static int quic_stateless_reset_token_init(struct quic_connection_id *quic_cid)
3120{
3121 if (global.cluster_secret) {
3122 /* Output secret */
3123 unsigned char *token = quic_cid->stateless_reset_token;
3124 size_t tokenlen = sizeof quic_cid->stateless_reset_token;
3125 /* Salt */
3126 const unsigned char *cid = quic_cid->cid.data;
3127 size_t cidlen = quic_cid->cid.len;
3128
3129 return quic_stateless_reset_token_cpy(token, tokenlen, cid, cidlen);
3130 }
3131 else {
3132 return RAND_bytes(quic_cid->stateless_reset_token,
3133 sizeof quic_cid->stateless_reset_token) == 1;
3134 }
3135}
3136
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003137/* Allocate a new CID with <seq_num> as sequence number and attach it to <root>
3138 * ebtree.
3139 *
3140 * The CID is randomly generated in part with the result altered to be
3141 * associated with the current thread ID. This means this function must only
3142 * be called by the quic_conn thread.
3143 *
3144 * Returns the new CID if succeeded, NULL if not.
3145 */
3146static struct quic_connection_id *new_quic_cid(struct eb_root *root,
3147 struct quic_conn *qc,
3148 int seq_num)
3149{
3150 struct quic_connection_id *cid;
3151
3152 cid = pool_alloc(pool_head_quic_connection_id);
3153 if (!cid)
3154 return NULL;
3155
3156 cid->cid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003157 if (RAND_bytes(cid->cid.data, cid->cid.len) != 1)
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003158 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003159
3160 quic_pin_cid_to_tid(cid->cid.data, tid);
Frédéric Lécaille28a17952022-05-06 15:07:20 +02003161 if (quic_stateless_reset_token_init(cid) != 1)
3162 goto err;
Frédéric Lécaille0226c522022-05-06 14:18:53 +02003163
3164 cid->qc = qc;
3165
3166 cid->seq_num.key = seq_num;
3167 cid->retire_prior_to = 0;
3168 /* insert the allocated CID in the quic_conn tree */
3169 eb64_insert(root, &cid->seq_num);
3170
3171 return cid;
3172
3173 err:
3174 pool_free(pool_head_quic_connection_id, cid);
3175 return NULL;
3176}
3177
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003178/* Build all the frames which must be sent just after the handshake have succeeded.
3179 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
3180 * a HANDSHAKE_DONE frame.
3181 * Return 1 if succeeded, 0 if not.
3182 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003183static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003184{
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003185 int i, first, max;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003186 struct quic_enc_level *qel;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003187 struct quic_frame *frm, *frmbak;
3188 struct list frm_list = LIST_HEAD_INIT(frm_list);
3189 struct eb64_node *node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003190
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02003191 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003192 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003193 if (qc_is_listener(qc)) {
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003194 frm = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01003195 if (!frm)
3196 return 0;
3197
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003198 LIST_INIT(&frm->reflist);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003199 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003200 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003201 }
3202
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003203 first = 1;
3204 max = qc->tx.params.active_connection_id_limit;
3205 for (i = first; i < max; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003206 struct quic_connection_id *cid;
3207
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003208 frm = pool_zalloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003209 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003210 goto err;
3211
Frédéric Lécailleb9171912022-04-21 17:32:10 +02003212 LIST_INIT(&frm->reflist);
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01003213 cid = new_quic_cid(&qc->cids, qc, i);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003214 if (!cid)
3215 goto err;
3216
Frédéric Lécaille74904a42022-01-27 15:35:56 +01003217 /* insert the allocated CID in the receiver datagram handler tree */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01003218 ebmb_insert(&quic_dghdlrs[tid].cids, &cid->node, cid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003219
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003220 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003221 LIST_APPEND(&frm_list, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003222 }
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003223
3224 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003225 qc->flags |= QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003226
3227 return 1;
3228
3229 err:
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003230 /* free the frames */
3231 list_for_each_entry_safe(frm, frmbak, &frm_list, list)
3232 pool_free(pool_head_quic_frame, frm);
3233
3234 node = eb64_first(&qc->cids);
3235 while (node) {
3236 struct quic_connection_id *cid;
3237
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003238
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003239 cid = eb64_entry(node, struct quic_connection_id, seq_num);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003240 if (cid->seq_num.key >= max)
3241 break;
3242
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003243 if (cid->seq_num.key < first)
3244 continue;
3245
Frédéric Lécaille411aa6d2022-03-21 12:01:22 +01003246 node = eb64_next(node);
Frédéric Lécailled64f68f2022-03-18 17:45:28 +01003247 ebmb_delete(&cid->node);
3248 eb64_delete(&cid->seq_num);
3249 pool_free(pool_head_quic_connection_id, cid);
3250 }
3251
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003252 return 0;
3253}
3254
3255/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille64670882022-04-01 11:57:19 +02003256void quic_free_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003257{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003258 struct eb64_node *n;
3259 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003260
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003261 n = eb64_first(&arngs->root);
3262 while (n) {
3263 struct eb64_node *next;
3264
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003265 ar = eb64_entry(n, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003266 next = eb64_next(n);
3267 eb64_delete(n);
Frédéric Lécaille82851bd2022-04-04 13:43:58 +02003268 pool_free(pool_head_quic_arng, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003269 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003270 }
3271}
3272
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003273/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
3274 * descending order.
3275 */
3276static inline size_t sack_gap(struct quic_arng_node *p,
3277 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003278{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003279 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280}
3281
3282
3283/* Remove the last elements of <ack_ranges> list of ack range updating its
3284 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003285 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003287static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003288{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003289 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003291 last = eb64_last(&arngs->root);
3292 while (last && arngs->enc_sz > limit) {
3293 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003294
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003295 prev = eb64_prev(last);
3296 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003297 return 0;
3298
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003299 last_node = eb64_entry(last, struct quic_arng_node, first);
3300 prev_node = eb64_entry(prev, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003301 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
3302 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
3303 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
3304 --arngs->sz;
3305 eb64_delete(last);
3306 pool_free(pool_head_quic_arng, last);
3307 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003308 }
3309
3310 return 1;
3311}
3312
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003313/* Set the encoded size of <arngs> QUIC ack ranges. */
3314static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
3315{
3316 struct eb64_node *node, *next;
3317 struct quic_arng_node *ar, *ar_next;
3318
3319 node = eb64_last(&arngs->root);
3320 if (!node)
3321 return;
3322
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003323 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003324 arngs->enc_sz = quic_int_getsize(ar->last) +
3325 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
3326
3327 while ((next = eb64_prev(node))) {
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003328 ar_next = eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003329 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
3330 quic_int_getsize(ar_next->last - ar_next->first.key);
3331 node = next;
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003332 ar = eb64_entry(node, struct quic_arng_node, first);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003333 }
3334}
3335
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003336/* Insert <ar> ack range into <argns> tree of ack ranges.
3337 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003338 */
3339static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003340struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
3341 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003342{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003343 struct quic_arng_node *new_ar;
3344
3345 new_ar = pool_alloc(pool_head_quic_arng);
3346 if (new_ar) {
3347 new_ar->first.key = ar->first;
3348 new_ar->last = ar->last;
3349 eb64_insert(&arngs->root, &new_ar->first);
3350 arngs->sz++;
3351 }
3352
3353 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003354}
3355
3356/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003357 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003358 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359 *
3360 * Descending order
3361 * ------------->
3362 * range1 range2
3363 * ..........|--------|..............|--------|
3364 * ^ ^ ^ ^
3365 * | | | |
3366 * last1 first1 last2 first2
3367 * ..........+--------+--------------+--------+......
3368 * diff1 gap12 diff2
3369 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003370 * To encode the previous list of ranges we must encode integers as follows in
3371 * descending order:
3372 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003373 * with diff1 = last1 - first1
3374 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003375 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003376 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003377 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003378int quic_update_ack_ranges_list(struct quic_arngs *arngs,
3379 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003380{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003381 struct eb64_node *le;
3382 struct quic_arng_node *new_node;
3383 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003384
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003385 new = NULL;
3386 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003387 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003388 if (!new_node)
3389 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003390
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003391 goto out;
3392 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003393
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003394 le = eb64_lookup_le(&arngs->root, ar->first);
3395 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003396 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003397 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003398 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01003399
3400 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003401 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003402 else {
3403 struct quic_arng_node *le_ar =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003404 eb64_entry(le, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003405
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003406 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02003407 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003408 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003409
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003410 if (le_ar->last + 1 >= ar->first) {
3411 le_ar->last = ar->last;
3412 new = le;
3413 new_node = le_ar;
3414 }
3415 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02003416 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003417 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003418 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02003419
3420 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003421 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003422 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003424 /* Verify that the new inserted node does not overlap the nodes
3425 * which follow it.
3426 */
3427 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003428 struct eb64_node *next;
3429 struct quic_arng_node *next_node;
3430
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003431 while ((next = eb64_next(new))) {
3432 next_node =
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003433 eb64_entry(next, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003434 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003435 break;
3436
3437 if (next_node->last > new_node->last)
3438 new_node->last = next_node->last;
3439 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003440 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003441 /* Decrement the size of these ranges. */
3442 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003443 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003444 }
3445
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003446 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003447 quic_arngs_set_enc_sz(arngs);
3448
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449 return 1;
3450}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003451/* Remove the header protection of packets at <el> encryption level.
3452 * Always succeeds.
3453 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003454static 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 +01003455{
3456 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003457 struct quic_rx_packet *pqpkt;
3458 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003459 struct quic_enc_level *app_qel;
3460
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003461 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3462 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003463 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003464 if (el == app_qel && qc_is_listener(qc) && qc->state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003465 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003466 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003467 goto out;
3468 }
3469 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003470 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003471 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003472 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003473 pqpkt->data, pqpkt->data + pqpkt->len)) {
3474 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003475 /* XXX TO DO XXX */
3476 }
3477 else {
3478 /* The AAD includes the packet number field */
3479 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3480 /* Store the packet into the tree of packets to decrypt. */
3481 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003482 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003483 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3484 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003485 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003486 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003487 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003488 MT_LIST_DELETE_SAFE(pkttmp1);
3489 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490 }
3491
3492 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003493 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494}
3495
3496/* Process all the CRYPTO frame at <el> encryption level.
3497 * Return 1 if succeeded, 0 if not.
3498 */
3499static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003500 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003501{
3502 struct eb64_node *node;
3503
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003504 node = eb64_first(&el->rx.crypto.frms);
3505 while (node) {
3506 struct quic_rx_crypto_frm *cf;
3507
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003508 cf = eb64_entry(node, struct quic_rx_crypto_frm, offset_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003509 if (cf->offset_node.key != el->rx.crypto.offset)
3510 break;
3511
3512 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3513 goto err;
3514
3515 node = eb64_next(node);
3516 quic_rx_packet_refdec(cf->pkt);
3517 eb64_delete(&cf->offset_node);
3518 pool_free(pool_head_quic_rx_crypto_frm, cf);
3519 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003520 return 1;
3521
3522 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003523 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 return 0;
3525}
3526
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003527/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003528 * 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 +02003529 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003530 * Return 1 if succeeded, 0 if not.
3531 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003532int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3533 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003534{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003535 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003536 int64_t largest_pn = -1;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003537 unsigned int largest_pn_time_received = 0;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01003538 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003539 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003540
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003541 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003542 qel = cur_el;
3543 next_tel:
3544 if (!qel)
3545 goto out;
3546
3547 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3548 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003549 while (node) {
3550 struct quic_rx_packet *pkt;
3551
Frédéric Lécaillea54e49d2022-05-10 15:15:24 +02003552 pkt = eb64_entry(node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003553 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003554 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003555 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003556 /* Drop the packet */
3557 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003558 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003559 }
3560 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003561 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003562 /* Drop the packet */
3563 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003564 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02003565 HA_ATOMIC_INC(&qc->prx_counters->dropped_parsing);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003566 }
3567 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003568 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3569
Frédéric Lécailleb0021452022-03-29 11:42:03 +02003570 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING || force_ack) {
3571 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_REQUIRED;
3572 qel->pktns->rx.nb_aepkts_since_last_ack++;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01003573 qc_idle_timer_rearm(qc, 1);
3574 }
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003575 if (pkt->pn > largest_pn) {
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003576 largest_pn = pkt->pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003577 largest_pn_time_received = pkt->time_received;
3578 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003579 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003580 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003581 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003582 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003583 }
3584 }
3585 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003586 eb64_delete(&pkt->pn_node);
3587 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003588 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003589 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003591 if (largest_pn != -1 && largest_pn > qel->pktns->rx.largest_pn) {
3592 /* Update the largest packet number. */
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02003593 qel->pktns->rx.largest_pn = largest_pn;
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02003594 /* Update the largest acknowledged packet timestamps */
3595 qel->pktns->rx.largest_time_received = largest_pn_time_received;
3596 qel->pktns->flags |= QUIC_FL_PKTNS_NEW_LARGEST_PN;
3597 }
3598
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003599 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600 goto err;
3601
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003602 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003603 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003604 qel = next_el;
Frédéric Lécaille91a211f2022-05-24 10:54:42 +02003605 largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003606 goto next_tel;
3607 }
3608
3609 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003610 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003611 return 1;
3612
3613 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003614 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615 return 0;
3616}
3617
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003618/* Check if it's possible to remove header protection for packets related to
3619 * encryption level <qel>. If <qel> is NULL, assume it's false.
3620 *
3621 * Return true if the operation is possible else false.
3622 */
3623static int qc_qel_may_rm_hp(struct quic_conn *qc, struct quic_enc_level *qel)
3624{
3625 enum quic_tls_enc_level tel;
3626
3627 if (!qel)
3628 return 0;
3629
3630 tel = ssl_to_quic_enc_level(qel->level);
3631
3632 /* check if tls secrets are available */
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003633 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003634 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille51c90652022-02-22 11:39:14 +01003635 return 0;
3636 }
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003637
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003638 if (!(qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003639 return 0;
3640
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01003641 /* check if the connection layer is ready before using app level */
Frédéric Lécaille298931d2022-01-28 21:41:06 +01003642 if ((tel == QUIC_TLS_ENC_LEVEL_APP || tel == QUIC_TLS_ENC_LEVEL_EARLY_DATA) &&
Frédéric Lécaille12aa26b2022-03-21 11:37:13 +01003643 qc->mux_state == QC_MUX_NULL)
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003644 return 0;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003645
3646 return 1;
3647}
3648
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003649/* Sends application level packets from <qc> QUIC connection */
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003650int qc_send_app_pkts(struct quic_conn *qc, int old_data, struct list *frms)
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003651{
3652 int ret;
3653 struct qring *qr;
3654
3655 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3656 if (!qr)
3657 /* Never happens */
3658 return 1;
3659
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003660 if (old_data)
3661 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01003662 ret = qc_prep_app_pkts(qc, qr, frms);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003663 if (ret == -1)
3664 goto err;
3665 else if (ret == 0)
3666 goto out;
3667
3668 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3669 goto err;
3670
3671 out:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003672 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003673 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3674 return 1;
3675
3676 err:
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003677 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003678 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3679 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3680 return 0;
3681}
3682
Frédéric Lécaillea9568412022-04-25 08:58:04 +02003683/* Sends handshake packets from up to two encryption levels <tel> and <next_te>
3684 * with <tel_frms> and <next_tel_frms> as frame list respectively for <qc>
3685 * QUIC connection
3686 * Returns 1 if succeeded, 0 if not.
3687 */
3688int qc_send_hdshk_pkts(struct quic_conn *qc, int old_data,
3689 enum quic_tls_enc_level tel, struct list *tel_frms,
3690 enum quic_tls_enc_level next_tel, struct list *next_tel_frms)
3691{
3692 int ret;
3693 struct qring *qr;
3694
3695 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
3696 if (!qr)
3697 /* Never happens */
3698 return 1;
3699
3700 if (old_data)
3701 qc->flags |= QUIC_FL_CONN_RETRANS_OLD_DATA;
3702 ret = qc_prep_pkts(qc, qr, tel, tel_frms, next_tel, next_tel_frms);
3703 if (ret == -1)
3704 goto err;
3705 else if (ret == 0)
3706 goto out;
3707
3708 if (!qc_send_ppkts(qr, qc->xprt_ctx))
3709 goto err;
3710
3711 out:
3712 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3713 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3714 return 1;
3715
3716 err:
3717 qc->flags &= ~QUIC_FL_CONN_RETRANS_OLD_DATA;
3718 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
3719 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc);
3720 return 0;
3721}
3722
3723/* Retransmit up to two datagrams depending on packet number space */
3724static void qc_dgrams_retransmit(struct quic_conn *qc)
3725{
3726 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3727 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3728 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
3729
3730 if (iqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3731 struct list ifrms = LIST_HEAD_INIT(ifrms);
3732 struct list hfrms = LIST_HEAD_INIT(hfrms);
3733
3734 qc_prep_hdshk_fast_retrans(qc, &ifrms, &hfrms);
3735 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &ifrms);
3736 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3737 if (!LIST_ISEMPTY(&ifrms)) {
3738 iqel->pktns->tx.pto_probe = 1;
3739 if (!LIST_ISEMPTY(&hfrms)) {
3740 hqel->pktns->tx.pto_probe = 1;
3741 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_INITIAL, &ifrms,
3742 QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms);
3743 }
3744 }
3745 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3746 qc_prep_fast_retrans(qc, hqel, &hfrms, NULL);
3747 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &hfrms);
3748 if (!LIST_ISEMPTY(&hfrms)) {
3749 hqel->pktns->tx.pto_probe = 1;
3750 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &hfrms,
3751 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3752 }
3753 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3754 }
3755 iqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3756 }
3757 else {
3758 int i;
3759 struct list frms1 = LIST_HEAD_INIT(frms1);
3760 struct list frms2 = LIST_HEAD_INIT(frms2);
3761
3762 if (hqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3763 hqel->pktns->tx.pto_probe = 0;
3764 for (i = 0; i < QUIC_MAX_NB_PTO_DGRAMS; i++) {
3765 qc_prep_fast_retrans(qc, hqel, &frms1, NULL);
3766 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3767 if (!LIST_ISEMPTY(&frms1)) {
3768 hqel->pktns->tx.pto_probe = 1;
3769 qc_send_hdshk_pkts(qc, 1, QUIC_TLS_ENC_LEVEL_HANDSHAKE, &frms1,
3770 QUIC_TLS_ENC_LEVEL_NONE, NULL);
3771 }
3772 }
3773 hqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3774 }
3775 else if (aqel->pktns->flags & QUIC_FL_PKTNS_PROBE_NEEDED) {
3776 aqel->pktns->tx.pto_probe = 0;
3777 qc_prep_fast_retrans(qc, aqel, &frms1, &frms2);
3778 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms1);
3779 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, &frms2);
3780 if (!LIST_ISEMPTY(&frms1)) {
3781 aqel->pktns->tx.pto_probe = 1;
3782 qc_send_app_pkts(qc, 1, &frms1);
3783 }
3784 if (!LIST_ISEMPTY(&frms2)) {
3785 aqel->pktns->tx.pto_probe = 1;
3786 qc_send_app_pkts(qc, 1, &frms2);
3787 }
3788 aqel->pktns->flags &= ~QUIC_FL_PKTNS_PROBE_NEEDED;
3789 }
3790 }
3791}
3792
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003793/* QUIC connection packet handler task (post handshake) */
3794static struct task *quic_conn_app_io_cb(struct task *t, void *context, unsigned int state)
3795{
3796 struct ssl_sock_ctx *ctx;
3797 struct quic_conn *qc;
3798 struct quic_enc_level *qel;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003799
3800
3801 ctx = context;
3802 qc = ctx->qc;
3803 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003804
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003805 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003806
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003807 /* Retranmissions */
3808 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3809 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_IO_CB, qc);
3810 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3811 qc_dgrams_retransmit(qc);
3812 }
3813
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003814 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
3815 qc_rm_hp_pkts(qc, qel);
3816
3817 if (!qc_treat_rx_pkts(qel, NULL, ctx, 0))
3818 goto err;
3819
Frédéric Lécaille47756802022-03-25 09:12:16 +01003820 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3821 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3822 goto out;
3823
Frédéric Lécaille3e3a6212022-04-25 10:17:00 +02003824 if (!qc_send_app_pkts(qc, 0, &qel->pktns->tx.frms))
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003825 goto err;
3826
Frédéric Lécaille47756802022-03-25 09:12:16 +01003827out:
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003828 return t;
3829
3830 err:
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003831 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &qc->state);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003832 return t;
3833}
3834
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003835/* QUIC connection packet handler task. */
3836struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003837{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003838 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003839 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003840 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003841 enum quic_tls_enc_level tel, next_tel;
3842 struct quic_enc_level *qel, *next_qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003843 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003844 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003845
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003846 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003847 qc = ctx->qc;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003848 TRACE_ENTER(QUIC_EV_CONN_IO_CB, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003849 qr = NULL;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003850 st = qc->state;
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003851 TRACE_PROTO("state", QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille7aef5f42022-04-25 10:33:12 +02003852
3853 /* Retranmissions */
3854 if (qc->flags & QUIC_FL_CONN_RETRANS_NEEDED) {
3855 TRACE_PROTO("retransmission needed", QUIC_EV_CONN_PHPKTS, qc);
3856 qc->flags &= ~QUIC_FL_CONN_RETRANS_NEEDED;
3857 qc_dgrams_retransmit(qc);
3858 }
3859
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003860 if (qc->flags & QUIC_FL_CONN_IO_CB_WAKEUP) {
3861 qc->flags &= ~QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003862 /* The I/O handler has been woken up by the dgram listener
3863 * after the anti-amplification was reached.
3864 */
3865 qc_set_timer(qc);
3866 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3867 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3868 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003869 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003870 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3871 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3872 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003873 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003874 if (st >= QUIC_HS_ST_COMPLETE &&
3875 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3876 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3877 /* There may be remaining Handshake packets to treat and acknowledge. */
3878 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3879 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3880 }
3881 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003882 goto err;
3883
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003884 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003885 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003886
3887 next_level:
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01003888 /* Treat packets waiting for header packet protection decryption */
3889 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) && qc_qel_may_rm_hp(qc, qel))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003890 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003891
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003892 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3893 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3894 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003895 goto err;
3896
Frédéric Lécaille47756802022-03-25 09:12:16 +01003897 if ((qc->flags & QUIC_FL_CONN_DRAINING) &&
3898 !(qc->flags & QUIC_FL_CONN_IMMEDIATE_CLOSE))
3899 goto out;
3900
Frédéric Lécaille1231d3c2022-04-28 15:43:46 +02003901 if (next_qel && next_qel == &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA] &&
3902 !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts)) {
3903 if ((next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3904 qel = next_qel;
3905 next_qel = NULL;
3906 goto next_level;
3907 }
3908 else {
3909 struct quic_rx_packet *pkt;
3910 struct mt_list *elt1, elt2;
3911 struct quic_enc_level *aqel = &qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA];
3912
3913 /* Drop these 0-RTT packets */
3914 TRACE_PROTO("drop all 0-RTT packets", QUIC_EV_CONN_PHPKTS, qc);
3915 mt_list_for_each_entry_safe(pkt, &aqel->rx.pqpkts, list, elt1, elt2) {
3916 MT_LIST_DELETE_SAFE(elt1);
3917 quic_rx_packet_refdec(pkt);
3918 }
3919 }
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003920 }
3921
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003922 st = qc->state;
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003923 if (st >= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02003924 if (!(qc->flags & QUIC_FL_CONN_POST_HANDSHAKE_FRAMES_BUILT) &&
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003925 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003926 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003927
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003928 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.flags &
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003929 QUIC_FL_TLS_SECRETS_DCD)) {
3930 /* Discard the Handshake keys. */
3931 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3932 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3933 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3934 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003935 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003936 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003937 }
3938
3939 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3940 /* There may be remaining handshake to build (acks) */
3941 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3942 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003943 }
3944
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003945 if (!qr)
3946 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003947 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3948 * be considered.
3949 */
3950 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003951 goto err;
Frédéric Lécaillefc888442022-04-11 15:39:34 +02003952 ret = qc_prep_pkts(qc, qr, tel, &qc->els[tel].pktns->tx.frms,
3953 next_tel, &qc->els[next_tel].pktns->tx.frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003954 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003955 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003956 else if (ret == 0)
3957 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003958
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003959 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003960 goto err;
3961
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003962 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003963 /* Check if there is something to do for the next level.
3964 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003965 if (next_qel && next_qel != qel &&
Frédéric Lécaillebd242082022-02-25 17:17:59 +01003966 (next_qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003967 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003968 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003969 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003970 goto next_level;
3971 }
3972
Frédéric Lécaille47756802022-03-25 09:12:16 +01003973 out:
3974 if (qr)
3975 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003976 TRACE_LEAVE(QUIC_EV_CONN_IO_CB, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003977 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003978
3979 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003980 if (qr)
3981 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécaille00e24002022-02-18 17:13:45 +01003982 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_IO_CB, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003983 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003984}
3985
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003986/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003987static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3988{
3989 int i;
3990
3991 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3992 if (qel->tx.crypto.bufs[i]) {
3993 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3994 qel->tx.crypto.bufs[i] = NULL;
3995 }
3996 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003997 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003998}
3999
4000/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004001 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004002 * Returns 1 if succeeded, 0 if not.
4003 */
4004static int quic_conn_enc_level_init(struct quic_conn *qc,
4005 enum quic_tls_enc_level level)
4006{
4007 struct quic_enc_level *qel;
4008
4009 qel = &qc->els[level];
4010 qel->level = quic_to_ssl_enc_level(level);
4011 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
4012 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
4013 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01004014 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004015
4016 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02004017 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02004018 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02004019 qel->rx.crypto.offset = 0;
4020 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021
4022 /* Allocate only one buffer. */
4023 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
4024 if (!qel->tx.crypto.bufs)
4025 goto err;
4026
4027 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
4028 if (!qel->tx.crypto.bufs[0])
4029 goto err;
4030
4031 qel->tx.crypto.bufs[0]->sz = 0;
4032 qel->tx.crypto.nb_buf = 1;
4033
4034 qel->tx.crypto.sz = 0;
4035 qel->tx.crypto.offset = 0;
4036
4037 return 1;
4038
4039 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004040 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004041 return 0;
4042}
4043
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004044/* Release the quic_conn <qc>. The connection is removed from the CIDs tree.
4045 * The connection tasklet is killed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004046 *
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004047 * This function must only be called by the thread responsible of the quic_conn
4048 * tasklet.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004049 */
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004050static void quic_conn_release(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004051{
4052 int i;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004053 struct ssl_sock_ctx *conn_ctx;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004054 struct eb64_node *node;
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004055 struct quic_tls_ctx *app_tls_ctx;
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004056 struct quic_rx_packet *pkt, *pktback;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004057
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004058 /* We must not free the quic-conn if the MUX is still allocated. */
4059 BUG_ON(qc->mux_state == QC_MUX_READY);
4060
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004061 /* free remaining stream descriptors */
4062 node = eb64_first(&qc->streams_by_id);
4063 while (node) {
4064 struct qc_stream_desc *stream;
4065
4066 stream = eb64_entry(node, struct qc_stream_desc, by_id);
4067 node = eb64_next(node);
4068
Amaury Denoyellec9acc312022-04-01 16:41:21 +02004069 /* all streams attached to the quic-conn are released, so
4070 * qc_stream_desc_free will liberate the stream instance.
4071 */
4072 BUG_ON(!stream->release);
4073 qc_stream_desc_free(stream);
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004074 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004075
Amaury Denoyelle2c31e122022-06-20 10:52:55 +02004076 /* Purge Rx packet list. */
4077 list_for_each_entry_safe(pkt, pktback, &qc->rx.pkt_list, qc_rx_pkt_list) {
4078 LIST_DELETE(&pkt->qc_rx_pkt_list);
4079 pool_free(pool_head_quic_rx_packet, pkt);
4080 }
4081
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004082 if (qc->idle_timer_task) {
4083 task_destroy(qc->idle_timer_task);
4084 qc->idle_timer_task = NULL;
4085 }
4086
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004087 if (qc->timer_task) {
4088 task_destroy(qc->timer_task);
4089 qc->timer_task = NULL;
4090 }
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004091
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004092 /* remove the connection from receiver cids trees */
4093 ebmb_delete(&qc->odcid_node);
4094 ebmb_delete(&qc->scid_node);
4095 free_quic_conn_cids(qc);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004096
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004097 conn_ctx = qc->xprt_ctx;
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004098 if (conn_ctx) {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004099 tasklet_free(conn_ctx->wait_event.tasklet);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004100 SSL_free(conn_ctx->ssl);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004101 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle2d9794b2022-01-20 17:43:20 +01004102 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004103
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004104 quic_tls_ku_free(qc);
4105 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
4106 quic_tls_ctx_secs_free(&qc->els[i].tls_ctx);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004107 quic_conn_enc_level_uninit(&qc->els[i]);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004108 }
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004109 quic_tls_ctx_secs_free(&qc->negotiated_ictx);
Frédéric Lécaille96fd1632022-04-01 11:21:47 +02004110
4111 app_tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
4112 pool_free(pool_head_quic_tls_secret, app_tls_ctx->rx.secret);
4113 pool_free(pool_head_quic_tls_secret, app_tls_ctx->tx.secret);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004114
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004115 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++) {
4116 quic_pktns_tx_pkts_release(&qc->pktns[i]);
Frédéric Lécaille64670882022-04-01 11:57:19 +02004117 quic_free_arngs(&qc->pktns[i].rx.arngs);
Frédéric Lécailleeb2a2da2022-04-01 12:15:24 +02004118 }
Frédéric Lécaille64670882022-04-01 11:57:19 +02004119
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01004120 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
4121 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004122 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004123}
4124
Amaury Denoyellee0be5732022-04-05 17:34:18 +02004125static void quic_close(struct connection *conn, void *xprt_ctx)
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004126{
4127 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
Amaury Denoyelle29632b82022-01-18 16:50:58 +01004128 struct quic_conn *qc = conn_ctx->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004129
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004130 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01004131
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004132 /* Next application data can be dropped. */
4133 qc->mux_state = QC_MUX_RELEASED;
4134
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004135 /* If the quic-conn timer has already expired free the quic-conn. */
4136 if (qc->flags & QUIC_FL_CONN_EXP_TIMER) {
4137 quic_conn_release(qc);
4138 TRACE_LEAVE(QUIC_EV_CONN_CLOSE);
4139 return;
4140 }
4141
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01004142 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004143}
4144
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004145/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01004146static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004147{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004148 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004149 struct quic_conn *qc;
4150 struct quic_pktns *pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004151
4152 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01004153 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004154 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01004155 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004156 task->expire = TICK_ETERNITY;
4157 pktns = quic_loss_pktns(qc);
4158 if (tick_isset(pktns->tx.loss_time)) {
4159 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
4160
4161 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
4162 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004163 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
4164 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004165 goto out;
4166 }
4167
4168 if (qc->path->in_flight) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004169 pktns = quic_pto_pktns(qc, qc->state >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécailledafbde62022-04-26 13:54:28 +02004170 if (qc->mux_state == QC_MUX_READY && qc->qcc->subs &&
4171 qc->qcc->subs->events & SUB_RETRY_SEND) {
4172 struct qcc *qcc = qc->qcc;
4173
4174 pktns->tx.pto_probe = QUIC_MAX_NB_PTO_DGRAMS;
4175 tasklet_wakeup(qcc->subs->tasklet);
4176 qcc->subs->events &= ~SUB_RETRY_SEND;
4177 if (!qcc->subs->events)
4178 qcc->subs = NULL;
4179 }
4180 else {
4181 qc->flags |= QUIC_FL_CONN_RETRANS_NEEDED;
4182 pktns->flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4183 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
4184 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
4185 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].flags |= QUIC_FL_PKTNS_PROBE_NEEDED;
4186 }
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01004187 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004188 }
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004189 else if (!qc_is_listener(qc) && qc->state <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004190 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
4191 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
4192
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004193 if (hel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194 hel->pktns->tx.pto_probe = 1;
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004195 if (iel->tls_ctx.flags == QUIC_FL_TLS_SECRETS_SET)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196 iel->pktns->tx.pto_probe = 1;
4197 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01004198
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4200 qc->path->loss.pto_count++;
4201
4202 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004203 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004204
4205 return task;
4206}
4207
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004208/* Parse the Retry token from buffer <token> with <end> a pointer to
4209 * one byte past the end of this buffer. This will extract the ODCID
4210 * which will be stored into <odcid>
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004211 *
4212 * Returns 0 on success else non-zero.
4213 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004214static int parse_retry_token(const unsigned char *token, const unsigned char *end,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004215 struct quic_cid *odcid)
4216{
4217 uint64_t odcid_len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004218 uint32_t timestamp;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004219
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004220 if (!quic_dec_int(&odcid_len, &token, end))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004221 return 1;
4222
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004223 /* RFC 9000 7.2. Negotiating Connection IDs:
4224 * When an Initial packet is sent by a client that has not previously
4225 * received an Initial or Retry packet from the server, the client
4226 * populates the Destination Connection ID field with an unpredictable
4227 * value. This Destination Connection ID MUST be at least 8 bytes in length.
4228 */
4229 if (odcid_len < QUIC_ODCID_MINLEN || odcid_len > QUIC_CID_MAXLEN)
4230 return 1;
4231
4232 if (end - token < odcid_len + sizeof timestamp)
4233 return 1;
4234
4235 timestamp = ntohl(read_u32(token + odcid_len));
4236 if (timestamp + MS_TO_TICKS(QUIC_RETRY_DURATION_MS) <= now_ms)
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004237 return 1;
4238
4239 memcpy(odcid->data, token, odcid_len);
4240 odcid->len = odcid_len;
4241
4242 return 0;
4243}
4244
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004245/* Allocate a new QUIC connection with <version> as QUIC version. <ipv4>
4246 * boolean is set to 1 for IPv4 connection, 0 for IPv6. <server> is set to 1
4247 * for QUIC servers (or haproxy listeners).
4248 * <dcid> is the destination connection ID, <scid> is the source connection ID,
4249 * <token> the token found to be used for this connection with <token_len> as
4250 * length. <saddr> is the source address.
4251 * Returns the connection if succeeded, NULL if not.
4252 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004253static struct quic_conn *qc_new_conn(const struct quic_version *qv, int ipv4,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004254 struct quic_cid *dcid, struct quic_cid *scid,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004255 const struct quic_cid *odcid,
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004256 struct sockaddr_storage *saddr,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004257 int server, int token, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004258{
4259 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004260 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004261 /* Initial CID. */
4262 struct quic_connection_id *icid;
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004263 char *buf_area = NULL;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004264 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004265
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004266 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004267 qc = pool_zalloc(pool_head_quic_conn);
4268 if (!qc) {
4269 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
4270 goto err;
4271 }
4272
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004273 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
4274 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004275 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004276 goto err;
4277 }
4278
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004279 qc->cids = EB_ROOT;
4280 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004281 if (server) {
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004282 struct proxy *prx;
4283
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004284 l = owner;
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004285 prx = l->bind_conf->frontend;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004286
Frédéric Lécaillea89659a2022-05-19 11:58:53 +02004287 qc->prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe,
4288 &quic_stats_module);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01004289 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004290 qc->state = QUIC_HS_ST_SERVER_INITIAL;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004291 /* Copy the initial DCID with the address. */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004292 qc->odcid.len = dcid->len;
4293 qc->odcid.addrlen = dcid->addrlen;
4294 memcpy(qc->odcid.data, dcid->data, dcid->len + dcid->addrlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004295
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01004296 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004297 if (scid->len)
4298 memcpy(qc->dcid.data, scid->data, scid->len);
4299 qc->dcid.len = scid->len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02004300 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02004301 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004302 }
4303 /* QUIC Client (outgoing connection to servers) */
4304 else {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02004305 qc->state = QUIC_HS_ST_CLIENT_INITIAL;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004306 if (dcid->len)
4307 memcpy(qc->dcid.data, dcid->data, dcid->len);
4308 qc->dcid.len = dcid->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004309 }
Amaury Denoyelle0b1f9312022-01-26 09:51:28 +01004310 qc->mux_state = QC_MUX_NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004311
Amaury Denoyelle0442efd2022-01-28 16:02:13 +01004312 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004313 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004314 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004315 goto err;
4316 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317
Frédéric Lécaille74904a42022-01-27 15:35:56 +01004318 /* insert the allocated CID in the receiver datagram handler tree */
4319 if (server)
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01004320 ebmb_insert(&quic_dghdlrs[tid].cids, &icid->node, icid->cid.len);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01004321
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322 /* Select our SCID which is the first CID with 0 as sequence number. */
4323 qc->scid = icid->cid;
4324
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004325 /* Packet number spaces initialization. */
4326 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
4327 quic_pktns_init(&qc->pktns[i]);
4328 /* QUIC encryption level context initialization. */
4329 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004330 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004331 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004332 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004333 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004334 /* Initialize the packet number space. */
4335 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
4336 }
4337
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004338 qc->original_version = qv;
4339 qc->tps_tls_ext = (qc->original_version->num & 0xff000000) == 0xff000000 ?
Frédéric Lécaillea956d152021-11-10 09:24:22 +01004340 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
4341 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004342 /* TX part. */
4343 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004344 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
4345 qc->tx.wbuf = qc->tx.rbuf = 0;
4346 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004347 /* RX part. */
4348 qc->rx.bytes = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004349 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
Frédéric Lécaille664741e2022-05-02 18:46:58 +02004350 for (i = 0; i < QCS_MAX_TYPES; i++)
4351 qc->rx.strms[i].nb_streams = 0;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02004352
4353 qc->nb_pkt_for_cc = 1;
4354 qc->nb_pkt_since_cc = 0;
4355
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004356 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004357 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004358 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01004359 goto err;
4360 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004361
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004362 /* XXX TO DO: Only one path at this time. */
4363 qc->path = &qc->paths[0];
4364 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
4365
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01004366 /* required to use MTLIST_IN_LIST */
4367 MT_LIST_INIT(&qc->accept_list);
4368
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004369 qc->streams_by_id = EB_ROOT_UNIQUE;
Amaury Denoyelled2f80a22022-04-15 17:30:49 +02004370 qc->stream_buf_count = 0;
Frédéric Lécaille8726d632022-05-03 10:32:21 +02004371 qc->sendto_err = 0;
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004372 memcpy(&qc->peer_addr, saddr, sizeof qc->peer_addr);
4373
Frédéric Lécaille748ece62022-05-21 23:58:40 +02004374 if (server && !qc_lstnr_params_init(qc, &l->bind_conf->quic_params,
4375 icid->stateless_reset_token,
4376 dcid->data, dcid->len,
4377 qc->scid.data, qc->scid.len,
4378 odcid->data, odcid->len, token))
Frédéric Lécaille395a64d2022-05-09 15:42:26 +02004379 goto err;
Amaury Denoyelle5c3859c2022-03-29 14:49:35 +02004380
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004381 if (qc_conn_alloc_ssl_ctx(qc) ||
4382 !quic_conn_init_timer(qc) ||
4383 !quic_conn_init_idle_timer_task(qc))
4384 goto err;
4385
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004386 if (!qc_new_isecs(qc, &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].tls_ctx,
4387 qc->original_version, dcid->data, dcid->len, 1))
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02004388 goto err;
4389
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004390 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004391
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004392 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004393
4394 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01004395 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Frédéric Lécaillec0b481f2022-02-02 15:39:55 +01004396 pool_free(pool_head_quic_conn_rxbuf, buf_area);
4397 if (qc)
4398 qc->rx.buf.area = NULL;
Frédéric Lécaillef293b692022-03-08 16:59:54 +01004399 quic_conn_release(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004400 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004401}
4402
4403/* Initialize the timer task of <qc> QUIC connection.
4404 * Returns 1 if succeeded, 0 if not.
4405 */
4406static int quic_conn_init_timer(struct quic_conn *qc)
4407{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01004408 /* Attach this task to the same thread ID used for the connection */
Willy Tarreau87168752022-06-13 16:31:53 +02004409 qc->timer_task = task_new_on(qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410 if (!qc->timer_task)
4411 return 0;
4412
4413 qc->timer = TICK_ETERNITY;
4414 qc->timer_task->process = process_timer;
Frédéric Lécaille7fbb94d2022-01-31 10:37:07 +01004415 qc->timer_task->context = qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004416
4417 return 1;
4418}
4419
Frédéric Lécaille47756802022-03-25 09:12:16 +01004420/* Rearm the idle timer for <qc> QUIC connection. */
4421static void qc_idle_timer_do_rearm(struct quic_conn *qc)
4422{
4423 unsigned int expire;
4424
4425 expire = QUIC_MAX(3 * quic_pto(qc), qc->max_idle_timeout);
4426 qc->idle_timer_task->expire = tick_add(now_ms, MS_TO_TICKS(expire));
4427}
4428
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004429/* Rearm the idle timer for <qc> QUIC connection depending on <read> boolean
4430 * which is set to 1 when receiving a packet , and 0 when sending packet
4431 */
4432static void qc_idle_timer_rearm(struct quic_conn *qc, int read)
4433{
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004434 if (read) {
4435 qc->flags |= QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4436 }
4437 else {
4438 qc->flags &= ~QUIC_FL_CONN_IDLE_TIMER_RESTARTED_AFTER_READ;
4439 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01004440 qc_idle_timer_do_rearm(qc);
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004441}
4442
4443/* The task handling the idle timeout */
4444static struct task *qc_idle_timer_task(struct task *t, void *ctx, unsigned int state)
4445{
4446 struct quic_conn *qc = ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004447 struct quic_counters *prx_counters = qc->prx_counters;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004448 unsigned int qc_flags = qc->flags;
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004449
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02004450 /* Notify the MUX before settings QUIC_FL_CONN_EXP_TIMER or the MUX
4451 * might free the quic-conn too early via quic_close().
4452 */
4453 qc_notify_close(qc);
4454
Amaury Denoyelledb71e3b2022-04-06 17:22:12 +02004455 /* If the MUX is still alive, keep the quic-conn. The MUX is
4456 * responsible to call quic_close to release it.
4457 */
4458 qc->flags |= QUIC_FL_CONN_EXP_TIMER;
4459 if (qc->mux_state != QC_MUX_READY)
4460 quic_conn_release(qc);
4461
4462 /* TODO if the quic-conn cannot be freed because of the MUX, we may at
4463 * least clean some parts of it such as the tasklet.
4464 */
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02004465
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004466 if (!(qc_flags & QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED)) {
4467 qc_flags |= QUIC_FL_CONN_HALF_OPEN_CNT_DECREMENTED;
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004468 HA_ATOMIC_DEC(&prx_counters->half_open_conn);
Frédéric Lécaille2aebaa42022-06-17 15:11:32 +02004469 }
Frédéric Lécaille530601c2022-03-10 15:11:57 +01004470
4471 return NULL;
4472}
4473
4474/* Initialize the idle timeout task for <qc>.
4475 * Returns 1 if succeeded, 0 if not.
4476 */
4477static int quic_conn_init_idle_timer_task(struct quic_conn *qc)
4478{
4479 qc->idle_timer_task = task_new_here();
4480 if (!qc->idle_timer_task)
4481 return 0;
4482
4483 qc->idle_timer_task->process = qc_idle_timer_task;
4484 qc->idle_timer_task->context = qc;
4485 qc_idle_timer_rearm(qc, 1);
4486 task_queue(qc->idle_timer_task);
4487
4488 return 1;
4489}
4490
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
4492 * past one byte of this buffer.
4493 */
4494static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
4495 struct quic_rx_packet *pkt)
4496{
4497 unsigned char dcid_len, scid_len;
4498
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004499 if (end == *buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004500 return 0;
4501
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502 /* Destination Connection ID Length */
4503 dcid_len = *(*buf)++;
4504 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
4505 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
4506 /* XXX MUST BE DROPPED */
4507 return 0;
4508
4509 if (dcid_len) {
4510 /* Check that the length of this received DCID matches the CID lengths
4511 * of our implementation for non Initials packets only.
4512 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004513 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
4514 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01004515 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004516 return 0;
4517
4518 memcpy(pkt->dcid.data, *buf, dcid_len);
4519 }
4520
4521 pkt->dcid.len = dcid_len;
4522 *buf += dcid_len;
4523
4524 /* Source Connection ID Length */
4525 scid_len = *(*buf)++;
4526 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
4527 /* XXX MUST BE DROPPED */
4528 return 0;
4529
4530 if (scid_len)
4531 memcpy(pkt->scid.data, *buf, scid_len);
4532 pkt->scid.len = scid_len;
4533 *buf += scid_len;
4534
4535 return 1;
4536}
4537
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004538/* Insert <pkt> RX packet in its <qel> RX packets tree */
4539static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
4540{
4541 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01004542 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004543 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
4544 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
4545 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004546}
4547
4548/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004549 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
4550 * byte past the end of the buffer containing this packet and <beg> the address of
4551 * the packet first byte.
4552 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
4553 * Returns 1 if succeeded, 0 if not.
4554 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004555static inline int qc_try_rm_hp(struct quic_conn *qc,
4556 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004557 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004558 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004559 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004560{
4561 unsigned char *pn = NULL; /* Packet number field */
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004562 enum quic_tls_enc_level tel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004563 struct quic_enc_level *qel;
4564 /* Only for traces. */
4565 struct quic_rx_packet *qpkt_trace;
4566
4567 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004568 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004569 /* The packet number is here. This is also the start minus
4570 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
4571 * protection.
4572 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004573 pn = buf;
Amaury Denoyelle8ae28072022-01-24 18:34:52 +01004574
4575 tel = quic_packet_type_enc_level(pkt->type);
4576 qel = &qc->els[tel];
4577
4578 if (qc_qel_may_rm_hp(qc, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004579 /* Note that the following function enables us to unprotect the packet
4580 * number and its length subsequently used to decrypt the entire
4581 * packets.
4582 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004583 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
4584 qel->pktns->rx.largest_pn, pn, beg, end)) {
4585 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004586 goto err;
4587 }
4588
4589 /* The AAD includes the packet number field found at <pn>. */
4590 pkt->aad_len = pn - beg + pkt->pnl;
4591 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004592 }
Frédéric Lécaille7d845f12022-02-21 19:22:09 +01004593 else {
Frédéric Lécaillebd242082022-02-25 17:17:59 +01004594 if (qel->tls_ctx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004595 /* If the packet number space has been discarded, this packet
4596 * will be not parsed.
4597 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004598 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004599 goto out;
4600 }
4601
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004602 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004603 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004604 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
4605 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004606 }
4607
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004608 *el = qel;
4609 /* No reference counter incrementation here!!! */
4610 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
4611 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
4612 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
4613 b_add(&qc->rx.buf, pkt->len);
4614 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004615 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004616 return 1;
4617
4618 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004619 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004620 return 0;
4621}
4622
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004623/* 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 +02004624 * Also set <*long_header> to 1 if this form is long, 0 if not and the version
4625 * of this packet into <*version>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004626 */
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004627static inline int qc_parse_hd_form(struct quic_rx_packet *pkt,
4628 unsigned char **buf, const unsigned char *end,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004629 int *long_header, uint32_t *version)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004630{
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004631 const unsigned char byte0 = **buf;
4632
4633 (*buf)++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004634 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004635 unsigned char type =
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004636 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004637
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004638 *long_header = 1;
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004639 /* Version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004640 if (!quic_read_uint32(version, (const unsigned char **)buf, end))
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004641 return 0;
4642
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004643 if (*version != QUIC_PROTOCOL_VERSION_2_DRAFT) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004644 pkt->type = type;
4645 }
4646 else {
4647 switch (type) {
4648 case 0:
4649 pkt->type = QUIC_PACKET_TYPE_RETRY;
4650 break;
4651 case 1:
4652 pkt->type = QUIC_PACKET_TYPE_INITIAL;
4653 break;
4654 case 2:
4655 pkt->type = QUIC_PACKET_TYPE_0RTT;
4656 break;
4657 case 3:
4658 pkt->type = QUIC_PACKET_TYPE_HANDSHAKE;
4659 break;
4660 }
4661 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004662 }
4663 else {
4664 pkt->type = QUIC_PACKET_TYPE_SHORT;
4665 *long_header = 0;
4666 }
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02004667
4668 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004669}
4670
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004671/* Return the QUIC version (quic_version struct) with <version> as version number
4672 * if supported or NULL if not.
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004673 */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004674static inline const struct quic_version *qc_supported_version(uint32_t version)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004675{
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004676 int i;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004677
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004678 for (i = 0; i < quic_versions_nb; i++)
4679 if (quic_versions[i].num == version)
4680 return &quic_versions[i];
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004681
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004682 return NULL;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004683}
4684
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004685/*
4686 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4687 * address <addr>.
4688 * Implementation of RFC9000 6. Version Negotiation
4689 *
4690 * TODO implement a rate-limiting sending of Version Negotiation packets
4691 *
4692 * Returns 0 on success else non-zero
4693 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004694static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4695 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004696{
4697 char buf[256];
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004698 int i = 0, j;
4699 uint32_t version;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004700 const socklen_t addrlen = get_addr_len(addr);
4701
4702 /*
4703 * header form
4704 * long header, fixed bit to 0 for Version Negotiation
4705 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004706 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4707 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004708
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004709 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004710 /* null version for Version Negotiation */
4711 buf[i++] = '\x00';
4712 buf[i++] = '\x00';
4713 buf[i++] = '\x00';
4714 buf[i++] = '\x00';
4715
4716 /* source connection id */
4717 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004718 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004719 i += pkt->scid.len;
4720
4721 /* destination connection id */
4722 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004723 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004724 i += pkt->dcid.len;
4725
4726 /* supported version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004727 for (j = 0; j < quic_versions_nb; j++) {
4728 version = htonl(quic_versions[j].num);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004729 memcpy(&buf[i], &version, sizeof(version));
4730 i += sizeof(version);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004731 }
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004732
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004733
4734 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4735 return 1;
4736
4737 return 0;
4738}
4739
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004740/* Send a stateless reset packet depending on <pkt> RX packet information
4741 * from <fd> UDP socket to <dst>
4742 * Return 1 if succeeded, 0 if not.
4743 */
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004744static int send_stateless_reset(struct listener *l, struct sockaddr_storage *dstaddr,
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004745 struct quic_rx_packet *rxpkt)
4746{
4747 int pktlen, rndlen;
4748 unsigned char pkt[64];
4749 const socklen_t addrlen = get_addr_len(dstaddr);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004750 struct proxy *prx;
4751 struct quic_counters *prx_counters;
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004752
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004753 prx = l->bind_conf->frontend;
4754 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004755 /* 10.3 Stateless Reset (https://www.rfc-editor.org/rfc/rfc9000.html#section-10.3)
4756 * The resulting minimum size of 21 bytes does not guarantee that a Stateless
4757 * Reset is difficult to distinguish from other packets if the recipient requires
4758 * the use of a connection ID. To achieve that end, the endpoint SHOULD ensure
4759 * that all packets it sends are at least 22 bytes longer than the minimum
4760 * connection ID length that it requests the peer to include in its packets,
4761 * adding PADDING frames as necessary. This ensures that any Stateless Reset
4762 * sent by the peer is indistinguishable from a valid packet sent to the endpoint.
4763 * An endpoint that sends a Stateless Reset in response to a packet that is
4764 * 43 bytes or shorter SHOULD send a Stateless Reset that is one byte shorter
4765 * than the packet it responds to.
4766 */
4767
4768 /* Note that we build at most a 42 bytes QUIC packet to mimic a short packet */
4769 pktlen = rxpkt->len <= 43 ? rxpkt->len - 1 : 0;
4770 pktlen = QUIC_MAX(QUIC_STATELESS_RESET_PACKET_MINLEN, pktlen);
4771 rndlen = pktlen - QUIC_STATELESS_RESET_TOKEN_LEN;
4772 /* Put a header of random bytes */
4773 if (RAND_bytes(pkt, rndlen) != 1)
4774 return 0;
4775
4776 /* Clear the most significant bit, and set the second one */
4777 *pkt = (*pkt & ~0x80) | 0x40;
4778 if (!quic_stateless_reset_token_cpy(pkt + rndlen, QUIC_STATELESS_RESET_TOKEN_LEN,
4779 rxpkt->dcid.data, rxpkt->dcid.len))
4780 return 0;
4781
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004782 if (sendto(l->rx.fd, pkt, pktlen, 0, (struct sockaddr *)dstaddr, addrlen) < 0)
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004783 return 0;
4784
Frédéric Lécailleeb791452022-05-24 16:01:39 +02004785 HA_ATOMIC_INC(&prx_counters->stateless_reset_sent);
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02004786 TRACE_PROTO("stateless reset sent", QUIC_EV_STATELESS_RST, NULL, &rxpkt->dcid);
4787 return 1;
4788}
4789
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004790/* QUIC server only function.
4791 * Add AAD to <add> buffer from <cid> connection ID and <addr> socket address.
4792 * This is the responsability of the caller to check <aad> size is big enough
4793 * to contain these data.
4794 * Return the number of bytes copied to <aad>.
4795 */
4796static int quic_generate_retry_token_aad(unsigned char *aad,
4797 uint32_t version,
4798 const struct quic_cid *cid,
4799 const struct sockaddr_storage *addr)
4800{
4801 unsigned char *p;
4802
4803 p = aad;
4804 memcpy(p, &version, sizeof version);
4805 p += sizeof version;
4806 p += quic_saddr_cpy(p, addr);
4807 memcpy(p, cid->data, cid->len);
4808 p += cid->len;
4809
4810 return p - aad;
4811}
4812
4813/* QUIC server only function.
4814 * Generate the token to be used in Retry packets. The token is written to
4815 * <buf> whith <len> as length. <odcid> is the original destination connection
4816 * ID and <dcid> is our side destination connection ID (or client source
4817 * connection ID).
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004818 * Returns the length of the encoded token or 0 on error.
4819 */
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004820static int quic_generate_retry_token(unsigned char *buf, size_t len,
4821 const uint32_t version,
4822 const struct quic_cid *odcid,
4823 const struct quic_cid *dcid,
4824 struct sockaddr_storage *addr)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004825{
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004826 unsigned char *p;
4827 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4828 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4829 size_t aadlen;
4830 unsigned char salt[QUIC_RETRY_TOKEN_SALTLEN];
4831 unsigned char key[QUIC_TLS_KEY_LEN];
4832 unsigned char iv[QUIC_TLS_IV_LEN];
4833 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4834 size_t seclen = strlen(global.cluster_secret);
4835 EVP_CIPHER_CTX *ctx = NULL;
4836 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4837 uint32_t timestamp = now_ms;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004838
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004839 /* We copy the odcid into the token, prefixed by its one byte
4840 * length, the format token byte. It is followed by an AEAD TAG, and finally
4841 * the random bytes used to derive the secret to encrypt the token.
4842 */
4843 if (1 + dcid->len + 1 + QUIC_TLS_TAG_LEN + sizeof salt > len)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004844 return 0;
4845
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004846 aadlen = quic_generate_retry_token_aad(aad, version, dcid, addr);
4847 if (RAND_bytes(salt, sizeof salt) != 1)
4848 goto err;
4849
4850 if (!quic_tls_derive_retry_token_secret(EVP_sha256(), key, sizeof key, iv, sizeof iv,
4851 salt, sizeof salt, sec, seclen))
4852 goto err;
4853
4854 if (!quic_tls_tx_ctx_init(&ctx, aead, key))
4855 goto err;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004856
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004857 /* Token build */
4858 p = buf;
4859 *p++ = QUIC_TOKEN_FMT_RETRY,
4860 *p++ = odcid->len;
4861 memcpy(p, odcid->data, odcid->len);
4862 p += odcid->len;
4863 write_u32(p, htonl(timestamp));
4864 p += sizeof timestamp;
4865
4866 /* Do not encrypt the QUIC_TOKEN_FMT_RETRY byte */
4867 if (!quic_tls_encrypt(buf + 1, p - buf - 1, aad, aadlen, ctx, aead, key, iv))
4868 goto err;
4869
4870 p += QUIC_TLS_TAG_LEN;
4871 memcpy(p, salt, sizeof salt);
4872 p += sizeof salt;
4873 EVP_CIPHER_CTX_free(ctx);
4874
4875 return p - buf;
4876
4877 err:
4878 if (ctx)
4879 EVP_CIPHER_CTX_free(ctx);
4880 return 0;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004881}
4882
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004883/* QUIC server only function.
4884 * Check the validity of the Retry token from <token> buffer with <tokenlen>
4885 * as length. If valid, the ODCID of <qc> QUIC connection will be put
4886 * into <odcid> connection ID. <dcid> is our side destination connection ID
4887 * of client source connection ID.
4888 * Return 1 if succeeded, 0 if not.
4889 */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004890static int quic_retry_token_check(const unsigned char *token, size_t tokenlen,
4891 const struct quic_version *qv,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004892 struct quic_cid *odcid,
4893 const struct quic_cid *dcid,
4894 struct quic_conn *qc,
4895 struct sockaddr_storage *addr)
4896{
4897 unsigned char buf[128];
4898 unsigned char aad[sizeof(uint32_t) + sizeof(in_port_t) +
4899 sizeof(struct in6_addr) + QUIC_HAP_CID_LEN];
4900 size_t aadlen;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004901 const unsigned char *salt;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004902 unsigned char key[QUIC_TLS_KEY_LEN];
4903 unsigned char iv[QUIC_TLS_IV_LEN];
4904 const unsigned char *sec = (const unsigned char *)global.cluster_secret;
4905 size_t seclen = strlen(global.cluster_secret);
4906 EVP_CIPHER_CTX *ctx = NULL;
4907 const EVP_CIPHER *aead = EVP_aes_128_gcm();
4908
4909 if (sizeof buf < tokenlen)
4910 return 0;
4911
Frédéric Lécaille301425b2022-06-14 17:40:39 +02004912 aadlen = quic_generate_retry_token_aad(aad, qv->num, dcid, addr);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004913 salt = token + tokenlen - QUIC_RETRY_TOKEN_SALTLEN;
4914 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 +02004915 salt, QUIC_RETRY_TOKEN_SALTLEN, sec, seclen)) {
4916 TRACE_PROTO("Could not derive retry secret", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004917 return 0;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02004918 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004919
4920 if (!quic_tls_rx_ctx_init(&ctx, aead, key))
4921 goto err;
4922
4923 /* Do not decrypt the QUIC_TOKEN_FMT_RETRY byte */
4924 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 +02004925 ctx, aead, key, iv)) {
4926 TRACE_PROTO("Could not decrypt retry token", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004927 goto err;
Frédéric Lécaille01d515e2022-06-07 11:39:00 +02004928 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004929
4930 if (parse_retry_token(buf, buf + tokenlen - QUIC_RETRY_TOKEN_SALTLEN - 1, odcid)) {
4931 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4932 goto err;
4933 }
4934
4935 EVP_CIPHER_CTX_free(ctx);
4936 return 1;
4937
4938 err:
4939 if (ctx)
4940 EVP_CIPHER_CTX_free(ctx);
4941 return 0;
4942}
4943
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004944/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4945 * the Initial <pkt> packet.
4946 *
4947 * Returns 0 on success else non-zero.
4948 */
4949static int send_retry(int fd, struct sockaddr_storage *addr,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004950 struct quic_rx_packet *pkt, const struct quic_version *qv)
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004951{
4952 unsigned char buf[128];
4953 int i = 0, token_len;
4954 const socklen_t addrlen = get_addr_len(addr);
4955 struct quic_cid scid;
4956
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004957 /* long header + fixed bit + packet type QUIC_PACKET_TYPE_RETRY */
4958 buf[i++] = (QUIC_PACKET_LONG_HEADER_BIT | QUIC_PACKET_FIXED_BIT) |
4959 (quic_pkt_type(QUIC_PACKET_TYPE_RETRY, qv->num) << QUIC_PACKET_TYPE_SHIFT);
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004960 /* version */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004961 buf[i++] = *((unsigned char *)&qv->num + 3);
4962 buf[i++] = *((unsigned char *)&qv->num + 2);
4963 buf[i++] = *((unsigned char *)&qv->num + 1);
4964 buf[i++] = *(unsigned char *)&qv->num;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004965
4966 /* Use the SCID from <pkt> for Retry DCID. */
4967 buf[i++] = pkt->scid.len;
4968 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4969 i += pkt->scid.len;
4970
4971 /* Generate a new CID to be used as SCID for the Retry packet. */
4972 scid.len = QUIC_HAP_CID_LEN;
4973 if (RAND_bytes(scid.data, scid.len) != 1)
4974 return 1;
4975
4976 buf[i++] = scid.len;
4977 memcpy(&buf[i], scid.data, scid.len);
4978 i += scid.len;
4979
4980 /* token */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004981 if (!(token_len = quic_generate_retry_token(&buf[i], sizeof(buf) - i, qv->num,
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02004982 &pkt->dcid, &pkt->scid, addr)))
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004983 return 1;
Frédéric Lécaillecc2764e2022-03-23 14:09:09 +01004984
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004985 i += token_len;
4986
4987 /* token integrity tag */
4988 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4989 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
Frédéric Lécaille86845c52022-06-08 19:28:36 +02004990 pkt->dcid.len, buf, i, qv)) {
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004991 return 1;
4992 }
4993
4994 i += QUIC_TLS_TAG_LEN;
4995
4996 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4997 return 1;
4998
4999 return 0;
5000}
5001
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005002/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
5003 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
5004 * concatenated to the <pkt> DCID field.
5005 *
5006 * Returns the instance or NULL if not found.
5007 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01005008static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005009 struct listener *l,
5010 struct sockaddr_storage *saddr)
5011{
5012 struct quic_conn *qc = NULL;
5013 struct ebmb_node *node;
5014 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005015 /* set if the quic_conn is found in the second DCID tree */
5016 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005017
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005018 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
5019 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
5020 pkt->type == QUIC_PACKET_TYPE_0RTT) {
5021 /* DCIDs of first packets coming from multiple clients may have
5022 * the same values. Let's distinguish them by concatenating the
5023 * socket addresses.
5024 */
5025 quic_cid_saddr_cat(&pkt->dcid, saddr);
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005026 node = ebmb_lookup(&quic_dghdlrs[tid].odcids, pkt->dcid.data,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005027 pkt->dcid.len + pkt->dcid.addrlen);
5028 if (node) {
5029 qc = ebmb_entry(node, struct quic_conn, odcid_node);
5030 goto end;
5031 }
5032 }
5033
5034 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
5035 * also for INITIAL/0-RTT non-first packets with the final DCID in
5036 * used.
5037 */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005038 node = ebmb_lookup(&quic_dghdlrs[tid].cids, pkt->dcid.data, pkt->dcid.len);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005039 if (!node)
5040 goto end;
5041
5042 id = ebmb_entry(node, struct quic_connection_id, node);
5043 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005044 found_in_dcid = 1;
5045
5046 end:
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005047 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
5048 * If already done, this is a noop.
5049 */
Frédéric Lécaille74904a42022-01-27 15:35:56 +01005050 if (qc && found_in_dcid)
Amaury Denoyelle250ac422021-12-22 11:29:05 +01005051 ebmb_delete(&qc->odcid_node);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005052
5053 return qc;
5054}
5055
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005056/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5057 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5058 * parameters of this session.
5059 * This is the responsibility of the caller to check the validity of all the
5060 * pointers passed as parameter to this function.
5061 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5062 * CO_ER_SSL_NO_MEM.
5063 */
5064static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5065 unsigned char *params, size_t params_len)
5066{
5067 int retry;
5068
5069 retry = 1;
5070 retry:
5071 *ssl = SSL_new(ssl_ctx);
5072 if (!*ssl) {
5073 if (!retry--)
5074 goto err;
5075
5076 pool_gc(NULL);
5077 goto retry;
5078 }
5079
5080 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005081 !SSL_set_ex_data(*ssl, ssl_qc_app_data_index, qc)) {
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005082 SSL_free(*ssl);
5083 *ssl = NULL;
5084 if (!retry--)
5085 goto err;
5086
5087 pool_gc(NULL);
5088 goto retry;
5089 }
5090
5091 return 0;
5092
5093 err:
5094 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5095 return -1;
5096}
5097
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005098/* Finalize <qc> QUIC connection:
5099 * - initialize the Initial QUIC TLS context for negotiated version,
5100 * - derive the secrets for this context,
5101 * - encode the transport parameters to be sent,
5102 * - set them into the TLS stack,
5103 * - initialize ->max_ack_delay and max_idle_timeout,
5104 *
5105 * MUST be called after having received the remote transport parameters.
5106 * Return 1 if succeeded, 0 if not.
5107 */
5108int qc_conn_finalize(struct quic_conn *qc, int server)
5109{
5110 struct quic_transport_params *tx_tp = &qc->tx.params;
5111 struct quic_transport_params *rx_tp = &qc->rx.params;
5112 const struct quic_version *ver;
5113
5114 if (tx_tp->version_information.negotiated_version &&
5115 tx_tp->version_information.negotiated_version != qc->original_version) {
5116 qc->negotiated_version =
5117 qc->tx.params.version_information.negotiated_version;
5118 if (!qc_new_isecs(qc, &qc->negotiated_ictx, qc->negotiated_version,
5119 qc->odcid.data, qc->odcid.len, !server))
5120 return 0;
5121
5122 ver = qc->negotiated_version;
5123 }
5124 else {
5125 ver = qc->original_version;
5126 }
5127
5128 qc->enc_params_len =
5129 quic_transport_params_encode(qc->enc_params,
5130 qc->enc_params + sizeof qc->enc_params,
5131 &qc->rx.params, ver, 1);
5132 if (!qc->enc_params_len)
5133 return 0;
5134
5135 if (!SSL_set_quic_transport_params(qc->xprt_ctx->ssl, qc->enc_params, qc->enc_params_len))
5136 return 0;
5137
5138 if (tx_tp->max_ack_delay)
5139 qc->max_ack_delay = tx_tp->max_ack_delay;
5140
5141 if (tx_tp->max_idle_timeout && rx_tp->max_idle_timeout)
5142 qc->max_idle_timeout =
5143 QUIC_MIN(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5144 else
5145 qc->max_idle_timeout =
5146 QUIC_MAX(tx_tp->max_idle_timeout, rx_tp->max_idle_timeout);
5147
5148 TRACE_PROTO("\nTX(remote) transp. params.", QUIC_EV_TRANSP_PARAMS, qc, tx_tp);
5149
5150 return 1;
5151}
5152
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005153/* Allocate the ssl_sock_ctx from connection <qc>. This creates the tasklet
5154 * used to process <qc> received packets. The allocated context is stored in
5155 * <qc.xprt_ctx>.
5156 *
5157 * Returns 0 on success else non-zero.
5158 */
Frédéric Lécaille3fd92f62022-05-19 14:35:20 +02005159static int qc_conn_alloc_ssl_ctx(struct quic_conn *qc)
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005160{
5161 struct bind_conf *bc = qc->li->bind_conf;
5162 struct ssl_sock_ctx *ctx = NULL;
5163
5164 ctx = pool_zalloc(pool_head_quic_conn_ctx);
5165 if (!ctx)
5166 goto err;
5167
5168 ctx->wait_event.tasklet = tasklet_new();
5169 if (!ctx->wait_event.tasklet)
5170 goto err;
5171
5172 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5173 ctx->wait_event.tasklet->context = ctx;
5174 ctx->wait_event.events = 0;
5175 ctx->subs = NULL;
5176 ctx->xprt_ctx = NULL;
5177 ctx->qc = qc;
5178
5179 /* Set tasklet tid based on the SCID selected by us for this
5180 * connection. The upper layer will also be binded on the same thread.
5181 */
Frédéric Lécaille220894a2022-01-26 18:04:50 +01005182 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(qc->scid.data);
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005183
5184 if (qc_is_listener(qc)) {
5185 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5186 qc->enc_params, qc->enc_params_len) == -1) {
5187 goto err;
5188 }
5189
5190 /* Enabling 0-RTT */
5191 if (bc->ssl_conf.early_data)
5192 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5193
5194 SSL_set_accept_state(ctx->ssl);
5195 }
5196
5197 ctx->xprt = xprt_get(XPRT_QUIC);
5198
5199 /* Store the allocated context in <qc>. */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005200 qc->xprt_ctx = ctx;
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01005201
5202 return 0;
5203
5204 err:
5205 if (ctx && ctx->wait_event.tasklet)
5206 tasklet_free(ctx->wait_event.tasklet);
5207 pool_free(pool_head_quic_conn_ctx, ctx);
5208
5209 return 1;
5210}
5211
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005212/* Check that all the bytes between <buf> included and <end> address
5213 * excluded are null. This is the responsability of the caller to
5214 * check that there is at least one byte between <buf> end <end>.
5215 * Return 1 if this all the bytes are null, 0 if not.
5216 */
5217static inline int quic_padding_check(const unsigned char *buf,
5218 const unsigned char *end)
5219{
5220 while (buf < end && !*buf)
5221 buf++;
5222
5223 return buf == end;
5224}
5225
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005226/* Parse a QUIC packet from UDP datagram found in <buf> buffer with <end> the
5227 * end of this buffer past one byte and populate <pkt> RX packet structure
5228 * with the information collected from the packet.
5229 * This function sets ->len <pkt> field value to the end of the packet past one
5230 * byte to enable the caller to run this function again to continue to parse
5231 * the remaing QUIC packets carried by the datagram.
5232 * Note that this function always sets this ->len value. If a paquet could
5233 * not be correctly found, ->len value will be set to the remaining number
5234 * bytes in the datagram to entirely consume this latter.
5235 */
5236static void qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
5237 struct quic_rx_packet *pkt, int first_pkt,
5238 struct quic_dgram *dgram)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005239{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005240 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005241 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005242 struct listener *l;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005243 struct proxy *prx;
5244 struct quic_counters *prx_counters;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005245 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005246 int drop_no_conn = 0, long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005247 size_t b_cspace;
5248 struct quic_enc_level *qel;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005249 uint32_t version;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005250 const struct quic_version *qv = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005251
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005252 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005253 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005254 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01005255 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005256 TRACE_ENTER(QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005257 l = dgram->owner;
5258 prx = l->bind_conf->frontend;
5259 prx_counters = EXTRA_COUNTERS_GET(prx->extra_counters_fe, &quic_stats_module);
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01005260 /* This ist only to please to traces and distinguish the
5261 * packet with parsed packet number from others.
5262 */
5263 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005264 if (end <= buf) {
5265 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5266 goto drop;
5267 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005268
5269 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005270 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillef6954c52022-05-21 14:42:21 +02005271 if (!first_pkt && quic_padding_check(buf, end)) {
5272 /* Some browsers may pad the remaining datagram space with null bytes.
5273 * That is what we called add padding out of QUIC packets. Such
5274 * datagrams must be considered as valid. But we can only consume
5275 * the remaining space.
5276 */
5277 pkt->len = end - buf;
5278 goto drop_no_conn;
5279 }
5280
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005281 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005282 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005283 }
5284
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005285 /* Header form */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005286 if (!qc_parse_hd_form(pkt, &buf, end, &long_header, &version)) {
Frédéric Lécailleea0ec272022-06-08 13:22:17 +02005287 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
5288 goto drop;
5289 }
5290
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005291 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005292 uint64_t len;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005293 struct quic_cid odcid;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005294 int check_token = 0;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005295
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005296 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005297 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005298 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005299 }
5300
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005301 if (pkt->type == QUIC_PACKET_TYPE_0RTT && !l->bind_conf->ssl_conf.early_data) {
5302 TRACE_PROTO("0-RTT packet not supported", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005303 drop_no_conn = 1;
Frédéric Lécaille3e266982022-04-27 15:37:28 +02005304 }
5305 else if (pkt->type == QUIC_PACKET_TYPE_INITIAL &&
5306 dgram->len < QUIC_INITIAL_PACKET_MINLEN) {
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005307 TRACE_PROTO("Too short datagram with an Initial packet", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3ccea6d2022-05-23 22:54:54 +02005308 HA_ATOMIC_INC(&prx_counters->too_short_initial_dgram);
Frédéric Lécaille87373e72022-04-27 11:42:08 +02005309 }
5310
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005311 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5312 * they must have the same DCID.
5313 */
5314 if (!first_pkt &&
5315 (pkt->dcid.len != dgram->dcid_len ||
5316 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5317 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005318 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005319 }
5320
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005321 /* Retry of Version Negotiation packets are only sent by servers */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005322 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !version) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005323 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005324 goto drop;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005325 }
5326
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005327 /* RFC9000 6. Version Negotiation */
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005328 qv = qc_supported_version(version);
5329 if (!qv) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005330 /* unsupported version, send Negotiation packet */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005331 if (send_version_negotiation(l->rx.fd, &dgram->saddr, pkt)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005332 TRACE_PROTO("VN packet not sent", QUIC_EV_CONN_LPKT);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005333 goto err;
5334 }
5335
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005336 TRACE_PROTO("VN packet sent", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005337 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01005338 }
5339
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005340 /* For Initial packets, and for servers (QUIC clients connections),
5341 * there is no Initial connection IDs storage.
5342 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005343 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005344 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005345
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005346 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
5347 end - buf < token_len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005348 TRACE_PROTO("Packet dropped",
5349 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005350 goto drop;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01005351 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005352
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01005353 /* TODO Retry should be automatically activated if
5354 * suspect network usage is detected.
5355 */
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005356 if (global.cluster_secret) {
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005357 if (!token_len) {
Willy Tarreau787e92a2022-05-20 16:06:01 +02005358 if (l->bind_conf->options & BC_O_QUIC_FORCE_RETRY) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005359 TRACE_PROTO("Initial without token, sending retry",
5360 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005361 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005362 TRACE_PROTO("Error during Retry generation",
5363 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005364 goto err;
5365 }
5366
5367 HA_ATOMIC_INC(&prx_counters->retry_sent);
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005368 goto err;
5369 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005370 }
5371 else {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005372 check_token = 1;
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005373 }
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01005374 }
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005375
5376 pkt->token = buf;
5377 pkt->token_len = token_len;
5378 buf += pkt->token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005379 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005380 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01005381 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005382 TRACE_PROTO("Packet dropped",
5383 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005384 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005385 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005386 }
5387
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005388 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
5389 end - buf < len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005390 TRACE_PROTO("Packet dropped",
5391 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005392 goto drop;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005393 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005394
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005395 payload = buf;
5396 pkt->len = len + payload - beg;
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005397 if (drop_no_conn)
5398 goto drop_no_conn;
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01005399
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005400 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005401 if (check_token && pkt->token) {
5402 if (*pkt->token == QUIC_TOKEN_FMT_RETRY) {
5403 const struct quic_version *ver = qc ? qc->original_version : qv;
5404 if (!quic_retry_token_check(pkt->token, pkt->token_len, ver, &odcid,
5405 &pkt->scid, qc, &dgram->saddr)) {
5406 HA_ATOMIC_INC(&prx_counters->retry_error);
5407 TRACE_PROTO("Wrong retry token",
5408 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5409 /* TODO: RFC 9000 8.1.2 A server SHOULD immediately close the connection
5410 * with an INVALID_TOKEN error.
5411 */
5412 goto drop;
5413 }
5414
5415 HA_ATOMIC_INC(&prx_counters->retry_validated);
5416 }
5417 else {
5418 /* TODO: New token check */
5419 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
5420 goto drop;
5421 }
5422 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01005423 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005424 int ipv4;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005425 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005426
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005427 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005428 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005429 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005430 }
5431
Willy Tarreau787e92a2022-05-20 16:06:01 +02005432 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 +02005433 HA_ATOMIC_LOAD(&prx_counters->half_open_conn) >= global.tune.quic_retry_threshold) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005434 TRACE_PROTO("Initial without token, sending retry",
5435 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005436 if (send_retry(l->rx.fd, &dgram->saddr, pkt, qv)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005437 TRACE_PROTO("Error during Retry generation",
5438 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécailledfd13012022-05-20 16:37:36 +02005439 goto err;
5440 }
5441
5442 HA_ATOMIC_INC(&prx_counters->retry_sent);
5443 goto err;
5444 }
5445
Frédéric Lécaille3f3ff472022-05-12 14:47:59 +02005446 /* RFC 9000 7.2. Negotiating Connection IDs:
5447 * When an Initial packet is sent by a client that has not previously
5448 * received an Initial or Retry packet from the server, the client
5449 * populates the Destination Connection ID field with an unpredictable
5450 * value. This Destination Connection ID MUST be at least 8 bytes in length.
5451 */
Frédéric Lécailledc364042022-01-27 16:51:54 +01005452 if (pkt->dcid.len < QUIC_ODCID_MINLEN) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005453 TRACE_PROTO("dropped packet",
5454 QUIC_EV_CONN_LPKT, NULL, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005455 goto drop;
Frédéric Lécailledc364042022-01-27 16:51:54 +01005456 }
5457
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005458 pkt->saddr = dgram->saddr;
5459 ipv4 = dgram->saddr.ss_family == AF_INET;
Frédéric Lécaille86845c52022-06-08 19:28:36 +02005460 qc = qc_new_conn(qv, ipv4, &pkt->dcid, &pkt->scid, &odcid,
Frédéric Lécaille748ece62022-05-21 23:58:40 +02005461 &pkt->saddr, 1, !!pkt->token_len, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005462 if (qc == NULL)
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005463 goto drop;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005464
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005465 HA_ATOMIC_INC(&prx_counters->half_open_conn);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02005466 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyelle8524f0f2022-02-08 15:03:40 +01005467 n = ebmb_insert(&quic_dghdlrs[tid].odcids, &qc->odcid_node,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005468 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005469
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005470 /* If the insertion failed, it means that another
5471 * thread has already allocated a QUIC connection for
5472 * the same CID. Liberate our allocated connection.
5473 */
5474 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005475 qc_to_purge = qc;
5476
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01005477 qc = ebmb_entry(n, struct quic_conn, odcid_node);
5478 pkt->qc = qc;
5479 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005480
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005481 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005482 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01005483 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005484 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005485 else {
Frédéric Lécaillef293b692022-03-08 16:59:54 +01005486 quic_conn_release(qc_to_purge);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01005487 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005488 }
5489 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005490 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005491 }
5492 }
5493 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005494 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005495 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005496 goto drop;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005497 }
5498
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005499 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005500 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005501
5502 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5503 * they must have the same DCID.
5504 */
5505 if (!first_pkt &&
5506 (pkt->dcid.len != dgram->dcid_len ||
5507 memcmp(dgram->dcid, pkt->dcid.data, pkt->dcid.len))) {
5508 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005509 goto drop;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005510 }
5511
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005512 buf += QUIC_HAP_CID_LEN;
5513
5514 /* A short packet is the last one of a UDP datagram. */
5515 payload = buf;
5516 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005517
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005518 qc = retrieve_qc_conn_from_cid(pkt, l, &dgram->saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005519 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005520 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005521 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
Frédéric Lécailleeb791452022-05-24 16:01:39 +02005522 if (global.cluster_secret && !send_stateless_reset(l, &dgram->saddr, pkt))
Frédéric Lécaillee2fb1bf2022-05-09 16:30:55 +02005523 TRACE_PROTO("stateless reset not sent", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005524 goto drop;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01005525 }
5526
Frédéric Lécaille8370c932021-11-08 17:01:46 +01005527 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005528 }
5529
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005530 if (qc->flags & QUIC_FL_CONN_CLOSING) {
5531 if (++qc->nb_pkt_since_cc >= qc->nb_pkt_for_cc) {
5532 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
5533 qc->nb_pkt_for_cc++;
5534 qc->nb_pkt_since_cc = 0;
5535 }
5536 /* Skip the entire datagram */
5537 pkt->len = end - beg;
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005538 TRACE_PROTO("Closing state connection",
5539 QUIC_EV_CONN_LPKT, pkt->qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005540 goto drop;
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02005541 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005542
5543 /* When multiple QUIC packets are coalesced on the same UDP datagram,
5544 * they must have the same DCID.
5545 *
5546 * This check must be done after the final update to pkt.len to
5547 * properly drop the packet on failure.
5548 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005549 if (first_pkt && !quic_peer_validated_addr(qc) &&
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005550 qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005551 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005552 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005553 /* Reset the anti-amplification bit. It will be set again
5554 * when sending the next packet if reached again.
5555 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005556 qc->flags &= ~QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
5557 qc->flags |= QUIC_FL_CONN_IO_CB_WAKEUP;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005558 io_cb_wakeup = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005559 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005560
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01005561 dgram->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005562
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005563 if (qc->err_code) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005564 TRACE_PROTO("Connection error",
5565 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005566 goto out;
5567 }
5568
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005569 pkt->raw_len = pkt->len;
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005570 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005571 b_cspace = b_contig_space(&qc->rx.buf);
5572 if (b_cspace < pkt->len) {
Amaury Denoyelle80d05722022-05-16 18:13:56 +02005573 /* Do not consume buf if space not at the end. */
5574 if (b_tail(&qc->rx.buf) + b_cspace < b_wrap(&qc->rx.buf)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005575 TRACE_PROTO("Packet dropped",
5576 QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Amaury Denoyelle80d05722022-05-16 18:13:56 +02005577 goto err;
5578 }
5579
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005580 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01005581 if (b_cspace) {
5582 b_putchr(&qc->rx.buf, 0x00);
5583 b_cspace--;
5584 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005585 b_add(&qc->rx.buf, b_cspace);
5586 if (b_contig_space(&qc->rx.buf) < pkt->len) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005587 TRACE_PROTO("Too big packet",
5588 QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len, qv);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01005589 qc_list_all_rx_pkts(qc);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005590 goto drop;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005591 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005592 }
5593
Amaury Denoyellee81fed92021-12-22 11:06:34 +01005594 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005595 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc, NULL, NULL, qv);
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005596 goto drop;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02005597 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005598
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005599 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt, NULL, qv);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005600 if (pkt->aad_len)
5601 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005602 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02005603 /* Wake up the connection packet handler task from here only if all
5604 * the contexts have been initialized, especially the mux context
5605 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
5606 * will start it if these contexts for the connection are not already
5607 * initialized.
5608 */
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005609 conn_ctx = qc->xprt_ctx;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01005610 if (conn_ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005611 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02005612
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005613 drop_no_conn:
5614 if (drop_no_conn)
5615 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005616 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005617
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02005618 return;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005619
Frédéric Lécaillecbd59c72022-05-20 08:11:26 +02005620 drop:
5621 HA_ATOMIC_INC(&prx_counters->dropped_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005622 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005623 /* Wakeup the I/O handler callback if the PTO timer must be armed.
5624 * This cannot be done by this thread.
5625 */
5626 if (io_cb_wakeup) {
Frédéric Lécaillefc790062022-03-28 17:10:31 +02005627 conn_ctx = qc->xprt_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01005628 if (conn_ctx && conn_ctx->wait_event.tasklet)
5629 tasklet_wakeup(conn_ctx->wait_event.tasklet);
5630 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01005631 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005632 if (!pkt->len)
5633 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005634 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005635 qc ? qc : NULL, pkt, NULL, qv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005636}
5637
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005638/* This function builds into <buf> buffer a QUIC long packet header.
5639 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005640 */
5641static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005642 int type, size_t pn_len,
5643 struct quic_conn *conn, const struct quic_version *ver)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005644{
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005645 if (end - *buf < sizeof ver->num + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005646 return 0;
5647
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005648 type = quic_pkt_type(type, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005649 /* #0 byte flags */
5650 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
5651 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
5652 /* Version */
Frédéric Lécaille301425b2022-06-14 17:40:39 +02005653 quic_write_uint32(buf, end, ver->num);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005654 *(*buf)++ = conn->dcid.len;
5655 /* Destination connection ID */
5656 if (conn->dcid.len) {
5657 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5658 *buf += conn->dcid.len;
5659 }
5660 /* Source connection ID */
5661 *(*buf)++ = conn->scid.len;
5662 if (conn->scid.len) {
5663 memcpy(*buf, conn->scid.data, conn->scid.len);
5664 *buf += conn->scid.len;
5665 }
5666
5667 return 1;
5668}
5669
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005670/* This function builds into <buf> buffer a QUIC short packet header.
5671 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005672 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005673static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
5674 size_t pn_len, struct quic_conn *conn,
5675 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005676{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005677 if (end - *buf < 1 + conn->dcid.len)
5678 return 0;
5679
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005680 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01005681 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
5682 ((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 +01005683 /* Destination connection ID */
5684 if (conn->dcid.len) {
5685 memcpy(*buf, conn->dcid.data, conn->dcid.len);
5686 *buf += conn->dcid.len;
5687 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005688
5689 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005690}
5691
5692/* Apply QUIC header protection to the packet with <buf> as first byte address,
5693 * <pn> as address of the Packet number field, <pnlen> being this field length
5694 * with <aead> as AEAD cipher and <key> as secret key.
5695 * Returns 1 if succeeded or 0 if failed.
5696 */
5697static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
5698 const EVP_CIPHER *aead, const unsigned char *key)
5699{
5700 int i, ret, outlen;
5701 EVP_CIPHER_CTX *ctx;
5702 /* We need an IV of at least 5 bytes: one byte for bytes #0
5703 * and at most 4 bytes for the packet number
5704 */
5705 unsigned char mask[5] = {0};
5706
5707 ret = 0;
5708 ctx = EVP_CIPHER_CTX_new();
5709 if (!ctx)
5710 return 0;
5711
5712 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
5713 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
5714 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
5715 goto out;
5716
5717 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
5718 for (i = 0; i < pnlen; i++)
5719 pn[i] ^= mask[i + 1];
5720
5721 ret = 1;
5722
5723 out:
5724 EVP_CIPHER_CTX_free(ctx);
5725
5726 return ret;
5727}
5728
5729/* Reduce the encoded size of <ack_frm> ACK frame removing the last
5730 * ACK ranges if needed to a value below <limit> in bytes.
5731 * Return 1 if succeeded, 0 if not.
5732 */
5733static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
5734{
5735 size_t room, ack_delay_sz;
5736
5737 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
5738 /* A frame is made of 1 byte for the frame type. */
5739 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005740 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005741 return 0;
5742
Frédéric Lécaille8090b512020-11-30 16:19:22 +01005743 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005744}
5745
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005746/* Prepare into <outlist> as most as possible ack-eliciting frame from their
5747 * <inlist> prebuilt frames for <qel> encryption level to be encoded in a buffer
5748 * with <room> as available room, and <*len> the packet Length field initialized
5749 * with the number of bytes already present in this buffer which must be taken
5750 * into an account for the Length packet field value. <headlen> is the number of
5751 * bytes already present in this packet before building frames.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005752 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005753 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005754 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005755 * 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 +01005756 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005757static inline int qc_build_frms(struct list *outlist, struct list *inlist,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005758 size_t room, size_t *len, size_t headlen,
5759 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005760 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005761{
Frédéric Lécailleea604992020-12-24 13:01:37 +01005762 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005763 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005764
Frédéric Lécailleea604992020-12-24 13:01:37 +01005765 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005766 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005767 return 0;
5768
Frédéric Lécailleea604992020-12-24 13:01:37 +01005769 /* If we are not probing we must take into an account the congestion
5770 * control window.
5771 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01005772 if (!qel->pktns->tx.pto_probe) {
5773 size_t remain = quic_path_prep_data(qc->path);
5774
5775 if (headlen > remain)
5776 return 0;
5777
5778 room = QUIC_MIN(room, remain - headlen);
5779 }
5780
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005781 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005782 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005783
5784 /* NOTE: switch/case block inside a loop, a successful status must be
5785 * returned by this function only if at least one frame could be built
5786 * in the switch/case block.
5787 */
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005788 list_for_each_entry_safe(cf, cfbak, inlist, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005789 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005790 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005791
Frédéric Lécailleea604992020-12-24 13:01:37 +01005792 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005793 break;
5794
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005795 switch (cf->type) {
5796 case QUIC_FT_CRYPTO:
5797 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005798 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005799 /* Compute the length of this CRYPTO frame header */
5800 hlen = 1 + quic_int_getsize(cf->crypto.offset);
5801 /* Compute the data length of this CRyPTO frame. */
5802 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
5803 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005804 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005805 if (!dlen)
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005806 continue;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005807
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005808 /* CRYPTO frame length. */
5809 flen = hlen + quic_int_getsize(dlen) + dlen;
5810 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005811 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005812 /* Add the CRYPTO data length and its encoded length to the packet
5813 * length and the length of this length.
5814 */
5815 *len += flen;
5816 room -= flen;
5817 if (dlen == cf->crypto.len) {
5818 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005819 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005820 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005821 }
5822 else {
5823 struct quic_frame *new_cf;
5824
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005825 new_cf = pool_zalloc(pool_head_quic_frame);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005826 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005827 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005828 continue;
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005829 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005830
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005831 LIST_INIT(&new_cf->reflist);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005832 new_cf->type = QUIC_FT_CRYPTO;
5833 new_cf->crypto.len = dlen;
5834 new_cf->crypto.offset = cf->crypto.offset;
5835 new_cf->crypto.qel = qel;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005836 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005837 /* Consume <dlen> bytes of the current frame. */
5838 cf->crypto.len -= dlen;
5839 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005840 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005841 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005842
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005843 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005844 if (cf->flags & QUIC_FL_TX_FRAME_LOST) {
5845 struct eb64_node *node = NULL;
5846 struct qc_stream_desc *stream_desc = NULL;
5847 struct quic_stream *strm = &cf->stream;
5848
5849 /* As this frame has been already lost, ensure the stream is always
5850 * available or the range of this frame is not consumed before
5851 * resending it.
5852 */
5853 node = eb64_lookup(&qc->streams_by_id, strm->id);
5854 if (!node) {
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005855 TRACE_PROTO("released stream", QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005856 LIST_DELETE(&cf->list);
5857 pool_free(pool_head_quic_frame, cf);
5858 continue;
5859 }
5860
5861 stream_desc = eb64_entry(node, struct qc_stream_desc, by_id);
5862 if (strm->offset.key + strm->len <= stream_desc->ack_offset) {
5863 TRACE_PROTO("ignored frame frame in already acked range",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005864 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005865 LIST_DELETE(&cf->list);
5866 pool_free(pool_head_quic_frame, cf);
5867 continue;
5868 }
5869 else if (strm->offset.key < stream_desc->ack_offset) {
5870 strm->offset.key = stream_desc->ack_offset;
5871 TRACE_PROTO("updated partially acked frame",
Frédéric Lécaillefdc1b962022-05-31 12:04:42 +02005872 QUIC_EV_CONN_PRSAFRM, qc, cf);
Frédéric Lécaille77cb38d2022-04-27 07:17:56 +02005873 }
5874 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005875 /* Note that these frames are accepted in short packets only without
5876 * "Length" packet field. Here, <*len> is used only to compute the
5877 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005878 *
5879 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005880 * excepting the variable ones. Note that +1 is for the type of this frame.
5881 */
5882 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005883 ((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 +02005884 /* Compute the data length of this STREAM frame. */
5885 avail_room = room - hlen - *len;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01005886 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005887 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005888 if ((ssize_t)avail_room <= 0)
5889 continue;
5890
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005891 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
5892 dlen = max_available_room(avail_room, &dlen_sz);
5893 if (dlen > cf->stream.len) {
5894 dlen = cf->stream.len;
5895 }
5896 dlen_sz = quic_int_getsize(dlen);
5897 flen = hlen + dlen_sz + dlen;
5898 }
5899 else {
5900 dlen = QUIC_MIN(avail_room, cf->stream.len);
5901 flen = hlen + dlen;
5902 }
5903 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005904 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005905 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005906 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005907 /* Add the STREAM data length and its encoded length to the packet
5908 * length and the length of this length.
5909 */
5910 *len += flen;
5911 room -= flen;
5912 if (dlen == cf->stream.len) {
5913 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005914 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005915 LIST_APPEND(outlist, &cf->list);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005916
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005917 /* The MUX stream might be released at this
5918 * stage. This can most notably happen on
5919 * retransmission.
5920 */
5921 if (qc->mux_state == QC_MUX_READY &&
5922 !cf->stream.stream->release) {
5923 qcc_streams_sent_done(cf->stream.stream->ctx,
5924 cf->stream.len,
5925 cf->stream.offset.key);
5926 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005927 }
5928 else {
5929 struct quic_frame *new_cf;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005930 struct buffer cf_buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005931
5932 new_cf = pool_zalloc(pool_head_quic_frame);
5933 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005934 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005935 continue;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005936 }
5937
Frédéric Lécailleb9171912022-04-21 17:32:10 +02005938 LIST_INIT(&new_cf->reflist);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005939 new_cf->type = cf->type;
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005940 new_cf->stream.stream = cf->stream.stream;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005941 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005942 new_cf->stream.id = cf->stream.id;
5943 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
5944 new_cf->stream.offset = cf->stream.offset;
5945 new_cf->stream.len = dlen;
5946 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
5947 /* FIN bit reset */
5948 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
5949 new_cf->stream.data = cf->stream.data;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005950 LIST_APPEND(outlist, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005951 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
5952 /* Consume <dlen> bytes of the current frame. */
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005953 cf_buf = b_make(b_orig(cf->stream.buf),
5954 b_size(cf->stream.buf),
5955 (char *)cf->stream.data - b_orig(cf->stream.buf), 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005956 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02005957 cf->stream.offset.key += dlen;
Amaury Denoyelle642ab062022-02-23 10:54:42 +01005958 cf->stream.data = (unsigned char *)b_peek(&cf_buf, dlen);
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005959
Amaury Denoyelle7272cd72022-03-29 15:15:54 +02005960 /* The MUX stream might be released at this
5961 * stage. This can most notably happen on
5962 * retransmission.
5963 */
5964 if (qc->mux_state == QC_MUX_READY &&
5965 !cf->stream.stream->release) {
5966 qcc_streams_sent_done(new_cf->stream.stream->ctx,
5967 new_cf->stream.len,
5968 new_cf->stream.offset.key);
5969 }
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02005970 }
Amaury Denoyelle54445d02022-03-10 16:44:14 +01005971
5972 /* TODO the MUX is notified about the frame sending via
5973 * previous qcc_streams_sent_done call. However, the
5974 * sending can fail later, for example if the sendto
5975 * system call returns an error. As the MUX has been
5976 * notified, the transport layer is responsible to
5977 * bufferize and resent the announced data later.
5978 */
5979
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005980 break;
5981
5982 default:
5983 flen = qc_frm_len(cf);
5984 BUG_ON(!flen);
5985 if (flen > room)
5986 continue;
5987
5988 *len += flen;
5989 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01005990 LIST_DELETE(&cf->list);
Frédéric Lécailleedc81462022-02-25 17:44:29 +01005991 LIST_APPEND(outlist, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02005992 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005993 }
Frédéric Lécaille573b56b2022-04-25 15:42:56 +02005994
5995 /* Successful status as soon as a frame could be built */
Frédéric Lécailleea604992020-12-24 13:01:37 +01005996 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005997 }
5998
Frédéric Lécailleea604992020-12-24 13:01:37 +01005999 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006000}
6001
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006002/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006003 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
6004 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006005 * filling the buffer with as much frames as possible from <frms> list of
6006 * prebuilt frames.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006007 * 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 +02006008 * 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 +02006009 * having returned from this function.
6010 * 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 +01006011 * number field in this packet. <pn_len> will also have the packet number
6012 * length as value.
6013 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006014 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006015 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006016static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
6017 size_t dglen, struct quic_tx_packet *pkt,
6018 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006019 int padding, int cc, int probe,
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006020 struct quic_enc_level *qel, struct quic_conn *qc,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006021 const struct quic_version *ver, struct list *frms)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006022{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006023 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006024 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006025 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
6026 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006027 struct quic_frame cc_frm = { };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006028 size_t ack_frm_len, head_len;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006029 int64_t rx_largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006030 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006031 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006032 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006033
Frédéric Lécailleea604992020-12-24 13:01:37 +01006034 /* Length field value with CRYPTO frames if present. */
6035 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006036 beg = pos;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006037 /* When not probing, and no immediate close is required, reduce the size of this
6038 * buffer to respect the congestion controller window.
6039 * 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 +01006040 */
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006041 if (!probe && !LIST_ISEMPTY(frms) && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006042 size_t path_room;
6043
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006044 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006045 if (end - beg > path_room)
6046 end = beg + path_room;
6047 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006048
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006049 /* Ensure there is enough room for the TLS encryption tag and a zero token
6050 * length field if any.
6051 */
6052 if (end - pos < QUIC_TLS_TAG_LEN +
6053 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
6054 goto no_room;
6055
6056 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006057 rx_largest_acked_pn = qel->pktns->rx.largest_acked_pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006058 /* packet number length */
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006059 *pn_len = quic_packet_number_length(pn, rx_largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006060 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006061 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006062 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006063 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006064 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc, ver)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006065 goto no_room;
6066
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006067 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
6068 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006069 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006070 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006071 /* Build an ACK frame if required. */
6072 ack_frm_len = 0;
Frédéric Lécaille337108e2022-04-25 10:38:27 +02006073 if ((qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !qel->pktns->tx.pto_probe) {
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006074 struct quic_arngs *arngs = &qel->pktns->rx.arngs;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006075 BUG_ON(eb_is_empty(&qel->pktns->rx.arngs.root));
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006076 ack_frm.tx_ack.arngs = arngs;
6077 if (qel->pktns->flags & QUIC_FL_PKTNS_NEW_LARGEST_PN) {
6078 qel->pktns->tx.ack_delay =
6079 quic_compute_ack_delay_us(qel->pktns->rx.largest_time_received, qc);
6080 qel->pktns->flags &= ~QUIC_FL_PKTNS_NEW_LARGEST_PN;
6081 }
6082 ack_frm.tx_ack.ack_delay = qel->pktns->tx.ack_delay;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006083 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
6084 * smallest frame (PING) and <*pn_len> more for the packet number. Note
6085 * that from here, we do not know if we will have to send a PING frame.
6086 * This will be decided after having computed the ack-eliciting frames
6087 * to be added to this packet.
6088 */
6089 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 +01006090 if (!ack_frm_len)
6091 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006092 }
6093
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006094 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006095 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01006096 len_frms = 0;
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006097 if (!cc && !LIST_ISEMPTY(frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006098 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01006099
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006100 TRACE_PROTO("Avail. ack eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006101 /* Initialize the length of the frames built below to <len>.
6102 * If any frame could be successfully built by qc_build_frms(),
6103 * we will have len_frms > len.
6104 */
6105 len_frms = len;
Frédéric Lécailleedc81462022-02-25 17:44:29 +01006106 if (!qc_build_frms(&frm_list, frms,
6107 end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01006108 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006109 qc, NULL, NULL, &room);
Frédéric Lécaille834399c2022-04-25 17:17:07 +02006110 if (!ack_frm_len && !qel->pktns->tx.pto_probe)
6111 goto no_room;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006112 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006113 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006114
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006115 /* Length (of the remaining data). Must not fail because, the buffer size
6116 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
6117 * for the encryption tag. It must be taken into an account for the length
6118 * of this packet.
6119 */
6120 if (len_frms)
6121 len = len_frms + QUIC_TLS_TAG_LEN;
6122 else
6123 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006124 /* CONNECTION_CLOSE frame */
6125 if (cc) {
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006126 cc_frm.type = qc->flags & QUIC_FL_CONN_APP_ALERT ?
6127 QUIC_FT_CONNECTION_CLOSE_APP : QUIC_FT_CONNECTION_CLOSE;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006128
Amaury Denoyellef9e190e2022-05-23 16:12:15 +02006129 cc_frm.connection_close.error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006130 len += qc_frm_len(&cc_frm);
6131 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006132 add_ping_frm = 0;
6133 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006134 len_sz = quic_int_getsize(len);
6135 /* Add this packet size to <dglen> */
6136 dglen += head_len + len_sz + len;
6137 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
6138 /* This is a maximum padding size */
6139 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
6140 /* The length field value is of this packet is <len> + <padding_len>
6141 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05006142 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01006143 * sizes from <padding_len>.
6144 */
6145 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
6146 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006147 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006148 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006149 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006150 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006151 add_ping_frm = 1;
6152 len += 1;
6153 }
6154 /* 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 +01006155 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006156 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
6157 }
6158
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006159 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
6160 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006161
6162 /* Packet number field address. */
6163 *buf_pn = pos;
6164
6165 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006166 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
6167 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006168
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006169 if (ack_frm_len) {
6170 if (!qc_build_frm(&pos, end, &ack_frm, pkt, qc))
6171 goto no_room;
6172
6173 pkt->largest_acked_pn = quic_pktns_get_largest_acked_pn(qel->pktns);
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006174 pkt->flags |= QUIC_FL_TX_PACKET_ACK;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006175 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006176
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006177 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006178 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006179 list_for_each_entry(cf, &frm_list, list) {
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006180 unsigned char *spos = pos;
6181
6182 if (!qc_build_frm(&spos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006183 ssize_t room = end - pos;
6184 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006185 qc, NULL, NULL, &room);
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006186 /* TODO: this should not have happened if qc_build_frms()
6187 * had correctly computed and sized the frames to be
6188 * added to this packet. Note that <cf> was added
6189 * from <frm_list> to <frms> list by qc_build_frms().
6190 */
6191 LIST_DELETE(&cf->list);
6192 LIST_INSERT(frms, &cf->list);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006193 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01006194 }
Frédéric Lécailledcc74ff2022-03-18 17:49:29 +01006195
6196 pos = spos;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006197 quic_tx_packet_refinc(pkt);
6198 cf->pkt = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006199 }
6200 }
6201
6202 /* Build a PING frame if needed. */
6203 if (add_ping_frm) {
6204 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006205 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006206 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006207 }
6208
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006209 /* Build a CONNECTION_CLOSE frame if needed. */
Frédéric Lécaille59bf2552022-03-28 12:13:09 +02006210 if (cc) {
6211 if (!qc_build_frm(&pos, end, &cc_frm, pkt, qc))
6212 goto no_room;
6213
6214 pkt->flags |= QUIC_FL_TX_PACKET_CC;
6215 }
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01006216
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006217 /* Build a PADDING frame if needed. */
6218 if (padding_len) {
6219 frm.type = QUIC_FT_PADDING;
6220 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01006221 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006222 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006223 }
6224
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006225 /* If this packet is ack-eliciting and we are probing let's
6226 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006227 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01006228 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
6229 qel->pktns->tx.pto_probe)
6230 qel->pktns->tx.pto_probe--;
6231
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006232 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006233 LIST_SPLICE(&pkt->frms, &frm_list);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006234 TRACE_PROTO("Packet ack-eliciting frames", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006235
6236 return 1;
6237
6238 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01006239 /* Replace the pre-built frames which could not be add to this packet */
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006240 LIST_SPLICE(frms, &frm_list);
Frédéric Lécailled8b798d2022-04-25 17:48:40 +02006241 TRACE_PROTO("Remaining ack-eliciting frames", QUIC_EV_CONN_FRMLIST, qc, frms);
Frédéric Lécailleb823bb72022-03-31 20:26:18 +02006242
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006243 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006244}
6245
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006246static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006247{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006248 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006249 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006250 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01006251 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006252 LIST_INIT(&pkt->frms);
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006253 pkt->time_sent = TICK_ETERNITY;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006254 pkt->next = NULL;
Frédéric Lécaille141982a2022-03-15 18:44:20 +01006255 pkt->largest_acked_pn = -1;
Frédéric Lécaille2899fe22022-03-21 10:43:53 +01006256 pkt->flags = 0;
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006257 pkt->refcnt = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006258}
6259
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006260/* Build a packet into <buf> packet buffer with <pkt_type> as packet
Frédéric Lécaille28c7ea32022-02-25 17:41:39 +01006261 * type for <qc> QUIC connection from <qel> encryption level from <frms> list
6262 * of prebuilt frames.
6263 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006264 * Return -2 if the packet could not be allocated or encrypted for any reason,
6265 * -1 if there was not enough room to build a packet.
Frédéric Lécaillee9a974a2022-03-13 19:19:12 +01006266 * XXX NOTE XXX
6267 * If you provide provide qc_build_pkt() with a big enough buffer to build a packet as big as
6268 * possible (to fill an MTU), the unique reason why this function may fail is the congestion
6269 * control window limitation.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006270 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02006271static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
6272 const unsigned char *buf_end,
Frédéric Lécaille301425b2022-06-14 17:40:39 +02006273 struct quic_enc_level *qel,
6274 struct quic_tls_ctx *tls_ctx, struct list *frms,
6275 struct quic_conn *qc, const struct quic_version *ver,
6276 size_t dglen, int padding,
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006277 int pkt_type, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006278{
6279 /* The pointer to the packet number field. */
6280 unsigned char *buf_pn;
6281 unsigned char *beg, *end, *payload;
6282 int64_t pn;
6283 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006284 struct quic_tx_packet *pkt;
6285
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006286 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006287 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006288 pkt = pool_alloc(pool_head_quic_tx_packet);
6289 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006290 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006291 *err = -2;
6292 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006293 }
6294
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02006295 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006296 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006297 pn_len = 0;
6298 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006299
6300 pn = qel->pktns->tx.next_pn + 1;
6301 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 +02006302 padding, cc, probe, qel, qc, ver, frms)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02006303 *err = -1;
6304 goto err;
6305 }
6306
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006307 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006308 payload = buf_pn + pn_len;
6309 payload_len = end - payload;
6310 aad_len = payload - beg;
6311
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01006312 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006313 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006314 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006315 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006316
6317 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006318 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006319 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
6320 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006321 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006322 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006323 goto err;
6324 }
6325
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01006326 /* Consume a packet number */
6327 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01006328 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille676b8492022-03-10 10:38:20 +01006329 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes && !quic_peer_validated_addr(qc))
Frédéric Lécaillefc790062022-03-28 17:10:31 +02006330 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006331 /* Now that a correct packet is built, let us consume <*pos> buffer. */
6332 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006333 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02006334 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006335 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006336 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006337 pkt->in_flight_len = pkt->len;
6338 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01006339 }
Frédéric Lécaille47756802022-03-25 09:12:16 +01006340 /* Always reset this flags */
6341 qc->flags &= ~QUIC_FL_CONN_IMMEDIATE_CLOSE;
Frédéric Lécailleb0021452022-03-29 11:42:03 +02006342 if (pkt->flags & QUIC_FL_TX_PACKET_ACK) {
6343 qel->pktns->flags &= ~QUIC_FL_PKTNS_ACK_REQUIRED;
6344 qel->pktns->rx.nb_aepkts_since_last_ack = 0;
6345 }
Frédéric Lécaille205e4f32022-03-28 17:38:27 +02006346
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006347 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006348 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006349
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006350 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006351
6352 err:
Frédéric Lécaillee2a1c1b2022-03-17 11:28:10 +01006353 /* TODO: what about the frames which have been built
6354 * for this packet.
6355 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006356 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01006357 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02006358 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006359}
6360
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006361
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006362/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6363 * event subscriber <es> is not allowed to change from a previous call as long
6364 * as at least one event is still subscribed. The <event_type> must only be a
6365 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
6366 */
6367static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6368{
Willy Tarreau784b8682022-04-11 14:18:10 +02006369 struct qcc *qcc = conn->handle.qc->qcc;
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006370
6371 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6372 BUG_ON(qcc->subs && qcc->subs != es);
6373
6374 es->events |= event_type;
6375 qcc->subs = es;
6376
6377 if (event_type & SUB_RETRY_RECV)
Willy Tarreau784b8682022-04-11 14:18:10 +02006378 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006379
6380 if (event_type & SUB_RETRY_SEND)
Willy Tarreau784b8682022-04-11 14:18:10 +02006381 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->handle.qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02006382
6383 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006384}
6385
6386/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6387 * The <es> pointer is not allowed to differ from the one passed to the
6388 * subscribe() call. It always returns zero.
6389 */
6390static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
6391{
6392 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
6393}
6394
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006395/* Store in <xprt_ctx> the context attached to <conn>.
6396 * Returns always 0.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006397 */
6398static int qc_conn_init(struct connection *conn, void **xprt_ctx)
6399{
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01006400 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006401
6402 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
6403
Amaury Denoyelle33ac3462022-01-18 16:44:34 +01006404 /* do not store the context if already set */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006405 if (*xprt_ctx)
6406 goto out;
6407
Willy Tarreau784b8682022-04-11 14:18:10 +02006408 *xprt_ctx = conn->handle.qc->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006409
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006410 out:
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01006411 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006412
6413 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006414}
6415
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006416/* Start the QUIC transport layer */
6417static int qc_xprt_start(struct connection *conn, void *ctx)
6418{
6419 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02006420 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006421
Willy Tarreau784b8682022-04-11 14:18:10 +02006422 qc = conn->handle.qc;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006423 if (qcc_install_app_ops(qc->qcc, qc->app_ops)) {
6424 TRACE_PROTO("Cannot install app layer", QUIC_EV_CONN_LPKT, qc);
Amaury Denoyelle5d774de2022-04-14 14:59:35 +02006425 /* prepare a CONNECTION_CLOSE frame */
6426 qc->err_code = QC_ERR_APPLICATION_ERROR;
6427 qc->flags |= QUIC_FL_CONN_IMMEDIATE_CLOSE;
Amaury Denoyelle05d4ae62022-04-13 17:40:26 +02006428 return -1;
Amaury Denoyellecfa2d562022-01-19 16:01:05 +01006429 }
6430
6431 /* mux-quic can now be considered ready. */
6432 qc->mux_state = QC_MUX_READY;
6433
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006434 tasklet_wakeup(qctx->wait_event.tasklet);
6435 return 1;
6436}
6437
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006438static struct ssl_sock_ctx *qc_get_ssl_sock_ctx(struct connection *conn)
6439{
Willy Tarreau784b8682022-04-11 14:18:10 +02006440 if (!conn || conn->xprt != xprt_get(XPRT_QUIC) || !conn->handle.qc || !conn->xprt_ctx)
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006441 return NULL;
6442
Willy Tarreau784b8682022-04-11 14:18:10 +02006443 return conn->handle.qc->xprt_ctx;
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006444}
6445
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006446/* transport-layer operations for QUIC connections. */
6447static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02006448 .close = quic_close,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01006449 .subscribe = quic_conn_subscribe,
6450 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006451 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02006452 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006453 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
6454 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01006455 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau54a1dcb2022-04-11 11:57:35 +02006456 .get_ssl_sock_ctx = qc_get_ssl_sock_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006457 .name = "QUIC",
6458};
6459
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006460static void __quic_conn_init(void)
6461{
6462 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
6463 xprt_register(XPRT_QUIC, &ssl_quic);
6464}
Willy Tarreau79367f92022-04-25 19:18:24 +02006465INITCALL0(STG_REGISTER, __quic_conn_init);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006466
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006467static void __quic_conn_deinit(void)
6468{
6469 BIO_meth_free(ha_quic_meth);
6470}
Willy Tarreau79367f92022-04-25 19:18:24 +02006471REGISTER_POST_DEINIT(__quic_conn_deinit);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006472
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01006473/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
6474 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05006475 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006476 */
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006477struct task *quic_lstnr_dghdlr(struct task *t, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006478{
6479 unsigned char *pos;
6480 const unsigned char *end;
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006481 struct quic_dghdlr *dghdlr = ctx;
6482 struct quic_dgram *dgram;
6483 int first_pkt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006484
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006485 while ((dgram = MT_LIST_POP(&dghdlr->dgrams, typeof(dgram), mt_list))) {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006486 pos = dgram->buf;
6487 end = pos + dgram->len;
6488 do {
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006489 struct quic_rx_packet *pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006490
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006491 pkt = pool_zalloc(pool_head_quic_rx_packet);
6492 if (!pkt)
6493 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006494
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02006495 LIST_INIT(&pkt->qc_rx_pkt_list);
Frédéric Lécaille7cc8b312022-05-05 12:04:28 +02006496 pkt->time_received = now_ms;
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006497 quic_rx_packet_refinc(pkt);
Frédéric Lécaille4646cf32022-04-27 15:09:53 +02006498 qc_lstnr_pkt_rcv(pos, end, pkt, first_pkt, dgram);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006499 first_pkt = 0;
6500 pos += pkt->len;
6501 quic_rx_packet_refdec(pkt);
Amaury Denoyelle23f908c2022-06-20 10:58:03 +02006502
6503 /* Free rejected packets */
6504 if (!pkt->refcnt) {
6505 BUG_ON(LIST_INLIST(&pkt->qc_rx_pkt_list));
6506 pool_free(pool_head_quic_rx_packet, pkt);
6507 }
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006508 } while (pos < end);
6509
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006510 /* Increasing the received bytes counter by the UDP datagram length
6511 * if this datagram could be associated to a connection.
6512 */
6513 if (dgram->qc)
6514 dgram->qc->rx.bytes += dgram->len;
Frédéric Lécaille841bf5e2022-02-02 09:41:27 +01006515
6516 /* Mark this datagram as consumed */
6517 HA_ATOMIC_STORE(&dgram->buf, NULL);
Frédéric Lécailledf1c7c72022-01-28 15:38:52 +01006518 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006519
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006520 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006521
6522 err:
Frédéric Lécaille25bc8872022-01-27 09:15:40 +01006523 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006524}
6525
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006526/* Retreive the DCID from a QUIC datagram or packet with <buf> as first octet.
6527 * Returns 1 if succeeded, 0 if not.
6528 */
Frédéric Lécaille6492e662022-05-17 17:23:16 +02006529int quic_get_dgram_dcid(unsigned char *buf, const unsigned char *end,
6530 unsigned char **dcid, size_t *dcid_len)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006531{
6532 int long_header;
6533 size_t minlen, skip;
6534
6535 if (!(*buf & QUIC_PACKET_FIXED_BIT))
6536 goto err;
6537
6538 long_header = *buf & QUIC_PACKET_LONG_HEADER_BIT;
Frédéric Lécaille36b28ed2022-05-09 18:08:13 +02006539 minlen = long_header ? QUIC_LONG_PACKET_MINLEN :
6540 QUIC_SHORT_PACKET_MINLEN + QUIC_HAP_CID_LEN + QUIC_TLS_TAG_LEN;
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006541 skip = long_header ? QUIC_LONG_PACKET_DCID_OFF : QUIC_SHORT_PACKET_DCID_OFF;
Frédéric Lécaillebfa32362022-02-02 10:44:36 +01006542 if (end - buf <= minlen)
Frédéric Lécaille3d4bfe72022-01-26 16:07:16 +01006543 goto err;
6544
6545 buf += skip;
6546 *dcid_len = long_header ? *buf++ : QUIC_HAP_CID_LEN;
6547 if (*dcid_len > QUIC_CID_MAXLEN || end - buf <= *dcid_len)
6548 goto err;
6549
6550 *dcid = buf;
6551
6552 return 1;
6553
6554 err:
6555 TRACE_PROTO("wrong datagram", QUIC_EV_CONN_LPKT);
6556 return 0;
6557}
6558
Amaury Denoyelleb515b0a2022-04-06 10:28:43 +02006559/* Notify the MUX layer if alive about an imminent close of <qc>. */
6560void qc_notify_close(struct quic_conn *qc)
6561{
6562 if (qc->flags & QUIC_FL_CONN_NOTIFY_CLOSE)
6563 return;
6564
6565 qc->flags |= QUIC_FL_CONN_NOTIFY_CLOSE;
6566
6567 /* wake up the MUX */
6568 if (qc->mux_state == QC_MUX_READY && qc->conn->mux->wake)
6569 qc->conn->mux->wake(qc->conn);
6570}
6571
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01006572/*
6573 * Local variables:
6574 * c-indent-level: 8
6575 * c-basic-offset: 8
6576 * End:
6577 */