blob: 7ea5f09405fe59e9bd626604013f124fcd98ec4f [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écailleba85acd2022-01-11 14:43:50 +0100119 { .mask = QUIC_EV_CONN_FREED, .name = "conn_freed", .desc = "releasing conn. memory" },
120 { .mask = QUIC_EV_CONN_CLOSE, .name = "conn_close", .desc = "closing conn." },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100121 { /* end */ }
122};
123
124static const struct name_desc quic_trace_lockon_args[4] = {
125 /* arg1 */ { /* already used by the connection */ },
126 /* arg2 */ { .name="quic", .desc="QUIC transport" },
127 /* arg3 */ { },
128 /* arg4 */ { }
129};
130
131static const struct name_desc quic_trace_decoding[] = {
132#define QUIC_VERB_CLEAN 1
133 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
134 { /* end */ }
135};
136
137
138struct trace_source trace_quic = {
139 .name = IST("quic"),
140 .desc = "QUIC xprt",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100141 .arg_def = TRC_ARG1_QCON, /* TRACE()'s first argument is always a quic_conn */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142 .default_cb = quic_trace,
143 .known_events = quic_trace_events,
144 .lockon_args = quic_trace_lockon_args,
145 .decoding = quic_trace_decoding,
146 .report_events = ~0, /* report everything by default */
147};
148
149#define TRACE_SOURCE &trace_quic
150INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
151
152static BIO_METHOD *ha_quic_meth;
153
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200154DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200155DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100156DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100157DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200158 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160DECLARE_POOL(pool_head_quic_connection_id,
161 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162DECLARE_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 +0100163DECLARE_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 +0100164DECLARE_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 +0100165DECLARE_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 +0100166DECLARE_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 +0200167DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100168DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100169
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200170static 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 +0200171 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100172 struct quic_conn *qc, size_t dglen, int pkt_type,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +0100173 int padding, int ack, int probe, int cc, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100174
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100175/* Only for debug purpose */
176struct enc_debug_info {
177 unsigned char *payload;
178 size_t payload_len;
179 unsigned char *aad;
180 size_t aad_len;
181 uint64_t pn;
182};
183
184/* Initializes a enc_debug_info struct (only for debug purpose) */
185static inline void enc_debug_info_init(struct enc_debug_info *edi,
186 unsigned char *payload, size_t payload_len,
187 unsigned char *aad, size_t aad_len, uint64_t pn)
188{
189 edi->payload = payload;
190 edi->payload_len = payload_len;
191 edi->aad = aad;
192 edi->aad_len = aad_len;
193 edi->pn = pn;
194}
195
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100196/* Trace callback for QUIC.
197 * These traces always expect that arg1, if non-null, is of type connection.
198 */
199static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
200 const struct ist where, const struct ist func,
201 const void *a1, const void *a2, const void *a3, const void *a4)
202{
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100203 const struct quic_conn *qc = a1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100204
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100205 if (qc) {
206 const struct quic_tls_secrets *secs;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100208 chunk_appendf(&trace_buf, " : qc@%p", qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209 if ((mask & QUIC_EV_CONN_INIT) && qc) {
210 chunk_appendf(&trace_buf, "\n odcid");
211 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100212 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100213 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100214 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 }
217
218 if (mask & QUIC_EV_CONN_ADDDATA) {
219 const enum ssl_encryption_level_t *level = a2;
220 const size_t *len = a3;
221
222 if (level) {
223 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
224
225 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
226 }
227 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100228 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100229 }
230 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
231 /* Initial read & write secrets. */
232 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
233 const unsigned char *rx_sec = a2;
234 const unsigned char *tx_sec = a3;
235
236 secs = &qc->els[level].tls_ctx.rx;
237 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
238 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
239 if (rx_sec)
240 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
241 quic_tls_keys_hexdump(&trace_buf, secs);
242 }
243 secs = &qc->els[level].tls_ctx.tx;
244 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
245 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
246 if (tx_sec)
247 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
248 quic_tls_keys_hexdump(&trace_buf, secs);
249 }
250 }
251 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
252 const enum ssl_encryption_level_t *level = a2;
253 const unsigned char *secret = a3;
254 const size_t *secret_len = a4;
255
256 if (level) {
257 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
258
259 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
260 if (secret && secret_len)
261 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
262 secs = &qc->els[lvl].tls_ctx.rx;
263 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
264 quic_tls_keys_hexdump(&trace_buf, secs);
265 }
266 }
267
268 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
269 const enum ssl_encryption_level_t *level = a2;
270 const unsigned char *secret = a3;
271 const size_t *secret_len = a4;
272
273 if (level) {
274 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
275
276 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
277 if (secret && secret_len)
278 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
279 secs = &qc->els[lvl].tls_ctx.tx;
280 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
281 quic_tls_keys_hexdump(&trace_buf, secs);
282 }
283
284 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100285
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100286 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287 const struct quic_tx_packet *pkt = a2;
288 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100289 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100290
291 if (qel) {
Amaury Denoyelle4fd53d72021-12-21 14:28:26 +0100292 const struct quic_pktns *pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100293 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100294 "if=%llu pp=%u",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100295 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100296 (unsigned long long)qc->path->cwnd,
297 (unsigned long long)qc->path->prep_in_flight,
298 (unsigned long long)qc->path->in_flight,
299 (unsigned long long)pktns->tx.in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100300 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100301 }
302 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200303 const struct quic_frame *frm;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100304 if (pkt->pn_node.key != (uint64_t)-1)
305 chunk_appendf(&trace_buf, " pn=%llu",(ull)pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100306 list_for_each_entry(frm, &pkt->frms, list)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100307 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille8b6ea172022-01-17 10:51:43 +0100308 chunk_appendf(&trace_buf, " rx.bytes=%llu tx.bytes=%llu",
309 (unsigned long long)qc->rx.bytes,
310 (unsigned long long)qc->tx.bytes);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 }
312
313 if (room) {
314 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
315 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
316 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100317 }
318 }
319
320 if (mask & QUIC_EV_CONN_HDSHK) {
321 const enum quic_handshake_state *state = a2;
322 const int *err = a3;
323
324 if (state)
325 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
326 if (err)
327 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
328 }
329
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100330 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 const struct quic_rx_packet *pkt = a2;
332 const unsigned long *pktlen = a3;
333 const SSL *ssl = a4;
334
335 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100336 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100337 pkt, quic_packet_type_enc_level_char(pkt->type));
338 if (pkt->pnl)
339 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
340 (unsigned long long)pkt->pn);
341 if (pkt->token_len)
342 chunk_appendf(&trace_buf, " toklen=%llu",
343 (unsigned long long)pkt->token_len);
344 if (pkt->aad_len)
345 chunk_appendf(&trace_buf, " aadlen=%llu",
346 (unsigned long long)pkt->aad_len);
347 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
348 pkt->flags, (unsigned long long)pkt->len);
349 }
350 if (pktlen)
351 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
352 if (ssl) {
353 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
354 chunk_appendf(&trace_buf, " el=%c",
355 quic_enc_level_char(ssl_to_quic_enc_level(level)));
356 }
357 }
358
359 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
360 const struct quic_rx_packet *pkt = a2;
361 const struct quic_rx_crypto_frm *cf = a3;
362 const SSL *ssl = a4;
363
364 if (pkt)
365 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
366 quic_packet_type_enc_level_char(pkt->type),
367 (unsigned long long)pkt->pn);
368 if (cf)
369 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
370 (unsigned long long)cf->offset_node.key,
371 (unsigned long long)cf->len);
372 if (ssl) {
373 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200374 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100375 quic_enc_level_char(ssl_to_quic_enc_level(level)));
376 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100377
378 if (qc->err_code)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100379 chunk_appendf(&trace_buf, " err_code=0x%llx", (ull)qc->err_code);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100380 }
381
382 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
383 const struct quic_frame *frm = a2;
384
385 if (frm)
386 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
387 }
388
389 if (mask & QUIC_EV_CONN_PHPKTS) {
390 const struct quic_enc_level *qel = a2;
391
392 if (qel) {
Frédéric Lécailledd51da52021-12-29 15:36:25 +0100393 const struct quic_pktns *pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100394 chunk_appendf(&trace_buf,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100395 " 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 +0100396 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200397 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +0100398 !!(HA_ATOMIC_LOAD(&qel->pktns->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100399 (unsigned long long)qc->path->cwnd,
400 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +0100402 (unsigned long long)pktns->tx.in_flight,
403 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 }
405 }
406
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100407 if (mask & QUIC_EV_CONN_ENCPKT) {
408 const struct enc_debug_info *edi = a2;
409
410 if (edi)
411 chunk_appendf(&trace_buf,
412 " payload=@%p payload_len=%llu"
413 " aad=@%p aad_len=%llu pn=%llu",
414 edi->payload, (unsigned long long)edi->payload_len,
415 edi->aad, (unsigned long long)edi->aad_len,
416 (unsigned long long)edi->pn);
417 }
418
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100419 if (mask & QUIC_EV_CONN_RMHP) {
420 const struct quic_rx_packet *pkt = a2;
421
422 if (pkt) {
423 const int *ret = a3;
424
425 chunk_appendf(&trace_buf, " pkt@%p", pkt);
426 if (ret && *ret)
427 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
428 pkt->pnl, (unsigned long long)pkt->pn);
429 }
430 }
431
432 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200433 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100434 const unsigned long *val1 = a3;
435 const unsigned long *val2 = a4;
436
437 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100438 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100439 if (val1)
440 chunk_appendf(&trace_buf, " %lu", *val1);
441 if (val2)
442 chunk_appendf(&trace_buf, "..%lu", *val2);
443 }
444
445 if (mask & QUIC_EV_CONN_RTTUPDT) {
446 const unsigned int *rtt_sample = a2;
447 const unsigned int *ack_delay = a3;
448 const struct quic_loss *ql = a4;
449
450 if (rtt_sample)
451 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
452 if (ack_delay)
453 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
454 if (ql)
455 chunk_appendf(&trace_buf,
456 " srtt=%ums rttvar=%ums min_rtt=%ums",
457 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
458 }
459 if (mask & QUIC_EV_CONN_CC) {
460 const struct quic_cc_event *ev = a2;
461 const struct quic_cc *cc = a3;
462
463 if (a2)
464 quic_cc_event_trace(&trace_buf, ev);
465 if (a3)
466 quic_cc_state_trace(&trace_buf, cc);
467 }
468
469 if (mask & QUIC_EV_CONN_PKTLOSS) {
470 const struct quic_pktns *pktns = a2;
471 const struct list *lost_pkts = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100472
473 if (pktns) {
474 chunk_appendf(&trace_buf, " pktns=%s",
475 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
476 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
477 if (pktns->tx.loss_time)
478 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100479 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100480 }
481 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
482 struct quic_tx_packet *pkt;
483
484 chunk_appendf(&trace_buf, " lost_pkts:");
485 list_for_each_entry(pkt, lost_pkts, list)
486 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
487 }
488 }
489
490 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100491 const struct quic_pktns *pktns = a2;
492 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100493 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100495 if (ifae_pkts)
496 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
497 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100498 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100499 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100501 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
502 pktns->tx.pto_probe);
Frédéric Lécaille22cfd832021-12-27 17:42:51 +0100503 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_SPTO)) {
504 if (pktns->tx.in_flight)
505 chunk_appendf(&trace_buf, " if=%llu", (ull)pktns->tx.in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506 if (pktns->tx.loss_time)
507 chunk_appendf(&trace_buf, " loss_time=%dms",
508 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
509 }
510 if (mask & QUIC_EV_CONN_SPTO) {
511 if (pktns->tx.time_of_last_eliciting)
512 chunk_appendf(&trace_buf, " tole=%dms",
513 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
514 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100515 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100516 }
517 }
518
519 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
520 chunk_appendf(&trace_buf,
521 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
522 }
523 }
524
525 if (mask & QUIC_EV_CONN_SPPKTS) {
526 const struct quic_tx_packet *pkt = a2;
527
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100528 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
529 (unsigned long long)qc->path->cwnd,
530 (unsigned long long)qc->path->prep_in_flight,
531 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 if (pkt) {
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100533 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 (unsigned long)pkt->pn_node.key,
535 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
536 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille0371cd52021-12-13 12:30:54 +0100537 (unsigned long long)pkt->in_flight_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100538 }
539 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100540
541 if (mask & QUIC_EV_CONN_SSLALERT) {
542 const uint8_t *alert = a2;
543 const enum ssl_encryption_level_t *level = a3;
544
545 if (alert)
546 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
547 if (level)
548 chunk_appendf(&trace_buf, " el=%c",
549 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
550 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100551
552 if (mask & QUIC_EV_CONN_BCFRMS) {
553 const size_t *sz1 = a2;
554 const size_t *sz2 = a3;
555 const size_t *sz3 = a4;
556
557 if (sz1)
558 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
559 if (sz2)
560 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
561 if (sz3)
562 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
563 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100564
565 if (mask & QUIC_EV_CONN_PSTRM) {
566 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100567
Frédéric Lécailled8b84432021-12-10 15:18:36 +0100568 if (frm)
Frédéric Lécaille1ede8232021-12-23 14:11:25 +0100569 chunk_frm_appendf(&trace_buf, frm);
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100570 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100571 }
572 if (mask & QUIC_EV_CONN_LPKT) {
573 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200574 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100575
Frédéric Lécaille8678eb02021-12-16 18:03:52 +0100576 if (pkt) {
577 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s",
578 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short");
579 if (pkt->pn_node.key != (uint64_t)-1)
580 chunk_appendf(&trace_buf, " pn=%llu", pkt->pn_node.key);
581 }
582
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200583 if (len)
584 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100585 }
586
587}
588
589/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100590static inline int quic_peer_validated_addr(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100591{
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200592 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +0100594 if (!qc_is_listener(qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100595 return 1;
596
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200597 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
598 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
599 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
600 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200601 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100602 return 1;
603
604 return 0;
605}
606
607/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
608 * both loss detection and PTO and schedule the task assiated to this timer if needed.
609 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100610static inline void qc_set_timer(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 struct quic_pktns *pktns;
613 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200614 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100616 TRACE_ENTER(QUIC_EV_CONN_STIMER, qc,
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100617 NULL, NULL, &qc->path->ifae_pkts);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100618
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100619 pktns = quic_loss_pktns(qc);
620 if (tick_isset(pktns->tx.loss_time)) {
621 qc->timer = pktns->tx.loss_time;
622 goto out;
623 }
624
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100625 /* anti-amplification: the timer must be
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626 * cancelled for a server which reached the anti-amplification limit.
627 */
Frédéric Lécaille078634d2022-01-04 16:59:42 +0100628 if (!quic_peer_validated_addr(qc) &&
629 (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100630 TRACE_PROTO("anti-amplification reached", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +0100631 qc->timer = TICK_ETERNITY;
632 goto out;
633 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100634
Amaury Denoyellee81fed92021-12-22 11:06:34 +0100635 if (!qc->path->ifae_pkts && quic_peer_validated_addr(qc)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100636 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637 /* Timer cancellation. */
638 qc->timer = TICK_ETERNITY;
639 goto out;
640 }
641
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200642 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
643 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100644 if (tick_isset(pto))
645 qc->timer = pto;
646 out:
Amaury Denoyelle0a29e132021-12-23 15:06:56 +0100647 if (qc->timer_task && qc->timer != TICK_ETERNITY)
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200648 task_schedule(qc->timer_task, qc->timer);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100649 TRACE_LEAVE(QUIC_EV_CONN_STIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100650}
651
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100652/* Derive new keys and ivs required for Key Update feature for <qc> QUIC
653 * connection.
654 * Return 1 if succeeded, 0 if not.
655 */
656static int quic_tls_key_update(struct quic_conn *qc)
657{
658 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
659 struct quic_tls_secrets *rx, *tx;
660 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
661 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
662
663 tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
664 rx = &tls_ctx->rx;
665 tx = &tls_ctx->tx;
666 nxt_rx = &qc->ku.nxt_rx;
667 nxt_tx = &qc->ku.nxt_tx;
668
669 /* Prepare new RX secrets */
670 if (!quic_tls_sec_update(rx->md, nxt_rx->secret, nxt_rx->secretlen,
671 rx->secret, rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100672 TRACE_DEVEL("New RX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100673 return 0;
674 }
675
676 if (!quic_tls_derive_keys(rx->aead, NULL, rx->md,
677 nxt_rx->key, nxt_rx->keylen,
678 nxt_rx->iv, nxt_rx->ivlen, NULL, 0,
679 nxt_rx->secret, nxt_rx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100680 TRACE_DEVEL("New RX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100681 return 0;
682 }
683
684 /* Prepare new TX secrets */
685 if (!quic_tls_sec_update(tx->md, nxt_tx->secret, nxt_tx->secretlen,
686 tx->secret, tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100687 TRACE_DEVEL("New TX secret update failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100688 return 0;
689 }
690
691 if (!quic_tls_derive_keys(tx->aead, NULL, tx->md,
692 nxt_tx->key, nxt_tx->keylen,
693 nxt_tx->iv, nxt_tx->ivlen, NULL, 0,
694 nxt_tx->secret, nxt_tx->secretlen)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100695 TRACE_DEVEL("New TX key derivation failed", QUIC_EV_CONN_RWSEC, qc);
Frédéric Lécaillea7973a62021-11-30 11:10:36 +0100696 return 0;
697 }
698
699 return 1;
700}
701
702/* Rotate the Key Update information for <qc> QUIC connection.
703 * Must be used after having updated them.
704 * Always succeeds.
705 */
706static void quic_tls_rotate_keys(struct quic_conn *qc)
707{
708 struct quic_tls_ctx *tls_ctx = &qc->els[QUIC_TLS_ENC_LEVEL_APP].tls_ctx;
709 unsigned char *curr_secret, *curr_iv, *curr_key;
710
711 /* Rotate the RX secrets */
712 curr_secret = tls_ctx->rx.secret;
713 curr_iv = tls_ctx->rx.iv;
714 curr_key = tls_ctx->rx.key;
715
716 tls_ctx->rx.secret = qc->ku.nxt_rx.secret;
717 tls_ctx->rx.iv = qc->ku.nxt_rx.iv;
718 tls_ctx->rx.key = qc->ku.nxt_rx.key;
719
720 qc->ku.nxt_rx.secret = qc->ku.prv_rx.secret;
721 qc->ku.nxt_rx.iv = qc->ku.prv_rx.iv;
722 qc->ku.nxt_rx.key = qc->ku.prv_rx.key;
723
724 qc->ku.prv_rx.secret = curr_secret;
725 qc->ku.prv_rx.iv = curr_iv;
726 qc->ku.prv_rx.key = curr_key;
727 qc->ku.prv_rx.pn = tls_ctx->rx.pn;
728
729 /* Update the TX secrets */
730 curr_secret = tls_ctx->tx.secret;
731 curr_iv = tls_ctx->tx.iv;
732 curr_key = tls_ctx->tx.key;
733
734 tls_ctx->tx.secret = qc->ku.nxt_tx.secret;
735 tls_ctx->tx.iv = qc->ku.nxt_tx.iv;
736 tls_ctx->tx.key = qc->ku.nxt_tx.key;
737
738 qc->ku.nxt_tx.secret = curr_secret;
739 qc->ku.nxt_tx.iv = curr_iv;
740 qc->ku.nxt_tx.key = curr_key;
741}
742
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100743#ifndef OPENSSL_IS_BORINGSSL
744int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
745 const uint8_t *read_secret,
746 const uint8_t *write_secret, size_t secret_len)
747{
748 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
749 struct quic_tls_ctx *tls_ctx =
750 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
751 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100752 struct quic_tls_secrets *rx, *tx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100753
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100754 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100755 BUG_ON(secret_len > QUIC_TLS_SECRET_LEN);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100756 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100757 TRACE_PROTO("CC required", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100758 goto out;
759 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100760
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100761 if (!quic_tls_ctx_keys_alloc(tls_ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100762 TRACE_DEVEL("keys allocation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100763 goto err;
764 }
765
766 rx = &tls_ctx->rx;
767 tx = &tls_ctx->tx;
768
769 rx->aead = tx->aead = tls_aead(cipher);
770 rx->md = tx->md = tls_md(cipher);
771 rx->hp = tx->hp = tls_hp(cipher);
772
773 if (!quic_tls_derive_keys(rx->aead, rx->hp, rx->md, rx->key, rx->keylen,
774 rx->iv, rx->ivlen, rx->hp_key, sizeof rx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100775 read_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100776 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100777 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100778 }
779
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100780 rx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100781
782 if (!write_secret)
783 goto tp;
784
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100785 if (!quic_tls_derive_keys(tx->aead, tx->hp, tx->md, tx->key, tx->keylen,
786 tx->iv, tx->ivlen, tx->hp_key, sizeof tx->hp_key,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100787 write_secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100788 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100789 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100790 }
791
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100792 tx->flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille4015cbb2021-12-14 19:29:34 +0100793 tp:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100794 if (objt_server(conn->target) && level == ssl_encryption_application) {
795 const unsigned char *buf;
796 size_t buflen;
797
798 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
799 if (!buflen)
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100800 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100801
802 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100803 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100804 }
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100805
806 if (level == ssl_encryption_application) {
807 struct quic_conn *qc = conn->qc;
808 struct quic_tls_kp *prv_rx = &qc->ku.prv_rx;
809 struct quic_tls_kp *nxt_rx = &qc->ku.nxt_rx;
810 struct quic_tls_kp *nxt_tx = &qc->ku.nxt_tx;
811
812 if (!(rx->secret = pool_alloc(pool_head_quic_tls_secret)) ||
813 !(tx->secret = pool_alloc(pool_head_quic_tls_secret))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100814 TRACE_DEVEL("Could not allocate secrete keys", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +0100815 goto err;
816 }
817
818 memcpy(rx->secret, read_secret, secret_len);
819 rx->secretlen = secret_len;
820 memcpy(tx->secret, write_secret, secret_len);
821 tx->secretlen = secret_len;
822 /* Initialize all the secret keys lengths */
823 prv_rx->secretlen = nxt_rx->secretlen = nxt_tx->secretlen = secret_len;
824 /* Prepare the next key update */
825 if (!quic_tls_key_update(qc))
826 goto err;
827 }
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100828 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100829 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn->qc, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100830 return 1;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100831
832 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100833 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RWSEC, conn->qc);
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100834 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100835}
836#else
837/* ->set_read_secret callback to derive the RX secrets at <level> encryption
838 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500839 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100840 */
841int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
842 const SSL_CIPHER *cipher,
843 const uint8_t *secret, size_t secret_len)
844{
845 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
846 struct quic_tls_ctx *tls_ctx =
847 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
848
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100849 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100850 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100851 TRACE_PROTO("CC required", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100852 goto out;
853 }
854
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100855 tls_ctx->rx.aead = tls_aead(cipher);
856 tls_ctx->rx.md = tls_md(cipher);
857 tls_ctx->rx.hp = tls_hp(cipher);
858
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100859 if (!(ctx->rx.key = pool_alloc(pool_head_quic_tls_key)))
860 goto err;
861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 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 +0100863 tls_ctx->rx.key, tls_ctx->rx.keylen,
864 tls_ctx->rx.iv, tls_ctx->rx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100865 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
866 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100867 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100868 goto err;
869 }
870
871 if (objt_server(conn->target) && level == ssl_encryption_application) {
872 const unsigned char *buf;
873 size_t buflen;
874
875 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
876 if (!buflen)
877 goto err;
878
879 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
880 goto err;
881 }
882
883 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100884 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100885 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100886
887 return 1;
888
889 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100890 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100891 return 0;
892}
893
894/* ->set_write_secret callback to derive the TX secrets at <level>
895 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500896 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100897 */
898int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
899 const SSL_CIPHER *cipher,
900 const uint8_t *secret, size_t secret_len)
901{
902 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
903 struct quic_tls_ctx *tls_ctx =
904 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
905
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100906 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100907 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100908 TRACE_PROTO("CC required", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100909 goto out;
910 }
911
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +0100912 if (!(ctx->tx.key = pool_alloc(pool_head_quic_tls_key)))
913 goto err;
914
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100915 tls_ctx->tx.aead = tls_aead(cipher);
916 tls_ctx->tx.md = tls_md(cipher);
917 tls_ctx->tx.hp = tls_hp(cipher);
918
919 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 +0100920 tls_ctx->tx.key, tls_ctx->tx.keylen,
921 tls_ctx->tx.iv, tls_ctx->tx.ivlen,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
923 secret, secret_len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100924 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100925 goto err;
926 }
927
928 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100929 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn->qc, &level, secret, &secret_len);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +0100930 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100931 return 1;
932
933 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +0100934 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100935 return 0;
936}
937#endif
938
939/* This function copies the CRYPTO data provided by the TLS stack found at <data>
940 * with <len> as size in CRYPTO buffers dedicated to store the information about
941 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
942 * It fails only if it could not managed to allocate enough CRYPTO buffers to
943 * store all the data.
944 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
945 */
946static int quic_crypto_data_cpy(struct quic_enc_level *qel,
947 const unsigned char *data, size_t len)
948{
949 struct quic_crypto_buf **qcb;
950 /* The remaining byte to store in CRYPTO buffers. */
951 size_t cf_offset, cf_len, *nb_buf;
952 unsigned char *pos;
953
954 nb_buf = &qel->tx.crypto.nb_buf;
955 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
956 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
957 cf_len = len;
958
959 while (len) {
960 size_t to_copy, room;
961
962 pos = (*qcb)->data + (*qcb)->sz;
963 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
964 to_copy = len > room ? room : len;
965 if (to_copy) {
966 memcpy(pos, data, to_copy);
967 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
968 qel->tx.crypto.sz += to_copy;
969 (*qcb)->sz += to_copy;
970 pos += to_copy;
971 len -= to_copy;
972 data += to_copy;
973 }
974 else {
975 struct quic_crypto_buf **tmp;
976
977 tmp = realloc(qel->tx.crypto.bufs,
978 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
979 if (tmp) {
980 qel->tx.crypto.bufs = tmp;
981 qcb = &qel->tx.crypto.bufs[*nb_buf];
982 *qcb = pool_alloc(pool_head_quic_crypto_buf);
983 if (!*qcb)
984 return 0;
985
986 (*qcb)->sz = 0;
987 ++*nb_buf;
988 }
989 else {
990 break;
991 }
992 }
993 }
994
995 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
996 * have been buffered.
997 */
998 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200999 struct quic_frame *frm;
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001000 struct quic_frame *found = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001001
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001002 /* There is at most one CRYPTO frame in this packet number
1003 * space. Let's look for it.
1004 */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001005 list_for_each_entry(frm, &qel->pktns->tx.frms, list) {
Frédéric Lécaille81cd3c82022-01-10 18:31:07 +01001006 if (frm->type != QUIC_FT_CRYPTO)
1007 continue;
1008
1009 /* Found */
1010 found = frm;
1011 break;
1012 }
1013
1014 if (found) {
1015 found->crypto.len += cf_len;
1016 }
1017 else {
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001018 frm = pool_alloc(pool_head_quic_frame);
1019 if (!frm)
1020 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001021
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001022 frm->type = QUIC_FT_CRYPTO;
1023 frm->crypto.offset = cf_offset;
1024 frm->crypto.len = cf_len;
1025 frm->crypto.qel = qel;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001026 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécailled4ecf942022-01-04 23:15:40 +01001027 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001028 }
1029
1030 return len == 0;
1031}
1032
1033
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001034/* Set <alert> TLS alert as QUIC CRYPTO_ERROR error */
1035void quic_set_tls_alert(struct quic_conn *qc, int alert)
1036{
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001037 HA_ATOMIC_STORE(&qc->err_code, QC_ERR_CRYPTO_ERROR | alert);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001038 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001039 TRACE_PROTO("Alert set", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001040}
1041
Frédéric Lécailleb0bd62d2021-12-14 19:34:08 +01001042/* Set the application for <qc> QUIC connection.
1043 * Return 1 if succeeded, 0 if not.
1044 */
1045int quic_set_app_ops(struct quic_conn *qc, const unsigned char *alpn, size_t alpn_len)
1046{
1047 const struct qcc_app_ops *app_ops;
1048
1049 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1050 app_ops = qc->qcc->app_ops = &h3_ops;
1051 }
1052 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1053 app_ops = qc->qcc->app_ops = &hq_interop_ops;
1054 }
1055 else
1056 return 0;
1057
1058 if (app_ops->init && !app_ops->init(qc->qcc))
1059 return 0;
1060
1061 if (app_ops->finalize)
1062 app_ops->finalize(qc->qcc->ctx);
1063
1064 return 1;
1065}
1066
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001067/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
1068 * wants to provide the QUIC layer with CRYPTO data.
1069 * Returns 1 if succeeded, 0 if not.
1070 */
1071int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
1072 const uint8_t *data, size_t len)
1073{
1074 struct connection *conn;
1075 enum quic_tls_enc_level tel;
1076 struct quic_enc_level *qel;
1077
1078 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001079 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001080 if (HA_ATOMIC_LOAD(&conn->qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001081 TRACE_PROTO("CC required", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001082 goto out;
1083 }
1084
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001085 tel = ssl_to_quic_enc_level(level);
1086 qel = &conn->qc->els[tel];
1087
1088 if (tel == -1) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001089 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001090 goto err;
1091 }
1092
1093 if (!quic_crypto_data_cpy(qel, data, len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001094 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001095 goto err;
1096 }
1097
1098 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001099 conn->qc, &level, &len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001100
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001101 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001102 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001103 return 1;
1104
1105 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001106 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107 return 0;
1108}
1109
1110int ha_quic_flush_flight(SSL *ssl)
1111{
1112 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1113
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001114 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn->qc);
1115 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116
1117 return 1;
1118}
1119
1120int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
1121{
1122 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1123
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001124 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn->qc, &alert, &level);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001125 quic_set_tls_alert(conn->qc, alert);
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01001126 HA_ATOMIC_STORE(&conn->qc->flags, QUIC_FL_CONN_IMMEDIATE_CLOSE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001127 return 1;
1128}
1129
1130/* QUIC TLS methods */
1131static SSL_QUIC_METHOD ha_quic_method = {
1132#ifdef OPENSSL_IS_BORINGSSL
1133 .set_read_secret = ha_set_rsec,
1134 .set_write_secret = ha_set_wsec,
1135#else
1136 .set_encryption_secrets = ha_quic_set_encryption_secrets,
1137#endif
1138 .add_handshake_data = ha_quic_add_handshake_data,
1139 .flush_flight = ha_quic_flush_flight,
1140 .send_alert = ha_quic_send_alert,
1141};
1142
1143/* Initialize the TLS context of a listener with <bind_conf> as configuration.
1144 * Returns an error count.
1145 */
1146int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
1147{
1148 struct proxy *curproxy = bind_conf->frontend;
1149 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
1150 int cfgerr = 0;
1151
1152#if 0
1153 /* XXX Did not manage to use this. */
1154 const char *ciphers =
1155 "TLS_AES_128_GCM_SHA256:"
1156 "TLS_AES_256_GCM_SHA384:"
1157 "TLS_CHACHA20_POLY1305_SHA256:"
1158 "TLS_AES_128_CCM_SHA256";
1159#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +02001160 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001161 long options =
1162 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
1163 SSL_OP_SINGLE_ECDH_USE |
1164 SSL_OP_CIPHER_SERVER_PREFERENCE;
1165 SSL_CTX *ctx;
1166
1167 ctx = SSL_CTX_new(TLS_server_method());
1168 bind_conf->initial_ctx = ctx;
1169
1170 SSL_CTX_set_options(ctx, options);
1171#if 0
1172 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
1173 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
1174 "for bind '%s' at [%s:%d].\n",
1175 curproxy->id, ciphers,
1176 bind_conf->arg, bind_conf->file, bind_conf->line);
1177 cfgerr++;
1178 }
1179#endif
1180
1181 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
1182 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
1183 "for bind '%s' at [%s:%d].\n",
1184 curproxy->id, groups,
1185 bind_conf->arg, bind_conf->file, bind_conf->line);
1186 cfgerr++;
1187 }
1188
1189 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
1190 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1191 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
1192 SSL_CTX_set_default_verify_paths(ctx);
1193
1194#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1195#ifdef OPENSSL_IS_BORINGSSL
1196 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1197 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1198#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1199 if (bind_conf->ssl_conf.early_data) {
1200 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01001201 SSL_CTX_set_max_early_data(ctx, 0xffffffff);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202 }
1203 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1204 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1205#else
1206 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1207#endif
1208 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1209#endif
1210 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1211
1212 return cfgerr;
1213}
1214
1215/* Decode an expected packet number from <truncated_on> its truncated value,
1216 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1217 * the number of bits used to encode this packet number (its length in bytes * 8).
1218 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1219 */
1220static uint64_t decode_packet_number(uint64_t largest_pn,
1221 uint32_t truncated_pn, unsigned int pn_nbits)
1222{
1223 uint64_t expected_pn = largest_pn + 1;
1224 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1225 uint64_t pn_hwin = pn_win / 2;
1226 uint64_t pn_mask = pn_win - 1;
1227 uint64_t candidate_pn;
1228
1229
1230 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1231 /* Note that <pn_win> > <pn_hwin>. */
1232 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1233 candidate_pn + pn_hwin <= expected_pn)
1234 return candidate_pn + pn_win;
1235
1236 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1237 return candidate_pn - pn_win;
1238
1239 return candidate_pn;
1240}
1241
1242/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1243 * cryptographic context.
1244 * <largest_pn> is the largest received packet number and <pn> the address of
1245 * the packet number field for this packet with <byte0> address of its first byte.
1246 * <end> points to one byte past the end of this packet.
1247 * Returns 1 if succeeded, 0 if not.
1248 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001249static int qc_do_rm_hp(struct quic_conn *qc,
1250 struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001251 int64_t largest_pn, unsigned char *pn,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001252 unsigned char *byte0, const unsigned char *end)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253{
1254 int ret, outlen, i, pnlen;
1255 uint64_t packet_number;
1256 uint32_t truncated_pn = 0;
1257 unsigned char mask[5] = {0};
1258 unsigned char *sample;
1259 EVP_CIPHER_CTX *cctx;
1260 unsigned char *hp_key;
1261
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001262 /* Check there is enough data in this packet. */
1263 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001264 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 return 0;
1266 }
1267
1268 cctx = EVP_CIPHER_CTX_new();
1269 if (!cctx) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001270 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271 return 0;
1272 }
1273
1274 ret = 0;
1275 sample = pn + QUIC_PACKET_PN_MAXLEN;
1276
1277 hp_key = tls_ctx->rx.hp_key;
1278 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1279 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1280 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001281 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001282 goto out;
1283 }
1284
1285 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1286 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1287 for (i = 0; i < pnlen; i++) {
1288 pn[i] ^= mask[i + 1];
1289 truncated_pn = (truncated_pn << 8) | pn[i];
1290 }
1291
1292 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1293 /* Store remaining information for this unprotected header */
1294 pkt->pn = packet_number;
1295 pkt->pnl = pnlen;
1296
1297 ret = 1;
1298
1299 out:
1300 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001301
1302 return ret;
1303}
1304
1305/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1306 * address, with <payload_len> as payload length, <aad> as address of
1307 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1308 * context.
1309 * Returns 1 if succeeded, 0 if not.
1310 */
1311static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1312 unsigned char *aad, size_t aad_len, uint64_t pn,
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001313 struct quic_tls_ctx *tls_ctx, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314{
1315 unsigned char iv[12];
1316 unsigned char *tx_iv = tls_ctx->tx.iv;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01001317 size_t tx_iv_sz = tls_ctx->tx.ivlen;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001318 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001319
1320 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001321 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001322 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323 }
1324
1325 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1326 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001327 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001328 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329 }
1330
1331 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001332
1333 err:
1334 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01001335 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, qc, &edi);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001336 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001337}
1338
1339/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1340 * Returns 1 if succeeded, 0 if not.
1341 */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001342static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001343{
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001344 int ret, kp_changed;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345 unsigned char iv[12];
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001346 struct quic_tls_ctx *tls_ctx = &qel->tls_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001347 unsigned char *rx_iv = tls_ctx->rx.iv;
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001348 size_t rx_iv_sz = tls_ctx->rx.ivlen;
1349 unsigned char *rx_key = tls_ctx->rx.key;
1350
1351 kp_changed = 0;
1352 if (pkt->type == QUIC_PACKET_TYPE_SHORT) {
1353 /* The two tested bits are not at the same position,
1354 * this is why they are first both inversed.
1355 */
1356 if (!(*pkt->data & QUIC_PACKET_KEY_PHASE_BIT) ^ !(tls_ctx->flags & QUIC_FL_TLS_KP_BIT_SET)) {
1357 if (pkt->pn < tls_ctx->rx.pn) {
1358 /* The lowest packet number of a previous key phase
1359 * cannot be null if it really stores previous key phase
1360 * secrets.
1361 */
1362 if (!pkt->qc->ku.prv_rx.pn)
1363 return 0;
1364
1365 rx_iv = pkt->qc->ku.prv_rx.iv;
1366 rx_key = pkt->qc->ku.prv_rx.key;
1367 }
1368 else if (pkt->pn > qel->pktns->rx.largest_pn) {
1369 /* Next key phase */
1370 kp_changed = 1;
1371 rx_iv = pkt->qc->ku.nxt_rx.iv;
1372 rx_key = pkt->qc->ku.nxt_rx.key;
1373 }
1374 }
1375 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001376
1377 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1378 return 0;
1379
1380 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1381 pkt->data, pkt->aad_len,
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001382 tls_ctx->rx.aead, rx_key, iv);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001383 if (!ret)
1384 return 0;
1385
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01001386 /* Update the keys only if the packet decryption succeeded. */
1387 if (kp_changed) {
1388 quic_tls_rotate_keys(pkt->qc);
1389 /* Toggle the Key Phase bit */
1390 tls_ctx->flags ^= QUIC_FL_TLS_KP_BIT_SET;
1391 /* Store the lowest packet number received for the current key phase */
1392 tls_ctx->rx.pn = pkt->pn;
1393 /* Prepare the next key update */
1394 if (!quic_tls_key_update(pkt->qc))
1395 return 0;
1396 }
1397
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001398 /* Update the packet length (required to parse the frames). */
1399 pkt->len = pkt->aad_len + ret;
1400
1401 return 1;
1402}
1403
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001404/* Remove from <qcs> stream the acknowledged frames.
1405 * Never fails.
1406 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001407static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001408{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001409 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001410 struct eb64_node *frm_node;
1411
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001412 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001413 frm_node = eb64_first(&qcs->tx.acked_frms);
1414 while (frm_node) {
1415 struct quic_stream *strm;
1416
1417 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1418 if (strm->offset.key != qcs->tx.ack_offset)
1419 break;
1420
1421 b_del(strm->buf, strm->len);
1422 qcs->tx.ack_offset += strm->len;
1423 frm_node = eb64_next(frm_node);
1424 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001425 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001426 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001427
1428 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001429}
1430
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001431/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001432static inline void qc_treat_acked_tx_frm(struct quic_conn *qc,
1433 struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001434{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001435 int stream_acked;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001436
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001437 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001438 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001439 switch (frm->type) {
1440 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1441 {
1442 struct qcs *qcs = frm->stream.qcs;
1443 struct quic_stream *strm = &frm->stream;
1444
1445 if (qcs->tx.ack_offset == strm->offset.key) {
1446 b_del(strm->buf, strm->len);
1447 qcs->tx.ack_offset += strm->len;
1448 LIST_DELETE(&frm->list);
1449 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001450 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001451 }
1452 else {
1453 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1454 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001455 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001456 }
1457 break;
1458 default:
1459 LIST_DELETE(&frm->list);
1460 pool_free(pool_head_quic_frame, frm);
1461 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001462
1463 if (stream_acked) {
1464 struct qcc *qcc = qc->qcc;
1465
1466 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1467 tasklet_wakeup(qcc->subs->tasklet);
1468 qcc->subs->events &= ~SUB_RETRY_SEND;
1469 if (!qcc->subs->events)
1470 qcc->subs = NULL;
1471 }
1472 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001473}
1474
1475/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1476 * deallocating them, and their TX frames.
1477 * Returns the last node reached to be used for the next range.
1478 * May be NULL if <largest> node could not be found.
1479 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001480static inline struct eb64_node *qc_ackrng_pkts(struct quic_conn *qc,
1481 struct eb_root *pkts,
1482 unsigned int *pkt_flags,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001483 struct list *newly_acked_pkts,
1484 struct eb64_node *largest_node,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001485 uint64_t largest, uint64_t smallest)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001486{
1487 struct eb64_node *node;
1488 struct quic_tx_packet *pkt;
1489
1490 if (largest_node)
1491 node = largest_node;
1492 else {
1493 node = eb64_lookup(pkts, largest);
1494 while (!node && largest > smallest) {
1495 node = eb64_lookup(pkts, --largest);
1496 }
1497 }
1498
1499 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001500 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001501
1502 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1503 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001504 LIST_INSERT(newly_acked_pkts, &pkt->list);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001505 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, qc, NULL, &pkt->pn_node.key);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001506 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001507 qc_treat_acked_tx_frm(qc, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001508 node = eb64_prev(node);
1509 eb64_delete(&pkt->pn_node);
1510 }
1511
1512 return node;
1513}
1514
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001515/* Remove all frames from <pkt_frm_list> and reinsert them in the
1516 * same order they have been sent into <pktns_frm_list>.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001517 */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001518static inline void qc_requeue_nacked_pkt_tx_frms(struct quic_conn *qc,
1519 struct list *pkt_frm_list,
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001520 struct list *pktns_frm_list)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001521{
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001522 struct quic_frame *frm, *frmbak;
1523 struct list tmp = LIST_HEAD_INIT(tmp);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001524
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001525 list_for_each_entry_safe(frm, frmbak, pkt_frm_list, list) {
1526 LIST_DELETE(&frm->list);
1527 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001528 LIST_APPEND(&tmp, &frm->list);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001529 }
1530
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01001531 LIST_SPLICE(pktns_frm_list, &tmp);
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001532}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533
1534/* Free the TX packets of <pkts> list */
1535static inline void free_quic_tx_pkts(struct list *pkts)
1536{
1537 struct quic_tx_packet *pkt, *tmp;
1538
1539 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001540 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001541 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001542 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001543 }
1544}
1545
1546/* Send a packet loss event nofification to the congestion controller
1547 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1548 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1549 * at <now_us> current time.
1550 * Always succeeds.
1551 */
1552static inline void qc_cc_loss_event(struct quic_conn *qc,
1553 unsigned int lost_bytes,
1554 unsigned int newest_time_sent,
1555 unsigned int period,
1556 unsigned int now_us)
1557{
1558 struct quic_cc_event ev = {
1559 .type = QUIC_CC_EVT_LOSS,
1560 .loss.now_ms = now_ms,
1561 .loss.max_ack_delay = qc->max_ack_delay,
1562 .loss.lost_bytes = lost_bytes,
1563 .loss.newest_time_sent = newest_time_sent,
1564 .loss.period = period,
1565 };
1566
1567 quic_cc_event(&qc->path->cc, &ev);
1568}
1569
1570/* Send a packet ack event nofication for each newly acked packet of
1571 * <newly_acked_pkts> list and free them.
1572 * Always succeeds.
1573 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001574static inline void qc_treat_newly_acked_pkts(struct quic_conn *qc,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001575 struct list *newly_acked_pkts)
1576{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001577 struct quic_tx_packet *pkt, *tmp;
1578 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1579
1580 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1581 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001582 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001583 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001584 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001585 ev.ack.acked = pkt->in_flight_len;
1586 ev.ack.time_sent = pkt->time_sent;
1587 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001588 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001589 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001590 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001591 }
1592
1593}
1594
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01001595/* Release all the frames attached to <pktns> packet number space */
1596static inline void qc_release_pktns_frms(struct quic_pktns *pktns)
1597{
1598 struct quic_frame *frm, *frmbak;
1599
1600 list_for_each_entry_safe(frm, frmbak, &pktns->tx.frms, list) {
1601 LIST_DELETE(&frm->list);
1602 pool_free(pool_head_quic_frame, frm);
1603 }
1604}
1605
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001606/* Handle <pkts> list of lost packets detected at <now_us> handling
1607 * their TX frames.
1608 * Send a packet loss event to the congestion controller if
1609 * in flight packet have been lost.
1610 * Also frees the packet in <pkts> list.
1611 * Never fails.
1612 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001613static inline void qc_release_lost_pkts(struct quic_conn *qc,
1614 struct quic_pktns *pktns,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001615 struct list *pkts,
1616 uint64_t now_us)
1617{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001618 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001619 uint64_t lost_bytes;
1620
1621 lost_bytes = 0;
1622 oldest_lost = newest_lost = NULL;
1623 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001624 struct list tmp = LIST_HEAD_INIT(tmp);
1625
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001626 lost_bytes += pkt->in_flight_len;
1627 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001628 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001629 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001630 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001631 /* Treat the frames of this lost packet. */
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01001632 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &pktns->tx.frms);
Willy Tarreau2b718102021-04-21 07:32:39 +02001633 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001634 if (!oldest_lost) {
1635 oldest_lost = newest_lost = pkt;
1636 }
1637 else {
1638 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001639 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001640 newest_lost = pkt;
1641 }
1642 }
1643
1644 if (lost_bytes) {
1645 /* Sent a packet loss event to the congestion controller. */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001646 qc_cc_loss_event(qc, lost_bytes, newest_lost->time_sent,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001647 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001648 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001649 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001650 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001651 }
1652}
1653
1654/* Look for packet loss from sent packets for <qel> encryption level of a
1655 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1656 * their tree if deemed as lost or set the <loss_time> value the packet number
1657 * space if any not deemed lost.
1658 * Should be called after having received an ACK frame with newly acknowledged
1659 * packets or when the the loss detection timer has expired.
1660 * Always succeeds.
1661 */
1662static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1663 struct quic_conn *qc,
1664 struct list *lost_pkts)
1665{
1666 struct eb_root *pkts;
1667 struct eb64_node *node;
1668 struct quic_loss *ql;
1669 unsigned int loss_delay;
1670
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001671 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001672 pkts = &pktns->tx.pkts;
1673 pktns->tx.loss_time = TICK_ETERNITY;
1674 if (eb_is_empty(pkts))
1675 goto out;
1676
1677 ql = &qc->path->loss;
1678 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1679 loss_delay += loss_delay >> 3;
1680 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1681
1682 node = eb64_first(pkts);
1683 while (node) {
1684 struct quic_tx_packet *pkt;
1685 int64_t largest_acked_pn;
1686 unsigned int loss_time_limit, time_sent;
1687
1688 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001689 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001690 node = eb64_next(node);
1691 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1692 break;
1693
1694 time_sent = pkt->time_sent;
1695 loss_time_limit = tick_add(time_sent, loss_delay);
1696 if (tick_is_le(time_sent, now_ms) ||
1697 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1698 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001699 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001700 }
1701 else {
Frédéric Lécailledc90c072021-12-27 18:15:27 +01001702 if (tick_isset(pktns->tx.loss_time))
1703 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1704 else
1705 pktns->tx.loss_time = loss_time_limit;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001706 }
1707 }
1708
1709 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001710 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc, pktns, lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001711}
1712
1713/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1714 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001715 * 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 +01001716 * acked ack-eliciting packet.
1717 * Return 1, if succeeded, 0 if not.
1718 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001719static inline int qc_parse_ack_frm(struct quic_conn *qc,
1720 struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001721 struct quic_enc_level *qel,
1722 unsigned int *rtt_sample,
1723 const unsigned char **pos, const unsigned char *end)
1724{
1725 struct quic_ack *ack = &frm->ack;
1726 uint64_t smallest, largest;
1727 struct eb_root *pkts;
1728 struct eb64_node *largest_node;
1729 unsigned int time_sent, pkt_flags;
1730 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1731 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1732
1733 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1734 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001735 qc, NULL, &ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001736 goto err;
1737 }
1738
1739 if (ack->first_ack_range > ack->largest_ack) {
1740 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001741 qc, NULL, &ack->first_ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001742 goto err;
1743 }
1744
1745 largest = ack->largest_ack;
1746 smallest = largest - ack->first_ack_range;
1747 pkts = &qel->pktns->tx.pkts;
1748 pkt_flags = 0;
1749 largest_node = NULL;
1750 time_sent = 0;
1751
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001752 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 +01001753 largest_node = eb64_lookup(pkts, largest);
1754 if (!largest_node) {
1755 TRACE_DEVEL("Largest acked packet not found",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001756 QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaille83b7a5b2021-11-17 16:16:04 +01001757 }
1758 else {
1759 time_sent = eb64_entry(&largest_node->node,
1760 struct quic_tx_packet, pn_node)->time_sent;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001761 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001762 }
1763
1764 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001765 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001766 do {
1767 uint64_t gap, ack_range;
1768
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001769 qc_ackrng_pkts(qc, pkts, &pkt_flags, &newly_acked_pkts,
1770 largest_node, largest, smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001771 if (!ack->ack_range_num--)
1772 break;
1773
1774 if (!quic_dec_int(&gap, pos, end))
1775 goto err;
1776
1777 if (smallest < gap + 2) {
1778 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001779 qc, NULL, &gap, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001780 goto err;
1781 }
1782
1783 largest = smallest - gap - 2;
1784 if (!quic_dec_int(&ack_range, pos, end))
1785 goto err;
1786
1787 if (largest < ack_range) {
1788 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001789 qc, NULL, &largest, &ack_range);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001790 goto err;
1791 }
1792
1793 /* Do not use this node anymore. */
1794 largest_node = NULL;
1795 /* Next range */
1796 smallest = largest - ack_range;
1797
1798 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001799 qc, NULL, &largest, &smallest);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001800 } while (1);
1801
1802 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001803 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001804
1805 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1806 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001807 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001808 }
1809
1810 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1811 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001812 qc_packet_loss_lookup(qel->pktns, qc, &lost_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001813 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001814 qc_release_lost_pkts(qc, qel->pktns, &lost_pkts, now_ms);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001815 }
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001816 qc_treat_newly_acked_pkts(qc, &newly_acked_pkts);
1817 if (quic_peer_validated_addr(qc))
1818 qc->path->loss.pto_count = 0;
1819 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001820 }
1821
1822
1823 return 1;
1824
1825 err:
1826 free_quic_tx_pkts(&newly_acked_pkts);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01001827 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001828 return 0;
1829}
1830
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001831/* This function gives the detail of the SSL error. It is used only
1832 * if the debug mode and the verbose mode are activated. It dump all
1833 * the SSL error until the stack was empty.
1834 */
1835static forceinline void qc_ssl_dump_errors(struct connection *conn)
1836{
1837 if (unlikely(global.mode & MODE_DEBUG)) {
1838 while (1) {
1839 unsigned long ret;
1840
1841 ret = ERR_get_error();
1842 if (!ret)
1843 return;
1844
1845 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1846 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1847 }
1848 }
1849}
1850
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001851int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1852 const char **str, int *len);
1853
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001854/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1855 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001856 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001857 * Return 1 if succeeded, 0 if not.
1858 */
1859static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001860 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001861 const unsigned char *data, size_t len,
1862 struct quic_rx_packet *pkt,
1863 struct quic_rx_crypto_frm *cf)
1864{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001865 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001866 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001867
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001868 ssl_err = SSL_ERROR_NONE;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01001869 qc = ctx->qc;
1870
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001871 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, qc);
1872
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001873 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1874 TRACE_PROTO("SSL_provide_quic_data() error",
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01001875 QUIC_EV_CONN_SSLDATA, qc, pkt, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001876 goto err;
1877 }
1878
1879 el->rx.crypto.offset += len;
1880 TRACE_PROTO("in order CRYPTO data",
Frédéric Lécaillee7ff2b22021-12-22 17:40:38 +01001881 QUIC_EV_CONN_SSLDATA, qc, NULL, cf, ctx->ssl);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001882
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001883 state = HA_ATOMIC_LOAD(&qc->state);
1884 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001885 ssl_err = SSL_do_handshake(ctx->ssl);
1886 if (ssl_err != 1) {
1887 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1888 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1889 TRACE_PROTO("SSL handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001890 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001891 goto out;
1892 }
1893
1894 TRACE_DEVEL("SSL handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001895 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001896 qc_ssl_dump_errors(ctx->conn);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01001897 ERR_clear_error();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001898 goto err;
1899 }
1900
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001901 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01001902 if (qc_is_listener(ctx->qc))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001903 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001904 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001905 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001906 } else {
1907 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1908 if (ssl_err != 1) {
1909 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1910 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1911 TRACE_DEVEL("SSL post handshake",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001912 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913 goto out;
1914 }
1915
1916 TRACE_DEVEL("SSL post handshake error",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001917 QUIC_EV_CONN_HDSHK, qc, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001918 goto err;
1919 }
1920
1921 TRACE_PROTO("SSL post handshake succeeded",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001922 QUIC_EV_CONN_HDSHK, qc, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001923 }
Amaury Denoyellee2288c32021-12-03 14:44:21 +01001924
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001925 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001926 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001927 return 1;
1928
1929 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01001930 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001931 return 0;
1932}
1933
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001934/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1935 * <pkt> RX packet.
1936 * Return it if succeeded, NULL if not.
1937 */
1938static inline
1939struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1940 struct quic_rx_packet *pkt)
1941{
1942 struct quic_rx_strm_frm *frm;
1943
1944 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1945 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001946 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001947 frm->len = stream_frm->len;
1948 frm->data = stream_frm->data;
1949 frm->pkt = pkt;
1950 }
1951
1952 return frm;
1953}
1954
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001955/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
Frédéric Lécaille3fe7df82021-12-15 15:32:55 +01001956 * Also update <strm_frm> frame to reflect the data which have been consumed.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001957 */
1958static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1959{
1960 size_t ret;
1961
1962 ret = 0;
1963 while (strm_frm->len) {
1964 size_t try;
1965
1966 try = b_contig_space(buf);
1967 if (!try)
1968 break;
1969
1970 if (try > strm_frm->len)
1971 try = strm_frm->len;
1972 memcpy(b_tail(buf), strm_frm->data, try);
1973 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001974 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001975 b_add(buf, try);
1976 ret += try;
1977 }
1978
1979 return ret;
1980}
1981
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01001982/* Copy as most as possible STREAM data from <strm_frm> into <buf> buffer.
1983 * Also update <strm_frm> frame to reflect the data which have been consumed.
1984 */
1985static size_t qc_rx_strm_frm_cpy(struct buffer *buf,
1986 struct quic_rx_strm_frm *strm_frm)
1987{
1988 size_t ret;
1989
1990 ret = 0;
1991 while (strm_frm->len) {
1992 size_t try;
1993
1994 try = b_contig_space(buf);
1995 if (!try)
1996 break;
1997
1998 if (try > strm_frm->len)
1999 try = strm_frm->len;
2000 memcpy(b_tail(buf), strm_frm->data, try);
2001 strm_frm->len -= try;
2002 strm_frm->offset_node.key += try;
2003 b_add(buf, try);
2004 ret += try;
2005 }
2006
2007 return ret;
2008}
2009
2010/* Process as much as possible RX STREAM frames received for <qcs> */
2011static size_t qc_treat_rx_strm_frms(struct qcs *qcs)
2012{
2013 int total;
2014 struct eb64_node *frm_node;
2015
2016 total = 0;
2017 frm_node = eb64_first(&qcs->rx.frms);
2018 while (frm_node) {
2019 int ret;
2020 struct quic_rx_strm_frm *frm;
2021
2022 frm = eb64_entry(&frm_node->node, struct quic_rx_strm_frm, offset_node);
2023 if (frm->offset_node.key != qcs->rx.offset)
2024 break;
2025
2026 ret = qc_rx_strm_frm_cpy(&qcs->rx.buf, frm);
2027 qcs->rx.offset += ret;
2028 total += ret;
2029 if (frm->len) {
2030 /* If there is remaining data in this frame
2031 * this is because the destination buffer is full.
2032 */
2033 break;
2034 }
2035
2036 frm_node = eb64_next(frm_node);
2037 quic_rx_packet_refdec(frm->pkt);
2038 eb64_delete(&frm->offset_node);
2039 pool_free(pool_head_quic_rx_strm_frm, frm);
2040 }
2041
2042 return total;
2043}
2044
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002045/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
2046 * streams may be open. The data are copied to the stream RX buffer if possible.
2047 * If not, the STREAM frame is stored to be treated again later.
2048 * We rely on the flow control so that not to store too much STREAM frames.
2049 * Return 1 if succeeded, 0 if not.
2050 */
2051static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
2052 struct quic_stream *strm_frm,
2053 struct quic_conn *qc)
2054{
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002055 int total;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002056 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002057 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002058 struct quic_rx_strm_frm *frm;
2059
2060 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2061 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002062 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002063 return 0;
2064 }
2065
2066 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002067 if (strm_frm->offset.key < strm->rx.offset) {
2068 size_t diff;
2069
2070 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2071 TRACE_PROTO("Already received STREAM data",
2072 QUIC_EV_CONN_PSTRM, qc);
2073 goto out;
2074 }
2075
2076 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2077 diff = strm->rx.offset - strm_frm->offset.key;
2078 strm_frm->offset.key = strm->rx.offset;
2079 strm_frm->len -= diff;
2080 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002081 }
2082
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002083 total = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002084 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002085 int ret;
2086
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002087 if (!qc_get_buf(strm, &strm->rx.buf)) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002088 goto store_frm;
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002089 }
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002090
2091 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002092 total += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002093 strm->rx.offset += ret;
2094 }
2095
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002096 total += qc_treat_rx_strm_frms(strm);
2097 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 +01002098 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécaillef1d38cb2021-12-20 12:02:13 +01002099 return 0;
2100 }
2101
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002102 if (!strm_frm->len)
2103 goto out;
2104
2105 store_frm:
2106 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2107 if (!frm) {
2108 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002109 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002110 return 0;
2111 }
2112
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002113 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002114 quic_rx_packet_refinc(pkt);
2115
2116 out:
2117 return 1;
2118}
2119
2120/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
2121 * streams may be open. The data are copied to the stream RX buffer if possible.
2122 * If not, the STREAM frame is stored to be treated again later.
2123 * We rely on the flow control so that not to store too much STREAM frames.
2124 * Return 1 if succeeded, 0 if not.
2125 */
2126static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
2127 struct quic_stream *strm_frm,
2128 struct quic_conn *qc)
2129{
2130 struct qcs *strm;
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002131 struct eb64_node *strm_node;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002132 struct quic_rx_strm_frm *frm;
2133 size_t strm_frm_len;
2134
2135 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
2136 if (!strm_node) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002137 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002138 return 0;
2139 }
2140
2141 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille10250b22021-12-22 16:13:43 +01002142 if (strm_frm->offset.key < strm->rx.offset) {
2143 size_t diff;
2144
2145 if (strm_frm->offset.key + strm_frm->len <= strm->rx.offset) {
2146 TRACE_PROTO("Already received STREAM data",
2147 QUIC_EV_CONN_PSTRM, qc);
2148 goto out;
2149 }
2150
2151 TRACE_PROTO("Partially already received STREAM data", QUIC_EV_CONN_PSTRM, qc);
2152 diff = strm->rx.offset - strm_frm->offset.key;
2153 strm_frm->offset.key = strm->rx.offset;
2154 strm_frm->len -= diff;
2155 strm_frm->data += diff;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002156 }
2157
2158 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002159 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002160 int ret;
2161
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02002162 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002163 goto store_frm;
2164
2165 /* qc_strm_cpy() will modify the offset, depending on the number
2166 * of bytes copied.
2167 */
2168 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
2169 /* Inform the application of the arrival of this new stream */
2170 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002171 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002172 return 0;
2173 }
2174
Amaury Denoyellea3f222d2021-12-06 11:24:00 +01002175 if (ret)
2176 qcs_notify_recv(strm);
2177
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002178 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002179 }
2180 /* Take this frame into an account for the stream flow control */
2181 strm->rx.offset += strm_frm_len;
2182 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002183 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002184 */
2185 if (!strm_frm->len)
2186 goto out;
2187
2188 store_frm:
2189 frm = new_quic_rx_strm_frm(strm_frm, pkt);
2190 if (!frm) {
2191 TRACE_PROTO("Could not alloc RX STREAM frame",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002192 QUIC_EV_CONN_PSTRM, qc);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002193 return 0;
2194 }
2195
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02002196 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002197 quic_rx_packet_refinc(pkt);
2198
2199 out:
2200 return 1;
2201}
2202
2203static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
2204 struct quic_stream *strm_frm,
2205 struct quic_conn *qc)
2206{
2207 if (strm_frm->id & QCS_ID_DIR_BIT)
2208 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
2209 else
2210 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
2211}
2212
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002213/* Prepare a fast retransmission from <qel> encryption level */
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002214static void qc_prep_fast_retrans(struct quic_enc_level *qel,
2215 struct quic_conn *qc)
2216{
2217 struct eb_root *pkts = &qel->pktns->tx.pkts;
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002218 struct eb64_node *node;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002219 struct quic_tx_packet *pkt;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002220
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002221 pkt = NULL;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002222 pkts = &qel->pktns->tx.pkts;
2223 node = eb64_first(pkts);
Frédéric Lécaillef010f0a2022-01-06 17:28:05 +01002224 /* Skip the empty packet (they have already been retransmitted) */
2225 while (node) {
2226 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2227 if (!LIST_ISEMPTY(&pkt->frms))
2228 break;
2229 node = eb64_next(node);
2230 }
2231
2232 if (!pkt)
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002233 return;
2234
Frédéric Lécaille7065dd02022-01-14 15:51:52 +01002235 qc_requeue_nacked_pkt_tx_frms(qc, &pkt->frms, &qel->pktns->tx.frms);
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002236}
2237
2238/* Prepare a fast retransmission during handshake after a client
2239 * has resent Initial packets. According to the RFC a server may retransmit
2240 * up to two datagrams of Initial packets if did not receive all Initial packets
2241 * and resend them coalescing with others (Handshake here).
2242 * (Listener only).
2243 */
2244static void qc_prep_hdshk_fast_retrans(struct quic_conn *qc)
2245{
2246 struct list itmp = LIST_HEAD_INIT(itmp);
2247 struct list htmp = LIST_HEAD_INIT(htmp);
2248 struct quic_frame *frm, *frmbak;
2249
2250 struct quic_enc_level *iqel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2251 struct quic_enc_level *hqel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2252 struct quic_enc_level *qel = iqel;
2253 struct eb_root *pkts;
2254 struct eb64_node *node;
2255 struct quic_tx_packet *pkt;
2256 struct list *tmp = &itmp;
2257
2258 start:
2259 pkt = NULL;
2260 pkts = &qel->pktns->tx.pkts;
2261 node = eb64_first(pkts);
2262 /* Skip the empty packet (they have already been retransmitted) */
2263 while (node) {
2264 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
2265 if (!LIST_ISEMPTY(&pkt->frms))
2266 break;
2267 node = eb64_next(node);
2268 }
2269
2270 if (!pkt)
2271 goto end;
2272
2273 qel->pktns->tx.pto_probe += 1;
2274 requeue:
2275 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
2276 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, qc, frm);
2277 LIST_DELETE(&frm->list);
2278 LIST_APPEND(tmp, &frm->list);
2279 }
2280
2281 if (qel == iqel) {
2282 if (pkt->next && pkt->next->type == QUIC_PACKET_TYPE_HANDSHAKE) {
2283 pkt = pkt->next;
2284 tmp = &htmp;
2285 hqel->pktns->tx.pto_probe += 1;
2286 goto requeue;
2287 }
2288
2289 qel = hqel;
2290 tmp = &htmp;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002291 goto start;
2292 }
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002293
2294 end:
2295 LIST_SPLICE(&iqel->pktns->tx.frms, &itmp);
2296 LIST_SPLICE(&hqel->pktns->tx.frms, &htmp);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002297}
2298
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002299/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
2300 * as I/O handler context and <qel> as encryption level.
2301 * Returns 1 if succeeded, 0 if failed.
2302 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002303static 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 +01002304 struct quic_enc_level *qel)
2305{
2306 struct quic_frame frm;
2307 const unsigned char *pos, *end;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002308 struct quic_conn *qc = ctx->qc;
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002309 int fast_retrans = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002310
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002311 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312 /* Skip the AAD */
2313 pos = pkt->data + pkt->aad_len;
2314 end = pkt->data + pkt->len;
2315
2316 while (pos < end) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002317 if (!qc_parse_frm(&frm, pkt, &pos, end, qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 goto err;
2319
Frédéric Lécaille1ede8232021-12-23 14:11:25 +01002320 TRACE_PROTO("RX frame", QUIC_EV_CONN_PSTRM, qc, &frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002321 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002322 case QUIC_FT_PADDING:
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002323 break;
2324 case QUIC_FT_PING:
2325 break;
2326 case QUIC_FT_ACK:
2327 {
2328 unsigned int rtt_sample;
2329
2330 rtt_sample = 0;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002331 if (!qc_parse_ack_frm(qc, &frm, qel, &rtt_sample, &pos, end))
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002332 goto err;
2333
2334 if (rtt_sample) {
2335 unsigned int ack_delay;
2336
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002337 ack_delay = !quic_application_pktns(qel->pktns, qc) ? 0 :
Frédéric Lécaille22576a22021-12-28 14:27:43 +01002338 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_CONFIRMED ?
2339 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, qc), qc->max_ack_delay)) :
2340 MS_TO_TICKS(quic_ack_delay_ms(&frm.ack, qc));
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002341 quic_loss_srtt_update(&qc->path->loss, rtt_sample, ack_delay, qc);
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002342 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002343 break;
2344 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002345 case QUIC_FT_STOP_SENDING:
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002346 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002347 case QUIC_FT_CRYPTO:
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002348 {
2349 struct quic_rx_crypto_frm *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002350
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01002351 if (unlikely(qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)) {
2352 /* XXX TO DO: <cfdebug> is used only for the traces. */
2353 struct quic_rx_crypto_frm cfdebug = { };
2354
2355 cfdebug.offset_node.key = frm.crypto.offset;
2356 cfdebug.len = frm.crypto.len;
2357 TRACE_PROTO("CRYPTO data discarded",
2358 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
2359 break;
2360 }
2361
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002362 if (unlikely(frm.crypto.offset < qel->rx.crypto.offset)) {
2363 if (frm.crypto.offset + frm.crypto.len <= qel->rx.crypto.offset) {
2364 /* XXX TO DO: <cfdebug> is used only for the traces. */
2365 struct quic_rx_crypto_frm cfdebug = { };
2366
2367 cfdebug.offset_node.key = frm.crypto.offset;
2368 cfdebug.len = frm.crypto.len;
2369 /* Nothing to do */
2370 TRACE_PROTO("Already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002371 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002372 if (qc_is_listener(ctx->qc) &&
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002373 qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL])
2374 fast_retrans = 1;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002375 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002376 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002377 else {
2378 size_t diff = qel->rx.crypto.offset - frm.crypto.offset;
2379 /* XXX TO DO: <cfdebug> is used only for the traces. */
2380 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002381
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002382 cfdebug.offset_node.key = frm.crypto.offset;
2383 cfdebug.len = frm.crypto.len;
2384 TRACE_PROTO("Partially already received CRYPTO data",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002385 QUIC_EV_CONN_ELRXPKTS, qc, pkt, &cfdebug);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002386 frm.crypto.len -= diff;
2387 frm.crypto.data += diff;
2388 frm.crypto.offset = qel->rx.crypto.offset;
2389 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002390 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002391
2392 if (frm.crypto.offset == qel->rx.crypto.offset) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002393 /* XXX TO DO: <cf> is used only for the traces. */
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002394 struct quic_rx_crypto_frm cfdebug = { };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002395
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002396 cfdebug.offset_node.key = frm.crypto.offset;
2397 cfdebug.len = frm.crypto.len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398 if (!qc_provide_cdata(qel, ctx,
2399 frm.crypto.data, frm.crypto.len,
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002400 pkt, &cfdebug))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401 goto err;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002402
2403 break;
2404 }
2405
2406 /* frm.crypto.offset > qel->rx.crypto.offset */
2407 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2408 if (!cf) {
2409 TRACE_DEVEL("CRYPTO frame allocation failed",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002410 QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002411 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412 }
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002413
2414 cf->offset_node.key = frm.crypto.offset;
2415 cf->len = frm.crypto.len;
2416 cf->data = frm.crypto.data;
2417 cf->pkt = pkt;
2418 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2419 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002420 break;
Frédéric Lécaillef9cb3a92021-12-02 11:25:58 +01002421 }
Frédéric Lécailled8b84432021-12-10 15:18:36 +01002422 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002423 {
2424 struct quic_stream *stream = &frm.stream;
2425
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002426 if (qc_is_listener(ctx->qc)) {
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002427 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2428 goto err;
2429 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2430 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002431
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002432 if (!qc_handle_strm_frm(pkt, stream, qc))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002433 goto err;
2434
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002435 break;
2436 }
Frédéric Lécaillef366cb72021-11-18 10:57:18 +01002437 case QUIC_FT_MAX_DATA:
2438 case QUIC_FT_MAX_STREAM_DATA:
2439 case QUIC_FT_MAX_STREAMS_BIDI:
2440 case QUIC_FT_MAX_STREAMS_UNI:
2441 case QUIC_FT_DATA_BLOCKED:
2442 case QUIC_FT_STREAM_DATA_BLOCKED:
2443 case QUIC_FT_STREAMS_BLOCKED_BIDI:
2444 case QUIC_FT_STREAMS_BLOCKED_UNI:
2445 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002446 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002447 break;
2448 case QUIC_FT_CONNECTION_CLOSE:
2449 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle5154e7a2021-12-08 14:51:04 +01002450 /* warn the mux to close the connection */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002451 qc->qcc->flags |= QC_CF_CC_RECV;
2452 tasklet_wakeup(qc->qcc->wait_event.tasklet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002453 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002454 case QUIC_FT_HANDSHAKE_DONE:
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002455 if (qc_is_listener(ctx->qc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002456 goto err;
2457
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002458 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002459 break;
2460 default:
2461 goto err;
2462 }
2463 }
2464
Frédéric Lécaille04e63aa2022-01-17 18:16:27 +01002465 if (fast_retrans)
2466 qc_prep_hdshk_fast_retrans(qc);
Frédéric Lécaille3bb457c2021-12-30 16:14:20 +01002467
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2469 * has successfully parse a Handshake packet. The Initial encryption must also
2470 * be discarded.
2471 */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002472 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && qc_is_listener(ctx->qc)) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002473 int state = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002474
2475 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002476 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01002477 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, qc);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002478 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002479 qc_set_timer(ctx->qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002480 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002481 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002482 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
Amaury Denoyelle17a74162021-12-21 14:45:39 +01002483 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002484 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002485 }
2486
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002487 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002488 return 1;
2489
2490 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002491 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 return 0;
2493}
2494
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002495/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002496 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002497 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2498 * This function must be called only after having built a correct datagram.
2499 * Always succeeds.
2500 */
2501static inline void qc_set_dg(struct cbuf *cbuf,
2502 uint16_t dglen, struct quic_tx_packet *pkt)
2503{
2504 write_u16(cb_wr(cbuf), dglen);
2505 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2506 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2507}
2508
Frédéric Lécaillee2660e62021-11-23 11:36:51 +01002509/* Prepare as much as possible packets into <qr> ring buffer for
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002510 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2511 * several packets in the same datagram. A header made of two fields is added
2512 * to each datagram: the datagram length followed by the address of the first
2513 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 * Returns 1 if succeeded, or 0 if something wrong happened.
2515 */
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01002516static int qc_prep_pkts(struct quic_conn *qc, struct qring *qr,
2517 enum quic_tls_enc_level tel,
2518 enum quic_tls_enc_level next_tel)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002519{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002520 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002521 struct cbuf *cbuf;
2522 unsigned char *end_buf, *end, *pos, *spos;
2523 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2524 /* length of datagrams */
2525 uint16_t dglen;
2526 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002527 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002528 /* Each datagram is prepended with its length followed by the
2529 * address of the first packet in the datagram.
2530 */
2531 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002532
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002533 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, qc);
2534
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002535 total = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002536 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002537 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002538 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002539 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002540 cbuf = qr->cbuf;
2541 spos = pos = cb_wr(cbuf);
2542 /* Leave at least <dglen> bytes at the end of this buffer
2543 * to ensure there is enough room to mark the end of prepared
2544 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002545 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002546 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2547 first_pkt = prv_pkt = NULL;
2548 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002549 int err, probe, ack, cc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002550 enum quic_pkt_type pkt_type;
2551
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002552 TRACE_POINT(QUIC_EV_CONN_PHPKTS, qc, qel);
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002553 probe = ack = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01002554 cc = HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IMMEDIATE_CLOSE;
2555 if (!cc) {
Frédéric Lécaille94fca872022-01-19 18:54:18 +01002556 probe = qel->pktns->tx.pto_probe;
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002557 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002558 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002559 /* Do not build any more packet if the TX secrets are not available or
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01002560 * 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 +01002561 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002562 * and if there is no more CRYPTO data available or in flight
2563 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002564 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002565 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002566 (!cc && !ack && !probe &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002567 (LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002568 qc->path->prep_in_flight >= qc->path->cwnd))) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002569 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002570 /* Set the current datagram as prepared into <cbuf> if
2571 * the was already a correct packet which was previously written.
2572 */
2573 if (prv_pkt)
2574 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 break;
2576 }
2577
2578 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002579 if (!prv_pkt) {
2580 /* Leave room for the datagram header */
2581 pos += dg_headlen;
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01002582 if (!quic_peer_validated_addr(qc) && qc_is_listener(qc)) {
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01002583 end = pos + QUIC_MIN(qc->path->mtu, 3 * qc->rx.bytes - qc->tx.prep_bytes);
2584 }
2585 else {
2586 end = pos + qc->path->mtu;
2587 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002588 }
2589
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002590 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01002591 pkt_type, ack, probe, cc, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002592 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002593 if (err < 0) {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002594 if (ack)
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01002595 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002596 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002597 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002598 case -2:
2599 goto err;
2600 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002601 /* If there was already a correct packet present, set the
2602 * current datagram as prepared into <cbuf>.
2603 */
2604 if (prv_pkt) {
2605 qc_set_dg(cbuf, dglen, first_pkt);
2606 goto stop_build;
2607 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002608 goto out;
2609 default:
Frédéric Lécaille63556772021-12-29 17:18:21 +01002610 break;
2611 }
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002612
Frédéric Lécaille63556772021-12-29 17:18:21 +01002613 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2614 if (!cur_pkt)
2615 goto err;
2616
2617 total += cur_pkt->len;
2618 /* keep trace of the first packet in the datagram */
2619 if (!first_pkt)
2620 first_pkt = cur_pkt;
2621 /* Attach the current one to the previous one */
2622 if (prv_pkt)
2623 prv_pkt->next = cur_pkt;
2624 /* Let's say we have to build a new dgram */
2625 prv_pkt = NULL;
2626 dglen += cur_pkt->len;
2627 /* Client: discard the Initial encryption keys as soon as
2628 * a handshake packet could be built.
2629 */
2630 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
2631 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2632 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2633 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, qc);
2634 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2635 qc_set_timer(qc);
Frédéric Lécaillea6255f52022-01-19 17:29:48 +01002636 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01002637 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns);
Frédéric Lécaille63556772021-12-29 17:18:21 +01002638 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
2639 }
2640 /* If the data for the current encryption level have all been sent,
2641 * select the next level.
2642 */
2643 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002644 (LIST_ISEMPTY(&qel->pktns->tx.frms))) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002645 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2646 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2647 next_tel = QUIC_TLS_ENC_LEVEL_APP;
2648 tel = next_tel;
2649 qel = &qc->els[tel];
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002650 if (!LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille63556772021-12-29 17:18:21 +01002651 /* If there is data for the next level, do not
2652 * consume a datagram.
2653 */
2654 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002655 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002656 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002657 /* If we have to build a new datagram, set the current datagram as
2658 * prepared into <cbuf>.
2659 */
2660 if (!prv_pkt) {
2661 qc_set_dg(cbuf, dglen, first_pkt);
2662 first_pkt = NULL;
2663 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002664 padding = 0;
2665 }
2666 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002667 (!qc_is_listener(qc) ||
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002668 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2669 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002670 }
2671 }
2672
2673 stop_build:
2674 /* Reset <wr> writer index if in front of <rd> index */
2675 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2676 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2677
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002678 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002679 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2680 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2681 /* Mark the end of contiguous data for the reader */
2682 write_u16(cb_wr(cbuf), 0);
2683 cb_add(cbuf, sizeof(uint16_t));
2684 }
2685 }
2686
2687 if (rd && rd <= cbuf->wr) {
2688 cb_wr_reset(cbuf);
Frédéric Lécaille99942d62022-01-07 14:32:31 +01002689 /* Let's try to reuse this buffer */
2690 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002691 }
2692 }
2693
2694 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002695 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002696 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697
2698 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002699 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002700 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002701}
2702
2703/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002704 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002705 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002706int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002707{
2708 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002709 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002710
Amaury Denoyellec15dd922021-12-21 11:41:52 +01002711 qc = ctx->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002712 cbuf = qr->cbuf;
2713 while (cb_contig_data(cbuf)) {
2714 unsigned char *pos;
2715 struct buffer tmpbuf = { };
2716 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2717 uint16_t dglen;
2718 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002719 unsigned int time_sent;
2720
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002721 pos = cb_rd(cbuf);
2722 dglen = read_u16(pos);
2723 /* End of prepared datagrams.
2724 * Reset the reader index only if in front of the writer index.
2725 */
2726 if (!dglen) {
2727 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2728
2729 if (wr && wr < cbuf->rd) {
2730 cb_rd_reset(cbuf);
2731 continue;
2732 }
2733 break;
2734 }
2735
2736 pos += sizeof dglen;
2737 first_pkt = read_ptr(pos);
2738 pos += sizeof first_pkt;
2739 tmpbuf.area = (char *)pos;
2740 tmpbuf.size = tmpbuf.data = dglen;
2741
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002742 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002743 for (pkt = first_pkt; pkt; pkt = pkt->next)
2744 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002745 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002746 &tmpbuf, tmpbuf.data, 0) <= 0) {
2747 for (pkt = first_pkt; pkt; pkt = pkt->next)
2748 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002749 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002750 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002751
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002752 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002753 qc->tx.bytes += tmpbuf.data;
2754 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002755
2756 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2757 pkt->time_sent = time_sent;
2758 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2759 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002760 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002762 qc->path->in_flight += pkt->in_flight_len;
2763 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2764 if (pkt->in_flight_len)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01002765 qc_set_timer(qc);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01002766 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, qc, pkt);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002767 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002768 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002769 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002770 }
2771 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772
2773 return 1;
2774}
2775
2776/* Build all the frames which must be sent just after the handshake have succeeded.
2777 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2778 * a HANDSHAKE_DONE frame.
2779 * Return 1 if succeeded, 0 if not.
2780 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002781static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002782{
2783 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002784 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785 struct quic_frame *frm;
2786
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002787 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01002789 if (qc_is_listener(qc)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002790 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002791 if (!frm)
2792 return 0;
2793
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002795 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002796 }
2797
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002798 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799 struct quic_connection_id *cid;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002800 struct listener *l = __objt_listener(qc->conn->target);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002801
2802 frm = pool_alloc(pool_head_quic_frame);
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002803 if (!frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002804 goto err;
2805
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01002806 cid = new_quic_cid(&qc->cids, qc, i);
2807 if (!cid)
2808 goto err;
2809
2810 /* insert the allocated CID in the receiver tree */
2811 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
2812 ebmb_insert(&l->rx.cids, &cid->node, cid->cid.len);
2813 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
2814
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002815 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01002816 LIST_APPEND(&qel->pktns->tx.frms, &frm->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817 }
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01002818 HA_ATOMIC_OR(&qc->flags, QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002819
2820 return 1;
2821
2822 err:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823 return 0;
2824}
2825
2826/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002827void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002828{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002829 struct eb64_node *n;
2830 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002831
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002832 n = eb64_first(&arngs->root);
2833 while (n) {
2834 struct eb64_node *next;
2835
2836 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2837 next = eb64_next(n);
2838 eb64_delete(n);
2839 free(ar);
2840 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002841 }
2842}
2843
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002844/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2845 * descending order.
2846 */
2847static inline size_t sack_gap(struct quic_arng_node *p,
2848 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002849{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002850 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002851}
2852
2853
2854/* Remove the last elements of <ack_ranges> list of ack range updating its
2855 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002856 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002857 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002858static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002860 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002861
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002862 last = eb64_last(&arngs->root);
2863 while (last && arngs->enc_sz > limit) {
2864 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002866 prev = eb64_prev(last);
2867 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002868 return 0;
2869
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002870 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2871 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2872 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2873 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2874 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2875 --arngs->sz;
2876 eb64_delete(last);
2877 pool_free(pool_head_quic_arng, last);
2878 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002879 }
2880
2881 return 1;
2882}
2883
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002884/* Set the encoded size of <arngs> QUIC ack ranges. */
2885static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2886{
2887 struct eb64_node *node, *next;
2888 struct quic_arng_node *ar, *ar_next;
2889
2890 node = eb64_last(&arngs->root);
2891 if (!node)
2892 return;
2893
2894 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2895 arngs->enc_sz = quic_int_getsize(ar->last) +
2896 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2897
2898 while ((next = eb64_prev(node))) {
2899 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2900 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2901 quic_int_getsize(ar_next->last - ar_next->first.key);
2902 node = next;
2903 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2904 }
2905}
2906
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002907/* Insert <ar> ack range into <argns> tree of ack ranges.
2908 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002909 */
2910static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002911struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2912 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002913{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002914 struct quic_arng_node *new_ar;
2915
2916 new_ar = pool_alloc(pool_head_quic_arng);
2917 if (new_ar) {
2918 new_ar->first.key = ar->first;
2919 new_ar->last = ar->last;
2920 eb64_insert(&arngs->root, &new_ar->first);
2921 arngs->sz++;
2922 }
2923
2924 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002925}
2926
2927/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002928 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002929 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002930 *
2931 * Descending order
2932 * ------------->
2933 * range1 range2
2934 * ..........|--------|..............|--------|
2935 * ^ ^ ^ ^
2936 * | | | |
2937 * last1 first1 last2 first2
2938 * ..........+--------+--------------+--------+......
2939 * diff1 gap12 diff2
2940 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002941 * To encode the previous list of ranges we must encode integers as follows in
2942 * descending order:
2943 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002944 * with diff1 = last1 - first1
2945 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002946 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002947 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002948 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002949int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2950 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002951{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002952 struct eb64_node *le;
2953 struct quic_arng_node *new_node;
2954 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002955
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002956 new = NULL;
2957 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002958 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002959 if (!new_node)
2960 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002961
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002962 goto out;
2963 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002964
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002965 le = eb64_lookup_le(&arngs->root, ar->first);
2966 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002967 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002968 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002969 return 0;
Frédéric Lécaille0e257832021-11-16 10:54:19 +01002970
2971 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002972 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002973 else {
2974 struct quic_arng_node *le_ar =
2975 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002976
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002977 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002978 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002979 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002981 if (le_ar->last + 1 >= ar->first) {
2982 le_ar->last = ar->last;
2983 new = le;
2984 new_node = le_ar;
2985 }
2986 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002987 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002988 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002990
2991 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002992 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002993 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002994
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002995 /* Verify that the new inserted node does not overlap the nodes
2996 * which follow it.
2997 */
2998 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002999 struct eb64_node *next;
3000 struct quic_arng_node *next_node;
3001
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003002 while ((next = eb64_next(new))) {
3003 next_node =
3004 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02003005 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003006 break;
3007
3008 if (next_node->last > new_node->last)
3009 new_node->last = next_node->last;
3010 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02003011 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003012 /* Decrement the size of these ranges. */
3013 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 }
3016
Frédéric Lécaille82b86522021-08-10 09:54:03 +02003017 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003018 quic_arngs_set_enc_sz(arngs);
3019
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020 return 1;
3021}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022/* Remove the header protection of packets at <el> encryption level.
3023 * Always succeeds.
3024 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003025static 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 +01003026{
3027 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003028 struct quic_rx_packet *pqpkt;
3029 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030 struct quic_enc_level *app_qel;
3031
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003032 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, qc);
3033 app_qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003034 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003035 if (el == app_qel && qc_is_listener(qc) &&
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003036 HA_ATOMIC_LOAD(&qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003037 TRACE_PROTO("hp not removed (handshake not completed)",
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003038 QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039 goto out;
3040 }
3041 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003042 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003043 if (!qc_do_rm_hp(qc, pqpkt, tls_ctx, el->pktns->rx.largest_pn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003044 pqpkt->data + pqpkt->pn_offset,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003045 pqpkt->data, pqpkt->data + pqpkt->len)) {
3046 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003047 /* XXX TO DO XXX */
3048 }
3049 else {
3050 /* The AAD includes the packet number field */
3051 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
3052 /* Store the packet into the tree of packets to decrypt. */
3053 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003054 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003055 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
3056 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003057 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003058 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, qc, pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003059 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003060 MT_LIST_DELETE_SAFE(pkttmp1);
3061 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003062 }
3063
3064 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003065 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003066}
3067
3068/* Process all the CRYPTO frame at <el> encryption level.
3069 * Return 1 if succeeded, 0 if not.
3070 */
3071static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003072 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003073{
3074 struct eb64_node *node;
3075
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003076 node = eb64_first(&el->rx.crypto.frms);
3077 while (node) {
3078 struct quic_rx_crypto_frm *cf;
3079
3080 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
3081 if (cf->offset_node.key != el->rx.crypto.offset)
3082 break;
3083
3084 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
3085 goto err;
3086
3087 node = eb64_next(node);
3088 quic_rx_packet_refdec(cf->pkt);
3089 eb64_delete(&cf->offset_node);
3090 pool_free(pool_head_quic_rx_crypto_frm, cf);
3091 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003092 return 1;
3093
3094 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003095 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003096 return 0;
3097}
3098
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003099/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003100 * 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 +02003101 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003102 * Return 1 if succeeded, 0 if not.
3103 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003104int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
3105 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003106{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003107 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003108 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003109 struct quic_conn *qc = ctx->conn->qc;
3110 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003111
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003112 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003113 qel = cur_el;
3114 next_tel:
3115 if (!qel)
3116 goto out;
3117
3118 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3119 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003120 while (node) {
3121 struct quic_rx_packet *pkt;
3122
3123 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003124 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003125 ctx->qc, pkt, NULL, ctx->ssl);
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01003126 if (!qc_pkt_decrypt(pkt, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003127 /* Drop the packet */
3128 TRACE_PROTO("packet decryption failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003129 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 }
3131 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003132 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003133 /* Drop the packet */
3134 TRACE_PROTO("packet parsing failed -> dropped",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003135 QUIC_EV_CONN_ELRXPKTS, ctx->qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003136 }
3137 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003138 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
3139
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003140 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003141 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
Frédéric Lécaille25eeebe2021-12-16 11:21:52 +01003142 HA_ATOMIC_BTS(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003143 if (pkt->pn > largest_pn)
3144 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003145 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003146 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147 TRACE_DEVEL("Could not update ack range list",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003148 QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149 }
3150 }
3151 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003152 eb64_delete(&pkt->pn_node);
3153 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003154 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003155 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003156
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02003157 /* Update the largest packet number. */
3158 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003159 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
3160 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003161 goto err;
3162
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003163 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003164 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003165 qel = next_el;
3166 goto next_tel;
3167 }
3168
3169 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003170 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003171 return 1;
3172
3173 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003174 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003175 return 0;
3176}
3177
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003178/* QUIC connection packet handler task. */
3179struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003180{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003181 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003182 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003183 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003184 enum quic_tls_enc_level tel, next_tel;
3185 struct quic_enc_level *qel, *next_qel;
3186 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003187 struct qring *qr; // Tx ring
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003188 int st, force_ack, zero_rtt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003189
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003190 ctx = context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003191 qc = ctx->qc;
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003192 TRACE_ENTER(QUIC_EV_CONN_HDSHK, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003193 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003194 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003195 TRACE_PROTO("state", QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01003196 if (HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_IO_CB_WAKEUP) {
3197 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
3198 /* The I/O handler has been woken up by the dgram listener
3199 * after the anti-amplification was reached.
3200 */
3201 qc_set_timer(qc);
3202 if (tick_isset(qc->timer) && tick_is_lt(qc->timer, now_ms))
3203 task_wakeup(qc->timer_task, TASK_WOKEN_MSG);
3204 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003205 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille4137b2d2021-12-17 18:24:16 +01003206 zero_rtt = st < QUIC_HS_ST_COMPLETE &&
3207 (!MT_LIST_ISEMPTY(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA].rx.pqpkts) ||
3208 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_EARLY_DATA]));
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003209 start:
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003210 if (st >= QUIC_HS_ST_COMPLETE &&
3211 qc_el_rx_pkts(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE])) {
3212 TRACE_PROTO("remaining Handshake packets", QUIC_EV_CONN_PHPKTS, qc);
3213 /* There may be remaining Handshake packets to treat and acknowledge. */
3214 tel = QUIC_TLS_ENC_LEVEL_HANDSHAKE;
3215 next_tel = QUIC_TLS_ENC_LEVEL_APP;
3216 }
3217 else if (!quic_get_tls_enc_levels(&tel, &next_tel, st, zero_rtt))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003218 goto err;
3219
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02003220 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02003221 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003222
3223 next_level:
3224 tls_ctx = &qel->tls_ctx;
3225
3226 /* If the header protection key for this level has been derived,
3227 * remove the packet header protections.
3228 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003229 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003230 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003231 qc_rm_hp_pkts(qc, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003232
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003233 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
3234 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3235 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003236 goto err;
3237
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003238 if (zero_rtt && next_qel && !MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) &&
3239 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET)) {
3240 qel = next_qel;
3241 next_qel = NULL;
3242 goto next_level;
3243 }
3244
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003245 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003246 if (st >= QUIC_HS_ST_COMPLETE) {
3247 if (!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_POST_HANDSHAKE_FRAMES_BUILT) &&
3248 !quic_build_post_handshake_frames(qc))
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003249 goto err;
Frédéric Lécaillefee7ba62021-12-06 12:09:08 +01003250
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003251 if (!(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].tls_ctx.rx.flags &
3252 QUIC_FL_TLS_SECRETS_DCD)) {
3253 /* Discard the Handshake keys. */
3254 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
3255 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, qc);
3256 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
3257 qc_set_timer(qc);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003258 qc_el_rx_pkts_del(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécaillee87524d2022-01-19 17:48:40 +01003259 qc_release_pktns_frms(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns);
Frédéric Lécaillede6f7c52022-01-03 17:25:53 +01003260 }
3261
3262 if (qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) {
3263 /* There may be remaining handshake to build (acks) */
3264 st = QUIC_HS_ST_SERVER_HANDSHAKE;
3265 }
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003266 }
3267
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003268 if (!qr)
3269 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
Frédéric Lécaillebec186d2022-01-12 15:32:55 +01003270 /* A listener does not send any O-RTT packet. O-RTT packet number space must not
3271 * be considered.
3272 */
3273 if (!quic_get_tls_enc_levels(&tel, &next_tel, st, 0))
Frédéric Lécailleee2b8b32022-01-03 11:14:30 +01003274 goto err;
3275 ret = qc_prep_pkts(qc, qr, tel, next_tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003276 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003277 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003278 else if (ret == 0)
3279 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003280
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003281 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003282 goto err;
3283
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003284 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003285 /* Check if there is something to do for the next level.
3286 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003287 if (next_qel && next_qel != qel &&
3288 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaille7d807c92021-12-06 08:56:38 +01003289 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || qc_el_rx_pkts(next_qel))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02003291 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003292 goto next_level;
3293 }
3294
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003295 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003296 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, qc, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003297 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003298
3299 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003300 if (qr)
3301 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003302 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02003303 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003304}
3305
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003306/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003307static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
3308{
3309 int i;
3310
3311 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
3312 if (qel->tx.crypto.bufs[i]) {
3313 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
3314 qel->tx.crypto.bufs[i] = NULL;
3315 }
3316 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003317 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003318}
3319
3320/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003321 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003322 * Returns 1 if succeeded, 0 if not.
3323 */
3324static int quic_conn_enc_level_init(struct quic_conn *qc,
3325 enum quic_tls_enc_level level)
3326{
3327 struct quic_enc_level *qel;
3328
3329 qel = &qc->els[level];
3330 qel->level = quic_to_ssl_enc_level(level);
3331 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
3332 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
3333 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
3334 qel->tls_ctx.rx.flags = 0;
3335 qel->tls_ctx.tx.flags = 0;
Frédéric Lécaillefc768ec2021-11-23 21:02:04 +01003336 qel->tls_ctx.flags = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003337
3338 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003339 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02003340 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02003341 qel->rx.crypto.offset = 0;
3342 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003343
3344 /* Allocate only one buffer. */
3345 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
3346 if (!qel->tx.crypto.bufs)
3347 goto err;
3348
3349 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
3350 if (!qel->tx.crypto.bufs[0])
3351 goto err;
3352
3353 qel->tx.crypto.bufs[0]->sz = 0;
3354 qel->tx.crypto.nb_buf = 1;
3355
3356 qel->tx.crypto.sz = 0;
3357 qel->tx.crypto.offset = 0;
3358
3359 return 1;
3360
3361 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003362 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003363 return 0;
3364}
3365
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003366/* Increment the <qc> refcount.
3367 *
3368 * This operation must be conducted when manipulating the quic_conn outside of
3369 * the connection pinned thread. These threads can only retrieve the connection
3370 * in the CID tree, so this function must be conducted under the CID lock.
3371 */
3372static inline void quic_conn_take(struct quic_conn *qc)
3373{
3374 HA_ATOMIC_INC(&qc->refcount);
3375}
3376
3377/* Decrement the <qc> refcount. If the refcount is zero *BEFORE* the
Ilya Shipitsin37d3e382022-01-07 14:46:15 +05003378 * subtraction, the quic_conn is freed.
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003379 */
3380static void quic_conn_drop(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003381{
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003382 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383 int i;
3384
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003385 if (!qc)
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003386 return;
3387
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003388 if (HA_ATOMIC_FETCH_SUB(&qc->refcount, 1))
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003389 return;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003390
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003391 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003392 if (conn_ctx)
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003393 pool_free(pool_head_quic_conn_ctx, conn_ctx);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003394
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003395 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003396 quic_conn_enc_level_uninit(&qc->els[i]);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003397
Amaury Denoyelle67e6cd52021-12-13 17:07:03 +01003398 pool_free(pool_head_quic_conn_rxbuf, qc->rx.buf.area);
3399 pool_free(pool_head_quic_conn, qc);
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003400 TRACE_PROTO("QUIC conn. freed", QUIC_EV_CONN_FREED, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003401}
3402
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003403/* Release the quic_conn <qc>. It will decrement its refcount so that the
3404 * connection will be freed once all threads have finished to work with it. The
3405 * connection is removed from the CIDs tree and thus cannot be found by other
3406 * threads after it.
3407 *
3408 * Do not use <qc> after it as it may be freed. This function must only be
3409 * called by the thread responsible of the quic_conn tasklet.
3410 */
3411static void quic_conn_release(struct quic_conn *qc)
3412{
3413 /* remove the connection from receiver cids trees */
3414 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3415 ebmb_delete(&qc->odcid_node);
3416 ebmb_delete(&qc->scid_node);
3417 free_quic_conn_cids(qc);
3418 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->li->rx.cids_lock);
3419
3420 quic_conn_drop(qc);
3421}
3422
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003423void quic_close(struct connection *conn, void *xprt_ctx)
3424{
3425 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
3426 struct quic_conn *qc = conn_ctx->conn->qc;
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003427
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003428 TRACE_ENTER(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle0a29e132021-12-23 15:06:56 +01003429 /* This task must be deleted by the connection-pinned thread. */
3430 if (qc->timer_task) {
3431 task_destroy(qc->timer_task);
3432 qc->timer_task = NULL;
3433 }
3434
Amaury Denoyelle9ab2fb32022-01-12 14:54:23 +01003435 if (conn_ctx->wait_event.tasklet) {
3436 tasklet_free(conn_ctx->wait_event.tasklet);
3437 conn_ctx->wait_event.tasklet = NULL;
3438 }
3439
Frédéric Lécailleba85acd2022-01-11 14:43:50 +01003440 TRACE_LEAVE(QUIC_EV_CONN_CLOSE, qc);
Amaury Denoyelle9c4da932022-01-21 14:54:58 +01003441
Amaury Denoyelle2eb7b302022-01-20 16:40:36 +01003442 /* TODO for now release the quic_conn on notification by the upper
3443 * layer. It could be useful to delay it if there is remaining data to
3444 * send or data to be acked.
3445 */
3446 quic_conn_release(qc);
Amaury Denoyelle414cac52021-09-22 11:14:37 +02003447}
3448
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003449/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01003450static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003451{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003452 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003453 struct quic_conn *qc;
3454 struct quic_pktns *pktns;
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003455 int i, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003456
3457 conn_ctx = task->context;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01003458 qc = conn_ctx->qc;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003459 TRACE_ENTER(QUIC_EV_CONN_PTIMER, qc,
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01003460 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003461 task->expire = TICK_ETERNITY;
3462 pktns = quic_loss_pktns(qc);
3463 if (tick_isset(pktns->tx.loss_time)) {
3464 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
3465
3466 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
3467 if (!LIST_ISEMPTY(&lost_pkts))
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003468 qc_release_lost_pkts(qc, pktns, &lost_pkts, now_ms);
3469 qc_set_timer(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003470 goto out;
3471 }
3472
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003473 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003474 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003475 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaille0fa553d2022-01-17 14:26:12 +01003476 if (pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL]) {
3477 pktns->tx.pto_probe = 1;
3478 if (qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.in_flight)
3479 qc->pktns[QUIC_TLS_PKTNS_HANDSHAKE].tx.pto_probe = 1;
3480 }
3481 else {
3482 pktns->tx.pto_probe = 2;
3483 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003484 }
Frédéric Lécaille1aa57d32022-01-12 09:46:02 +01003485 else if (!qc_is_listener(qc) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003486 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3487 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3488
3489 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3490 hel->pktns->tx.pto_probe = 1;
3491 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3492 iel->pktns->tx.pto_probe = 1;
3493 }
Frédéric Lécaillea56054e2021-12-31 16:35:28 +01003494
3495 for (i = QUIC_TLS_ENC_LEVEL_INITIAL; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
3496 int j;
3497
3498 if (i == QUIC_TLS_ENC_LEVEL_APP && !quic_peer_validated_addr(qc))
3499 continue;
3500
3501 for (j = 0; j < qc->els[i].pktns->tx.pto_probe; j++)
3502 qc_prep_fast_retrans(&qc->els[i], qc);
3503 }
3504
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003505 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3506 qc->path->loss.pto_count++;
3507
3508 out:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003509 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, qc, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510
3511 return task;
3512}
3513
3514/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3515 * connections used to identify the first Initial packets of client connecting
3516 * to listeners. This parameter must be NULL for QUIC connections attached
3517 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3518 * <scid> is the source connection ID with <scid_len> as length.
3519 * Returns 1 if succeeded, 0 if not.
3520 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003521static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003522 unsigned char *dcid, size_t dcid_len, size_t dcid_addr_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003523 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524{
3525 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003526 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003527 /* Initial CID. */
3528 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003529 char *buf_area;
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003530 struct listener *l = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003531
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003532 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003533 qc = pool_zalloc(pool_head_quic_conn);
3534 if (!qc) {
3535 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3536 goto err;
3537 }
3538
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003539 HA_ATOMIC_STORE(&qc->refcount, 0);
3540
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003541 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3542 if (!buf_area) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003543 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003544 goto err;
3545 }
3546
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003547 qc->cids = EB_ROOT;
3548 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003549 if (server) {
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003550 l = owner;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003551
Frédéric Lécaille2fe8b3b2022-01-10 12:10:10 +01003552 qc->flags |= QUIC_FL_CONN_LISTENER;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003553 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003554 /* Copy the initial DCID with the address. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003555 qc->odcid.len = dcid_len;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01003556 qc->odcid.addrlen = dcid_addr_len;
3557 memcpy(qc->odcid.data, dcid, dcid_len + dcid_addr_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558
Amaury Denoyelle42b9f1c2021-11-24 15:29:53 +01003559 /* copy the packet SCID to reuse it as DCID for sending */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560 if (scid_len)
3561 memcpy(qc->dcid.data, scid, scid_len);
3562 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003563 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003564 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003565 }
3566 /* QUIC Client (outgoing connection to servers) */
3567 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003568 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569 if (dcid_len)
3570 memcpy(qc->dcid.data, dcid, dcid_len);
3571 qc->dcid.len = dcid_len;
3572 }
3573
3574 /* Initialize the output buffer */
3575 qc->obuf.pos = qc->obuf.data;
3576
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003577 icid = new_quic_cid(&qc->cids, qc, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003578 if (!icid) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003579 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003580 goto err;
3581 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003582
Amaury Denoyelled6a352a2021-11-24 15:32:46 +01003583 /* insert the allocated CID in the receiver tree */
3584 if (server) {
3585 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
3586 ebmb_insert(&l->rx.cids, &icid->node, icid->cid.len);
3587 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
3588 }
3589
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590 /* Select our SCID which is the first CID with 0 as sequence number. */
3591 qc->scid = icid->cid;
3592
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593 /* Packet number spaces initialization. */
3594 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3595 quic_pktns_init(&qc->pktns[i]);
3596 /* QUIC encryption level context initialization. */
3597 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003598 if (!quic_conn_enc_level_init(qc, i)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003599 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003601 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003602 /* Initialize the packet number space. */
3603 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3604 }
3605
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003606 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003607 qc->tps_tls_ext = qc->version & 0xff000000 ?
3608 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3609 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003610 /* TX part. */
3611 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003612 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3613 qc->tx.wbuf = qc->tx.rbuf = 0;
3614 qc->tx.bytes = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003615 /* RX part. */
3616 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003617 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003618 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3619 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3620 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003621 if (!quic_tls_ku_init(qc)) {
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003622 TRACE_PROTO("Key update initialization failed", QUIC_EV_CONN_INIT, qc);
Frédéric Lécaille40df78f2021-11-30 10:59:37 +01003623 goto err;
3624 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003625
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003626 /* XXX TO DO: Only one path at this time. */
3627 qc->path = &qc->paths[0];
3628 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3629
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003630 TRACE_LEAVE(QUIC_EV_CONN_INIT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003631
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003632 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003633
3634 err:
Amaury Denoyellee770ce32021-12-21 14:51:56 +01003635 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT, qc ? qc : NULL);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01003636 quic_conn_drop(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003637 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003638}
3639
3640/* Initialize the timer task of <qc> QUIC connection.
3641 * Returns 1 if succeeded, 0 if not.
3642 */
3643static int quic_conn_init_timer(struct quic_conn *qc)
3644{
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01003645 /* Attach this task to the same thread ID used for the connection */
3646 qc->timer_task = task_new(1UL << qc->tid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003647 if (!qc->timer_task)
3648 return 0;
3649
3650 qc->timer = TICK_ETERNITY;
3651 qc->timer_task->process = process_timer;
3652 qc->timer_task->context = qc->conn->xprt_ctx;
3653
3654 return 1;
3655}
3656
3657/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3658 * past one byte of this buffer.
3659 */
3660static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3661 struct quic_rx_packet *pkt)
3662{
3663 unsigned char dcid_len, scid_len;
3664
3665 /* Version */
3666 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3667 return 0;
3668
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003669 /* Destination Connection ID Length */
3670 dcid_len = *(*buf)++;
3671 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3672 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3673 /* XXX MUST BE DROPPED */
3674 return 0;
3675
3676 if (dcid_len) {
3677 /* Check that the length of this received DCID matches the CID lengths
3678 * of our implementation for non Initials packets only.
3679 */
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01003680 if (pkt->type != QUIC_PACKET_TYPE_INITIAL &&
3681 pkt->type != QUIC_PACKET_TYPE_0RTT &&
Amaury Denoyelled4962512021-12-14 17:17:28 +01003682 dcid_len != QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003683 return 0;
3684
3685 memcpy(pkt->dcid.data, *buf, dcid_len);
3686 }
3687
3688 pkt->dcid.len = dcid_len;
3689 *buf += dcid_len;
3690
3691 /* Source Connection ID Length */
3692 scid_len = *(*buf)++;
3693 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3694 /* XXX MUST BE DROPPED */
3695 return 0;
3696
3697 if (scid_len)
3698 memcpy(pkt->scid.data, *buf, scid_len);
3699 pkt->scid.len = scid_len;
3700 *buf += scid_len;
3701
3702 return 1;
3703}
3704
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003705/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3706 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3707 * encryption level matching with the packet type. <*qel> may be null if not found.
3708 * Note that <ctx> may be null (for Initial packets).
3709 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003710static 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 +02003711 struct quic_enc_level **qel)
3712{
3713 enum quic_tls_enc_level tel;
3714
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003715 tel = quic_packet_type_enc_level(pkt->type);
3716 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3717 *qel = NULL;
3718 return 0;
3719 }
3720
3721 *qel = &qc->els[tel];
Frédéric Lécaille917a7db2022-01-03 17:00:35 +01003722 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD)
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003723 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003724
3725 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003726 (tel != QUIC_TLS_ENC_LEVEL_APP ||
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003727 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003728 return 1;
3729
3730 return 0;
3731}
3732
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003733/* Insert <pkt> RX packet in its <qel> RX packets tree */
3734static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3735{
3736 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille2ce5acf2021-12-20 14:41:19 +01003737 quic_rx_packet_refinc(pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003738 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3739 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3740 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003741}
3742
3743/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003744 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3745 * byte past the end of the buffer containing this packet and <beg> the address of
3746 * the packet first byte.
3747 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3748 * Returns 1 if succeeded, 0 if not.
3749 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003750static inline int qc_try_rm_hp(struct quic_conn *qc,
3751 struct quic_rx_packet *pkt,
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003752 unsigned char *buf, unsigned char *beg,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003753 const unsigned char *end,
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003754 struct quic_enc_level **el)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003755{
3756 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003757 struct quic_enc_level *qel;
3758 /* Only for traces. */
3759 struct quic_rx_packet *qpkt_trace;
3760
3761 qpkt_trace = NULL;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003762 TRACE_ENTER(QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003763 /* The packet number is here. This is also the start minus
3764 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3765 * protection.
3766 */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01003767 pn = buf;
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003768 if (qc_pkt_may_rm_hp(qc, pkt, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003769 /* Note that the following function enables us to unprotect the packet
3770 * number and its length subsequently used to decrypt the entire
3771 * packets.
3772 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003773 if (!qc_do_rm_hp(qc, pkt, &qel->tls_ctx,
3774 qel->pktns->rx.largest_pn, pn, beg, end)) {
3775 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003776 goto err;
3777 }
3778
3779 /* The AAD includes the packet number field found at <pn>. */
3780 pkt->aad_len = pn - beg + pkt->pnl;
3781 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003782 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003783 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003784 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3785 /* If the packet number space has been discarded, this packet
3786 * will be not parsed.
3787 */
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003788 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003789 goto out;
3790 }
3791
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003792 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003793 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003794 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3795 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003796 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003797 else {
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003798 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003799 goto err;
3800 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003801
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003802 *el = qel;
3803 /* No reference counter incrementation here!!! */
3804 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3805 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3806 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3807 b_add(&qc->rx.buf, pkt->len);
3808 out:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003809 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003810 return 1;
3811
3812 err:
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003813 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, qc, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814 return 0;
3815}
3816
3817/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3818 * Also set <*long_header> to 1 if this form is long, 0 if not.
3819 */
3820static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3821 unsigned char byte0, int *long_header)
3822{
3823 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3824 pkt->type =
3825 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3826 *long_header = 1;
3827 }
3828 else {
3829 pkt->type = QUIC_PACKET_TYPE_SHORT;
3830 *long_header = 0;
3831 }
3832}
3833
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003834/*
3835 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3836 */
3837static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3838{
3839 int j = 0, version;
3840
3841 do {
3842 version = quic_supported_version[j];
3843 if (version == pkt->version)
3844 return 1;
3845
3846 version = quic_supported_version[++j];
3847 } while(version);
3848
3849 return 0;
3850}
3851
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003852__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003853static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3854 struct quic_rx_packet *pkt,
3855 struct quic_dgram_ctx *dgram_ctx,
3856 struct sockaddr_storage *saddr)
3857{
3858 unsigned char *beg;
3859 uint64_t len;
3860 struct quic_conn *qc;
3861 struct eb_root *cids;
3862 struct ebmb_node *node;
3863 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003864 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003865 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003866 size_t b_cspace;
3867 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003868
3869 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003870 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003871 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3872 if (end <= *buf)
3873 goto err;
3874
3875 /* Fixed bit */
3876 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3877 /* XXX TO BE DISCARDED */
3878 goto err;
3879
3880 srv_conn = dgram_ctx->owner;
3881 beg = *buf;
3882 /* Header form */
3883 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3884 if (long_header) {
3885 size_t cid_lookup_len;
3886
3887 if (!quic_packet_read_long_header(buf, end, pkt))
3888 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003889
3890 /* unsupported QUIC version */
3891 if (!qc_pkt_is_supported_version(pkt)) {
3892 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3893 goto err;
3894 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003895
3896 /* For Initial packets, and for servers (QUIC clients connections),
3897 * there is no Initial connection IDs storage.
3898 */
3899 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3900 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3901 cid_lookup_len = pkt->dcid.len;
3902 }
3903 else {
3904 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003905 cid_lookup_len = QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003906 }
3907
3908 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3909 if (!node)
3910 goto err;
3911
3912 qc = ebmb_entry(node, struct quic_conn, scid_node);
3913
3914 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3915 qc->dcid.len = pkt->scid.len;
3916 if (pkt->scid.len)
3917 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3918 }
3919
3920 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3921 uint64_t token_len;
3922
3923 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3924 goto err;
3925
3926 /* XXX TO DO XXX 0 value means "the token is not present".
3927 * A server which sends an Initial packet must not set the token.
3928 * So, a client which receives an Initial packet with a token
3929 * MUST discard the packet or generate a connection error with
3930 * PROTOCOL_VIOLATION as type.
3931 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3932 */
3933 pkt->token_len = token_len;
3934 }
3935 }
3936 else {
3937 /* XXX TO DO: Short header XXX */
Amaury Denoyelled4962512021-12-14 17:17:28 +01003938 if (end - *buf < QUIC_HAP_CID_LEN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003939 goto err;
3940
3941 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
Amaury Denoyelled4962512021-12-14 17:17:28 +01003942 node = ebmb_lookup(cids, *buf, QUIC_HAP_CID_LEN);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003943 if (!node)
3944 goto err;
3945
3946 qc = ebmb_entry(node, struct quic_conn, scid_node);
Amaury Denoyelled4962512021-12-14 17:17:28 +01003947 *buf += QUIC_HAP_CID_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003948 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003949
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003950 /* Only packets packets with long headers and not RETRY or VERSION as type
3951 * have a length field.
3952 */
3953 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3954 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3955 goto err;
3956
3957 pkt->len = len;
3958 }
3959 else if (!long_header) {
3960 /* A short packet is the last one of an UDP datagram. */
3961 pkt->len = end - *buf;
3962 }
3963
3964 conn_ctx = qc->conn->xprt_ctx;
3965
3966 /* Increase the total length of this packet by the header length. */
3967 pkt->len += *buf - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003968
3969 /* When multiple QUIC packets are coalesced on the same UDP datagram,
3970 * they must have the same DCID.
3971 *
3972 * This check must be done after the final update to pkt.len to
3973 * properly drop the packet on failure.
3974 */
3975 if (!dgram_ctx->dcid.len) {
3976 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
3977 }
3978 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003979 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003980 goto err;
3981 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01003982 dgram_ctx->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003983
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003984 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3985 b_cspace = b_contig_space(&qc->rx.buf);
3986 if (b_cspace < pkt->len) {
3987 /* Let us consume the remaining contiguous space. */
3988 b_add(&qc->rx.buf, b_cspace);
3989 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3990 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003991 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc, pkt, &pkt->len);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003992 goto err;
3993 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003994 }
3995
Amaury Denoyellee81fed92021-12-22 11:06:34 +01003996 if (!qc_try_rm_hp(qc, pkt, *buf, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003997 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01003998 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003999 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004000 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004001
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004002 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
4003 if (pkt->aad_len)
4004 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004005 /* Wake the tasklet of the QUIC connection packet handler. */
4006 if (conn_ctx)
4007 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4008
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004009 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004010
4011 return pkt->len;
4012
4013 err:
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004014 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004015 return -1;
4016}
4017
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004018/*
4019 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
4020 * address <addr>.
4021 * Implementation of RFC9000 6. Version Negotiation
4022 *
4023 * TODO implement a rate-limiting sending of Version Negotiation packets
4024 *
4025 * Returns 0 on success else non-zero
4026 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004027static int send_version_negotiation(int fd, struct sockaddr_storage *addr,
4028 struct quic_rx_packet *pkt)
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004029{
4030 char buf[256];
4031 int i = 0, j, version;
4032 const socklen_t addrlen = get_addr_len(addr);
4033
4034 /*
4035 * header form
4036 * long header, fixed bit to 0 for Version Negotiation
4037 */
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004038 if (RAND_bytes((unsigned char *)buf, 1) != 1)
4039 return 1;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004040
Frédéric Lécailleea78ee12021-11-18 13:54:43 +01004041 buf[i++] |= '\x80';
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004042 /* null version for Version Negotiation */
4043 buf[i++] = '\x00';
4044 buf[i++] = '\x00';
4045 buf[i++] = '\x00';
4046 buf[i++] = '\x00';
4047
4048 /* source connection id */
4049 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004050 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004051 i += pkt->scid.len;
4052
4053 /* destination connection id */
4054 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01004055 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004056 i += pkt->dcid.len;
4057
4058 /* supported version */
4059 j = 0;
4060 do {
4061 version = htonl(quic_supported_version[j]);
4062 memcpy(&buf[i], &version, sizeof(version));
4063 i += sizeof(version);
4064
4065 version = quic_supported_version[++j];
4066 } while (version);
4067
4068 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4069 return 1;
4070
4071 return 0;
4072}
4073
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004074/* Generate the token to be used in Retry packets. The token is written to
4075 * <buf> which is expected to be <len> bytes.
4076 *
4077 * Various parameters are expected to be encoded in the token. For now, only
4078 * the DCID from <pkt> is stored. This is useful to implement a stateless Retry
4079 * as this CID must be repeated by the server in the transport parameters.
4080 *
4081 * TODO add the client address to validate the token origin.
4082 *
4083 * Returns the length of the encoded token or 0 on error.
4084 */
4085static int generate_retry_token(unsigned char *buf, unsigned char len,
4086 struct quic_rx_packet *pkt)
4087{
4088 const size_t token_len = 1 + pkt->dcid.len;
4089 unsigned char i = 0;
4090
4091 if (token_len > len)
4092 return 0;
4093
4094 buf[i++] = pkt->dcid.len;
4095 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
4096 i += pkt->dcid.len;
4097
4098 return i;
4099}
4100
4101/* Generate a Retry packet and send it on <fd> socket to <addr> in response to
4102 * the Initial <pkt> packet.
4103 *
4104 * Returns 0 on success else non-zero.
4105 */
4106static int send_retry(int fd, struct sockaddr_storage *addr,
4107 struct quic_rx_packet *pkt)
4108{
4109 unsigned char buf[128];
4110 int i = 0, token_len;
4111 const socklen_t addrlen = get_addr_len(addr);
4112 struct quic_cid scid;
4113
4114 /* long header + fixed bit + packet type 0x3 */
4115 buf[i++] = 0xf0;
4116 /* version */
4117 buf[i++] = 0x00;
4118 buf[i++] = 0x00;
4119 buf[i++] = 0x00;
4120 buf[i++] = 0x01;
4121
4122 /* Use the SCID from <pkt> for Retry DCID. */
4123 buf[i++] = pkt->scid.len;
4124 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
4125 i += pkt->scid.len;
4126
4127 /* Generate a new CID to be used as SCID for the Retry packet. */
4128 scid.len = QUIC_HAP_CID_LEN;
4129 if (RAND_bytes(scid.data, scid.len) != 1)
4130 return 1;
4131
4132 buf[i++] = scid.len;
4133 memcpy(&buf[i], scid.data, scid.len);
4134 i += scid.len;
4135
4136 /* token */
4137 if (!(token_len = generate_retry_token(&buf[i], &buf[i] - buf, pkt)))
4138 return 1;
4139 i += token_len;
4140
4141 /* token integrity tag */
4142 if ((&buf[i] - buf < QUIC_TLS_TAG_LEN) ||
4143 !quic_tls_generate_retry_integrity_tag(pkt->dcid.data,
4144 pkt->dcid.len, buf, i)) {
4145 return 1;
4146 }
4147
4148 i += QUIC_TLS_TAG_LEN;
4149
4150 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
4151 return 1;
4152
4153 return 0;
4154}
4155
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004156/* Retrieve a quic_conn instance from the <pkt> DCID field. If the packet is of
4157 * type INITIAL, the ODCID tree is first used. In this case, <saddr> is
4158 * concatenated to the <pkt> DCID field.
4159 *
4160 * Returns the instance or NULL if not found.
4161 */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004162static struct quic_conn *retrieve_qc_conn_from_cid(struct quic_rx_packet *pkt,
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004163 struct listener *l,
4164 struct sockaddr_storage *saddr)
4165{
4166 struct quic_conn *qc = NULL;
4167 struct ebmb_node *node;
4168 struct quic_connection_id *id;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004169 /* set if the quic_conn is found in the second DCID tree */
4170 int found_in_dcid = 0;
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004171
4172 HA_RWLOCK_RDLOCK(QUIC_LOCK, &l->rx.cids_lock);
4173
4174 /* Look first into ODCIDs tree for INITIAL/0-RTT packets. */
4175 if (pkt->type == QUIC_PACKET_TYPE_INITIAL ||
4176 pkt->type == QUIC_PACKET_TYPE_0RTT) {
4177 /* DCIDs of first packets coming from multiple clients may have
4178 * the same values. Let's distinguish them by concatenating the
4179 * socket addresses.
4180 */
4181 quic_cid_saddr_cat(&pkt->dcid, saddr);
4182 node = ebmb_lookup(&l->rx.odcids, pkt->dcid.data,
4183 pkt->dcid.len + pkt->dcid.addrlen);
4184 if (node) {
4185 qc = ebmb_entry(node, struct quic_conn, odcid_node);
4186 goto end;
4187 }
4188 }
4189
4190 /* Look into DCIDs tree for non-INITIAL/0-RTT packets. This may be used
4191 * also for INITIAL/0-RTT non-first packets with the final DCID in
4192 * used.
4193 */
4194 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, pkt->dcid.len);
4195 if (!node)
4196 goto end;
4197
4198 id = ebmb_entry(node, struct quic_connection_id, node);
4199 qc = id->qc;
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004200 found_in_dcid = 1;
4201
4202 end:
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004203 if (qc)
4204 quic_conn_take(qc);
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004205 HA_RWLOCK_RDUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004206
4207 /* If found in DCIDs tree, remove the quic_conn from the ODCIDs tree.
4208 * If already done, this is a noop.
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004209 *
4210 * node.leaf_p is first checked to avoid unnecessary locking.
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004211 */
Amaury Denoyellec6fab982021-12-23 16:27:56 +01004212 if (qc && found_in_dcid && qc->odcid_node.node.leaf_p) {
Amaury Denoyelle250ac422021-12-22 11:29:05 +01004213 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
4214 ebmb_delete(&qc->odcid_node);
4215 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4216 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004217
4218 return qc;
4219}
4220
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004221/* Parse the Retry token from buffer <token> whose size is <token_len>. This
4222 * will extract the parameters stored in the token : <odcid>.
4223 *
4224 * Returns 0 on success else non-zero.
4225 */
4226static int parse_retry_token(const unsigned char *token, uint64_t token_len,
4227 struct quic_cid *odcid)
4228{
4229 uint64_t odcid_len;
4230
4231 if (!quic_dec_int(&odcid_len, &token, token + token_len))
4232 return 1;
4233
4234 memcpy(odcid->data, token, odcid_len);
4235 odcid->len = odcid_len;
4236
4237 return 0;
4238}
4239
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004240static ssize_t qc_lstnr_pkt_rcv(unsigned char *buf, const unsigned char *end,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004241 struct quic_rx_packet *pkt,
4242 struct quic_dgram_ctx *dgram_ctx,
4243 struct sockaddr_storage *saddr)
4244{
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004245 unsigned char *beg, *payload;
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004246 struct quic_conn *qc, *qc_to_purge = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004247 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004248 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004249 int long_header = 0, io_cb_wakeup = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004250 size_t b_cspace;
4251 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004253 beg = buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004254 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004255 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01004256 qel = NULL;
Frédéric Lécaille8678eb02021-12-16 18:03:52 +01004257 TRACE_ENTER(QUIC_EV_CONN_LPKT);
4258 /* This ist only to please to traces and distinguish the
4259 * packet with parsed packet number from others.
4260 */
4261 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004262 if (end <= buf)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004263 goto err;
4264
4265 /* Fixed bit */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004266 if (!(*buf & QUIC_PACKET_FIXED_BIT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004267 /* XXX TO BE DISCARDED */
4268 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4269 goto err;
4270 }
4271
4272 l = dgram_ctx->owner;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004273 /* Header form */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004274 qc_parse_hd_form(pkt, *buf++, &long_header);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004275 if (long_header) {
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004276 uint64_t len;
4277
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004278 if (!quic_packet_read_long_header(&buf, end, pkt)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004279 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4280 goto err;
4281 }
4282
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004283 /* Retry of Version Negotiation packets are only sent by servers */
4284 if (pkt->type == QUIC_PACKET_TYPE_RETRY || !pkt->version) {
4285 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4286 goto err;
4287 }
4288
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004289 /* RFC9000 6. Version Negotiation */
4290 if (!qc_pkt_is_supported_version(pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004291 /* unsupported version, send Negotiation packet */
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004292 if (send_version_negotiation(l->rx.fd, saddr, pkt)) {
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004293 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
4294 goto err;
4295 }
4296
4297 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004298 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01004299 }
4300
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004301 /* For Initial packets, and for servers (QUIC clients connections),
4302 * there is no Initial connection IDs storage.
4303 */
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004304 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004305 uint64_t token_len;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004306
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004307 if (!quic_dec_int(&token_len, (const unsigned char **)&buf, end) ||
4308 end - buf < token_len) {
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004309 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4310 goto err;
Frédéric Lécaillea5da31d2021-12-14 19:44:14 +01004311 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004312
4313 /* XXX TO DO XXX 0 value means "the token is not present".
4314 * A server which sends an Initial packet must not set the token.
4315 * So, a client which receives an Initial packet with a token
4316 * MUST discard the packet or generate a connection error with
4317 * PROTOCOL_VIOLATION as type.
4318 * The token must be provided in a Retry packet or NEW_TOKEN frame.
4319 */
4320 pkt->token_len = token_len;
Amaury Denoyelleb76ae692022-01-11 14:16:37 +01004321
4322 /* TODO Retry should be automatically activated if
4323 * suspect network usage is detected.
4324 */
4325 if (!token_len && l->bind_conf->quic_force_retry) {
4326 TRACE_PROTO("Initial without token, sending retry", QUIC_EV_CONN_LPKT);
4327 if (send_retry(l->rx.fd, saddr, pkt)) {
4328 TRACE_PROTO("Error during Retry generation", QUIC_EV_CONN_LPKT);
4329 goto err;
4330 }
4331
4332 goto err;
4333 }
4334 else {
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004335 pkt->token = buf;
4336 buf += pkt->token_len;
4337 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004338 }
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004339 else if (pkt->type != QUIC_PACKET_TYPE_0RTT) {
Amaury Denoyelled4962512021-12-14 17:17:28 +01004340 if (pkt->dcid.len != QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004341 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4342 goto err;
4343 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004344 }
4345
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004346 if (!quic_dec_int(&len, (const unsigned char **)&buf, end) ||
4347 end - buf < len) {
4348 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4349 goto err;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004350 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004351
Frédéric Lécaille2c15a662021-12-22 20:39:12 +01004352 payload = buf;
4353 pkt->len = len + payload - beg;
4354
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004355 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Amaury Denoyelle8efe0322021-12-15 16:32:56 +01004356 if (!qc) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004357 int ipv4;
4358 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004359 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004360 const unsigned char *salt = initial_salt_v1;
4361 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004362
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004363 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
Amaury Denoyelle47e1f6d2021-12-17 10:58:05 +01004364 TRACE_PROTO("Non Initial packet", QUIC_EV_CONN_LPKT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004365 goto err;
4366 }
4367
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004368 pkt->saddr = *saddr;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004369 ipv4 = saddr->ss_family == AF_INET;
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004370 qc = qc_new_conn(pkt->version, ipv4,
4371 pkt->dcid.data, pkt->dcid.len, pkt->dcid.addrlen,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004372 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004373 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004374 goto err;
4375
4376 odcid = &qc->rx.params.original_destination_connection_id;
4377 /* Copy the transport parameters. */
4378 qc->rx.params = l->bind_conf->quic_params;
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004379
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004380 /* Copy original_destination_connection_id transport parameter. */
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004381 if (pkt->token_len) {
4382 if (parse_retry_token(pkt->token, pkt->token_len, odcid)) {
4383 TRACE_PROTO("Error during Initial token parsing", QUIC_EV_CONN_LPKT, qc);
4384 goto err;
4385 }
Amaury Denoyellec3b6f4d2022-01-11 12:03:09 +01004386 /* Copy retry_source_connection_id transport parameter. */
4387 quic_cid_cpy(&qc->rx.params.retry_source_connection_id,
4388 &pkt->dcid);
Amaury Denoyelle5ff1c972022-01-11 14:11:32 +01004389 }
4390 else {
4391 memcpy(odcid->data, &pkt->dcid.data, pkt->dcid.len);
4392 odcid->len = pkt->dcid.len;
4393 }
4394
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004395 /* Copy the initial source connection ID. */
4396 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4397 qc->enc_params_len =
4398 quic_transport_params_encode(qc->enc_params,
4399 qc->enc_params + sizeof qc->enc_params,
4400 &qc->rx.params, 1);
4401 if (!qc->enc_params_len)
4402 goto err;
4403
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004404 /* NOTE: the socket address has been concatenated to the destination ID
4405 * chosen by the client for Initial packets.
4406 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004407 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
4408 salt = initial_salt_draft_29;
4409 salt_len = sizeof initial_salt_draft_29;
4410 }
4411 if (!qc_new_isecs(qc, salt, salt_len,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004412 pkt->dcid.data, pkt->dcid.len, 1)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004413 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille497fa782021-05-31 15:16:13 +02004414 goto err;
4415 }
4416
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01004417 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02004418 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01004419 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node,
4420 qc->odcid.len + qc->odcid.addrlen);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004421
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004422 /* If the insertion failed, it means that another
4423 * thread has already allocated a QUIC connection for
4424 * the same CID. Liberate our allocated connection.
4425 */
4426 if (unlikely(n != &qc->odcid_node)) {
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004427 qc_to_purge = qc;
4428
Amaury Denoyelleaff4ec82021-11-24 15:16:08 +01004429 qc = ebmb_entry(n, struct quic_conn, odcid_node);
4430 pkt->qc = qc;
4431 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004432
4433 quic_conn_take(qc);
4434 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
4435
4436 if (likely(!qc_to_purge)) {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004437 /* Enqueue this packet. */
Frédéric Lécaillef67b3562021-11-15 16:21:40 +01004438 pkt->qc = qc;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004439 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
4440 /* Try to accept a new connection. */
4441 listener_accept(l);
4442 }
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004443 else {
4444 quic_conn_drop(qc_to_purge);
4445 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004446 }
4447 else {
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004448 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004449 }
4450 }
4451 else {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004452 if (end - buf < QUIC_HAP_CID_LEN) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
4454 goto err;
4455 }
4456
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004457 memcpy(pkt->dcid.data, buf, QUIC_HAP_CID_LEN);
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004458 pkt->dcid.len = QUIC_HAP_CID_LEN;
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004459 buf += QUIC_HAP_CID_LEN;
4460
4461 /* A short packet is the last one of a UDP datagram. */
4462 payload = buf;
4463 pkt->len = end - beg;
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004464
Amaury Denoyelled6b16672021-12-23 10:37:19 +01004465 qc = retrieve_qc_conn_from_cid(pkt, l, saddr);
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004466 if (!qc) {
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004467 size_t pktlen = end - buf;
Frédéric Lécaille4d118d62021-12-21 14:48:58 +01004468 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, NULL, pkt, &pktlen);
4469 goto err;
4470 }
4471
Frédéric Lécaille8370c932021-11-08 17:01:46 +01004472 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004473 }
4474
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004475
4476 /* When multiple QUIC packets are coalesced on the same UDP datagram,
4477 * they must have the same DCID.
4478 *
4479 * This check must be done after the final update to pkt.len to
4480 * properly drop the packet on failure.
4481 */
4482 if (!dgram_ctx->dcid.len) {
4483 memcpy(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len);
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004484 if (!quic_peer_validated_addr(qc) &&
4485 HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED) {
4486 TRACE_PROTO("PTO timer must be armed after anti-amplication was reached",
4487 QUIC_EV_CONN_LPKT, qc);
4488 /* Reset the anti-amplification bit. It will be set again
4489 * when sending the next packet if reached again.
4490 */
4491 HA_ATOMIC_BTR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED_BIT);
4492 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_IO_CB_WAKEUP_BIT);
4493 io_cb_wakeup = 1;
4494 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004495 }
4496 else if (memcmp(dgram_ctx->dcid.data, pkt->dcid.data, pkt->dcid.len)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004497 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498 goto err;
4499 }
Amaury Denoyelleadb22762021-12-14 15:04:14 +01004500 dgram_ctx->qc = qc;
4501
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004503 if (HA_ATOMIC_LOAD(&qc->err_code)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004504 TRACE_PROTO("Connection error", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004505 goto out;
4506 }
4507
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004508 pkt->raw_len = pkt->len;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004509 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004510 quic_rx_pkts_del(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004511 b_cspace = b_contig_space(&qc->rx.buf);
4512 if (b_cspace < pkt->len) {
4513 /* Let us consume the remaining contiguous space. */
Frédéric Lécailled61bc8d2021-12-02 14:46:19 +01004514 if (b_cspace) {
4515 b_putchr(&qc->rx.buf, 0x00);
4516 b_cspace--;
4517 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004518 b_add(&qc->rx.buf, b_cspace);
4519 if (b_contig_space(&qc->rx.buf) < pkt->len) {
4520 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004521 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc, pkt, &pkt->len);
Frédéric Lécaille91ac6c32021-12-17 16:11:54 +01004522 qc_list_all_rx_pkts(qc);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004523 goto err;
4524 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004525 }
4526
Amaury Denoyellee81fed92021-12-22 11:06:34 +01004527 if (!qc_try_rm_hp(qc, pkt, payload, beg, end, &qel)) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004528 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004529 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02004530 goto err;
4531 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004532
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004533 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004534 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004535 if (pkt->aad_len)
4536 qc_pkt_insert(pkt, qel);
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004537 out:
Frédéric Lécaille01abc462021-07-21 09:34:27 +02004538 /* Wake up the connection packet handler task from here only if all
4539 * the contexts have been initialized, especially the mux context
4540 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
4541 * will start it if these contexts for the connection are not already
4542 * initialized.
4543 */
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01004544 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
Frédéric Lécailleb80b20c2022-01-12 17:46:56 +01004545 if (conn_ctx && HA_ATOMIC_LOAD(&qc->qcc))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004546 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02004547
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004548 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc ? qc : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004549
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004550 if (qc)
4551 quic_conn_drop(qc);
4552
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004553 return pkt->len;
4554
4555 err:
Frédéric Lécaille6b663152022-01-04 17:03:11 +01004556 /* Wakeup the I/O handler callback if the PTO timer must be armed.
4557 * This cannot be done by this thread.
4558 */
4559 if (io_cb_wakeup) {
4560 conn_ctx = HA_ATOMIC_LOAD(&qc->xprt_ctx);
4561 if (conn_ctx && conn_ctx->wait_event.tasklet)
4562 tasklet_wakeup(conn_ctx->wait_event.tasklet);
4563 }
Frédéric Lécaillef7ef9762021-12-31 16:37:58 +01004564 /* If length not found, consume the entire datagram */
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01004565 if (!pkt->len)
4566 pkt->len = end - beg;
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004567 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004568 qc ? qc : NULL, pkt);
Amaury Denoyelle76f47ca2021-12-23 10:02:50 +01004569
4570 if (qc)
4571 quic_conn_drop(qc);
4572
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004573 return -1;
4574}
4575
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004576/* This function builds into <buf> buffer a QUIC long packet header.
4577 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004578 */
4579static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
4580 int type, size_t pn_len, struct quic_conn *conn)
4581{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004582 if (end - *buf < sizeof conn->version + conn->dcid.len + conn->scid.len + 3)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004583 return 0;
4584
4585 /* #0 byte flags */
4586 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
4587 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
4588 /* Version */
4589 quic_write_uint32(buf, end, conn->version);
4590 *(*buf)++ = conn->dcid.len;
4591 /* Destination connection ID */
4592 if (conn->dcid.len) {
4593 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4594 *buf += conn->dcid.len;
4595 }
4596 /* Source connection ID */
4597 *(*buf)++ = conn->scid.len;
4598 if (conn->scid.len) {
4599 memcpy(*buf, conn->scid.data, conn->scid.len);
4600 *buf += conn->scid.len;
4601 }
4602
4603 return 1;
4604}
4605
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004606/* This function builds into <buf> buffer a QUIC short packet header.
4607 * Return 1 if enough room to build this header, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004608 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004609static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
4610 size_t pn_len, struct quic_conn *conn,
4611 unsigned char tls_flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004612{
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004613 if (end - *buf < 1 + conn->dcid.len)
4614 return 0;
4615
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004616 /* #0 byte flags */
Frédéric Lécaillea7d2c092021-11-30 11:18:18 +01004617 *(*buf)++ = QUIC_PACKET_FIXED_BIT |
4618 ((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 +01004619 /* Destination connection ID */
4620 if (conn->dcid.len) {
4621 memcpy(*buf, conn->dcid.data, conn->dcid.len);
4622 *buf += conn->dcid.len;
4623 }
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004624
4625 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004626}
4627
4628/* Apply QUIC header protection to the packet with <buf> as first byte address,
4629 * <pn> as address of the Packet number field, <pnlen> being this field length
4630 * with <aead> as AEAD cipher and <key> as secret key.
4631 * Returns 1 if succeeded or 0 if failed.
4632 */
4633static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
4634 const EVP_CIPHER *aead, const unsigned char *key)
4635{
4636 int i, ret, outlen;
4637 EVP_CIPHER_CTX *ctx;
4638 /* We need an IV of at least 5 bytes: one byte for bytes #0
4639 * and at most 4 bytes for the packet number
4640 */
4641 unsigned char mask[5] = {0};
4642
4643 ret = 0;
4644 ctx = EVP_CIPHER_CTX_new();
4645 if (!ctx)
4646 return 0;
4647
4648 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
4649 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
4650 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
4651 goto out;
4652
4653 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
4654 for (i = 0; i < pnlen; i++)
4655 pn[i] ^= mask[i + 1];
4656
4657 ret = 1;
4658
4659 out:
4660 EVP_CIPHER_CTX_free(ctx);
4661
4662 return ret;
4663}
4664
4665/* Reduce the encoded size of <ack_frm> ACK frame removing the last
4666 * ACK ranges if needed to a value below <limit> in bytes.
4667 * Return 1 if succeeded, 0 if not.
4668 */
4669static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
4670{
4671 size_t room, ack_delay_sz;
4672
4673 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
4674 /* A frame is made of 1 byte for the frame type. */
4675 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004676 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004677 return 0;
4678
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004679 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004680}
4681
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004682/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
4683 * 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 +01004684 * and <*len> the packet Length field initialized with the number of bytes already present
4685 * 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 +02004686 * <headlen> is the number of bytes already present in this packet before building frames.
4687 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004688 * Update consequently <*len> to reflect the size of these frames built
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004689 * by this function. Also attach these frames to <l> frame list.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004690 * Return 1 if succeeded, 0 if not.
4691 */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004692static inline int qc_build_frms(struct list *l,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004693 size_t room, size_t *len, size_t headlen,
4694 struct quic_enc_level *qel,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004695 struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004696{
Frédéric Lécailleea604992020-12-24 13:01:37 +01004697 int ret;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004698 struct quic_frame *cf, *cfbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004699
Frédéric Lécailleea604992020-12-24 13:01:37 +01004700 ret = 0;
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004701 if (*len > room)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004702 return 0;
4703
Frédéric Lécailleea604992020-12-24 13:01:37 +01004704 /* If we are not probing we must take into an account the congestion
4705 * control window.
4706 */
Frédéric Lécaillef4e5a7c2022-01-17 17:56:20 +01004707 if (!qel->pktns->tx.pto_probe) {
4708 size_t remain = quic_path_prep_data(qc->path);
4709
4710 if (headlen > remain)
4711 return 0;
4712
4713 room = QUIC_MIN(room, remain - headlen);
4714 }
4715
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004716 TRACE_PROTO("************** frames build (headlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004717 QUIC_EV_CONN_BCFRMS, qc, &headlen);
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004718 list_for_each_entry_safe(cf, cfbak, &qel->pktns->tx.frms, list) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004719 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004720 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004721
Frédéric Lécailleea604992020-12-24 13:01:37 +01004722 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004723 break;
4724
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004725 switch (cf->type) {
4726 case QUIC_FT_CRYPTO:
4727 TRACE_PROTO(" New CRYPTO frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004728 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004729 /* Compute the length of this CRYPTO frame header */
4730 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4731 /* Compute the data length of this CRyPTO frame. */
4732 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4733 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004734 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->crypto.len, &dlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004735 if (!dlen)
4736 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004737
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004738 /* CRYPTO frame length. */
4739 flen = hlen + quic_int_getsize(dlen) + dlen;
4740 TRACE_PROTO(" CRYPTO frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004741 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004742 /* Add the CRYPTO data length and its encoded length to the packet
4743 * length and the length of this length.
4744 */
4745 *len += flen;
4746 room -= flen;
4747 if (dlen == cf->crypto.len) {
4748 /* <cf> CRYPTO data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004749 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004750 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004751 }
4752 else {
4753 struct quic_frame *new_cf;
4754
4755 new_cf = pool_alloc(pool_head_quic_frame);
4756 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004757 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004758 return 0;
4759 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004760
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004761 new_cf->type = QUIC_FT_CRYPTO;
4762 new_cf->crypto.len = dlen;
4763 new_cf->crypto.offset = cf->crypto.offset;
4764 new_cf->crypto.qel = qel;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004765 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004766 /* Consume <dlen> bytes of the current frame. */
4767 cf->crypto.len -= dlen;
4768 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004769 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004770 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004771
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004772 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004773 /* Note that these frames are accepted in short packets only without
4774 * "Length" packet field. Here, <*len> is used only to compute the
4775 * sum of the lengths of the already built frames for this packet.
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004776 *
4777 * Compute the length of this STREAM frame "header" made a all the field
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004778 * excepting the variable ones. Note that +1 is for the type of this frame.
4779 */
4780 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004781 ((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 +02004782 /* Compute the data length of this STREAM frame. */
4783 avail_room = room - hlen - *len;
4784 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004785 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004786
Frédéric Lécailled8b84432021-12-10 15:18:36 +01004787 TRACE_PROTO(" New STREAM frame build (room, len)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004788 QUIC_EV_CONN_BCFRMS, qc, &room, len);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004789 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4790 dlen = max_available_room(avail_room, &dlen_sz);
4791 if (dlen > cf->stream.len) {
4792 dlen = cf->stream.len;
4793 }
4794 dlen_sz = quic_int_getsize(dlen);
4795 flen = hlen + dlen_sz + dlen;
4796 }
4797 else {
4798 dlen = QUIC_MIN(avail_room, cf->stream.len);
4799 flen = hlen + dlen;
4800 }
4801 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004802 QUIC_EV_CONN_BCFRMS, qc, &hlen, &cf->stream.len, &dlen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004803 TRACE_PROTO(" STREAM frame length (flen)",
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004804 QUIC_EV_CONN_BCFRMS, qc, &flen);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004805 /* Add the STREAM data length and its encoded length to the packet
4806 * length and the length of this length.
4807 */
4808 *len += flen;
4809 room -= flen;
4810 if (dlen == cf->stream.len) {
4811 /* <cf> STREAM data have been consumed. */
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004812 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004813 LIST_APPEND(l, &cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004814 }
4815 else {
4816 struct quic_frame *new_cf;
4817
4818 new_cf = pool_zalloc(pool_head_quic_frame);
4819 if (!new_cf) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004820 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, qc);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004821 return 0;
4822 }
4823
4824 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004825 new_cf->stream.qcs = cf->stream.qcs;
4826 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004827 new_cf->stream.id = cf->stream.id;
4828 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4829 new_cf->stream.offset = cf->stream.offset;
4830 new_cf->stream.len = dlen;
4831 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4832 /* FIN bit reset */
4833 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4834 new_cf->stream.data = cf->stream.data;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004835 LIST_APPEND(l, &new_cf->list);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004836 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4837 /* Consume <dlen> bytes of the current frame. */
4838 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004839 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004840 cf->stream.data += dlen;
4841 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004842 break;
4843
4844 default:
4845 flen = qc_frm_len(cf);
4846 BUG_ON(!flen);
4847 if (flen > room)
4848 continue;
4849
4850 *len += flen;
4851 room -= flen;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004852 LIST_DELETE(&cf->list);
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004853 LIST_APPEND(l, &cf->list);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004854 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004855 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004856 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004857 }
4858
Frédéric Lécailleea604992020-12-24 13:01:37 +01004859 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004860}
4861
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004862/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004863 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4864 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4865 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004866 * 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 +02004867 * 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 +02004868 * having returned from this function.
4869 * 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 +01004870 * number field in this packet. <pn_len> will also have the packet number
4871 * length as value.
4872 *
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004873 * Return 1 if succeeded (enough room to buile this packet), O if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004874 */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004875static int qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
4876 size_t dglen, struct quic_tx_packet *pkt,
4877 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004878 int ack, int padding, int cc, int probe,
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004879 struct quic_enc_level *qel, struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004880{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004881 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004882 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004883 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4884 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004885 struct quic_frame cc_frm = { . type = QUIC_FT_CONNECTION_CLOSE, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004886 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004887 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004888 int add_ping_frm;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004889 struct list frm_list = LIST_HEAD_INIT(frm_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004890
Frédéric Lécailleea604992020-12-24 13:01:37 +01004891 /* Length field value with CRYPTO frames if present. */
4892 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004893 beg = pos;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004894 /* When not probing and not acking, and no immediate close is required,
4895 * reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004896 * the congestion controller window. So, we do not limit the size of this
4897 * packet if we have an ACK frame to send because an ACK frame is not
4898 * ack-eliciting. This size will be limited if we have ack-eliciting
4899 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004900 */
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01004901 if (!probe && !ack && !cc) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004902 size_t path_room;
4903
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004904 path_room = quic_path_prep_data(qc->path);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004905 if (end - beg > path_room)
4906 end = beg + path_room;
4907 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004908
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004909 /* Ensure there is enough room for the TLS encryption tag and a zero token
4910 * length field if any.
4911 */
4912 if (end - pos < QUIC_TLS_TAG_LEN +
4913 (pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0))
4914 goto no_room;
4915
4916 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004917 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004918 /* packet number length */
4919 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004920 /* Build the header */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004921 if ((pkt->type == QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004922 !quic_build_packet_short_header(&pos, end, *pn_len, qc, qel->tls_ctx.flags)) ||
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004923 (pkt->type != QUIC_PACKET_TYPE_SHORT &&
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004924 !quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, qc)))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01004925 goto no_room;
4926
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004927 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4928 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004929 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004930 head_len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004931 /* Build an ACK frame if required. */
4932 ack_frm_len = 0;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004933 if (!cc && ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004934 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004935 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004936 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4937 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4938 * that from here, we do not know if we will have to send a PING frame.
4939 * This will be decided after having computed the ack-eliciting frames
4940 * to be added to this packet.
4941 */
4942 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 +01004943 if (!ack_frm_len)
4944 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004945 }
4946
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004947 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004948 len = ack_frm_len + *pn_len;
Frédéric Lécaille1fc5e162021-11-22 14:25:57 +01004949 len_frms = 0;
Frédéric Lécaille82468ea2022-01-14 20:23:22 +01004950 if (!cc && !LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004951 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004952
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004953 /* Initialize the length of the frames built below to <len>.
4954 * If any frame could be successfully built by qc_build_frms(),
4955 * we will have len_frms > len.
4956 */
4957 len_frms = len;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004958 if (!qc_build_frms(&frm_list, end - pos, &len_frms, pos - beg, qel, qc)) {
Frédéric Lécailleea604992020-12-24 13:01:37 +01004959 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01004960 qc, NULL, NULL, &room);
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004961 }
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004962 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004963
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004964 /* Length (of the remaining data). Must not fail because, the buffer size
4965 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4966 * for the encryption tag. It must be taken into an account for the length
4967 * of this packet.
4968 */
4969 if (len_frms)
4970 len = len_frms + QUIC_TLS_TAG_LEN;
4971 else
4972 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004973 /* CONNECTION_CLOSE frame */
4974 if (cc) {
4975 struct quic_connection_close *cc = &cc_frm.connection_close;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004976
Amaury Denoyelle17a74162021-12-21 14:45:39 +01004977 cc->error_code = qc->err_code;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01004978 len += qc_frm_len(&cc_frm);
4979 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004980 add_ping_frm = 0;
4981 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004982 len_sz = quic_int_getsize(len);
4983 /* Add this packet size to <dglen> */
4984 dglen += head_len + len_sz + len;
4985 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4986 /* This is a maximum padding size */
4987 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4988 /* The length field value is of this packet is <len> + <padding_len>
4989 * the size of which may be greater than the initial computed size
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +05004990 * <len_sz>. So, let's deduce the difference between these to packet
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004991 * sizes from <padding_len>.
4992 */
4993 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4994 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004995 }
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01004996 else if (LIST_ISEMPTY(&frm_list) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004997 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004998 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004999 add_ping_frm = 1;
5000 len += 1;
5001 }
5002 /* 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 +01005003 if (!ack_frm_len && !cc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005004 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
5005 }
5006
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005007 if (pkt->type != QUIC_PACKET_TYPE_SHORT && !quic_enc_int(&pos, end, len))
5008 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005009
5010 /* Packet number field address. */
5011 *buf_pn = pos;
5012
5013 /* Packet number encoding. */
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005014 if (!quic_packet_number_encode(&pos, end, pn, *pn_len))
5015 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005016
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005017 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005018 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005019
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005020 /* Ack-eliciting frames */
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005021 if (!LIST_ISEMPTY(&frm_list)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005022 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005023
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005024 list_for_each_entry(cf, &frm_list, list) {
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005025 if (!qc_build_frm(&pos, end, cf, pkt, qc)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005026 ssize_t room = end - pos;
5027 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005028 qc, NULL, NULL, &room);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005029 break;
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01005030 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005031 }
5032 }
5033
5034 /* Build a PING frame if needed. */
5035 if (add_ping_frm) {
5036 frm.type = QUIC_FT_PING;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005037 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005038 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005039 }
5040
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005041 /* Build a CONNECTION_CLOSE frame if needed. */
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005042 if (cc && !qc_build_frm(&pos, end, &cc_frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005043 goto no_room;
Frédéric Lécaille66cbb822021-11-17 11:56:21 +01005044
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005045 /* Build a PADDING frame if needed. */
5046 if (padding_len) {
5047 frm.type = QUIC_FT_PADDING;
5048 frm.padding.len = padding_len;
Amaury Denoyelle17a74162021-12-21 14:45:39 +01005049 if (!qc_build_frm(&pos, end, &frm, pkt, qc))
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005050 goto no_room;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005051 }
5052
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005053 /* If this packet is ack-eliciting and we are probing let's
5054 * decrement the PTO probe counter.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005055 */
Frédéric Lécaille466e9da2021-12-29 12:04:13 +01005056 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING &&
5057 qel->pktns->tx.pto_probe)
5058 qel->pktns->tx.pto_probe--;
5059
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005060 pkt->len = pos - beg;
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005061 LIST_SPLICE(&pkt->frms, &frm_list);
5062 TRACE_PROTO("Ack eliciting frame", QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005063
5064 return 1;
5065
5066 no_room:
Frédéric Lécaillecba4cd42022-01-14 20:39:18 +01005067 /* Replace the pre-built frames which could not be add to this packet */
5068 LIST_SPLICE(&qel->pktns->tx.frms, &frm_list);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005069 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005070 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005071}
5072
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005073static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005074{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005075 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005076 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005077 pkt->in_flight_len = 0;
Frédéric Lécaille0371cd52021-12-13 12:30:54 +01005078 pkt->pn_node.key = (uint64_t)-1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005079 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005080 pkt->next = NULL;
5081 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005082}
5083
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005084/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005085static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
5086{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005087 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005088
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005089 if (!pkt)
5090 return;
5091
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005092 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02005093 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02005094 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005095 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005096 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005097}
5098
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005099/* Build a packet into <buf> packet buffer with <pkt_type> as packet
5100 * type for <qc> QUIC connection from <qel> encryption level.
5101 * Return -2 if the packet could not be allocated or encrypted for any reason,
5102 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005103 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02005104static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
5105 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005106 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01005107 struct quic_conn *qc, size_t dglen, int padding,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005108 int pkt_type, int ack, int probe, int cc, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005109{
5110 /* The pointer to the packet number field. */
5111 unsigned char *buf_pn;
5112 unsigned char *beg, *end, *payload;
5113 int64_t pn;
5114 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005115 struct quic_tls_ctx *tls_ctx;
5116 struct quic_tx_packet *pkt;
5117
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005118 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005119 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005120 pkt = pool_alloc(pool_head_quic_tx_packet);
5121 if (!pkt) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005122 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005123 *err = -2;
5124 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005125 }
5126
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02005127 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005128 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005129 pn_len = 0;
5130 buf_pn = NULL;
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005131
5132 pn = qel->pktns->tx.next_pn + 1;
5133 if (!qc_do_build_pkt(*pos, buf_end, dglen, pkt, pn, &pn_len, &buf_pn,
Frédéric Lécaillece6602d2022-01-17 11:06:10 +01005134 ack, padding, cc, probe, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02005135 *err = -1;
5136 goto err;
5137 }
5138
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005139 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005140 payload = buf_pn + pn_len;
5141 payload_len = end - payload;
5142 aad_len = payload - beg;
5143
5144 tls_ctx = &qel->tls_ctx;
Frédéric Lécaille5f7f1182022-01-10 11:00:16 +01005145 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005146 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005147 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005148 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005149
5150 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005151 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005152 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
5153 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005154 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005155 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005156 goto err;
5157 }
5158
Frédéric Lécaille73dcc6e2021-12-07 15:27:44 +01005159 /* Consume a packet number */
5160 qel->pktns->tx.next_pn++;
Frédéric Lécailleca98a7f2021-11-10 17:30:15 +01005161 qc->tx.prep_bytes += pkt->len;
Frédéric Lécaille41a07602022-01-04 16:57:37 +01005162 if (qc->tx.prep_bytes >= 3 * qc->rx.bytes)
5163 HA_ATOMIC_OR(&qc->flags, QUIC_FL_CONN_ANTI_AMPLIFICATION_REACHED);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005164 /* Now that a correct packet is built, let us consume <*pos> buffer. */
5165 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005166 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02005167 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005168 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005169 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005170 pkt->in_flight_len = pkt->len;
5171 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01005172 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005173 pkt->pktns = qel->pktns;
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005174 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005175
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005176 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005177
5178 err:
5179 free_quic_tx_packet(pkt);
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005180 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02005181 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005182}
5183
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005184/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
5185 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005186 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005187static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
5188 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005189{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005190 size_t try, done = 0;
5191
5192 if (!conn_ctrl_ready(conn))
5193 return 0;
5194
5195 if (!fd_recv_ready(conn->handle.fd))
5196 return 0;
5197
5198 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005199
5200 /* read the largest possible block. For this, we perform only one call
5201 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005202 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005203 */
5204 while (count > 0) {
5205 try = b_contig_space(buf);
5206 if (!try)
5207 break;
5208
5209 if (try > count)
5210 try = count;
5211
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01005212 b_add(buf, try);
5213 done += try;
5214 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005215 }
5216
5217 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5218 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5219
5220 leave:
5221 return done;
5222
5223 read0:
5224 conn_sock_read0(conn);
5225 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5226
5227 /* Now a final check for a possible asynchronous low-level error
5228 * report. This can happen when a connection receives a reset
5229 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
5230 * we might have come from there by just checking POLL_HUP instead
5231 * of recv()'s return value 0, so we have no way to tell there was
5232 * an error without checking.
5233 */
Willy Tarreauf5090652021-04-06 17:23:40 +02005234 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005235 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5236 goto leave;
5237}
5238
5239
5240/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5241 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5242 * other pending data for example, but this flag is ignored at the moment.
5243 * Only one call to send() is performed, unless the buffer wraps, in which case
5244 * a second call may be performed. The connection's flags are updated with
5245 * whatever special event is detected (error, empty). The caller is responsible
5246 * for taking care of those events and avoiding the call if inappropriate. The
5247 * function does not call the connection's polling update function, so the caller
5248 * is responsible for this. It's up to the caller to update the buffer's contents
5249 * based on the return value.
5250 */
5251static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
5252{
5253 ssize_t ret;
5254 size_t try, done;
5255 int send_flag;
5256
5257 if (!conn_ctrl_ready(conn))
5258 return 0;
5259
5260 if (!fd_send_ready(conn->handle.fd))
5261 return 0;
5262
5263 done = 0;
5264 /* send the largest possible block. For this we perform only one call
5265 * to send() unless the buffer wraps and we exactly fill the first hunk,
5266 * in which case we accept to do it once again.
5267 */
5268 while (count) {
5269 try = b_contig_data(buf, done);
5270 if (try > count)
5271 try = count;
5272
5273 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
5274 if (try < count || flags & CO_SFL_MSG_MORE)
5275 send_flag |= MSG_MORE;
5276
5277 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
5278 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
5279 if (ret > 0) {
5280 count -= ret;
5281 done += ret;
5282
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005283 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005284 conn->flags |= CO_FL_WAIT_L4L6;
5285 /* if the system buffer is full, don't insist */
5286 if (ret < try)
5287 break;
5288 }
5289 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
5290 /* nothing written, we need to poll for write first */
5291 fd_cant_send(conn->handle.fd);
5292 break;
5293 }
5294 else if (errno != EINTR) {
5295 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
5296 break;
5297 }
5298 }
5299 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
5300 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5301
5302 if (done > 0) {
5303 /* we count the total bytes sent, and the send rate for 32-byte
5304 * blocks. The reason for the latter is that freq_ctr are
5305 * limited to 4GB and that it's not enough per second.
5306 */
5307 _HA_ATOMIC_ADD(&global.out_bytes, done);
5308 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
5309 }
5310 return done;
5311}
5312
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005313/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5314 * event subscriber <es> is not allowed to change from a previous call as long
5315 * as at least one event is still subscribed. The <event_type> must only be a
5316 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
5317 */
5318static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5319{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005320 struct qcc *qcc = conn->qc->qcc;
5321
5322 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
5323 BUG_ON(qcc->subs && qcc->subs != es);
5324
5325 es->events |= event_type;
5326 qcc->subs = es;
5327
5328 if (event_type & SUB_RETRY_RECV)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005329 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005330
5331 if (event_type & SUB_RETRY_SEND)
Amaury Denoyelle7aaeb5b2021-12-21 14:29:15 +01005332 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn->qc, qcc);
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02005333
5334 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005335}
5336
5337/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5338 * The <es> pointer is not allowed to differ from the one passed to the
5339 * subscribe() call. It always returns zero.
5340 */
5341static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
5342{
5343 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
5344}
5345
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005346/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
5347 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
5348 * parameters of this session.
5349 * This is the responsibility of the caller to check the validity of all the
5350 * pointers passed as parameter to this function.
5351 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
5352 * CO_ER_SSL_NO_MEM.
5353 */
5354static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
5355 unsigned char *params, size_t params_len)
5356{
5357 int retry;
5358
5359 retry = 1;
5360 retry:
5361 *ssl = SSL_new(ssl_ctx);
5362 if (!*ssl) {
5363 if (!retry--)
5364 goto err;
5365
5366 pool_gc(NULL);
5367 goto retry;
5368 }
5369
5370 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
5371 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
5372 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
5373 goto err;
5374
5375 SSL_free(*ssl);
5376 *ssl = NULL;
5377 if (!retry--)
5378 goto err;
5379
5380 pool_gc(NULL);
5381 goto retry;
5382 }
5383
5384 return 0;
5385
5386 err:
5387 qc->conn->err_code = CO_ER_SSL_NO_MEM;
5388 return -1;
5389}
5390
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005391/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
5392 * connection with <xprt_ctx> as address of the xprt context.
5393 * Returns 1 if succeeded, 0 if not.
5394 */
5395static int qc_conn_init(struct connection *conn, void **xprt_ctx)
5396{
Amaury Denoyelle9979d0d2021-12-23 16:32:24 +01005397 struct ssl_sock_ctx *ctx = NULL;
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005398 struct quic_conn *qc = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005399
5400 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
5401
5402 if (*xprt_ctx)
5403 goto out;
5404
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005405 ctx = pool_zalloc(pool_head_quic_conn_ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005406 if (!ctx) {
5407 conn->err_code = CO_ER_SYS_MEMLIM;
5408 goto err;
5409 }
5410
5411 ctx->wait_event.tasklet = tasklet_new();
5412 if (!ctx->wait_event.tasklet) {
5413 conn->err_code = CO_ER_SYS_MEMLIM;
5414 goto err;
5415 }
5416
5417 ctx->wait_event.tasklet->process = quic_conn_io_cb;
5418 ctx->wait_event.tasklet->context = ctx;
5419 ctx->wait_event.events = 0;
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005420 HA_ATOMIC_STORE(&ctx->conn, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005421 ctx->subs = NULL;
5422 ctx->xprt_ctx = NULL;
5423
5424 ctx->xprt = xprt_get(XPRT_QUIC);
5425 if (objt_server(conn->target)) {
5426 /* Server */
5427 struct server *srv = __objt_server(conn->target);
Amaury Denoyelled4962512021-12-14 17:17:28 +01005428 unsigned char dcid[QUIC_HAP_CID_LEN];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005429 int ssl_err, ipv4;
5430
5431 ssl_err = SSL_ERROR_NONE;
5432 if (RAND_bytes(dcid, sizeof dcid) != 1)
5433 goto err;
5434
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005435 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005436 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Amaury Denoyellec92cbfc2021-12-14 17:20:59 +01005437 dcid, sizeof dcid, 0, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005438 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005439 goto err;
5440
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005441 ctx->qc = qc;
5442
Frédéric Lécaille6de72872021-06-11 15:44:24 +02005443 /* Insert our SCID, the connection ID for the QUIC client. */
5444 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
5445
5446 conn->qc = qc;
5447 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02005448 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
5449 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005450 goto err;
5451
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005452 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005453 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005454 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
5455 qc->enc_params_len =
5456 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
5457 &qc->rx.params, 0);
5458 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005459 goto err;
5460
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005461 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
5462 qc->enc_params, qc->enc_params_len) == -1)
5463 goto err;
5464
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02005465 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005466 SSL_set_connect_state(ctx->ssl);
5467 ssl_err = SSL_do_handshake(ctx->ssl);
5468 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02005469 int st;
5470
5471 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005472 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
5473 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005474 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005475 }
5476 else {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005477 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, qc, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005478 goto err;
5479 }
5480 }
5481 }
5482 else if (objt_listener(conn->target)) {
5483 /* Listener */
5484 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
5485
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005486 qc = ctx->conn->qc;
Amaury Denoyellec15dd922021-12-21 11:41:52 +01005487 ctx->qc = qc;
5488
Frédéric Lécaillef57c3332021-12-09 10:06:21 +01005489 qc->tid = ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02005490 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
5491 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005492 goto err;
5493
Frédéric Lécaillead3c07a2021-12-14 19:23:43 +01005494 /* Enabling 0-RTT */
5495 if (bc->ssl_conf.early_data)
5496 SSL_set_quic_early_data_enabled(ctx->ssl, 1);
5497
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005498 SSL_set_accept_state(ctx->ssl);
5499 }
5500
Frédéric Lécailled77c50b2021-11-23 15:59:59 +01005501 HA_ATOMIC_STORE(xprt_ctx, ctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005502
5503 /* Leave init state and start handshake */
5504 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005505
5506 out:
Amaury Denoyelle7ca7c842021-12-22 18:20:38 +01005507 HA_ATOMIC_STORE(&qc->xprt_ctx, ctx);
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005508 TRACE_LEAVE(QUIC_EV_CONN_NEW, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005509
5510 return 0;
5511
5512 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02005513 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005514 tasklet_free(ctx->wait_event.tasklet);
5515 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01005516 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005517 return -1;
5518}
5519
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005520/* Start the QUIC transport layer */
5521static int qc_xprt_start(struct connection *conn, void *ctx)
5522{
5523 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02005524 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005525
5526 qc = conn->qc;
5527 if (!quic_conn_init_timer(qc)) {
Frédéric Lécaillefde2a982021-12-27 15:12:09 +01005528 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, qc);
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005529 return 0;
5530 }
5531
5532 tasklet_wakeup(qctx->wait_event.tasklet);
5533 return 1;
5534}
5535
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005536/* transport-layer operations for QUIC connections. */
5537static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02005538 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005539 .snd_buf = quic_conn_from_buf,
5540 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01005541 .subscribe = quic_conn_subscribe,
5542 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005543 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02005544 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005545 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
5546 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01005547 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005548 .name = "QUIC",
5549};
5550
5551__attribute__((constructor))
5552static void __quic_conn_init(void)
5553{
5554 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
5555 xprt_register(XPRT_QUIC, &ssl_quic);
5556}
5557
5558__attribute__((destructor))
5559static void __quic_conn_deinit(void)
5560{
5561 BIO_meth_free(ha_quic_meth);
5562}
5563
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005564/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
5565 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005566 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005567 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005568static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005569 struct sockaddr_storage *saddr, qpkt_read_func *func)
5570{
5571 unsigned char *pos;
5572 const unsigned char *end;
5573 struct quic_dgram_ctx dgram_ctx = {
Amaury Denoyelleadb22762021-12-14 15:04:14 +01005574 .qc = NULL,
5575 .dcid.len = 0,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005576 .owner = owner,
5577 };
5578
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005579 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005580 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005581 do {
5582 int ret;
5583 struct quic_rx_packet *pkt;
5584
Willy Tarreaue4498932021-03-22 21:13:05 +01005585 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005586 if (!pkt)
5587 goto err;
5588
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005589 quic_rx_packet_refinc(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005590 ret = func(pos, end, pkt, &dgram_ctx, saddr);
5591 pos += pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02005592 quic_rx_packet_refdec(pkt);
Frédéric Lécaille01cfec72021-12-22 10:17:01 +01005593 if (ret == -1)
Frédéric Lécaille865b0782021-09-23 07:33:20 +02005594 /* If the packet length could not be found, we cannot continue. */
5595 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005596 } while (pos < end);
5597
5598 /* Increasing the received bytes counter by the UDP datagram length
5599 * if this datagram could be associated to a connection.
5600 */
5601 if (dgram_ctx.qc)
5602 dgram_ctx.qc->rx.bytes += len;
5603
Amaury Denoyellece340fe2022-01-11 14:20:46 +01005604 return pos - (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005605
5606 err:
5607 return -1;
5608}
5609
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01005610ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005611 struct sockaddr_storage *saddr)
5612{
5613 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
5614}
5615
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005616/* Function to automatically activate QUIC traces on stdout.
5617 * Activated via the compilation flag -DENABLE_QUIC_STDOUT_TRACES.
5618 * Main use for now is in the docker image for QUIC interop testing.
5619 */
5620static void quic_init_stdout_traces(void)
5621{
5622#ifdef ENABLE_QUIC_STDOUT_TRACES
5623 trace_quic.sink = sink_find("stdout");
5624 trace_quic.level = TRACE_LEVEL_DEVELOPER;
Amaury Denoyelle118b2cb2021-11-25 16:05:16 +01005625 trace_quic.state = TRACE_STATE_RUNNING;
5626#endif
5627}
5628INITCALL0(STG_INIT, quic_init_stdout_traces);
5629
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01005630/*
5631 * Local variables:
5632 * c-indent-level: 8
5633 * c-basic-offset: 8
5634 * End:
5635 */