blob: 630f8d16d5d482090879b1a6b6e5f1b7a49dd4a9 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <import/ebmbtree.h>
26
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010027#include <haproxy/buf-t.h>
28#include <haproxy/compat.h>
29#include <haproxy/api.h>
30#include <haproxy/debug.h>
31#include <haproxy/tools.h>
32#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010033
34#include <haproxy/connection.h>
35#include <haproxy/fd.h>
36#include <haproxy/freq_ctr.h>
37#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038#include <haproxy/h3.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010039#include <haproxy/hq_interop.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010041#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010042#include <haproxy/pipe.h>
43#include <haproxy/proxy.h>
44#include <haproxy/quic_cc.h>
45#include <haproxy/quic_frame.h>
46#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020047#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010048#include <haproxy/quic_tls.h>
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +010049#include <haproxy/sink.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010050#include <haproxy/ssl_sock.h>
51#include <haproxy/stream_interface.h>
52#include <haproxy/task.h>
53#include <haproxy/trace.h>
54#include <haproxy/xprt_quic.h>
55
Amaury Denoyellea22d8602021-11-10 15:17:56 +010056/* list of supported QUIC versions by this implementation */
57static int quic_supported_version[] = {
58 0x00000001,
Frédéric Lécaille56d3e1b2021-11-19 14:32:52 +010059 0xff00001d, /* draft-29 */
Amaury Denoyellea22d8602021-11-10 15:17:56 +010060
61 /* placeholder, do not add entry after this */
62 0x0
63};
64
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020065/* This is the values of some QUIC transport parameters when absent.
66 * Should be used to initialize any transport parameters (local or remote)
67 * before updating them with customized values.
68 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010069struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020070 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020071 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
72 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020073 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074};
75
76/* trace source and events */
77static void quic_trace(enum trace_level level, uint64_t mask, \
78 const struct trace_source *src,
79 const struct ist where, const struct ist func,
80 const void *a1, const void *a2, const void *a3, const void *a4);
81
82static const struct trace_event quic_trace_events[] = {
83 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
84 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
85 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
86 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
87 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
88 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
89 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050090 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010091 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
92 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
93 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
94 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
95 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
96 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
97 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
98 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
99 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
100 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
101 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
102 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
103 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
104 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
105 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
106 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
107 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
108 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
109 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100110 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
111 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
112 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
113 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
114 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
115 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100116 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200117 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
118 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100119 { /* end */ }
120};
121
122static const struct name_desc quic_trace_lockon_args[4] = {
123 /* arg1 */ { /* already used by the connection */ },
124 /* arg2 */ { .name="quic", .desc="QUIC transport" },
125 /* arg3 */ { },
126 /* arg4 */ { }
127};
128
129static const struct name_desc quic_trace_decoding[] = {
130#define QUIC_VERB_CLEAN 1
131 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
132 { /* end */ }
133};
134
135
136struct trace_source trace_quic = {
137 .name = IST("quic"),
138 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100139 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100140 .default_cb = quic_trace,
141 .known_events = quic_trace_events,
142 .lockon_args = quic_trace_lockon_args,
143 .decoding = quic_trace_decoding,
144 .report_events = ~0, /* report everything by default */
145};
146
147#define TRACE_SOURCE &trace_quic
148INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
149
150static BIO_METHOD *ha_quic_meth;
151
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200152DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200153DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100154DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200156 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100157DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_POOL(pool_head_quic_connection_id,
159 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_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 +0100161DECLARE_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 +0100162DECLARE_STATIC_POOL(pool_head_quic_rx_crypto_frm, "quic_rx_crypto_frm_pool", sizeof(struct quic_rx_crypto_frm));
Frédéric Lécailledfbae762021-02-18 09:59:01 +0100163DECLARE_POOL(pool_head_quic_rx_strm_frm, "quic_rx_strm_frm", sizeof(struct quic_rx_strm_frm));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100164DECLARE_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 +0200165DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100166DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100167
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200168static struct quic_tx_packet *qc_build_pkt(unsigned char **pos, const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +0200169 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100170 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +0100171 int padding, int ack, int nb_pto_dgrams, int cc, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100172
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100173/* Only for debug purpose */
174struct enc_debug_info {
175 unsigned char *payload;
176 size_t payload_len;
177 unsigned char *aad;
178 size_t aad_len;
179 uint64_t pn;
180};
181
182/* Initializes a enc_debug_info struct (only for debug purpose) */
183static inline void enc_debug_info_init(struct enc_debug_info *edi,
184 unsigned char *payload, size_t payload_len,
185 unsigned char *aad, size_t aad_len, uint64_t pn)
186{
187 edi->payload = payload;
188 edi->payload_len = payload_len;
189 edi->aad = aad;
190 edi->aad_len = aad_len;
191 edi->pn = pn;
192}
193
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100194/* Trace callback for QUIC.
195 * These traces always expect that arg1, if non-null, is of type connection.
196 */
197static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
198 const struct ist where, const struct ist func,
199 const void *a1, const void *a2, const void *a3, const void *a4)
200{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100201 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100202
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100203 if (qc) {
204 const struct quic_tls_secrets *secs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100205
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100206 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207 if ((mask & QUIC_EV_CONN_INIT) && qc) {
208 chunk_appendf(&trace_buf, "\n odcid");
209 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100210 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100211 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100212 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100213 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 }
215
216 if (mask & QUIC_EV_CONN_ADDDATA) {
217 const enum ssl_encryption_level_t *level = a2;
218 const size_t *len = a3;
219
220 if (level) {
221 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
222
223 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
224 }
225 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100226 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100227 }
228 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
229 /* Initial read & write secrets. */
230 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
231 const unsigned char *rx_sec = a2;
232 const unsigned char *tx_sec = a3;
233
234 secs = &qc->els[level].tls_ctx.rx;
235 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
236 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
237 if (rx_sec)
238 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
239 quic_tls_keys_hexdump(&trace_buf, secs);
240 }
241 secs = &qc->els[level].tls_ctx.tx;
242 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
243 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
244 if (tx_sec)
245 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
246 quic_tls_keys_hexdump(&trace_buf, secs);
247 }
248 }
249 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
250 const enum ssl_encryption_level_t *level = a2;
251 const unsigned char *secret = a3;
252 const size_t *secret_len = a4;
253
254 if (level) {
255 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
256
257 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
258 if (secret && secret_len)
259 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
260 secs = &qc->els[lvl].tls_ctx.rx;
261 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
262 quic_tls_keys_hexdump(&trace_buf, secs);
263 }
264 }
265
266 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
267 const enum ssl_encryption_level_t *level = a2;
268 const unsigned char *secret = a3;
269 const size_t *secret_len = a4;
270
271 if (level) {
272 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
273
274 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
275 if (secret && secret_len)
276 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
277 secs = &qc->els[lvl].tls_ctx.tx;
278 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
279 quic_tls_keys_hexdump(&trace_buf, secs);
280 }
281
282 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100283
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100284 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285 const struct quic_tx_packet *pkt = a2;
286 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100287 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288
289 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100290 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100292 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100293 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100294 (unsigned long long)qc->path->cwnd,
295 (unsigned long long)qc->path->prep_in_flight,
296 (unsigned long long)qc->path->in_flight,
297 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100298 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100299 }
300 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200301 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100302 if (pkt->pn_node.key != (uint64_t)-1)
303 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100305 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100306 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
307 }
308
309 if (room) {
310 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
311 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
312 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100313 }
314 }
315
316 if (mask & QUIC_EV_CONN_HDSHK) {
317 const enum quic_handshake_state *state = a2;
318 const int *err = a3;
319
320 if (state)
321 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
322 if (err)
323 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
324 }
325
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100326 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100327 const struct quic_rx_packet *pkt = a2;
328 const unsigned long *pktlen = a3;
329 const SSL *ssl = a4;
330
331 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100332 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100333 pkt, quic_packet_type_enc_level_char(pkt->type));
334 if (pkt->pnl)
335 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
336 (unsigned long long)pkt->pn);
337 if (pkt->token_len)
338 chunk_appendf(&trace_buf, " toklen=%llu",
339 (unsigned long long)pkt->token_len);
340 if (pkt->aad_len)
341 chunk_appendf(&trace_buf, " aadlen=%llu",
342 (unsigned long long)pkt->aad_len);
343 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
344 pkt->flags, (unsigned long long)pkt->len);
345 }
346 if (pktlen)
347 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
348 if (ssl) {
349 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
350 chunk_appendf(&trace_buf, " el=%c",
351 quic_enc_level_char(ssl_to_quic_enc_level(level)));
352 }
353 }
354
355 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
356 const struct quic_rx_packet *pkt = a2;
357 const struct quic_rx_crypto_frm *cf = a3;
358 const SSL *ssl = a4;
359
360 if (pkt)
361 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
362 quic_packet_type_enc_level_char(pkt->type),
363 (unsigned long long)pkt->pn);
364 if (cf)
365 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
366 (unsigned long long)cf->offset_node.key,
367 (unsigned long long)cf->len);
368 if (ssl) {
369 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200370 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100371 quic_enc_level_char(ssl_to_quic_enc_level(level)));
372 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100373
374 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100375 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100376 }
377
378 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
379 const struct quic_frame *frm = a2;
380
381 if (frm)
382 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
383 }
384
385 if (mask & QUIC_EV_CONN_PHPKTS) {
386 const struct quic_enc_level *qel = a2;
387
388 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100389 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100390 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100391 " 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 +0100392 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200393 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100394 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100395 (unsigned long long)qc->path->cwnd,
396 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100398 (unsigned long long)pktns->tx.in_flight,
399 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100400 }
401 }
402
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100403 if (mask & QUIC_EV_CONN_ENCPKT) {
404 const struct enc_debug_info *edi = a2;
405
406 if (edi)
407 chunk_appendf(&trace_buf,
408 " payload=@%p payload_len=%llu"
409 " aad=@%p aad_len=%llu pn=%llu",
410 edi->payload, (unsigned long long)edi->payload_len,
411 edi->aad, (unsigned long long)edi->aad_len,
412 (unsigned long long)edi->pn);
413 }
414
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100415 if (mask & QUIC_EV_CONN_RMHP) {
416 const struct quic_rx_packet *pkt = a2;
417
418 if (pkt) {
419 const int *ret = a3;
420
421 chunk_appendf(&trace_buf, " pkt@%p", pkt);
422 if (ret && *ret)
423 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
424 pkt->pnl, (unsigned long long)pkt->pn);
425 }
426 }
427
428 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200429 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100430 const unsigned long *val1 = a3;
431 const unsigned long *val2 = a4;
432
433 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100434 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100435 if (val1)
436 chunk_appendf(&trace_buf, " %lu", *val1);
437 if (val2)
438 chunk_appendf(&trace_buf, "..%lu", *val2);
439 }
440
441 if (mask & QUIC_EV_CONN_RTTUPDT) {
442 const unsigned int *rtt_sample = a2;
443 const unsigned int *ack_delay = a3;
444 const struct quic_loss *ql = a4;
445
446 if (rtt_sample)
447 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
448 if (ack_delay)
449 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
450 if (ql)
451 chunk_appendf(&trace_buf,
452 " srtt=%ums rttvar=%ums min_rtt=%ums",
453 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
454 }
455 if (mask & QUIC_EV_CONN_CC) {
456 const struct quic_cc_event *ev = a2;
457 const struct quic_cc *cc = a3;
458
459 if (a2)
460 quic_cc_event_trace(&trace_buf, ev);
461 if (a3)
462 quic_cc_state_trace(&trace_buf, cc);
463 }
464
465 if (mask & QUIC_EV_CONN_PKTLOSS) {
466 const struct quic_pktns *pktns = a2;
467 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100468
469 if (pktns) {
470 chunk_appendf(&trace_buf, " pktns=%s",
471 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
472 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
473 if (pktns->tx.loss_time)
474 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100475 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100476 }
477 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
478 struct quic_tx_packet *pkt;
479
480 chunk_appendf(&trace_buf, " lost_pkts:");
481 list_for_each_entry(pkt, lost_pkts, list)
482 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
483 }
484 }
485
486 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100487 const struct quic_pktns *pktns = a2;
488 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100489 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100490
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100491 if (ifae_pkts)
492 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
493 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100495 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100496 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100497 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
498 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100499 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
500 if (pktns->tx.in_flight)
501 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100502 if (pktns->tx.loss_time)
503 chunk_appendf(&trace_buf, " loss_time=%dms",
504 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
505 }
506 if (mask & QUIC_EV_CONN_SPTO) {
507 if (pktns->tx.time_of_last_eliciting)
508 chunk_appendf(&trace_buf, " tole=%dms",
509 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
510 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100511 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512 }
513 }
514
515 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
516 chunk_appendf(&trace_buf,
517 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
518 }
519 }
520
521 if (mask & QUIC_EV_CONN_SPPKTS) {
522 const struct quic_tx_packet *pkt = a2;
523
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100524 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
525 (unsigned long long)qc->path->cwnd,
526 (unsigned long long)qc->path->prep_in_flight,
527 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100529 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530 (unsigned long)pkt->pn_node.key,
531 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
532 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100533 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 }
535 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100536
537 if (mask & QUIC_EV_CONN_SSLALERT) {
538 const uint8_t *alert = a2;
539 const enum ssl_encryption_level_t *level = a3;
540
541 if (alert)
542 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
543 if (level)
544 chunk_appendf(&trace_buf, " el=%c",
545 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
546 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100547
548 if (mask & QUIC_EV_CONN_BCFRMS) {
549 const size_t *sz1 = a2;
550 const size_t *sz2 = a3;
551 const size_t *sz3 = a4;
552
553 if (sz1)
554 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
555 if (sz2)
556 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
557 if (sz3)
558 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
559 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100560
561 if (mask & QUIC_EV_CONN_PSTRM) {
562 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100563
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100564 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100565 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100566 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100567 }
568 if (mask & QUIC_EV_CONN_LPKT) {
569 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200570 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100571
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100572 if (pkt) {
573 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
574 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
575 if (pkt->pn_node.key != (uint64_t)-1)
576 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
577 }
578
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200579 if (len)
580 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 }
582
583}
584
585/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100586static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200588 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100589
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100590 if (objt_server(qc->conn->target))
591 return 1;
592
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200593 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
594 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
595 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
596 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200597 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 return 1;
599
600 return 0;
601}
602
603/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
604 * both loss detection and PTO and schedule the task assiated to this timer if needed.
605 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100606static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100607{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100608 struct quic_pktns *pktns;
609 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200610 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100612 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100613 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100614
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615 pktns = quic_loss_pktns(qc);
616 if (tick_isset(pktns->tx.loss_time)) {
617 qc->timer = pktns->tx.loss_time;
618 goto out;
619 }
620
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100621 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622 * cancelled for a server which reached the anti-amplification limit.
623 */
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100624 if (qc->flags & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100625 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100626 qc->timer = TICK_ETERNITY;
627 goto out;
628 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100629
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100630 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100631 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632 /* Timer cancellation. */
633 qc->timer = TICK_ETERNITY;
634 goto out;
635 }
636
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200637 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
638 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100639 if (tick_isset(pto))
640 qc->timer = pto;
641 out:
Amaury Denoyelle0a29e132021-12-23 15:06:56 +0100642 if (qc->timer_task && qc->timer != TICK_ETERNITY)
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200643 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100644 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100645}
646
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100647/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
648 * connection.
649 * Return 1 if succeeded, 0 if not.
650 */
651static int quic_tls_key_update(struct quic_conn *qc)
652{
653 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
654 struct quic_tls_secrets *rx, *tx;
655 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
656 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
657
658 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
659 rx = &tls_ctx->rx;
660 tx = &tls_ctx->tx;
661 nxt_rx = &qc->ku.nxt_rx;
662 nxt_tx = &qc->ku.nxt_tx;
663
664 /* Prepare new RX secrets */
665 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
666 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100667 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100668 return 0;
669 }
670
671 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
672 nxt_rx->key, nxt_rx->keylen,
673 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
674 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100675 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100676 return 0;
677 }
678
679 /* Prepare new TX secrets */
680 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
681 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100682 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100683 return 0;
684 }
685
686 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
687 nxt_tx->key, nxt_tx->keylen,
688 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
689 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100690 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100691 return 0;
692 }
693
694 return 1;
695}
696
697/* Rotate the Key Update information for <qc> QUIC connection.
698 * Must be used after having updated them.
699 * Always succeeds.
700 */
701static void quic_tls_rotate_keys(struct quic_conn *qc)
702{
703 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
704 unsigned char *curr_secret, *curr_iv, *curr_key;
705
706 /* Rotate the RX secrets */
707 curr_secret = tls_ctx->rx.secret;
708 curr_iv = tls_ctx->rx.iv;
709 curr_key = tls_ctx->rx.key;
710
711 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
712 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
713 tls_ctx->rx.key = qc->ku.nxt_rx.key;
714
715 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
716 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
717 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
718
719 qc->ku.prv_rx.secret = curr_secret;
720 qc->ku.prv_rx.iv = curr_iv;
721 qc->ku.prv_rx.key = curr_key;
722 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
723
724 /* Update the TX secrets */
725 curr_secret = tls_ctx->tx.secret;
726 curr_iv = tls_ctx->tx.iv;
727 curr_key = tls_ctx->tx.key;
728
729 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
730 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
731 tls_ctx->tx.key = qc->ku.nxt_tx.key;
732
733 qc->ku.nxt_tx.secret = curr_secret;
734 qc->ku.nxt_tx.iv = curr_iv;
735 qc->ku.nxt_tx.key = curr_key;
736}
737
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100738#ifndef OPENSSL_IS_BORINGSSL
739int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
740 const uint8_t *read_secret,
741 const uint8_t *write_secret, size_t secret_len)
742{
743 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
744 struct quic_tls_ctx *tls_ctx =
745 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
746 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100747 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100748
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100749 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100750 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100751 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100752 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100753 goto out;
754 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100755
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100756 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100757 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100758 goto err;
759 }
760
761 rx = &tls_ctx->rx;
762 tx = &tls_ctx->tx;
763
764 rx->aead = tx->aead = tls_aead(cipher);
765 rx->md = tx->md = tls_md(cipher);
766 rx->hp = tx->hp = tls_hp(cipher);
767
768 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
769 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100770 read_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100771 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100772 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100773 }
774
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100775 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100776
777 if (!write_secret)
778 goto tp;
779
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100780 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
781 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100782 write_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100783 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100784 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100785 }
786
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100787 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100788 tp:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100789 if (objt_server(conn->target) && level == ssl_encryption_application) {
790 const unsigned char *buf;
791 size_t buflen;
792
793 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
794 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100795 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100796
797 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100798 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100799 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100800
801 if (level == ssl_encryption_application) {
802 struct quic_conn *qc = conn->qc;
803 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
804 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
805 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
806
807 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
808 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100809 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100810 goto err;
811 }
812
813 memcpy(rx->secret, read_secret, secret_len);
814 rx->secretlen = secret_len;
815 memcpy(tx->secret, write_secret, secret_len);
816 tx->secretlen = secret_len;
817 /* Initialize all the secret keys lengths */
818 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
819 /* Prepare the next key update */
820 if (!quic_tls_key_update(qc))
821 goto err;
822 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100823 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100824 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100825 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100826
827 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100828 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100829 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100830}
831#else
832/* ->set_read_secret callback to derive the RX secrets at <level> encryption
833 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500834 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100835 */
836int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
837 const SSL_CIPHER *cipher,
838 const uint8_t *secret, size_t secret_len)
839{
840 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
841 struct quic_tls_ctx *tls_ctx =
842 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
843
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100844 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100845 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100846 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100847 goto out;
848 }
849
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100850 tls_ctx->rx.aead = tls_aead(cipher);
851 tls_ctx->rx.md = tls_md(cipher);
852 tls_ctx->rx.hp = tls_hp(cipher);
853
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100854 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
855 goto err;
856
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100857 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100858 tls_ctx->rx.key, tls_ctx->rx.keylen,
859 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100860 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
861 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100862 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100863 goto err;
864 }
865
866 if (objt_server(conn->target) && level == ssl_encryption_application) {
867 const unsigned char *buf;
868 size_t buflen;
869
870 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
871 if (!buflen)
872 goto err;
873
874 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
875 goto err;
876 }
877
878 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100879 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100880 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100881
882 return 1;
883
884 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100885 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100886 return 0;
887}
888
889/* ->set_write_secret callback to derive the TX secrets at <level>
890 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500891 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100892 */
893int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
894 const SSL_CIPHER *cipher,
895 const uint8_t *secret, size_t secret_len)
896{
897 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
898 struct quic_tls_ctx *tls_ctx =
899 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
900
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100901 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100902 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100903 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100904 goto out;
905 }
906
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100907 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
908 goto err;
909
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100910 tls_ctx->tx.aead = tls_aead(cipher);
911 tls_ctx->tx.md = tls_md(cipher);
912 tls_ctx->tx.hp = tls_hp(cipher);
913
914 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100915 tls_ctx->tx.key, tls_ctx->tx.keylen,
916 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100917 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
918 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100919 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 goto err;
921 }
922
923 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100924 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100925 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100926 return 1;
927
928 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100929 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100930 return 0;
931}
932#endif
933
934/* This function copies the CRYPTO data provided by the TLS stack found at <data>
935 * with <len> as size in CRYPTO buffers dedicated to store the information about
936 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
937 * It fails only if it could not managed to allocate enough CRYPTO buffers to
938 * store all the data.
939 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
940 */
941static int quic_crypto_data_cpy(struct quic_enc_level *qel,
942 const unsigned char *data, size_t len)
943{
944 struct quic_crypto_buf **qcb;
945 /* The remaining byte to store in CRYPTO buffers. */
946 size_t cf_offset, cf_len, *nb_buf;
947 unsigned char *pos;
948
949 nb_buf = &qel->tx.crypto.nb_buf;
950 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
951 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
952 cf_len = len;
953
954 while (len) {
955 size_t to_copy, room;
956
957 pos = (*qcb)->data + (*qcb)->sz;
958 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
959 to_copy = len > room ? room : len;
960 if (to_copy) {
961 memcpy(pos, data, to_copy);
962 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
963 qel->tx.crypto.sz += to_copy;
964 (*qcb)->sz += to_copy;
965 pos += to_copy;
966 len -= to_copy;
967 data += to_copy;
968 }
969 else {
970 struct quic_crypto_buf **tmp;
971
972 tmp = realloc(qel->tx.crypto.bufs,
973 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
974 if (tmp) {
975 qel->tx.crypto.bufs = tmp;
976 qcb = &qel->tx.crypto.bufs[*nb_buf];
977 *qcb = pool_alloc(pool_head_quic_crypto_buf);
978 if (!*qcb)
979 return 0;
980
981 (*qcb)->sz = 0;
982 ++*nb_buf;
983 }
984 else {
985 break;
986 }
987 }
988 }
989
990 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
991 * have been buffered.
992 */
993 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200994 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100995
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200996 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100997 if (!frm)
998 return 0;
999
1000 frm->type = QUIC_FT_CRYPTO;
1001 frm->crypto.offset = cf_offset;
1002 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02001003 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001004 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001005 }
1006
1007 return len == 0;
1008}
1009
1010
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001011/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1012void quic_set_tls_alert(struct quic_conn *qc, int alert)
1013{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001014 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001015 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001016 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001017}
1018
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001019/* Set the application for <qc> QUIC connection.
1020 * Return 1 if succeeded, 0 if not.
1021 */
1022int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1023{
1024 const struct qcc_app_ops *app_ops;
1025
1026 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1027 app_ops = qc->qcc->app_ops = &h3_ops;
1028 }
1029 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1030 app_ops = qc->qcc->app_ops = &hq_interop_ops;
1031 }
1032 else
1033 return 0;
1034
1035 if (app_ops->init && !app_ops->init(qc->qcc))
1036 return 0;
1037
1038 if (app_ops->finalize)
1039 app_ops->finalize(qc->qcc->ctx);
1040
1041 return 1;
1042}
1043
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001044/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1045 * wants to provide the QUIC layer with CRYPTO data.
1046 * Returns 1 if succeeded, 0 if not.
1047 */
1048int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1049 const uint8_t *data, size_t len)
1050{
1051 struct connection *conn;
1052 enum quic_tls_enc_level tel;
1053 struct quic_enc_level *qel;
1054
1055 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001056 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001057 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001058 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001059 goto out;
1060 }
1061
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001062 tel = ssl_to_quic_enc_level(level);
1063 qel = &conn->qc->els[tel];
1064
1065 if (tel == -1) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001066 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001067 goto err;
1068 }
1069
1070 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001071 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001072 goto err;
1073 }
1074
1075 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001076 conn->qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001077
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001078 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001079 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001080 return 1;
1081
1082 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001083 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001084 return 0;
1085}
1086
1087int ha_quic_flush_flight(SSL *ssl)
1088{
1089 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1090
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001091 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc);
1092 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001093
1094 return 1;
1095}
1096
1097int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1098{
1099 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1100
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001101 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001102 quic_set_tls_alert(conn->qc, alert);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001103 HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001104 return 1;
1105}
1106
1107/* QUIC TLS methods */
1108static SSL_QUIC_METHOD ha_quic_method = {
1109#ifdef OPENSSL_IS_BORINGSSL
1110 .set_read_secret = ha_set_rsec,
1111 .set_write_secret = ha_set_wsec,
1112#else
1113 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1114#endif
1115 .add_handshake_data = ha_quic_add_handshake_data,
1116 .flush_flight = ha_quic_flush_flight,
1117 .send_alert = ha_quic_send_alert,
1118};
1119
1120/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1121 * Returns an error count.
1122 */
1123int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1124{
1125 struct proxy *curproxy = bind_conf->frontend;
1126 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1127 int cfgerr = 0;
1128
1129#if 0
1130 /* XXX Did not manage to use this. */
1131 const char *ciphers =
1132 "TLS_AES_128_GCM_SHA256:"
1133 "TLS_AES_256_GCM_SHA384:"
1134 "TLS_CHACHA20_POLY1305_SHA256:"
1135 "TLS_AES_128_CCM_SHA256";
1136#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001137 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001138 long options =
1139 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1140 SSL_OP_SINGLE_ECDH_USE |
1141 SSL_OP_CIPHER_SERVER_PREFERENCE;
1142 SSL_CTX *ctx;
1143
1144 ctx = SSL_CTX_new(TLS_server_method());
1145 bind_conf->initial_ctx = ctx;
1146
1147 SSL_CTX_set_options(ctx, options);
1148#if 0
1149 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1150 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1151 "for bind '%s' at [%s:%d].\n",
1152 curproxy->id, ciphers,
1153 bind_conf->arg, bind_conf->file, bind_conf->line);
1154 cfgerr++;
1155 }
1156#endif
1157
1158 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1159 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1160 "for bind '%s' at [%s:%d].\n",
1161 curproxy->id, groups,
1162 bind_conf->arg, bind_conf->file, bind_conf->line);
1163 cfgerr++;
1164 }
1165
1166 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1167 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1168 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1169 SSL_CTX_set_default_verify_paths(ctx);
1170
1171#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1172#ifdef OPENSSL_IS_BORINGSSL
1173 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1174 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1175#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1176 if (bind_conf->ssl_conf.early_data) {
1177 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001178 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001179 }
1180 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1181 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1182#else
1183 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1184#endif
1185 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1186#endif
1187 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1188
1189 return cfgerr;
1190}
1191
1192/* Decode an expected packet number from <truncated_on> its truncated value,
1193 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1194 * the number of bits used to encode this packet number (its length in bytes * 8).
1195 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1196 */
1197static uint64_t decode_packet_number(uint64_t largest_pn,
1198 uint32_t truncated_pn, unsigned int pn_nbits)
1199{
1200 uint64_t expected_pn = largest_pn + 1;
1201 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1202 uint64_t pn_hwin = pn_win / 2;
1203 uint64_t pn_mask = pn_win - 1;
1204 uint64_t candidate_pn;
1205
1206
1207 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1208 /* Note that <pn_win> > <pn_hwin>. */
1209 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1210 candidate_pn + pn_hwin <= expected_pn)
1211 return candidate_pn + pn_win;
1212
1213 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1214 return candidate_pn - pn_win;
1215
1216 return candidate_pn;
1217}
1218
1219/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1220 * cryptographic context.
1221 * <largest_pn> is the largest received packet number and <pn> the address of
1222 * the packet number field for this packet with <byte0> address of its first byte.
1223 * <end> points to one byte past the end of this packet.
1224 * Returns 1 if succeeded, 0 if not.
1225 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001226static int qc_do_rm_hp(struct quic_conn *qc,
1227 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001228 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001229 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001230{
1231 int ret, outlen, i, pnlen;
1232 uint64_t packet_number;
1233 uint32_t truncated_pn = 0;
1234 unsigned char mask[5] = {0};
1235 unsigned char *sample;
1236 EVP_CIPHER_CTX *cctx;
1237 unsigned char *hp_key;
1238
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001239 /* Check there is enough data in this packet. */
1240 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001241 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001242 return 0;
1243 }
1244
1245 cctx = EVP_CIPHER_CTX_new();
1246 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001247 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001248 return 0;
1249 }
1250
1251 ret = 0;
1252 sample = pn + QUIC_PACKET_PN_MAXLEN;
1253
1254 hp_key = tls_ctx->rx.hp_key;
1255 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1256 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1257 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001258 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001259 goto out;
1260 }
1261
1262 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1263 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1264 for (i = 0; i < pnlen; i++) {
1265 pn[i] ^= mask[i + 1];
1266 truncated_pn = (truncated_pn << 8) | pn[i];
1267 }
1268
1269 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1270 /* Store remaining information for this unprotected header */
1271 pkt->pn = packet_number;
1272 pkt->pnl = pnlen;
1273
1274 ret = 1;
1275
1276 out:
1277 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001278
1279 return ret;
1280}
1281
1282/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1283 * address, with <payload_len> as payload length, <aad> as address of
1284 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1285 * context.
1286 * Returns 1 if succeeded, 0 if not.
1287 */
1288static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1289 unsigned char *aad, size_t aad_len, uint64_t pn,
1290 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1291{
1292 unsigned char iv[12];
1293 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001294 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001295 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001296
1297 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001298 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn->qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001299 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300 }
1301
1302 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1303 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001304 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn->qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001305 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306 }
1307
1308 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001309
1310 err:
1311 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001312 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn->qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001313 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314}
1315
1316/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1317 * Returns 1 if succeeded, 0 if not.
1318 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001319static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001320{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001321 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001322 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001323 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001324 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001325 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1326 unsigned char *rx_key = tls_ctx->rx.key;
1327
1328 kp_changed = 0;
1329 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1330 /* The two tested bits are not at the same position,
1331 * this is why they are first both inversed.
1332 */
1333 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1334 if (pkt->pn < tls_ctx->rx.pn) {
1335 /* The lowest packet number of a previous key phase
1336 * cannot be null if it really stores previous key phase
1337 * secrets.
1338 */
1339 if (!pkt->qc->ku.prv_rx.pn)
1340 return 0;
1341
1342 rx_iv = pkt->qc->ku.prv_rx.iv;
1343 rx_key = pkt->qc->ku.prv_rx.key;
1344 }
1345 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1346 /* Next key phase */
1347 kp_changed = 1;
1348 rx_iv = pkt->qc->ku.nxt_rx.iv;
1349 rx_key = pkt->qc->ku.nxt_rx.key;
1350 }
1351 }
1352 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001353
1354 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1355 return 0;
1356
1357 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1358 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001359 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001360 if (!ret)
1361 return 0;
1362
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001363 /* Update the keys only if the packet decryption succeeded. */
1364 if (kp_changed) {
1365 quic_tls_rotate_keys(pkt->qc);
1366 /* Toggle the Key Phase bit */
1367 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1368 /* Store the lowest packet number received for the current key phase */
1369 tls_ctx->rx.pn = pkt->pn;
1370 /* Prepare the next key update */
1371 if (!quic_tls_key_update(pkt->qc))
1372 return 0;
1373 }
1374
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001375 /* Update the packet length (required to parse the frames). */
1376 pkt->len = pkt->aad_len + ret;
1377
1378 return 1;
1379}
1380
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001381/* Remove from <qcs> stream the acknowledged frames.
1382 * Never fails.
1383 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001384static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001385{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001386 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001387 struct eb64_node *frm_node;
1388
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001389 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001390 frm_node = eb64_first(&qcs->tx.acked_frms);
1391 while (frm_node) {
1392 struct quic_stream *strm;
1393
1394 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1395 if (strm->offset.key != qcs->tx.ack_offset)
1396 break;
1397
1398 b_del(strm->buf, strm->len);
1399 qcs->tx.ack_offset += strm->len;
1400 frm_node = eb64_next(frm_node);
1401 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001402 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001403 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001404
1405 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001406}
1407
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001408/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001409static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1410 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001411{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001412 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001413
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001414 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001415 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001416 switch (frm->type) {
1417 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1418 {
1419 struct qcs *qcs = frm->stream.qcs;
1420 struct quic_stream *strm = &frm->stream;
1421
1422 if (qcs->tx.ack_offset == strm->offset.key) {
1423 b_del(strm->buf, strm->len);
1424 qcs->tx.ack_offset += strm->len;
1425 LIST_DELETE(&frm->list);
1426 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001427 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001428 }
1429 else {
1430 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1431 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001432 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001433 }
1434 break;
1435 default:
1436 LIST_DELETE(&frm->list);
1437 pool_free(pool_head_quic_frame, frm);
1438 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001439
1440 if (stream_acked) {
1441 struct qcc *qcc = qc->qcc;
1442
1443 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1444 tasklet_wakeup(qcc->subs->tasklet);
1445 qcc->subs->events &= ~SUB_RETRY_SEND;
1446 if (!qcc->subs->events)
1447 qcc->subs = NULL;
1448 }
1449 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001450}
1451
1452/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1453 * deallocating them, and their TX frames.
1454 * Returns the last node reached to be used for the next range.
1455 * May be NULL if <largest> node could not be found.
1456 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001457static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1458 struct eb_root *pkts,
1459 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001460 struct list *newly_acked_pkts,
1461 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001462 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001463{
1464 struct eb64_node *node;
1465 struct quic_tx_packet *pkt;
1466
1467 if (largest_node)
1468 node = largest_node;
1469 else {
1470 node = eb64_lookup(pkts, largest);
1471 while (!node && largest > smallest) {
1472 node = eb64_lookup(pkts, --largest);
1473 }
1474 }
1475
1476 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001477 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001478
1479 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1480 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001481 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001482 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001483 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001484 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001485 node = eb64_prev(node);
1486 eb64_delete(&pkt->pn_node);
1487 }
1488
1489 return node;
1490}
1491
1492/* Treat <frm> frame whose packet it is attached to has just been detected as non
1493 * acknowledged.
1494 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001495static inline void qc_treat_nacked_tx_frm(struct quic_conn *qc,
1496 struct quic_frame *frm,
1497 struct quic_pktns *pktns)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001498{
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001499 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001500 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001501 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502}
1503
1504
1505/* Free the TX packets of <pkts> list */
1506static inline void free_quic_tx_pkts(struct list *pkts)
1507{
1508 struct quic_tx_packet *pkt, *tmp;
1509
1510 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001511 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001512 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001513 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001514 }
1515}
1516
1517/* Send a packet loss event nofification to the congestion controller
1518 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1519 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1520 * at <now_us> current time.
1521 * Always succeeds.
1522 */
1523static inline void qc_cc_loss_event(struct quic_conn *qc,
1524 unsigned int lost_bytes,
1525 unsigned int newest_time_sent,
1526 unsigned int period,
1527 unsigned int now_us)
1528{
1529 struct quic_cc_event ev = {
1530 .type = QUIC_CC_EVT_LOSS,
1531 .loss.now_ms = now_ms,
1532 .loss.max_ack_delay = qc->max_ack_delay,
1533 .loss.lost_bytes = lost_bytes,
1534 .loss.newest_time_sent = newest_time_sent,
1535 .loss.period = period,
1536 };
1537
1538 quic_cc_event(&qc->path->cc, &ev);
1539}
1540
1541/* Send a packet ack event nofication for each newly acked packet of
1542 * <newly_acked_pkts> list and free them.
1543 * Always succeeds.
1544 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001545static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001546 struct list *newly_acked_pkts)
1547{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001548 struct quic_tx_packet *pkt, *tmp;
1549 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1550
1551 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1552 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001553 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001554 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001555 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001556 ev.ack.acked = pkt->in_flight_len;
1557 ev.ack.time_sent = pkt->time_sent;
1558 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001559 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001560 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001561 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001562 }
1563
1564}
1565
1566/* Handle <pkts> list of lost packets detected at <now_us> handling
1567 * their TX frames.
1568 * Send a packet loss event to the congestion controller if
1569 * in flight packet have been lost.
1570 * Also frees the packet in <pkts> list.
1571 * Never fails.
1572 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001573static inline void qc_release_lost_pkts(struct quic_conn *qc,
1574 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001575 struct list *pkts,
1576 uint64_t now_us)
1577{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001578 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001579 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 uint64_t lost_bytes;
1581
1582 lost_bytes = 0;
1583 oldest_lost = newest_lost = NULL;
1584 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1585 lost_bytes += pkt->in_flight_len;
1586 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001587 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001588 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001589 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001590 /* Treat the frames of this lost packet. */
1591 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001592 qc_treat_nacked_tx_frm(qc, frm, pktns);
Willy Tarreau2b718102021-04-21 07:32:39 +02001593 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001594 if (!oldest_lost) {
1595 oldest_lost = newest_lost = pkt;
1596 }
1597 else {
1598 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001599 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600 newest_lost = pkt;
1601 }
1602 }
1603
1604 if (lost_bytes) {
1605 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001606 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001607 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001608 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001609 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001610 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001611 }
1612}
1613
1614/* Look for packet loss from sent packets for <qel> encryption level of a
1615 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1616 * their tree if deemed as lost or set the <loss_time> value the packet number
1617 * space if any not deemed lost.
1618 * Should be called after having received an ACK frame with newly acknowledged
1619 * packets or when the the loss detection timer has expired.
1620 * Always succeeds.
1621 */
1622static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1623 struct quic_conn *qc,
1624 struct list *lost_pkts)
1625{
1626 struct eb_root *pkts;
1627 struct eb64_node *node;
1628 struct quic_loss *ql;
1629 unsigned int loss_delay;
1630
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001631 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001632 pkts = &pktns->tx.pkts;
1633 pktns->tx.loss_time = TICK_ETERNITY;
1634 if (eb_is_empty(pkts))
1635 goto out;
1636
1637 ql = &qc->path->loss;
1638 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1639 loss_delay += loss_delay >> 3;
1640 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1641
1642 node = eb64_first(pkts);
1643 while (node) {
1644 struct quic_tx_packet *pkt;
1645 int64_t largest_acked_pn;
1646 unsigned int loss_time_limit, time_sent;
1647
1648 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001649 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001650 node = eb64_next(node);
1651 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1652 break;
1653
1654 time_sent = pkt->time_sent;
1655 loss_time_limit = tick_add(time_sent, loss_delay);
1656 if (tick_is_le(time_sent, now_ms) ||
1657 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1658 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001659 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001660 }
1661 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001662 if (tick_isset(pktns->tx.loss_time))
1663 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1664 else
1665 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001666 }
1667 }
1668
1669 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001670 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001671}
1672
1673/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1674 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001675 * 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 +01001676 * acked ack-eliciting packet.
1677 * Return 1, if succeeded, 0 if not.
1678 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001679static inline int qc_parse_ack_frm(struct quic_conn *qc,
1680 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001681 struct quic_enc_level *qel,
1682 unsigned int *rtt_sample,
1683 const unsigned char **pos, const unsigned char *end)
1684{
1685 struct quic_ack *ack = &frm->ack;
1686 uint64_t smallest, largest;
1687 struct eb_root *pkts;
1688 struct eb64_node *largest_node;
1689 unsigned int time_sent, pkt_flags;
1690 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1691 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1692
1693 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1694 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001695 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001696 goto err;
1697 }
1698
1699 if (ack->first_ack_range > ack->largest_ack) {
1700 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001701 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001702 goto err;
1703 }
1704
1705 largest = ack->largest_ack;
1706 smallest = largest - ack->first_ack_range;
1707 pkts = &qel->pktns->tx.pkts;
1708 pkt_flags = 0;
1709 largest_node = NULL;
1710 time_sent = 0;
1711
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001712 if ((int64_t)ack->largest_ack > HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001713 largest_node = eb64_lookup(pkts, largest);
1714 if (!largest_node) {
1715 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001716 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001717 }
1718 else {
1719 time_sent = eb64_entry(&largest_node->node,
1720 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001721 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001722 }
1723
1724 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001725 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001726 do {
1727 uint64_t gap, ack_range;
1728
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001729 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1730 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001731 if (!ack->ack_range_num--)
1732 break;
1733
1734 if (!quic_dec_int(&gap, pos, end))
1735 goto err;
1736
1737 if (smallest < gap + 2) {
1738 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001739 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001740 goto err;
1741 }
1742
1743 largest = smallest - gap - 2;
1744 if (!quic_dec_int(&ack_range, pos, end))
1745 goto err;
1746
1747 if (largest < ack_range) {
1748 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001749 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001750 goto err;
1751 }
1752
1753 /* Do not use this node anymore. */
1754 largest_node = NULL;
1755 /* Next range */
1756 smallest = largest - ack_range;
1757
1758 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001759 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001760 } while (1);
1761
1762 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001763 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001764
1765 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1766 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001767 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001768 }
1769
1770 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1771 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001772 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001773 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001774 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001775 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001776 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1777 if (quic_peer_validated_addr(qc))
1778 qc->path->loss.pto_count = 0;
1779 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 }
1781
1782
1783 return 1;
1784
1785 err:
1786 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001787 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001788 return 0;
1789}
1790
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001791/* This function gives the detail of the SSL error. It is used only
1792 * if the debug mode and the verbose mode are activated. It dump all
1793 * the SSL error until the stack was empty.
1794 */
1795static forceinline void qc_ssl_dump_errors(struct connection *conn)
1796{
1797 if (unlikely(global.mode & MODE_DEBUG)) {
1798 while (1) {
1799 unsigned long ret;
1800
1801 ret = ERR_get_error();
1802 if (!ret)
1803 return;
1804
1805 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1806 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1807 }
1808 }
1809}
1810
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001811int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1812 const char **str, int *len);
1813
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001814/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1815 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001816 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001817 * Return 1 if succeeded, 0 if not.
1818 */
1819static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001820 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001821 const unsigned char *data, size_t len,
1822 struct quic_rx_packet *pkt,
1823 struct quic_rx_crypto_frm *cf)
1824{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001825 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001826 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001827
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001829 qc = ctx->qc;
1830
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001831 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1832
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001833 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1834 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001835 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001836 goto err;
1837 }
1838
1839 el->rx.crypto.offset += len;
1840 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001841 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001842
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001843 state = HA_ATOMIC_LOAD(&qc->state);
1844 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001845 ssl_err = SSL_do_handshake(ctx->ssl);
1846 if (ssl_err != 1) {
1847 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1848 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1849 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001850 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001851 goto out;
1852 }
1853
1854 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001855 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001856 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001857 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001858 goto err;
1859 }
1860
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001861 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001862 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001863 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001864 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001865 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001866 } else {
1867 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1868 if (ssl_err != 1) {
1869 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1870 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1871 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001872 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001873 goto out;
1874 }
1875
1876 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001877 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001878 goto err;
1879 }
1880
1881 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001882 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001883 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001884
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001885 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001886 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887 return 1;
1888
1889 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001890 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001891 return 0;
1892}
1893
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001894/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1895 * <pkt> RX packet.
1896 * Return it if succeeded, NULL if not.
1897 */
1898static inline
1899struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1900 struct quic_rx_packet *pkt)
1901{
1902 struct quic_rx_strm_frm *frm;
1903
1904 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1905 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001906 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001907 frm->len = stream_frm->len;
1908 frm->data = stream_frm->data;
1909 frm->pkt = pkt;
1910 }
1911
1912 return frm;
1913}
1914
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001915/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001916 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001917 */
1918static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1919{
1920 size_t ret;
1921
1922 ret = 0;
1923 while (strm_frm->len) {
1924 size_t try;
1925
1926 try = b_contig_space(buf);
1927 if (!try)
1928 break;
1929
1930 if (try > strm_frm->len)
1931 try = strm_frm->len;
1932 memcpy(b_tail(buf), strm_frm->data, try);
1933 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001934 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001935 b_add(buf, try);
1936 ret += try;
1937 }
1938
1939 return ret;
1940}
1941
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001942/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1943 * Also update <strm_frm> frame to reflect the data which have been consumed.
1944 */
1945static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1946 struct quic_rx_strm_frm *strm_frm)
1947{
1948 size_t ret;
1949
1950 ret = 0;
1951 while (strm_frm->len) {
1952 size_t try;
1953
1954 try = b_contig_space(buf);
1955 if (!try)
1956 break;
1957
1958 if (try > strm_frm->len)
1959 try = strm_frm->len;
1960 memcpy(b_tail(buf), strm_frm->data, try);
1961 strm_frm->len -= try;
1962 strm_frm->offset_node.key += try;
1963 b_add(buf, try);
1964 ret += try;
1965 }
1966
1967 return ret;
1968}
1969
1970/* Process as much as possible RX STREAM frames received for <qcs> */
1971static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
1972{
1973 int total;
1974 struct eb64_node *frm_node;
1975
1976 total = 0;
1977 frm_node = eb64_first(&qcs->rx.frms);
1978 while (frm_node) {
1979 int ret;
1980 struct quic_rx_strm_frm *frm;
1981
1982 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
1983 if (frm->offset_node.key != qcs->rx.offset)
1984 break;
1985
1986 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
1987 qcs->rx.offset += ret;
1988 total += ret;
1989 if (frm->len) {
1990 /* If there is remaining data in this frame
1991 * this is because the destination buffer is full.
1992 */
1993 break;
1994 }
1995
1996 frm_node = eb64_next(frm_node);
1997 quic_rx_packet_refdec(frm->pkt);
1998 eb64_delete(&frm->offset_node);
1999 pool_free(pool_head_quic_rx_strm_frm, frm);
2000 }
2001
2002 return total;
2003}
2004
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002005/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2006 * streams may be open. The data are copied to the stream RX buffer if possible.
2007 * If not, the STREAM frame is stored to be treated again later.
2008 * We rely on the flow control so that not to store too much STREAM frames.
2009 * Return 1 if succeeded, 0 if not.
2010 */
2011static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2012 struct quic_stream *strm_frm,
2013 struct quic_conn *qc)
2014{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002015 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002016 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002017 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002018 struct quic_rx_strm_frm *frm;
2019
2020 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2021 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002022 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002023 return 0;
2024 }
2025
2026 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002027 if (strm_frm->offset.key < strm->rx.offset) {
2028 size_t diff;
2029
2030 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2031 TRACE_PROTO("Already received STREAM data",
2032 QUIC_EV_CONN_PSTRM, qc);
2033 goto out;
2034 }
2035
2036 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2037 diff = strm->rx.offset - strm_frm->offset.key;
2038 strm_frm->offset.key = strm->rx.offset;
2039 strm_frm->len -= diff;
2040 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002041 }
2042
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002043 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002044 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002045 int ret;
2046
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002047 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002048 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002049 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002050
2051 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002052 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002053 strm->rx.offset += ret;
2054 }
2055
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002056 total += qc_treat_rx_strm_frms(strm);
2057 if (total && qc->qcc->app_ops->decode_qcs(strm, strm_frm->fin, qc->qcc->ctx) < 0) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002058 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002059 return 0;
2060 }
2061
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002062 if (!strm_frm->len)
2063 goto out;
2064
2065 store_frm:
2066 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2067 if (!frm) {
2068 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002069 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002070 return 0;
2071 }
2072
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002073 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002074 quic_rx_packet_refinc(pkt);
2075
2076 out:
2077 return 1;
2078}
2079
2080/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2081 * streams may be open. The data are copied to the stream RX buffer if possible.
2082 * If not, the STREAM frame is stored to be treated again later.
2083 * We rely on the flow control so that not to store too much STREAM frames.
2084 * Return 1 if succeeded, 0 if not.
2085 */
2086static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2087 struct quic_stream *strm_frm,
2088 struct quic_conn *qc)
2089{
2090 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002091 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002092 struct quic_rx_strm_frm *frm;
2093 size_t strm_frm_len;
2094
2095 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2096 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002097 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002098 return 0;
2099 }
2100
2101 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002102 if (strm_frm->offset.key < strm->rx.offset) {
2103 size_t diff;
2104
2105 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2106 TRACE_PROTO("Already received STREAM data",
2107 QUIC_EV_CONN_PSTRM, qc);
2108 goto out;
2109 }
2110
2111 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2112 diff = strm->rx.offset - strm_frm->offset.key;
2113 strm_frm->offset.key = strm->rx.offset;
2114 strm_frm->len -= diff;
2115 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002116 }
2117
2118 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002119 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002120 int ret;
2121
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002122 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002123 goto store_frm;
2124
2125 /* qc_strm_cpy() will modify the offset, depending on the number
2126 * of bytes copied.
2127 */
2128 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2129 /* Inform the application of the arrival of this new stream */
2130 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002131 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002132 return 0;
2133 }
2134
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002135 if (ret)
2136 qcs_notify_recv(strm);
2137
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002138 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002139 }
2140 /* Take this frame into an account for the stream flow control */
2141 strm->rx.offset += strm_frm_len;
2142 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002143 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002144 */
2145 if (!strm_frm->len)
2146 goto out;
2147
2148 store_frm:
2149 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2150 if (!frm) {
2151 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002152 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002153 return 0;
2154 }
2155
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002156 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002157 quic_rx_packet_refinc(pkt);
2158
2159 out:
2160 return 1;
2161}
2162
2163static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2164 struct quic_stream *strm_frm,
2165 struct quic_conn *qc)
2166{
2167 if (strm_frm->id & QCS_ID_DIR_BIT)
2168 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2169 else
2170 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2171}
2172
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002173/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2174 * as I/O handler context and <qel> as encryption level.
2175 * Returns 1 if succeeded, 0 if failed.
2176 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002177static 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 +01002178 struct quic_enc_level *qel)
2179{
2180 struct quic_frame frm;
2181 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002182 struct quic_conn *qc = ctx->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002183
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002184 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002185 /* Skip the AAD */
2186 pos = pkt->data + pkt->aad_len;
2187 end = pkt->data + pkt->len;
2188
2189 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002190 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191 goto err;
2192
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002193 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002194 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002195 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002196 break;
2197 case QUIC_FT_PING:
2198 break;
2199 case QUIC_FT_ACK:
2200 {
2201 unsigned int rtt_sample;
2202
2203 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002204 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002205 goto err;
2206
2207 if (rtt_sample) {
2208 unsigned int ack_delay;
2209
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002210 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002211 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2212 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2213 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002214 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002215 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002216 break;
2217 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002218 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002219 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002220 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002221 {
2222 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002223
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002224 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2225 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2226 /* XXX TO DO: <cfdebug> is used only for the traces. */
2227 struct quic_rx_crypto_frm cfdebug = { };
2228
2229 cfdebug.offset_node.key = frm.crypto.offset;
2230 cfdebug.len = frm.crypto.len;
2231 /* Nothing to do */
2232 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002233 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002234 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002235 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002236 else {
2237 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2238 /* XXX TO DO: <cfdebug> is used only for the traces. */
2239 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002240
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002241 cfdebug.offset_node.key = frm.crypto.offset;
2242 cfdebug.len = frm.crypto.len;
2243 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002244 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002245 frm.crypto.len -= diff;
2246 frm.crypto.data += diff;
2247 frm.crypto.offset = qel->rx.crypto.offset;
2248 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002250
2251 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002252 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002253 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002254
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002255 cfdebug.offset_node.key = frm.crypto.offset;
2256 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002257 if (!qc_provide_cdata(qel, ctx,
2258 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002259 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002260 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002261
2262 break;
2263 }
2264
2265 /* frm.crypto.offset > qel->rx.crypto.offset */
2266 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2267 if (!cf) {
2268 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002269 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002270 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002271 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002272
2273 cf->offset_node.key = frm.crypto.offset;
2274 cf->len = frm.crypto.len;
2275 cf->data = frm.crypto.data;
2276 cf->pkt = pkt;
2277 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2278 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002279 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002280 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002281 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002282 {
2283 struct quic_stream *stream = &frm.stream;
2284
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002285 if (objt_listener(ctx->conn->target)) {
2286 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2287 goto err;
2288 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2289 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002290
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002291 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002292 goto err;
2293
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002294 break;
2295 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002296 case QUIC_FT_MAX_DATA:
2297 case QUIC_FT_MAX_STREAM_DATA:
2298 case QUIC_FT_MAX_STREAMS_BIDI:
2299 case QUIC_FT_MAX_STREAMS_UNI:
2300 case QUIC_FT_DATA_BLOCKED:
2301 case QUIC_FT_STREAM_DATA_BLOCKED:
2302 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2303 case QUIC_FT_STREAMS_BLOCKED_UNI:
2304 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002305 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002306 break;
2307 case QUIC_FT_CONNECTION_CLOSE:
2308 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002309 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002310 qc->qcc->flags |= QC_CF_CC_RECV;
2311 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002313 case QUIC_FT_HANDSHAKE_DONE:
2314 if (objt_listener(ctx->conn->target))
2315 goto err;
2316
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002317 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 break;
2319 default:
2320 goto err;
2321 }
2322 }
2323
2324 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2325 * has successfully parse a Handshake packet. The Initial encryption must also
2326 * be discarded.
2327 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002328 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002329 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002330
2331 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002332 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002333 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002334 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002335 qc_set_timer(ctx->qc);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002336 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002337 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002338 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002339 }
2340
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002341 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002342 return 1;
2343
2344 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002345 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002346 return 0;
2347}
2348
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002349/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002350 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002351 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2352 * This function must be called only after having built a correct datagram.
2353 * Always succeeds.
2354 */
2355static inline void qc_set_dg(struct cbuf *cbuf,
2356 uint16_t dglen, struct quic_tx_packet *pkt)
2357{
2358 write_u16(cb_wr(cbuf), dglen);
2359 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2360 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2361}
2362
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002363/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002364 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2365 * several packets in the same datagram. A header made of two fields is added
2366 * to each datagram: the datagram length followed by the address of the first
2367 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002368 * Returns 1 if succeeded, or 0 if something wrong happened.
2369 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002370static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002371{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002372 enum quic_tls_enc_level tel, next_tel;
2373 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002374 struct cbuf *cbuf;
2375 unsigned char *end_buf, *end, *pos, *spos;
2376 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2377 /* length of datagrams */
2378 uint16_t dglen;
2379 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002380 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002381 /* Each datagram is prepended with its length followed by the
2382 * address of the first packet in the datagram.
2383 */
2384 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002385
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002386 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2387
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01002388 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state), 0)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002389 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002390 goto err;
2391 }
2392
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002393 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002394 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002395 total = 0;
2396 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002397 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002398 cbuf = qr->cbuf;
2399 spos = pos = cb_wr(cbuf);
2400 /* Leave at least <dglen> bytes at the end of this buffer
2401 * to ensure there is enough room to mark the end of prepared
2402 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002403 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002404 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2405 first_pkt = prv_pkt = NULL;
2406 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002407 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002408 enum quic_pkt_type pkt_type;
2409
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002410 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002411 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002412 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2413 if (!cc) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002414 if (!prv_pkt)
2415 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002416 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002417 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002418 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002419 * if there is nothing to send, i.e. if no CONNECTION_CLOSE or ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002420 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002421 * and if there is no more CRYPTO data available or in flight
2422 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002423 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002424 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002425 (!cc && !ack && !probe &&
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002426 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2427 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002428 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002429 /* Set the current datagram as prepared into <cbuf> if
2430 * the was already a correct packet which was previously written.
2431 */
2432 if (prv_pkt)
2433 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002434 break;
2435 }
2436
2437 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002438 if (!prv_pkt) {
2439 /* Leave room for the datagram header */
2440 pos += dg_headlen;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002441 if (!quic_peer_validated_addr(qc) && objt_listener(qc->conn->target)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002442 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
2443 qc->flags |= QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED;
2444 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2445 }
2446 else {
2447 end = pos + qc->path->mtu;
2448 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002449 }
2450
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002451 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002452 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002453 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002454 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002455 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002456 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002457 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002458 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002459 case -2:
2460 goto err;
2461 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002462 /* If there was already a correct packet present, set the
2463 * current datagram as prepared into <cbuf>.
2464 */
2465 if (prv_pkt) {
2466 qc_set_dg(cbuf, dglen, first_pkt);
2467 goto stop_build;
2468 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002469 goto out;
2470 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002471 break;
2472 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002473
Frédéric Lécaille63556772021-12-29 17:18:21 +01002474 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2475 if (!cur_pkt)
2476 goto err;
2477
2478 total += cur_pkt->len;
2479 /* keep trace of the first packet in the datagram */
2480 if (!first_pkt)
2481 first_pkt = cur_pkt;
2482 /* Attach the current one to the previous one */
2483 if (prv_pkt)
2484 prv_pkt->next = cur_pkt;
2485 /* Let's say we have to build a new dgram */
2486 prv_pkt = NULL;
2487 dglen += cur_pkt->len;
2488 /* Client: discard the Initial encryption keys as soon as
2489 * a handshake packet could be built.
2490 */
2491 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2492 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2493 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2494 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2495 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2496 qc_set_timer(qc);
2497 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2498 }
2499 /* If the data for the current encryption level have all been sent,
2500 * select the next level.
2501 */
2502 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
2503 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms))) {
2504 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2505 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2506 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2507 tel = next_tel;
2508 qel = &qc->els[tel];
2509 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
2510 /* If there is data for the next level, do not
2511 * consume a datagram.
2512 */
2513 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002515 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002516 /* If we have to build a new datagram, set the current datagram as
2517 * prepared into <cbuf>.
2518 */
2519 if (!prv_pkt) {
2520 qc_set_dg(cbuf, dglen, first_pkt);
2521 first_pkt = NULL;
2522 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002523 padding = 0;
2524 }
2525 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
2526 (objt_server(qc->conn->target) ||
2527 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2528 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002529 }
2530 }
2531
2532 stop_build:
2533 /* Reset <wr> writer index if in front of <rd> index */
2534 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2535 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2536
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002537 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002538 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2539 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2540 /* Mark the end of contiguous data for the reader */
2541 write_u16(cb_wr(cbuf), 0);
2542 cb_add(cbuf, sizeof(uint16_t));
2543 }
2544 }
2545
2546 if (rd && rd <= cbuf->wr) {
2547 cb_wr_reset(cbuf);
2548 if (pos == spos) {
2549 /* Reuse the same buffer if nothing was built. */
2550 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002551 }
2552 }
2553 }
2554
2555 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002556 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002557 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002558
2559 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002560 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002561 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562}
2563
2564/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002565 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002566 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002567int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002568{
2569 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002570 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002571
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002572 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002573 cbuf = qr->cbuf;
2574 while (cb_contig_data(cbuf)) {
2575 unsigned char *pos;
2576 struct buffer tmpbuf = { };
2577 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2578 uint16_t dglen;
2579 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002580 unsigned int time_sent;
2581
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002582 pos = cb_rd(cbuf);
2583 dglen = read_u16(pos);
2584 /* End of prepared datagrams.
2585 * Reset the reader index only if in front of the writer index.
2586 */
2587 if (!dglen) {
2588 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2589
2590 if (wr && wr < cbuf->rd) {
2591 cb_rd_reset(cbuf);
2592 continue;
2593 }
2594 break;
2595 }
2596
2597 pos += sizeof dglen;
2598 first_pkt = read_ptr(pos);
2599 pos += sizeof first_pkt;
2600 tmpbuf.area = (char *)pos;
2601 tmpbuf.size = tmpbuf.data = dglen;
2602
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002603 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002604 for (pkt = first_pkt; pkt; pkt = pkt->next)
2605 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002606 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002607 &tmpbuf, tmpbuf.data, 0) <= 0) {
2608 for (pkt = first_pkt; pkt; pkt = pkt->next)
2609 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002610 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002611 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002613 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002614 qc->tx.bytes += tmpbuf.data;
2615 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002616
2617 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2618 pkt->time_sent = time_sent;
2619 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2620 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002621 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002622 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002623 qc->path->in_flight += pkt->in_flight_len;
2624 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2625 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002626 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002627 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002628 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002629 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002630 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 }
2632 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002633
2634 return 1;
2635}
2636
2637/* Build all the frames which must be sent just after the handshake have succeeded.
2638 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2639 * a HANDSHAKE_DONE frame.
2640 * Return 1 if succeeded, 0 if not.
2641 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002642static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002643{
2644 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002645 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002646 struct quic_frame *frm;
2647
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002648 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002649 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002650 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002651 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002652 if (!frm)
2653 return 0;
2654
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002655 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002656 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657 }
2658
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002659 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002660 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002661 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002662
2663 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002664 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665 goto err;
2666
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002667 cid = new_quic_cid(&qc->cids, qc, i);
2668 if (!cid)
2669 goto err;
2670
2671 /* insert the allocated CID in the receiver tree */
2672 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2673 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2674 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2675
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002676 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002677 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002678 }
2679
2680 return 1;
2681
2682 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002683 return 0;
2684}
2685
2686/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002687void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002688{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002689 struct eb64_node *n;
2690 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002691
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002692 n = eb64_first(&arngs->root);
2693 while (n) {
2694 struct eb64_node *next;
2695
2696 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2697 next = eb64_next(n);
2698 eb64_delete(n);
2699 free(ar);
2700 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002701 }
2702}
2703
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002704/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2705 * descending order.
2706 */
2707static inline size_t sack_gap(struct quic_arng_node *p,
2708 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002709{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002710 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002711}
2712
2713
2714/* Remove the last elements of <ack_ranges> list of ack range updating its
2715 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002716 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002717 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002718static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002719{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002720 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002721
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002722 last = eb64_last(&arngs->root);
2723 while (last && arngs->enc_sz > limit) {
2724 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002725
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002726 prev = eb64_prev(last);
2727 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002728 return 0;
2729
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002730 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2731 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2732 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2733 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2734 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2735 --arngs->sz;
2736 eb64_delete(last);
2737 pool_free(pool_head_quic_arng, last);
2738 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002739 }
2740
2741 return 1;
2742}
2743
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002744/* Set the encoded size of <arngs> QUIC ack ranges. */
2745static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2746{
2747 struct eb64_node *node, *next;
2748 struct quic_arng_node *ar, *ar_next;
2749
2750 node = eb64_last(&arngs->root);
2751 if (!node)
2752 return;
2753
2754 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2755 arngs->enc_sz = quic_int_getsize(ar->last) +
2756 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2757
2758 while ((next = eb64_prev(node))) {
2759 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2760 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2761 quic_int_getsize(ar_next->last - ar_next->first.key);
2762 node = next;
2763 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2764 }
2765}
2766
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002767/* Insert <ar> ack range into <argns> tree of ack ranges.
2768 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002769 */
2770static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002771struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2772 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002773{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002774 struct quic_arng_node *new_ar;
2775
2776 new_ar = pool_alloc(pool_head_quic_arng);
2777 if (new_ar) {
2778 new_ar->first.key = ar->first;
2779 new_ar->last = ar->last;
2780 eb64_insert(&arngs->root, &new_ar->first);
2781 arngs->sz++;
2782 }
2783
2784 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002785}
2786
2787/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002789 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002790 *
2791 * Descending order
2792 * ------------->
2793 * range1 range2
2794 * ..........|--------|..............|--------|
2795 * ^ ^ ^ ^
2796 * | | | |
2797 * last1 first1 last2 first2
2798 * ..........+--------+--------------+--------+......
2799 * diff1 gap12 diff2
2800 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002801 * To encode the previous list of ranges we must encode integers as follows in
2802 * descending order:
2803 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002804 * with diff1 = last1 - first1
2805 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002806 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002807 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002808 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002809int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2810 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002811{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002812 struct eb64_node *le;
2813 struct quic_arng_node *new_node;
2814 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002815
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002816 new = NULL;
2817 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002818 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002819 if (!new_node)
2820 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002821
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002822 goto out;
2823 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002824
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002825 le = eb64_lookup_le(&arngs->root, ar->first);
2826 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002827 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002828 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002830
2831 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002832 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002833 else {
2834 struct quic_arng_node *le_ar =
2835 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002836
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002837 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002838 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002839 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002840
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002841 if (le_ar->last + 1 >= ar->first) {
2842 le_ar->last = ar->last;
2843 new = le;
2844 new_node = le_ar;
2845 }
2846 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002847 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002848 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002849 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002850
2851 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002853 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002854
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002855 /* Verify that the new inserted node does not overlap the nodes
2856 * which follow it.
2857 */
2858 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002859 struct eb64_node *next;
2860 struct quic_arng_node *next_node;
2861
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002862 while ((next = eb64_next(new))) {
2863 next_node =
2864 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002865 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002866 break;
2867
2868 if (next_node->last > new_node->last)
2869 new_node->last = next_node->last;
2870 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002871 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002872 /* Decrement the size of these ranges. */
2873 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002874 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002875 }
2876
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002877 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002878 quic_arngs_set_enc_sz(arngs);
2879
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002880 return 1;
2881}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002882/* Remove the header protection of packets at <el> encryption level.
2883 * Always succeeds.
2884 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002885static 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 +01002886{
2887 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002888 struct quic_rx_packet *pqpkt;
2889 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890 struct quic_enc_level *app_qel;
2891
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002892 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
2893 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002895 if (el == app_qel && objt_listener(qc->conn->target) &&
2896 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002897 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002898 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899 goto out;
2900 }
2901 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002902 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002903 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002904 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002905 pqpkt->data, pqpkt->data + pqpkt->len)) {
2906 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002907 /* XXX TO DO XXX */
2908 }
2909 else {
2910 /* The AAD includes the packet number field */
2911 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2912 /* Store the packet into the tree of packets to decrypt. */
2913 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002914 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002915 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2916 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002917 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002918 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002919 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002920 MT_LIST_DELETE_SAFE(pkttmp1);
2921 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922 }
2923
2924 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002925 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002926}
2927
2928/* Process all the CRYPTO frame at <el> encryption level.
2929 * Return 1 if succeeded, 0 if not.
2930 */
2931static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002932 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002933{
2934 struct eb64_node *node;
2935
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002936 node = eb64_first(&el->rx.crypto.frms);
2937 while (node) {
2938 struct quic_rx_crypto_frm *cf;
2939
2940 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2941 if (cf->offset_node.key != el->rx.crypto.offset)
2942 break;
2943
2944 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2945 goto err;
2946
2947 node = eb64_next(node);
2948 quic_rx_packet_refdec(cf->pkt);
2949 eb64_delete(&cf->offset_node);
2950 pool_free(pool_head_quic_rx_crypto_frm, cf);
2951 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002952 return 1;
2953
2954 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002955 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002956 return 0;
2957}
2958
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002959/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002960 * 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 +02002961 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002962 * Return 1 if succeeded, 0 if not.
2963 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002964int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2965 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002966{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002967 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002968 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002969 struct quic_conn *qc = ctx->conn->qc;
2970 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002971
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002972 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002973 qel = cur_el;
2974 next_tel:
2975 if (!qel)
2976 goto out;
2977
2978 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2979 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 while (node) {
2981 struct quic_rx_packet *pkt;
2982
2983 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002984 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002985 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01002986 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987 /* Drop the packet */
2988 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002989 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990 }
2991 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002992 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 /* Drop the packet */
2994 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002995 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996 }
2997 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002998 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2999
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003000 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003001 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003002 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003003 if (pkt->pn > largest_pn)
3004 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003005 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003006 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003008 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003009 }
3010 }
3011 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003012 eb64_delete(&pkt->pn_node);
3013 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003015 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003016
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003017 /* Update the largest packet number. */
3018 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003019 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3020 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 goto err;
3022
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003023 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003024 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003025 qel = next_el;
3026 goto next_tel;
3027 }
3028
3029 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003030 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003031 return 1;
3032
3033 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003034 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003035 return 0;
3036}
3037
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003038/* QUIC connection packet handler task. */
3039struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003041 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003042 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003043 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 enum quic_tls_enc_level tel, next_tel;
3045 struct quic_enc_level *qel, *next_qel;
3046 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003047 struct qring *qr; // Tx ring
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003048 int prev_st, st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003049
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003050 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003051 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003052 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003053 st = HA_ATOMIC_LOAD(&qc->state);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003054 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003056 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3057 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3058 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003059 start:
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003060 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003061 goto err;
3062
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003063 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003064 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065
3066 next_level:
3067 tls_ctx = &qel->tls_ctx;
3068
3069 /* If the header protection key for this level has been derived,
3070 * remove the packet header protections.
3071 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003072 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003073 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003074 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003075
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003076 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003077 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3078 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3079 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080 goto err;
3081
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003082 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3083 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3084 qel = next_qel;
3085 next_qel = NULL;
3086 goto next_level;
3087 }
3088
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003089 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02003090 if (st >= QUIC_HS_ST_COMPLETE &&
3091 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003092 /* Discard the Handshake keys. */
3093 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003094 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003095 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003096 qc_set_timer(qc);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003097 if (!quic_build_post_handshake_frames(qc))
3098 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003099
3100 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
3101 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003102 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003103 }
3104
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003105 if (!qr)
3106 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003107 ret = qc_prep_pkts(qc, qr);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003108 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003109 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003110 else if (ret == 0)
3111 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003112
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003113 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114 goto err;
3115
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003116 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003117 /* Check if there is something to do for the next level.
3118 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003119 if (next_qel && next_qel != qel &&
3120 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003121 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003122 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003123 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003124 goto next_level;
3125 }
3126
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003127 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003128 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003129 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130
3131 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003132 if (qr)
3133 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003134 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003135 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003136}
3137
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003138/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003139static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3140{
3141 int i;
3142
3143 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3144 if (qel->tx.crypto.bufs[i]) {
3145 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3146 qel->tx.crypto.bufs[i] = NULL;
3147 }
3148 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003149 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003150}
3151
3152/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003153 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003154 * Returns 1 if succeeded, 0 if not.
3155 */
3156static int quic_conn_enc_level_init(struct quic_conn *qc,
3157 enum quic_tls_enc_level level)
3158{
3159 struct quic_enc_level *qel;
3160
3161 qel = &qc->els[level];
3162 qel->level = quic_to_ssl_enc_level(level);
3163 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3164 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3165 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3166 qel->tls_ctx.rx.flags = 0;
3167 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003168 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003169
3170 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003171 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003172 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003173 qel->rx.crypto.offset = 0;
3174 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175
3176 /* Allocate only one buffer. */
3177 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3178 if (!qel->tx.crypto.bufs)
3179 goto err;
3180
3181 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3182 if (!qel->tx.crypto.bufs[0])
3183 goto err;
3184
3185 qel->tx.crypto.bufs[0]->sz = 0;
3186 qel->tx.crypto.nb_buf = 1;
3187
3188 qel->tx.crypto.sz = 0;
3189 qel->tx.crypto.offset = 0;
3190
3191 return 1;
3192
3193 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003194 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003195 return 0;
3196}
3197
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003198/* Increment the <qc> refcount.
3199 *
3200 * This operation must be conducted when manipulating the quic_conn outside of
3201 * the connection pinned thread. These threads can only retrieve the connection
3202 * in the CID tree, so this function must be conducted under the CID lock.
3203 */
3204static inline void quic_conn_take(struct quic_conn *qc)
3205{
3206 HA_ATOMIC_INC(&qc->refcount);
3207}
3208
3209/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
3210 * substraction, the quic_conn is freed.
3211 */
3212static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003214 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215 int i;
3216
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003217 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003218 return;
3219
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003220 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3221 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1)) {
3222 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3223 return;
3224 }
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003225
3226 /* remove the connection from receiver cids trees */
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003227 ebmb_delete(&qc->odcid_node);
3228 ebmb_delete(&qc->scid_node);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003229 free_quic_conn_cids(qc);
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003230 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003231
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003232 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
3233 if (conn_ctx) {
3234 tasklet_free(conn_ctx->wait_event.tasklet);
3235 conn_ctx->wait_event.tasklet = NULL;
3236
3237 pool_free(pool_head_quic_conn_ctx, conn_ctx);
3238 }
3239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003240 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003241 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003242
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003243 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3244 pool_free(pool_head_quic_conn, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003245}
3246
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003247void quic_close(struct connection *conn, void *xprt_ctx)
3248{
3249 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3250 struct quic_conn *qc = conn_ctx->conn->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003251
3252 /* This task must be deleted by the connection-pinned thread. */
3253 if (qc->timer_task) {
3254 task_destroy(qc->timer_task);
3255 qc->timer_task = NULL;
3256 }
3257
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003258 quic_conn_drop(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003259}
3260
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003261/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003262static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003263{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003264 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003265 struct quic_conn *qc;
3266 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003267 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003268
3269 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003270 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003271 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003272 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003273 task->expire = TICK_ETERNITY;
3274 pktns = quic_loss_pktns(qc);
3275 if (tick_isset(pktns->tx.loss_time)) {
3276 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3277
3278 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3279 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003280 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3281 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003282 goto out;
3283 }
3284
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003285 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003286 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003287 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003288 pktns->tx.pto_probe = 1;
3289 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003290 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003291 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3292 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3293
3294 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3295 hel->pktns->tx.pto_probe = 1;
3296 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3297 iel->pktns->tx.pto_probe = 1;
3298 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003299 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3300 qc->path->loss.pto_count++;
3301
3302 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003303 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003304
3305 return task;
3306}
3307
3308/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3309 * connections used to identify the first Initial packets of client connecting
3310 * to listeners. This parameter must be NULL for QUIC connections attached
3311 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3312 * <scid> is the source connection ID with <scid_len> as length.
3313 * Returns 1 if succeeded, 0 if not.
3314 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003315static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003316 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003317 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003318{
3319 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003320 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321 /* Initial CID. */
3322 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003323 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003324 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003325
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003326 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003327 qc = pool_zalloc(pool_head_quic_conn);
3328 if (!qc) {
3329 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3330 goto err;
3331 }
3332
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003333 HA_ATOMIC_STORE(&qc->refcount, 0);
3334
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003335 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3336 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003337 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003338 goto err;
3339 }
3340
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003341 qc->cids = EB_ROOT;
3342 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003343 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003344 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003345
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003346 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003347 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003349 qc->odcid.addrlen = dcid_addr_len;
3350 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003351
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003352 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003353 if (scid_len)
3354 memcpy(qc->dcid.data, scid, scid_len);
3355 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003356 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003357 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003358 }
3359 /* QUIC Client (outgoing connection to servers) */
3360 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003361 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003362 if (dcid_len)
3363 memcpy(qc->dcid.data, dcid, dcid_len);
3364 qc->dcid.len = dcid_len;
3365 }
3366
3367 /* Initialize the output buffer */
3368 qc->obuf.pos = qc->obuf.data;
3369
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003370 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003371 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003372 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003373 goto err;
3374 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003375
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003376 /* insert the allocated CID in the receiver tree */
3377 if (server) {
3378 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3379 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3380 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3381 }
3382
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383 /* Select our SCID which is the first CID with 0 as sequence number. */
3384 qc->scid = icid->cid;
3385
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003386 /* Packet number spaces initialization. */
3387 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3388 quic_pktns_init(&qc->pktns[i]);
3389 /* QUIC encryption level context initialization. */
3390 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003391 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003392 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003393 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003394 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003395 /* Initialize the packet number space. */
3396 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3397 }
3398
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003399 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003400 qc->tps_tls_ext = qc->version & 0xff000000 ?
3401 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3402 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003403 /* TX part. */
3404 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003405 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3406 qc->tx.wbuf = qc->tx.rbuf = 0;
3407 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003408 /* RX part. */
3409 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003410 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003411 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3412 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3413 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003414 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003415 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003416 goto err;
3417 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003418
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003419 /* XXX TO DO: Only one path at this time. */
3420 qc->path = &qc->paths[0];
3421 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3422
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003423 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003424
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003425 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003426
3427 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003428 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003429 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003430 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003431}
3432
3433/* Initialize the timer task of <qc> QUIC connection.
3434 * Returns 1 if succeeded, 0 if not.
3435 */
3436static int quic_conn_init_timer(struct quic_conn *qc)
3437{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003438 /* Attach this task to the same thread ID used for the connection */
3439 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003440 if (!qc->timer_task)
3441 return 0;
3442
3443 qc->timer = TICK_ETERNITY;
3444 qc->timer_task->process = process_timer;
3445 qc->timer_task->context = qc->conn->xprt_ctx;
3446
3447 return 1;
3448}
3449
3450/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3451 * past one byte of this buffer.
3452 */
3453static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3454 struct quic_rx_packet *pkt)
3455{
3456 unsigned char dcid_len, scid_len;
3457
3458 /* Version */
3459 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3460 return 0;
3461
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003462 /* Destination Connection ID Length */
3463 dcid_len = *(*buf)++;
3464 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3465 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3466 /* XXX MUST BE DROPPED */
3467 return 0;
3468
3469 if (dcid_len) {
3470 /* Check that the length of this received DCID matches the CID lengths
3471 * of our implementation for non Initials packets only.
3472 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003473 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3474 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003475 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003476 return 0;
3477
3478 memcpy(pkt->dcid.data, *buf, dcid_len);
3479 }
3480
3481 pkt->dcid.len = dcid_len;
3482 *buf += dcid_len;
3483
3484 /* Source Connection ID Length */
3485 scid_len = *(*buf)++;
3486 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3487 /* XXX MUST BE DROPPED */
3488 return 0;
3489
3490 if (scid_len)
3491 memcpy(pkt->scid.data, *buf, scid_len);
3492 pkt->scid.len = scid_len;
3493 *buf += scid_len;
3494
3495 return 1;
3496}
3497
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003498/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3499 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3500 * encryption level matching with the packet type. <*qel> may be null if not found.
3501 * Note that <ctx> may be null (for Initial packets).
3502 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003503static int qc_pkt_may_rm_hp(struct quic_conn *qc, struct quic_rx_packet *pkt,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003504 struct quic_enc_level **qel)
3505{
3506 enum quic_tls_enc_level tel;
3507
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003508 tel = quic_packet_type_enc_level(pkt->type);
3509 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3510 *qel = NULL;
3511 return 0;
3512 }
3513
3514 *qel = &qc->els[tel];
3515 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003516 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003517 return 0;
3518 }
3519
3520 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003521 (tel != QUIC_TLS_ENC_LEVEL_APP ||
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003522 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003523 return 1;
3524
3525 return 0;
3526}
3527
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003528/* Insert <pkt> RX packet in its <qel> RX packets tree */
3529static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3530{
3531 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003532 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003533 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3534 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3535 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003536}
3537
3538/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003539 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3540 * byte past the end of the buffer containing this packet and <beg> the address of
3541 * the packet first byte.
3542 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3543 * Returns 1 if succeeded, 0 if not.
3544 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003545static inline int qc_try_rm_hp(struct quic_conn *qc,
3546 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003547 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003549 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003550{
3551 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003552 struct quic_enc_level *qel;
3553 /* Only for traces. */
3554 struct quic_rx_packet *qpkt_trace;
3555
3556 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003557 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 /* The packet number is here. This is also the start minus
3559 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3560 * protection.
3561 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003562 pn = buf;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003563 if (qc_pkt_may_rm_hp(qc, pkt, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003564 /* Note that the following function enables us to unprotect the packet
3565 * number and its length subsequently used to decrypt the entire
3566 * packets.
3567 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003568 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3569 qel->pktns->rx.largest_pn, pn, beg, end)) {
3570 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003571 goto err;
3572 }
3573
3574 /* The AAD includes the packet number field found at <pn>. */
3575 pkt->aad_len = pn - beg + pkt->pnl;
3576 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003577 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003578 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003579 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3580 /* If the packet number space has been discarded, this packet
3581 * will be not parsed.
3582 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003583 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003584 goto out;
3585 }
3586
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003587 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003588 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003589 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3590 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003591 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003592 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003593 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003594 goto err;
3595 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003596
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003597 *el = qel;
3598 /* No reference counter incrementation here!!! */
3599 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3600 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3601 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3602 b_add(&qc->rx.buf, pkt->len);
3603 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003604 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003605 return 1;
3606
3607 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003608 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 return 0;
3610}
3611
3612/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3613 * Also set <*long_header> to 1 if this form is long, 0 if not.
3614 */
3615static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3616 unsigned char byte0, int *long_header)
3617{
3618 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3619 pkt->type =
3620 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3621 *long_header = 1;
3622 }
3623 else {
3624 pkt->type = QUIC_PACKET_TYPE_SHORT;
3625 *long_header = 0;
3626 }
3627}
3628
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003629/*
3630 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3631 */
3632static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3633{
3634 int j = 0, version;
3635
3636 do {
3637 version = quic_supported_version[j];
3638 if (version == pkt->version)
3639 return 1;
3640
3641 version = quic_supported_version[++j];
3642 } while(version);
3643
3644 return 0;
3645}
3646
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003647__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003648static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3649 struct quic_rx_packet *pkt,
3650 struct quic_dgram_ctx *dgram_ctx,
3651 struct sockaddr_storage *saddr)
3652{
3653 unsigned char *beg;
3654 uint64_t len;
3655 struct quic_conn *qc;
3656 struct eb_root *cids;
3657 struct ebmb_node *node;
3658 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003659 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003661 size_t b_cspace;
3662 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003663
3664 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003665 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003666 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3667 if (end <= *buf)
3668 goto err;
3669
3670 /* Fixed bit */
3671 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3672 /* XXX TO BE DISCARDED */
3673 goto err;
3674
3675 srv_conn = dgram_ctx->owner;
3676 beg = *buf;
3677 /* Header form */
3678 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3679 if (long_header) {
3680 size_t cid_lookup_len;
3681
3682 if (!quic_packet_read_long_header(buf, end, pkt))
3683 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003684
3685 /* unsupported QUIC version */
3686 if (!qc_pkt_is_supported_version(pkt)) {
3687 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3688 goto err;
3689 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003690
3691 /* For Initial packets, and for servers (QUIC clients connections),
3692 * there is no Initial connection IDs storage.
3693 */
3694 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3695 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3696 cid_lookup_len = pkt->dcid.len;
3697 }
3698 else {
3699 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003700 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003701 }
3702
3703 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3704 if (!node)
3705 goto err;
3706
3707 qc = ebmb_entry(node, struct quic_conn, scid_node);
3708
3709 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3710 qc->dcid.len = pkt->scid.len;
3711 if (pkt->scid.len)
3712 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3713 }
3714
3715 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3716 uint64_t token_len;
3717
3718 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3719 goto err;
3720
3721 /* XXX TO DO XXX 0 value means "the token is not present".
3722 * A server which sends an Initial packet must not set the token.
3723 * So, a client which receives an Initial packet with a token
3724 * MUST discard the packet or generate a connection error with
3725 * PROTOCOL_VIOLATION as type.
3726 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3727 */
3728 pkt->token_len = token_len;
3729 }
3730 }
3731 else {
3732 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003733 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003734 goto err;
3735
3736 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003737 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003738 if (!node)
3739 goto err;
3740
3741 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003742 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003743 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003744
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003745 /* Only packets packets with long headers and not RETRY or VERSION as type
3746 * have a length field.
3747 */
3748 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3749 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3750 goto err;
3751
3752 pkt->len = len;
3753 }
3754 else if (!long_header) {
3755 /* A short packet is the last one of an UDP datagram. */
3756 pkt->len = end - *buf;
3757 }
3758
3759 conn_ctx = qc->conn->xprt_ctx;
3760
3761 /* Increase the total length of this packet by the header length. */
3762 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003763
3764 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3765 * they must have the same DCID.
3766 *
3767 * This check must be done after the final update to pkt.len to
3768 * properly drop the packet on failure.
3769 */
3770 if (!dgram_ctx->dcid.len) {
3771 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3772 }
3773 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003774 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003775 goto err;
3776 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003777 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003778
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003779 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3780 b_cspace = b_contig_space(&qc->rx.buf);
3781 if (b_cspace < pkt->len) {
3782 /* Let us consume the remaining contiguous space. */
3783 b_add(&qc->rx.buf, b_cspace);
3784 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3785 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003786 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003787 goto err;
3788 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789 }
3790
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003791 if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003792 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003793 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003795 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003796
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003797 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3798 if (pkt->aad_len)
3799 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003800 /* Wake the tasklet of the QUIC connection packet handler. */
3801 if (conn_ctx)
3802 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3803
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003804 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003805
3806 return pkt->len;
3807
3808 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003809 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003810 return -1;
3811}
3812
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003813/*
3814 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3815 * address <addr>.
3816 * Implementation of RFC9000 6. Version Negotiation
3817 *
3818 * TODO implement a rate-limiting sending of Version Negotiation packets
3819 *
3820 * Returns 0 on success else non-zero
3821 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003822static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
3823 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003824{
3825 char buf[256];
3826 int i = 0, j, version;
3827 const socklen_t addrlen = get_addr_len(addr);
3828
3829 /*
3830 * header form
3831 * long header, fixed bit to 0 for Version Negotiation
3832 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003833 if (RAND_bytes((unsigned char *)buf, 1) != 1)
3834 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003835
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01003836 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003837 /* null version for Version Negotiation */
3838 buf[i++] = '\x00';
3839 buf[i++] = '\x00';
3840 buf[i++] = '\x00';
3841 buf[i++] = '\x00';
3842
3843 /* source connection id */
3844 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003845 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003846 i += pkt->scid.len;
3847
3848 /* destination connection id */
3849 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003850 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003851 i += pkt->dcid.len;
3852
3853 /* supported version */
3854 j = 0;
3855 do {
3856 version = htonl(quic_supported_version[j]);
3857 memcpy(&buf[i], &version, sizeof(version));
3858 i += sizeof(version);
3859
3860 version = quic_supported_version[++j];
3861 } while (version);
3862
3863 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3864 return 1;
3865
3866 return 0;
3867}
3868
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003869/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
3870 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
3871 * concatenated to the <pkt> DCID field.
3872 *
3873 * Returns the instance or NULL if not found.
3874 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003875static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003876 struct listener *l,
3877 struct sockaddr_storage *saddr)
3878{
3879 struct quic_conn *qc = NULL;
3880 struct ebmb_node *node;
3881 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01003882 /* set if the quic_conn is found in the second DCID tree */
3883 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003884
3885 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
3886
3887 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
3888 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
3889 pkt->type == QUIC_PACKET_TYPE_0RTT) {
3890 /* DCIDs of first packets coming from multiple clients may have
3891 * the same values. Let's distinguish them by concatenating the
3892 * socket addresses.
3893 */
3894 quic_cid_saddr_cat(&pkt->dcid, saddr);
3895 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
3896 pkt->dcid.len + pkt->dcid.addrlen);
3897 if (node) {
3898 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3899 goto end;
3900 }
3901 }
3902
3903 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
3904 * also for INITIAL/0-RTT non-first packets with the final DCID in
3905 * used.
3906 */
3907 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
3908 if (!node)
3909 goto end;
3910
3911 id = ebmb_entry(node, struct quic_connection_id, node);
3912 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01003913 found_in_dcid = 1;
3914
3915 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003916 if (qc)
3917 quic_conn_take(qc);
Amaury Denoyelle250ac422021-12-22 11:29:05 +01003918 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003919
3920 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
3921 * If already done, this is a noop.
Amaury Denoyelle250ac422021-12-22 11:29:05 +01003922 *
3923 * node.leaf_p is first checked to avoid unnecessary locking.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003924 */
Amaury Denoyellec6fab982021-12-23 16:27:56 +01003925 if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) {
Amaury Denoyelle250ac422021-12-22 11:29:05 +01003926 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3927 ebmb_delete(&qc->odcid_node);
3928 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3929 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003930
3931 return qc;
3932}
3933
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003934static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003935 struct quic_rx_packet *pkt,
3936 struct quic_dgram_ctx *dgram_ctx,
3937 struct sockaddr_storage *saddr)
3938{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003939 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003940 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003941 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003942 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003943 int long_header = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003944 size_t b_cspace;
3945 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003946
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003947 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003948 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003949 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003950 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01003951 TRACE_ENTER(QUIC_EV_CONN_LPKT);
3952 /* This ist only to please to traces and distinguish the
3953 * packet with parsed packet number from others.
3954 */
3955 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003956 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957 goto err;
3958
3959 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003960 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003961 /* XXX TO BE DISCARDED */
3962 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3963 goto err;
3964 }
3965
3966 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003967 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003968 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003969 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01003970 uint64_t len;
3971
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003972 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003973 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3974 goto err;
3975 }
3976
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01003977 /* Retry of Version Negotiation packets are only sent by servers */
3978 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
3979 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3980 goto err;
3981 }
3982
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003983 /* RFC9000 6. Version Negotiation */
3984 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003985 /* unsupported version, send Negotiation packet */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01003986 if (send_version_negotiation(l->rx.fd, saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003987 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
3988 goto err;
3989 }
3990
3991 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003992 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003993 }
3994
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003995 /* For Initial packets, and for servers (QUIC clients connections),
3996 * there is no Initial connection IDs storage.
3997 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01003998 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003999 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004000
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004001 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4002 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004003 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4004 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004005 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004006
4007 /* XXX TO DO XXX 0 value means "the token is not present".
4008 * A server which sends an Initial packet must not set the token.
4009 * So, a client which receives an Initial packet with a token
4010 * MUST discard the packet or generate a connection error with
4011 * PROTOCOL_VIOLATION as type.
4012 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4013 */
4014 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004015 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004016 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004017 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004018 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4019 goto err;
4020 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021 }
4022
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004023 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4024 end - buf < len) {
4025 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4026 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004027 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004028
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004029 payload = buf;
4030 pkt->len = len + payload - beg;
4031
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004032 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004033 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004034 int ipv4;
4035 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004036 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004037 const unsigned char *salt = initial_salt_v1;
4038 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004039
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004040 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004041 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004042 goto err;
4043 }
4044
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004045 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004046 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004047 qc = qc_new_conn(pkt->version, ipv4,
4048 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004049 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004050 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004051 goto err;
4052
4053 odcid = &qc->rx.params.original_destination_connection_id;
4054 /* Copy the transport parameters. */
4055 qc->rx.params = l->bind_conf->quic_params;
4056 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004057 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4058 odcid->len = pkt->dcid.len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004059 /* Copy the initial source connection ID. */
4060 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4061 qc->enc_params_len =
4062 quic_transport_params_encode(qc->enc_params,
4063 qc->enc_params + sizeof qc->enc_params,
4064 &qc->rx.params, 1);
4065 if (!qc->enc_params_len)
4066 goto err;
4067
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004068 /* NOTE: the socket address has been concatenated to the destination ID
4069 * chosen by the client for Initial packets.
4070 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004071 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4072 salt = initial_salt_draft_29;
4073 salt_len = sizeof initial_salt_draft_29;
4074 }
4075 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004076 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004077 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004078 goto err;
4079 }
4080
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004081 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004082 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004083 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4084 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004085
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004086 /* If the insertion failed, it means that another
4087 * thread has already allocated a QUIC connection for
4088 * the same CID. Liberate our allocated connection.
4089 */
4090 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004091 qc_to_purge = qc;
4092
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004093 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4094 pkt->qc = qc;
4095 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004096
4097 quic_conn_take(qc);
4098 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4099
4100 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004101 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004102 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004103 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4104 /* Try to accept a new connection. */
4105 listener_accept(l);
4106 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004107 else {
4108 quic_conn_drop(qc_to_purge);
4109 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004110 }
4111 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004112 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004113 }
4114 }
4115 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004116 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004117 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4118 goto err;
4119 }
4120
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004121 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004122 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004123 buf += QUIC_HAP_CID_LEN;
4124
4125 /* A short packet is the last one of a UDP datagram. */
4126 payload = buf;
4127 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004128
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004129 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004130 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004131 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004132 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4133 goto err;
4134 }
4135
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004136 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004137 }
4138
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004139
4140 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4141 * they must have the same DCID.
4142 *
4143 * This check must be done after the final update to pkt.len to
4144 * properly drop the packet on failure.
4145 */
4146 if (!dgram_ctx->dcid.len) {
4147 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
4148 }
4149 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004150 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004151 goto err;
4152 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004153 dgram_ctx->qc = qc;
4154
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004155
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004156 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004157 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004158 goto out;
4159 }
4160
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004161 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004162 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004163 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004164 b_cspace = b_contig_space(&qc->rx.buf);
4165 if (b_cspace < pkt->len) {
4166 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004167 if (b_cspace) {
4168 b_putchr(&qc->rx.buf, 0x00);
4169 b_cspace--;
4170 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004171 b_add(&qc->rx.buf, b_cspace);
4172 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4173 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004174 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004175 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004176 goto err;
4177 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004178 }
4179
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004180 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004181 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004182 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004183 goto err;
4184 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004185
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004186 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004187 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004188 if (pkt->aad_len)
4189 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004190 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004191 /* Wake up the connection packet handler task from here only if all
4192 * the contexts have been initialized, especially the mux context
4193 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4194 * will start it if these contexts for the connection are not already
4195 * initialized.
4196 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004197 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4198 if (conn_ctx && conn_ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004200
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004201 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004202
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004203 if (qc)
4204 quic_conn_drop(qc);
4205
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004206 return pkt->len;
4207
4208 err:
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004209 /* If length not found, consume the entire packet */
4210 if (!pkt->len)
4211 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004212 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004213 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004214
4215 if (qc)
4216 quic_conn_drop(qc);
4217
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004218 return -1;
4219}
4220
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004221/* This function builds into <buf> buffer a QUIC long packet header.
4222 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004223 */
4224static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4225 int type, size_t pn_len, struct quic_conn *conn)
4226{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004227 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004228 return 0;
4229
4230 /* #0 byte flags */
4231 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4232 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4233 /* Version */
4234 quic_write_uint32(buf, end, conn->version);
4235 *(*buf)++ = conn->dcid.len;
4236 /* Destination connection ID */
4237 if (conn->dcid.len) {
4238 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4239 *buf += conn->dcid.len;
4240 }
4241 /* Source connection ID */
4242 *(*buf)++ = conn->scid.len;
4243 if (conn->scid.len) {
4244 memcpy(*buf, conn->scid.data, conn->scid.len);
4245 *buf += conn->scid.len;
4246 }
4247
4248 return 1;
4249}
4250
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004251/* This function builds into <buf> buffer a QUIC short packet header.
4252 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004254static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4255 size_t pn_len, struct quic_conn *conn,
4256 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004257{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004258 if (end - *buf < 1 + conn->dcid.len)
4259 return 0;
4260
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004261 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004262 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4263 ((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 +01004264 /* Destination connection ID */
4265 if (conn->dcid.len) {
4266 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4267 *buf += conn->dcid.len;
4268 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004269
4270 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004271}
4272
4273/* Apply QUIC header protection to the packet with <buf> as first byte address,
4274 * <pn> as address of the Packet number field, <pnlen> being this field length
4275 * with <aead> as AEAD cipher and <key> as secret key.
4276 * Returns 1 if succeeded or 0 if failed.
4277 */
4278static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4279 const EVP_CIPHER *aead, const unsigned char *key)
4280{
4281 int i, ret, outlen;
4282 EVP_CIPHER_CTX *ctx;
4283 /* We need an IV of at least 5 bytes: one byte for bytes #0
4284 * and at most 4 bytes for the packet number
4285 */
4286 unsigned char mask[5] = {0};
4287
4288 ret = 0;
4289 ctx = EVP_CIPHER_CTX_new();
4290 if (!ctx)
4291 return 0;
4292
4293 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4294 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4295 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4296 goto out;
4297
4298 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4299 for (i = 0; i < pnlen; i++)
4300 pn[i] ^= mask[i + 1];
4301
4302 ret = 1;
4303
4304 out:
4305 EVP_CIPHER_CTX_free(ctx);
4306
4307 return ret;
4308}
4309
4310/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4311 * ACK ranges if needed to a value below <limit> in bytes.
4312 * Return 1 if succeeded, 0 if not.
4313 */
4314static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4315{
4316 size_t room, ack_delay_sz;
4317
4318 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4319 /* A frame is made of 1 byte for the frame type. */
4320 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004321 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322 return 0;
4323
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004324 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004325}
4326
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004327/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4328 * for <qel> encryption level to be encoded in a buffer with <room> as available room,
Frédéric Lécailleea604992020-12-24 13:01:37 +01004329 * and <*len> the packet Length field initialized with the number of bytes already present
4330 * in this buffer which must be taken into an account for the Length packet field value.
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004331 * <headlen> is the number of bytes already present in this packet before building frames.
4332 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004333 * Update consequently <*len> to reflect the size of these frames built
4334 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004335 * Return 1 if succeeded, 0 if not.
4336 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004337static inline int qc_build_frms(struct quic_tx_packet *pkt,
4338 size_t room, size_t *len, size_t headlen,
4339 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004340 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004341{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004342 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004343 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004344 struct mt_list *tmp1, tmp2;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004345 size_t remain = quic_path_prep_data(qc->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004346
Frédéric Lécailleea604992020-12-24 13:01:37 +01004347 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004348 if (*len > room || headlen > remain)
4349 return 0;
4350
Frédéric Lécailleea604992020-12-24 13:01:37 +01004351 /* If we are not probing we must take into an account the congestion
4352 * control window.
4353 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004354 if (!qel->pktns->tx.pto_probe)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004355 room = QUIC_MIN(room, quic_path_prep_data(qc->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004356 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004357 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004358 mt_list_for_each_entry_safe(cf, &qel->pktns->tx.frms, mt_list, tmp1, tmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004359 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004360 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004361
Frédéric Lécailleea604992020-12-24 13:01:37 +01004362 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004363 break;
4364
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004365 switch (cf->type) {
4366 case QUIC_FT_CRYPTO:
4367 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004368 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004369 /* Compute the length of this CRYPTO frame header */
4370 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4371 /* Compute the data length of this CRyPTO frame. */
4372 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4373 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004374 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004375 if (!dlen)
4376 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004377
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004378 /* CRYPTO frame length. */
4379 flen = hlen + quic_int_getsize(dlen) + dlen;
4380 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004381 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004382 /* Add the CRYPTO data length and its encoded length to the packet
4383 * length and the length of this length.
4384 */
4385 *len += flen;
4386 room -= flen;
4387 if (dlen == cf->crypto.len) {
4388 /* <cf> CRYPTO data have been consumed. */
4389 MT_LIST_DELETE_SAFE(tmp1);
4390 LIST_APPEND(&pkt->frms, &cf->list);
4391 }
4392 else {
4393 struct quic_frame *new_cf;
4394
4395 new_cf = pool_alloc(pool_head_quic_frame);
4396 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004397 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004398 return 0;
4399 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004400
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004401 new_cf->type = QUIC_FT_CRYPTO;
4402 new_cf->crypto.len = dlen;
4403 new_cf->crypto.offset = cf->crypto.offset;
4404 new_cf->crypto.qel = qel;
4405 LIST_APPEND(&pkt->frms, &new_cf->list);
4406 /* Consume <dlen> bytes of the current frame. */
4407 cf->crypto.len -= dlen;
4408 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004409 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004410 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004411
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004412 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004413 /* Note that these frames are accepted in short packets only without
4414 * "Length" packet field. Here, <*len> is used only to compute the
4415 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004416 *
4417 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004418 * excepting the variable ones. Note that +1 is for the type of this frame.
4419 */
4420 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004421 ((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 +02004422 /* Compute the data length of this STREAM frame. */
4423 avail_room = room - hlen - *len;
4424 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004425 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004426
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004427 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004428 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004429 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4430 dlen = max_available_room(avail_room, &dlen_sz);
4431 if (dlen > cf->stream.len) {
4432 dlen = cf->stream.len;
4433 }
4434 dlen_sz = quic_int_getsize(dlen);
4435 flen = hlen + dlen_sz + dlen;
4436 }
4437 else {
4438 dlen = QUIC_MIN(avail_room, cf->stream.len);
4439 flen = hlen + dlen;
4440 }
4441 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004442 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004443 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004444 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004445 /* Add the STREAM data length and its encoded length to the packet
4446 * length and the length of this length.
4447 */
4448 *len += flen;
4449 room -= flen;
4450 if (dlen == cf->stream.len) {
4451 /* <cf> STREAM data have been consumed. */
4452 MT_LIST_DELETE_SAFE(tmp1);
4453 LIST_APPEND(&pkt->frms, &cf->list);
4454 }
4455 else {
4456 struct quic_frame *new_cf;
4457
4458 new_cf = pool_zalloc(pool_head_quic_frame);
4459 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004460 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004461 return 0;
4462 }
4463
4464 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004465 new_cf->stream.qcs = cf->stream.qcs;
4466 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004467 new_cf->stream.id = cf->stream.id;
4468 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4469 new_cf->stream.offset = cf->stream.offset;
4470 new_cf->stream.len = dlen;
4471 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4472 /* FIN bit reset */
4473 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4474 new_cf->stream.data = cf->stream.data;
4475 LIST_APPEND(&pkt->frms, &new_cf->list);
4476 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4477 /* Consume <dlen> bytes of the current frame. */
4478 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004479 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004480 cf->stream.data += dlen;
4481 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004482 break;
4483
4484 default:
4485 flen = qc_frm_len(cf);
4486 BUG_ON(!flen);
4487 if (flen > room)
4488 continue;
4489
4490 *len += flen;
4491 room -= flen;
4492 MT_LIST_DELETE_SAFE(tmp1);
4493 LIST_APPEND(&pkt->frms, &cf->list);
4494 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004495 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004496 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004497 }
4498
Frédéric Lécailleea604992020-12-24 13:01:37 +01004499 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004500}
4501
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004502/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004503 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4504 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4505 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004506 * 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 +02004507 * 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 +02004508 * having returned from this function.
4509 * 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 +01004510 * number field in this packet. <pn_len> will also have the packet number
4511 * length as value.
4512 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004513 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004514 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004515static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4516 size_t dglen, struct quic_tx_packet *pkt,
4517 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4518 int ack, int padding, int cc, int nb_pto_dgrams,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004519 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004520{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004521 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004522 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4524 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004525 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004526 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004527 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004528 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004529
Frédéric Lécailleea604992020-12-24 13:01:37 +01004530 /* Length field value with CRYPTO frames if present. */
4531 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004532 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004533 /* When not probing and not acking, and no immediate close is required,
4534 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004535 * the congestion controller window. So, we do not limit the size of this
4536 * packet if we have an ACK frame to send because an ACK frame is not
4537 * ack-eliciting. This size will be limited if we have ack-eliciting
4538 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004539 */
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004540 if (!nb_pto_dgrams && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004541 size_t path_room;
4542
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004543 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004544 if (end - beg > path_room)
4545 end = beg + path_room;
4546 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004547
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004548 /* Ensure there is enough room for the TLS encryption tag and a zero token
4549 * length field if any.
4550 */
4551 if (end - pos < QUIC_TLS_TAG_LEN +
4552 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4553 goto no_room;
4554
4555 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004556 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004557 /* packet number length */
4558 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004559 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004560 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004561 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004562 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004563 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004564 goto no_room;
4565
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004566 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4567 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004568 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004569 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004570 /* Build an ACK frame if required. */
4571 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004572 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004573 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004574 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004575 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4576 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4577 * that from here, we do not know if we will have to send a PING frame.
4578 * This will be decided after having computed the ack-eliciting frames
4579 * to be added to this packet.
4580 */
4581 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 +01004582 if (!ack_frm_len)
4583 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004584 }
4585
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004586 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004587 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004588 len_frms = 0;
4589 if (!cc && !MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004590 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004591
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004592 /* Initialize the length of the frames built below to <len>.
4593 * If any frame could be successfully built by qc_build_frms(),
4594 * we will have len_frms > len.
4595 */
4596 len_frms = len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004597 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004598 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004599 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004600 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004601 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004602
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004603 /* Length (of the remaining data). Must not fail because, the buffer size
4604 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4605 * for the encryption tag. It must be taken into an account for the length
4606 * of this packet.
4607 */
4608 if (len_frms)
4609 len = len_frms + QUIC_TLS_TAG_LEN;
4610 else
4611 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004612 /* CONNECTION_CLOSE frame */
4613 if (cc) {
4614 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004615
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004616 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004617 len += qc_frm_len(&cc_frm);
4618 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004619 add_ping_frm = 0;
4620 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004621 len_sz = quic_int_getsize(len);
4622 /* Add this packet size to <dglen> */
4623 dglen += head_len + len_sz + len;
4624 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4625 /* This is a maximum padding size */
4626 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4627 /* The length field value is of this packet is <len> + <padding_len>
4628 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004629 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004630 * sizes from <padding_len>.
4631 */
4632 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4633 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004634 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004635 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004636 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004637 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004638 add_ping_frm = 1;
4639 len += 1;
4640 }
4641 /* 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 +01004642 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004643 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4644 }
4645
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004646 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
4647 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004648
4649 /* Packet number field address. */
4650 *buf_pn = pos;
4651
4652 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004653 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
4654 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004655
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004656 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004657 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004658
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004659 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004660 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004661 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004662
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004663 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004664 list_for_each_entry(cf, &pkt->frms, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004665 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004666 ssize_t room = end - pos;
4667 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004668 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004669 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004670 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004671 }
4672 }
4673
4674 /* Build a PING frame if needed. */
4675 if (add_ping_frm) {
4676 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004677 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004678 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004679 }
4680
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004681 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004682 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004683 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004684
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004685 /* Build a PADDING frame if needed. */
4686 if (padding_len) {
4687 frm.type = QUIC_FT_PADDING;
4688 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004689 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004690 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004691 }
4692
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004693 /* If this packet is ack-eliciting and we are probing let's
4694 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004695 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01004696 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
4697 qel->pktns->tx.pto_probe)
4698 qel->pktns->tx.pto_probe--;
4699
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004700 pkt->len = pos - beg;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004701
4702 return 1;
4703
4704 no_room:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004705 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004706 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004707}
4708
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004709static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004710{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004711 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004712 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004713 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01004714 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004715 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004716 pkt->next = NULL;
4717 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004718}
4719
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004720/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4722{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004723 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004724
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004725 if (!pkt)
4726 return;
4727
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004728 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004729 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004730 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004731 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004732 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004733}
4734
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004735/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4736 * type for <qc> QUIC connection from <qel> encryption level.
4737 * Return -2 if the packet could not be allocated or encrypted for any reason,
4738 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004739 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004740static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4741 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004742 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004743 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004744 int pkt_type, int ack, int nb_pto_dgrams, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004745{
4746 /* The pointer to the packet number field. */
4747 unsigned char *buf_pn;
4748 unsigned char *beg, *end, *payload;
4749 int64_t pn;
4750 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004751 struct quic_tls_ctx *tls_ctx;
4752 struct quic_tx_packet *pkt;
4753
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004754 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004755 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004756 pkt = pool_alloc(pool_head_quic_tx_packet);
4757 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004758 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004759 *err = -2;
4760 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004761 }
4762
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004763 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004764 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004765 pn_len = 0;
4766 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004767
4768 pn = qel->pktns->tx.next_pn + 1;
4769 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
4770 ack, padding, cc, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004771 *err = -1;
4772 goto err;
4773 }
4774
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004775 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004776 payload = buf_pn + pn_len;
4777 payload_len = end - payload;
4778 aad_len = payload - beg;
4779
4780 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004781 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4782 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004783 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004784 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004785
4786 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004787 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004788 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4789 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004790 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004791 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004792 goto err;
4793 }
4794
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004795 /* Consume a packet number */
4796 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01004797 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004798 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4799 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004800 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004801 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004802 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004803 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004804 pkt->in_flight_len = pkt->len;
4805 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004806 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004807 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004808 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004809
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004810 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004811
4812 err:
4813 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004814 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004815 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004816}
4817
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004818/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4819 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004820 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004821static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4822 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004823{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004824 size_t try, done = 0;
4825
4826 if (!conn_ctrl_ready(conn))
4827 return 0;
4828
4829 if (!fd_recv_ready(conn->handle.fd))
4830 return 0;
4831
4832 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004833
4834 /* read the largest possible block. For this, we perform only one call
4835 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004836 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004837 */
4838 while (count > 0) {
4839 try = b_contig_space(buf);
4840 if (!try)
4841 break;
4842
4843 if (try > count)
4844 try = count;
4845
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004846 b_add(buf, try);
4847 done += try;
4848 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004849 }
4850
4851 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4852 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4853
4854 leave:
4855 return done;
4856
4857 read0:
4858 conn_sock_read0(conn);
4859 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4860
4861 /* Now a final check for a possible asynchronous low-level error
4862 * report. This can happen when a connection receives a reset
4863 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4864 * we might have come from there by just checking POLL_HUP instead
4865 * of recv()'s return value 0, so we have no way to tell there was
4866 * an error without checking.
4867 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004868 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004869 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4870 goto leave;
4871}
4872
4873
4874/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4875 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4876 * other pending data for example, but this flag is ignored at the moment.
4877 * Only one call to send() is performed, unless the buffer wraps, in which case
4878 * a second call may be performed. The connection's flags are updated with
4879 * whatever special event is detected (error, empty). The caller is responsible
4880 * for taking care of those events and avoiding the call if inappropriate. The
4881 * function does not call the connection's polling update function, so the caller
4882 * is responsible for this. It's up to the caller to update the buffer's contents
4883 * based on the return value.
4884 */
4885static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4886{
4887 ssize_t ret;
4888 size_t try, done;
4889 int send_flag;
4890
4891 if (!conn_ctrl_ready(conn))
4892 return 0;
4893
4894 if (!fd_send_ready(conn->handle.fd))
4895 return 0;
4896
4897 done = 0;
4898 /* send the largest possible block. For this we perform only one call
4899 * to send() unless the buffer wraps and we exactly fill the first hunk,
4900 * in which case we accept to do it once again.
4901 */
4902 while (count) {
4903 try = b_contig_data(buf, done);
4904 if (try > count)
4905 try = count;
4906
4907 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4908 if (try < count || flags & CO_SFL_MSG_MORE)
4909 send_flag |= MSG_MORE;
4910
4911 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4912 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4913 if (ret > 0) {
4914 count -= ret;
4915 done += ret;
4916
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004917 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004918 conn->flags |= CO_FL_WAIT_L4L6;
4919 /* if the system buffer is full, don't insist */
4920 if (ret < try)
4921 break;
4922 }
4923 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4924 /* nothing written, we need to poll for write first */
4925 fd_cant_send(conn->handle.fd);
4926 break;
4927 }
4928 else if (errno != EINTR) {
4929 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4930 break;
4931 }
4932 }
4933 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4934 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4935
4936 if (done > 0) {
4937 /* we count the total bytes sent, and the send rate for 32-byte
4938 * blocks. The reason for the latter is that freq_ctr are
4939 * limited to 4GB and that it's not enough per second.
4940 */
4941 _HA_ATOMIC_ADD(&global.out_bytes, done);
4942 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4943 }
4944 return done;
4945}
4946
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004947/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4948 * event subscriber <es> is not allowed to change from a previous call as long
4949 * as at least one event is still subscribed. The <event_type> must only be a
4950 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4951 */
4952static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4953{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004954 struct qcc *qcc = conn->qc->qcc;
4955
4956 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4957 BUG_ON(qcc->subs && qcc->subs != es);
4958
4959 es->events |= event_type;
4960 qcc->subs = es;
4961
4962 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004963 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004964
4965 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004966 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004967
4968 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004969}
4970
4971/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4972 * The <es> pointer is not allowed to differ from the one passed to the
4973 * subscribe() call. It always returns zero.
4974 */
4975static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4976{
4977 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4978}
4979
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004980/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4981 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4982 * parameters of this session.
4983 * This is the responsibility of the caller to check the validity of all the
4984 * pointers passed as parameter to this function.
4985 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4986 * CO_ER_SSL_NO_MEM.
4987 */
4988static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4989 unsigned char *params, size_t params_len)
4990{
4991 int retry;
4992
4993 retry = 1;
4994 retry:
4995 *ssl = SSL_new(ssl_ctx);
4996 if (!*ssl) {
4997 if (!retry--)
4998 goto err;
4999
5000 pool_gc(NULL);
5001 goto retry;
5002 }
5003
5004 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5005 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
5006 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5007 goto err;
5008
5009 SSL_free(*ssl);
5010 *ssl = NULL;
5011 if (!retry--)
5012 goto err;
5013
5014 pool_gc(NULL);
5015 goto retry;
5016 }
5017
5018 return 0;
5019
5020 err:
5021 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5022 return -1;
5023}
5024
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005025/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5026 * connection with <xprt_ctx> as address of the xprt context.
5027 * Returns 1 if succeeded, 0 if not.
5028 */
5029static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5030{
Amaury Denoyelle9979d0d2021-12-23 16:32:24 +01005031 struct ssl_sock_ctx *ctx = NULL;
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005032 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005033
5034 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5035
5036 if (*xprt_ctx)
5037 goto out;
5038
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005039 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005040 if (!ctx) {
5041 conn->err_code = CO_ER_SYS_MEMLIM;
5042 goto err;
5043 }
5044
5045 ctx->wait_event.tasklet = tasklet_new();
5046 if (!ctx->wait_event.tasklet) {
5047 conn->err_code = CO_ER_SYS_MEMLIM;
5048 goto err;
5049 }
5050
5051 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5052 ctx->wait_event.tasklet->context = ctx;
5053 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005054 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005055 ctx->subs = NULL;
5056 ctx->xprt_ctx = NULL;
5057
5058 ctx->xprt = xprt_get(XPRT_QUIC);
5059 if (objt_server(conn->target)) {
5060 /* Server */
5061 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005062 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005063 int ssl_err, ipv4;
5064
5065 ssl_err = SSL_ERROR_NONE;
5066 if (RAND_bytes(dcid, sizeof dcid) != 1)
5067 goto err;
5068
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005069 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005070 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005071 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005072 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005073 goto err;
5074
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005075 ctx->qc = qc;
5076
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005077 /* Insert our SCID, the connection ID for the QUIC client. */
5078 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5079
5080 conn->qc = qc;
5081 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005082 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5083 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005084 goto err;
5085
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005086 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005087 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005088 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5089 qc->enc_params_len =
5090 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5091 &qc->rx.params, 0);
5092 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005093 goto err;
5094
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005095 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5096 qc->enc_params, qc->enc_params_len) == -1)
5097 goto err;
5098
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005099 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005100 SSL_set_connect_state(ctx->ssl);
5101 ssl_err = SSL_do_handshake(ctx->ssl);
5102 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005103 int st;
5104
5105 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005106 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5107 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005108 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005109 }
5110 else {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005111 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005112 goto err;
5113 }
5114 }
5115 }
5116 else if (objt_listener(conn->target)) {
5117 /* Listener */
5118 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
5119
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005120 qc = ctx->conn->qc;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005121 ctx->qc = qc;
5122
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005123 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005124 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5125 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005126 goto err;
5127
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005128 /* Enabling 0-RTT */
5129 if (bc->ssl_conf.early_data)
5130 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5131
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005132 SSL_set_accept_state(ctx->ssl);
5133 }
5134
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005135 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005136
5137 /* Leave init state and start handshake */
5138 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005139
5140 out:
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005141 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005142 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005143
5144 return 0;
5145
5146 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005147 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005148 tasklet_free(ctx->wait_event.tasklet);
5149 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005150 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005151 return -1;
5152}
5153
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005154/* Start the QUIC transport layer */
5155static int qc_xprt_start(struct connection *conn, void *ctx)
5156{
5157 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005158 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005159
5160 qc = conn->qc;
5161 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005162 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005163 return 0;
5164 }
5165
5166 tasklet_wakeup(qctx->wait_event.tasklet);
5167 return 1;
5168}
5169
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005170/* transport-layer operations for QUIC connections. */
5171static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005172 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005173 .snd_buf = quic_conn_from_buf,
5174 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005175 .subscribe = quic_conn_subscribe,
5176 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005177 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005178 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005179 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5180 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005181 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005182 .name = "QUIC",
5183};
5184
5185__attribute__((constructor))
5186static void __quic_conn_init(void)
5187{
5188 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5189 xprt_register(XPRT_QUIC, &ssl_quic);
5190}
5191
5192__attribute__((destructor))
5193static void __quic_conn_deinit(void)
5194{
5195 BIO_meth_free(ha_quic_meth);
5196}
5197
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005198/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5199 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005200 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005201 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005202static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005203 struct sockaddr_storage *saddr, qpkt_read_func *func)
5204{
5205 unsigned char *pos;
5206 const unsigned char *end;
5207 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005208 .qc = NULL,
5209 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005210 .owner = owner,
5211 };
5212
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005213 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005214 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005215 do {
5216 int ret;
5217 struct quic_rx_packet *pkt;
5218
Willy Tarreaue4498932021-03-22 21:13:05 +01005219 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005220 if (!pkt)
5221 goto err;
5222
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005223 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005224 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5225 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005226 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005227 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005228 /* If the packet length could not be found, we cannot continue. */
5229 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005230 } while (pos < end);
5231
5232 /* Increasing the received bytes counter by the UDP datagram length
5233 * if this datagram could be associated to a connection.
5234 */
5235 if (dgram_ctx.qc)
5236 dgram_ctx.qc->rx.bytes += len;
5237
5238 return pos - (unsigned char *)buf;
5239
5240 err:
5241 return -1;
5242}
5243
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005244ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005245 struct sockaddr_storage *saddr)
5246{
5247 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5248}
5249
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005250/* Function to automatically activate QUIC traces on stdout.
5251 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5252 * Main use for now is in the docker image for QUIC interop testing.
5253 */
5254static void quic_init_stdout_traces(void)
5255{
5256#ifdef ENABLE_QUIC_STDOUT_TRACES
5257 trace_quic.sink = sink_find("stdout");
5258 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005259 trace_quic.state = TRACE_STATE_RUNNING;
5260#endif
5261}
5262INITCALL0(STG_INIT, quic_init_stdout_traces);
5263
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005264/*
5265 * Local variables:
5266 * c-indent-level: 8
5267 * c-basic-offset: 8
5268 * End:
5269 */