blob: 27b67ee64f83cff4da4f3c1ba5e15c4335afb053 [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>
49#include <haproxy/ssl_sock.h>
50#include <haproxy/stream_interface.h>
51#include <haproxy/task.h>
52#include <haproxy/trace.h>
53#include <haproxy/xprt_quic.h>
54
Amaury Denoyellea22d8602021-11-10 15:17:56 +010055/* list of supported QUIC versions by this implementation */
56static int quic_supported_version[] = {
57 0x00000001,
58
59 /* placeholder, do not add entry after this */
60 0x0
61};
62
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020063/* This is the values of some QUIC transport parameters when absent.
64 * Should be used to initialize any transport parameters (local or remote)
65 * before updating them with customized values.
66 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010067struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille46be7e92021-10-22 15:04:27 +020068 .max_udp_payload_size = QUIC_PACKET_MAXLEN,
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020069 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
70 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020071 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010072};
73
74/* trace source and events */
75static void quic_trace(enum trace_level level, uint64_t mask, \
76 const struct trace_source *src,
77 const struct ist where, const struct ist func,
78 const void *a1, const void *a2, const void *a3, const void *a4);
79
80static const struct trace_event quic_trace_events[] = {
81 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
82 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
83 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
84 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
85 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
86 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
87 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050088 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010089 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
90 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
91 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
92 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
93 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
94 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
95 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
96 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
97 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
98 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
99 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
100 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
101 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
102 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
103 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
104 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
105 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
106 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
107 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100108 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
109 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
110 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
111 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
112 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
113 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100114 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200115 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
116 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100117 { /* end */ }
118};
119
120static const struct name_desc quic_trace_lockon_args[4] = {
121 /* arg1 */ { /* already used by the connection */ },
122 /* arg2 */ { .name="quic", .desc="QUIC transport" },
123 /* arg3 */ { },
124 /* arg4 */ { }
125};
126
127static const struct name_desc quic_trace_decoding[] = {
128#define QUIC_VERB_CLEAN 1
129 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
130 { /* end */ }
131};
132
133
134struct trace_source trace_quic = {
135 .name = IST("quic"),
136 .desc = "QUIC xprt",
137 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
138 .default_cb = quic_trace,
139 .known_events = quic_trace_events,
140 .lockon_args = quic_trace_lockon_args,
141 .decoding = quic_trace_decoding,
142 .report_events = ~0, /* report everything by default */
143};
144
145#define TRACE_SOURCE &trace_quic
146INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
147
148static BIO_METHOD *ha_quic_meth;
149
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200150DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillec1029f62021-10-20 11:09:58 +0200151DECLARE_POOL(pool_head_quic_rxbuf, "quic_rxbuf_pool", QUIC_RX_BUFSZ);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +0100152DECLARE_POOL(pool_head_quic_conn_rxbuf, "quic_conn_rxbuf", QUIC_CONN_RX_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100153DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200154 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100156DECLARE_POOL(pool_head_quic_connection_id,
157 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158DECLARE_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 +0100159DECLARE_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 +0100160DECLARE_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 +0100161DECLARE_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 +0100162DECLARE_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 +0200163DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100164DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100165
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200166static 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 +0200167 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +0100168 struct quic_conn *qc, size_t dglen, int pkt_type,
169 int padding, int ack, int nb_pto_dgrams, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100170
171/* Add traces to <buf> depending on <frm> TX frame type. */
172static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200173 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100174{
175 switch (frm->type) {
176 case QUIC_FT_CRYPTO:
177 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
178 (unsigned long long)frm->crypto.offset,
179 (unsigned long long)frm->crypto.len);
180 break;
181 default:
182 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
183 }
184}
185
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100186/* Only for debug purpose */
187struct enc_debug_info {
188 unsigned char *payload;
189 size_t payload_len;
190 unsigned char *aad;
191 size_t aad_len;
192 uint64_t pn;
193};
194
195/* Initializes a enc_debug_info struct (only for debug purpose) */
196static inline void enc_debug_info_init(struct enc_debug_info *edi,
197 unsigned char *payload, size_t payload_len,
198 unsigned char *aad, size_t aad_len, uint64_t pn)
199{
200 edi->payload = payload;
201 edi->payload_len = payload_len;
202 edi->aad = aad;
203 edi->aad_len = aad_len;
204 edi->pn = pn;
205}
206
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207/* Trace callback for QUIC.
208 * These traces always expect that arg1, if non-null, is of type connection.
209 */
210static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
211 const struct ist where, const struct ist func,
212 const void *a1, const void *a2, const void *a3, const void *a4)
213{
214 const struct connection *conn = a1;
215
216 if (conn) {
217 struct quic_tls_secrets *secs;
218 struct quic_conn *qc;
219
220 qc = conn->qc;
221 chunk_appendf(&trace_buf, " : conn@%p", conn);
222 if ((mask & QUIC_EV_CONN_INIT) && qc) {
223 chunk_appendf(&trace_buf, "\n odcid");
224 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100225 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100226 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100227 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100228 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100229 }
230
231 if (mask & QUIC_EV_CONN_ADDDATA) {
232 const enum ssl_encryption_level_t *level = a2;
233 const size_t *len = a3;
234
235 if (level) {
236 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
237
238 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
239 }
240 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100241 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100242 }
243 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
244 /* Initial read & write secrets. */
245 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
246 const unsigned char *rx_sec = a2;
247 const unsigned char *tx_sec = a3;
248
249 secs = &qc->els[level].tls_ctx.rx;
250 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
251 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
252 if (rx_sec)
253 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
254 quic_tls_keys_hexdump(&trace_buf, secs);
255 }
256 secs = &qc->els[level].tls_ctx.tx;
257 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
258 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
259 if (tx_sec)
260 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
261 quic_tls_keys_hexdump(&trace_buf, secs);
262 }
263 }
264 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
265 const enum ssl_encryption_level_t *level = a2;
266 const unsigned char *secret = a3;
267 const size_t *secret_len = a4;
268
269 if (level) {
270 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
271
272 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
273 if (secret && secret_len)
274 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
275 secs = &qc->els[lvl].tls_ctx.rx;
276 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
277 quic_tls_keys_hexdump(&trace_buf, secs);
278 }
279 }
280
281 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
282 const enum ssl_encryption_level_t *level = a2;
283 const unsigned char *secret = a3;
284 const size_t *secret_len = a4;
285
286 if (level) {
287 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
288
289 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
290 if (secret && secret_len)
291 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
292 secs = &qc->els[lvl].tls_ctx.tx;
293 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
294 quic_tls_keys_hexdump(&trace_buf, secs);
295 }
296
297 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100298
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100299 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 const struct quic_tx_packet *pkt = a2;
301 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100302 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100303
304 if (qel) {
305 struct quic_pktns *pktns;
306
307 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100308 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
309 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100310 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 (unsigned long long)qc->path->cwnd,
312 (unsigned long long)qc->path->prep_in_flight,
313 (unsigned long long)qc->path->in_flight,
314 (unsigned long long)pktns->tx.in_flight,
315 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200318 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
320 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
321 list_for_each_entry(frm, &pkt->frms, list)
322 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100323 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
324 }
325
326 if (room) {
327 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
328 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
329 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 }
331 }
332
333 if (mask & QUIC_EV_CONN_HDSHK) {
334 const enum quic_handshake_state *state = a2;
335 const int *err = a3;
336
337 if (state)
338 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
339 if (err)
340 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
341 }
342
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100343 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100344 const struct quic_rx_packet *pkt = a2;
345 const unsigned long *pktlen = a3;
346 const SSL *ssl = a4;
347
348 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100349 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100350 pkt, quic_packet_type_enc_level_char(pkt->type));
351 if (pkt->pnl)
352 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
353 (unsigned long long)pkt->pn);
354 if (pkt->token_len)
355 chunk_appendf(&trace_buf, " toklen=%llu",
356 (unsigned long long)pkt->token_len);
357 if (pkt->aad_len)
358 chunk_appendf(&trace_buf, " aadlen=%llu",
359 (unsigned long long)pkt->aad_len);
360 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
361 pkt->flags, (unsigned long long)pkt->len);
362 }
363 if (pktlen)
364 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
365 if (ssl) {
366 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
367 chunk_appendf(&trace_buf, " el=%c",
368 quic_enc_level_char(ssl_to_quic_enc_level(level)));
369 }
370 }
371
372 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
373 const struct quic_rx_packet *pkt = a2;
374 const struct quic_rx_crypto_frm *cf = a3;
375 const SSL *ssl = a4;
376
377 if (pkt)
378 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
379 quic_packet_type_enc_level_char(pkt->type),
380 (unsigned long long)pkt->pn);
381 if (cf)
382 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
383 (unsigned long long)cf->offset_node.key,
384 (unsigned long long)cf->len);
385 if (ssl) {
386 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200387 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100388 quic_enc_level_char(ssl_to_quic_enc_level(level)));
389 }
390 }
391
392 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
393 const struct quic_frame *frm = a2;
394
395 if (frm)
396 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
397 }
398
399 if (mask & QUIC_EV_CONN_PHPKTS) {
400 const struct quic_enc_level *qel = a2;
401
402 if (qel) {
403 struct quic_pktns *pktns;
404
405 pktns = qc->pktns;
406 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200407 " qel=%c state=%s ack?%d cwnd=%llu ppif=%lld pif=%llu if=%llu pp=%u pdg=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100408 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200409 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille2766e782021-08-30 17:16:07 +0200410 !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100411 (unsigned long long)qc->path->cwnd,
412 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100413 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100414 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
415 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100416 }
417 }
418
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100419 if (mask & QUIC_EV_CONN_ENCPKT) {
420 const struct enc_debug_info *edi = a2;
421
422 if (edi)
423 chunk_appendf(&trace_buf,
424 " payload=@%p payload_len=%llu"
425 " aad=@%p aad_len=%llu pn=%llu",
426 edi->payload, (unsigned long long)edi->payload_len,
427 edi->aad, (unsigned long long)edi->aad_len,
428 (unsigned long long)edi->pn);
429 }
430
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100431 if (mask & QUIC_EV_CONN_RMHP) {
432 const struct quic_rx_packet *pkt = a2;
433
434 if (pkt) {
435 const int *ret = a3;
436
437 chunk_appendf(&trace_buf, " pkt@%p", pkt);
438 if (ret && *ret)
439 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
440 pkt->pnl, (unsigned long long)pkt->pn);
441 }
442 }
443
444 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200445 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100446 const unsigned long *val1 = a3;
447 const unsigned long *val2 = a4;
448
449 if (frm)
450 chunk_tx_frm_appendf(&trace_buf, frm);
451 if (val1)
452 chunk_appendf(&trace_buf, " %lu", *val1);
453 if (val2)
454 chunk_appendf(&trace_buf, "..%lu", *val2);
455 }
456
457 if (mask & QUIC_EV_CONN_RTTUPDT) {
458 const unsigned int *rtt_sample = a2;
459 const unsigned int *ack_delay = a3;
460 const struct quic_loss *ql = a4;
461
462 if (rtt_sample)
463 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
464 if (ack_delay)
465 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
466 if (ql)
467 chunk_appendf(&trace_buf,
468 " srtt=%ums rttvar=%ums min_rtt=%ums",
469 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
470 }
471 if (mask & QUIC_EV_CONN_CC) {
472 const struct quic_cc_event *ev = a2;
473 const struct quic_cc *cc = a3;
474
475 if (a2)
476 quic_cc_event_trace(&trace_buf, ev);
477 if (a3)
478 quic_cc_state_trace(&trace_buf, cc);
479 }
480
481 if (mask & QUIC_EV_CONN_PKTLOSS) {
482 const struct quic_pktns *pktns = a2;
483 const struct list *lost_pkts = a3;
484 struct quic_conn *qc = conn->qc;
485
486 if (pktns) {
487 chunk_appendf(&trace_buf, " pktns=%s",
488 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
489 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
490 if (pktns->tx.loss_time)
491 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100492 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100493 }
494 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
495 struct quic_tx_packet *pkt;
496
497 chunk_appendf(&trace_buf, " lost_pkts:");
498 list_for_each_entry(pkt, lost_pkts, list)
499 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
500 }
501 }
502
503 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
504 struct quic_conn *qc = conn->qc;
505 const struct quic_pktns *pktns = a2;
506 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100507 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100508
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100509 if (ifae_pkts)
510 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
511 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100512 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100513 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100514 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100515 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
516 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 if (mask & QUIC_EV_CONN_STIMER) {
518 if (pktns->tx.loss_time)
519 chunk_appendf(&trace_buf, " loss_time=%dms",
520 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
521 }
522 if (mask & QUIC_EV_CONN_SPTO) {
523 if (pktns->tx.time_of_last_eliciting)
524 chunk_appendf(&trace_buf, " tole=%dms",
525 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
526 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100527 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100528 }
529 }
530
531 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
532 chunk_appendf(&trace_buf,
533 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
534 }
535 }
536
537 if (mask & QUIC_EV_CONN_SPPKTS) {
538 const struct quic_tx_packet *pkt = a2;
539
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100540 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
541 (unsigned long long)qc->path->cwnd,
542 (unsigned long long)qc->path->prep_in_flight,
543 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100544 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100545 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100546 (unsigned long)pkt->pn_node.key,
547 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
548 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100549 (unsigned long long)pkt->in_flight_len,
550 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100551 }
552 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100553
554 if (mask & QUIC_EV_CONN_SSLALERT) {
555 const uint8_t *alert = a2;
556 const enum ssl_encryption_level_t *level = a3;
557
558 if (alert)
559 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
560 if (level)
561 chunk_appendf(&trace_buf, " el=%c",
562 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
563 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100564
565 if (mask & QUIC_EV_CONN_BCFRMS) {
566 const size_t *sz1 = a2;
567 const size_t *sz2 = a3;
568 const size_t *sz3 = a4;
569
570 if (sz1)
571 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
572 if (sz2)
573 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
574 if (sz3)
575 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
576 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100577
578 if (mask & QUIC_EV_CONN_PSTRM) {
579 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100580
581 if (a2) {
582 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100583
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100584 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
585 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
586 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
587 (unsigned long long)s->id,
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200588 (unsigned long long)s->offset.key,
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100589 (unsigned long long)s->len);
590 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100591 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100592 }
593 if (mask & QUIC_EV_CONN_LPKT) {
594 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200595 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596
597 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200598 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100599 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200600 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
601 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200602 if (len)
603 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100604 }
605
606}
607
608/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200609static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100610{
611 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200612 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100613
614 qc = ctx->conn->qc;
615 if (objt_server(qc->conn->target))
616 return 1;
617
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200618 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
619 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
620 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
621 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200622 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100623 return 1;
624
625 return 0;
626}
627
628/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
629 * both loss detection and PTO and schedule the task assiated to this timer if needed.
630 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200631static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100632{
633 struct quic_conn *qc;
634 struct quic_pktns *pktns;
635 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200636 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100637
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100638 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
639 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100640 qc = ctx->conn->qc;
641 pktns = quic_loss_pktns(qc);
642 if (tick_isset(pktns->tx.loss_time)) {
643 qc->timer = pktns->tx.loss_time;
644 goto out;
645 }
646
647 /* XXX TODO: anti-amplification: the timer must be
648 * cancelled for a server which reached the anti-amplification limit.
649 */
650
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100651 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
652 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100653 /* Timer cancellation. */
654 qc->timer = TICK_ETERNITY;
655 goto out;
656 }
657
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200658 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
659 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100660 if (tick_isset(pto))
661 qc->timer = pto;
662 out:
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200663 if (qc->timer != TICK_ETERNITY)
664 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100665 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
666}
667
668#ifndef OPENSSL_IS_BORINGSSL
669int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
670 const uint8_t *read_secret,
671 const uint8_t *write_secret, size_t secret_len)
672{
673 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
674 struct quic_tls_ctx *tls_ctx =
675 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
676 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
677
678 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
679 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
680 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
681 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
682
683 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
684 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
685 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
686 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
687 read_secret, secret_len)) {
688 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
689 return 0;
690 }
691
692 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
693 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
694 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
695 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
696 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
697 write_secret, secret_len)) {
698 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
699 return 0;
700 }
701
702 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
703 if (objt_server(conn->target) && level == ssl_encryption_application) {
704 const unsigned char *buf;
705 size_t buflen;
706
707 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
708 if (!buflen)
709 return 0;
710
711 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
712 return 0;
713 }
714 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
715
716 return 1;
717}
718#else
719/* ->set_read_secret callback to derive the RX secrets at <level> encryption
720 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500721 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100722 */
723int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
724 const SSL_CIPHER *cipher,
725 const uint8_t *secret, size_t secret_len)
726{
727 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
728 struct quic_tls_ctx *tls_ctx =
729 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
730
731 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
732 tls_ctx->rx.aead = tls_aead(cipher);
733 tls_ctx->rx.md = tls_md(cipher);
734 tls_ctx->rx.hp = tls_hp(cipher);
735
736 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
737 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
738 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
739 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
740 secret, secret_len)) {
741 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
742 goto err;
743 }
744
745 if (objt_server(conn->target) && level == ssl_encryption_application) {
746 const unsigned char *buf;
747 size_t buflen;
748
749 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
750 if (!buflen)
751 goto err;
752
753 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
754 goto err;
755 }
756
757 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
758 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
759
760 return 1;
761
762 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100763 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100764 return 0;
765}
766
767/* ->set_write_secret callback to derive the TX secrets at <level>
768 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500769 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100770 */
771int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
772 const SSL_CIPHER *cipher,
773 const uint8_t *secret, size_t secret_len)
774{
775 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
776 struct quic_tls_ctx *tls_ctx =
777 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
778
779 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
780 tls_ctx->tx.aead = tls_aead(cipher);
781 tls_ctx->tx.md = tls_md(cipher);
782 tls_ctx->tx.hp = tls_hp(cipher);
783
784 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
785 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
786 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
787 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
788 secret, secret_len)) {
789 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
790 goto err;
791 }
792
793 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
794 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
795
796 return 1;
797
798 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100799 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100800 return 0;
801}
802#endif
803
804/* This function copies the CRYPTO data provided by the TLS stack found at <data>
805 * with <len> as size in CRYPTO buffers dedicated to store the information about
806 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
807 * It fails only if it could not managed to allocate enough CRYPTO buffers to
808 * store all the data.
809 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
810 */
811static int quic_crypto_data_cpy(struct quic_enc_level *qel,
812 const unsigned char *data, size_t len)
813{
814 struct quic_crypto_buf **qcb;
815 /* The remaining byte to store in CRYPTO buffers. */
816 size_t cf_offset, cf_len, *nb_buf;
817 unsigned char *pos;
818
819 nb_buf = &qel->tx.crypto.nb_buf;
820 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
821 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
822 cf_len = len;
823
824 while (len) {
825 size_t to_copy, room;
826
827 pos = (*qcb)->data + (*qcb)->sz;
828 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
829 to_copy = len > room ? room : len;
830 if (to_copy) {
831 memcpy(pos, data, to_copy);
832 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
833 qel->tx.crypto.sz += to_copy;
834 (*qcb)->sz += to_copy;
835 pos += to_copy;
836 len -= to_copy;
837 data += to_copy;
838 }
839 else {
840 struct quic_crypto_buf **tmp;
841
842 tmp = realloc(qel->tx.crypto.bufs,
843 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
844 if (tmp) {
845 qel->tx.crypto.bufs = tmp;
846 qcb = &qel->tx.crypto.bufs[*nb_buf];
847 *qcb = pool_alloc(pool_head_quic_crypto_buf);
848 if (!*qcb)
849 return 0;
850
851 (*qcb)->sz = 0;
852 ++*nb_buf;
853 }
854 else {
855 break;
856 }
857 }
858 }
859
860 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
861 * have been buffered.
862 */
863 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200864 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100865
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200866 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100867 if (!frm)
868 return 0;
869
870 frm->type = QUIC_FT_CRYPTO;
871 frm->crypto.offset = cf_offset;
872 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200873 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200874 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100875 }
876
877 return len == 0;
878}
879
880
881/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
882 * wants to provide the QUIC layer with CRYPTO data.
883 * Returns 1 if succeeded, 0 if not.
884 */
885int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
886 const uint8_t *data, size_t len)
887{
888 struct connection *conn;
889 enum quic_tls_enc_level tel;
890 struct quic_enc_level *qel;
891
892 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
893 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
894 tel = ssl_to_quic_enc_level(level);
895 qel = &conn->qc->els[tel];
896
897 if (tel == -1) {
898 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
899 goto err;
900 }
901
902 if (!quic_crypto_data_cpy(qel, data, len)) {
903 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
904 goto err;
905 }
906
907 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
908 conn, &level, &len);
909
910 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
911 return 1;
912
913 err:
914 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
915 return 0;
916}
917
918int ha_quic_flush_flight(SSL *ssl)
919{
920 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
921
922 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
923 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
924
925 return 1;
926}
927
928int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
929{
930 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
931
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100932 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100933 return 1;
934}
935
936/* QUIC TLS methods */
937static SSL_QUIC_METHOD ha_quic_method = {
938#ifdef OPENSSL_IS_BORINGSSL
939 .set_read_secret = ha_set_rsec,
940 .set_write_secret = ha_set_wsec,
941#else
942 .set_encryption_secrets = ha_quic_set_encryption_secrets,
943#endif
944 .add_handshake_data = ha_quic_add_handshake_data,
945 .flush_flight = ha_quic_flush_flight,
946 .send_alert = ha_quic_send_alert,
947};
948
949/* Initialize the TLS context of a listener with <bind_conf> as configuration.
950 * Returns an error count.
951 */
952int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
953{
954 struct proxy *curproxy = bind_conf->frontend;
955 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
956 int cfgerr = 0;
957
958#if 0
959 /* XXX Did not manage to use this. */
960 const char *ciphers =
961 "TLS_AES_128_GCM_SHA256:"
962 "TLS_AES_256_GCM_SHA384:"
963 "TLS_CHACHA20_POLY1305_SHA256:"
964 "TLS_AES_128_CCM_SHA256";
965#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200966 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100967 long options =
968 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
969 SSL_OP_SINGLE_ECDH_USE |
970 SSL_OP_CIPHER_SERVER_PREFERENCE;
971 SSL_CTX *ctx;
972
973 ctx = SSL_CTX_new(TLS_server_method());
974 bind_conf->initial_ctx = ctx;
975
976 SSL_CTX_set_options(ctx, options);
977#if 0
978 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
979 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
980 "for bind '%s' at [%s:%d].\n",
981 curproxy->id, ciphers,
982 bind_conf->arg, bind_conf->file, bind_conf->line);
983 cfgerr++;
984 }
985#endif
986
987 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
988 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
989 "for bind '%s' at [%s:%d].\n",
990 curproxy->id, groups,
991 bind_conf->arg, bind_conf->file, bind_conf->line);
992 cfgerr++;
993 }
994
995 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
996 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
997 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
998 SSL_CTX_set_default_verify_paths(ctx);
999
1000#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1001#ifdef OPENSSL_IS_BORINGSSL
1002 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
1003 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1004#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
1005 if (bind_conf->ssl_conf.early_data) {
1006 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
1007 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
1008 }
1009 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
1010 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1011#else
1012 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1013#endif
1014 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1015#endif
1016 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1017
1018 return cfgerr;
1019}
1020
1021/* Decode an expected packet number from <truncated_on> its truncated value,
1022 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1023 * the number of bits used to encode this packet number (its length in bytes * 8).
1024 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1025 */
1026static uint64_t decode_packet_number(uint64_t largest_pn,
1027 uint32_t truncated_pn, unsigned int pn_nbits)
1028{
1029 uint64_t expected_pn = largest_pn + 1;
1030 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1031 uint64_t pn_hwin = pn_win / 2;
1032 uint64_t pn_mask = pn_win - 1;
1033 uint64_t candidate_pn;
1034
1035
1036 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1037 /* Note that <pn_win> > <pn_hwin>. */
1038 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1039 candidate_pn + pn_hwin <= expected_pn)
1040 return candidate_pn + pn_win;
1041
1042 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1043 return candidate_pn - pn_win;
1044
1045 return candidate_pn;
1046}
1047
1048/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1049 * cryptographic context.
1050 * <largest_pn> is the largest received packet number and <pn> the address of
1051 * the packet number field for this packet with <byte0> address of its first byte.
1052 * <end> points to one byte past the end of this packet.
1053 * Returns 1 if succeeded, 0 if not.
1054 */
1055static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1056 int64_t largest_pn, unsigned char *pn,
1057 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001058 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001059{
1060 int ret, outlen, i, pnlen;
1061 uint64_t packet_number;
1062 uint32_t truncated_pn = 0;
1063 unsigned char mask[5] = {0};
1064 unsigned char *sample;
1065 EVP_CIPHER_CTX *cctx;
1066 unsigned char *hp_key;
1067
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001068 /* Check there is enough data in this packet. */
1069 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1070 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1071 return 0;
1072 }
1073
1074 cctx = EVP_CIPHER_CTX_new();
1075 if (!cctx) {
1076 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1077 return 0;
1078 }
1079
1080 ret = 0;
1081 sample = pn + QUIC_PACKET_PN_MAXLEN;
1082
1083 hp_key = tls_ctx->rx.hp_key;
1084 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1085 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1086 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1087 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1088 goto out;
1089 }
1090
1091 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1092 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1093 for (i = 0; i < pnlen; i++) {
1094 pn[i] ^= mask[i + 1];
1095 truncated_pn = (truncated_pn << 8) | pn[i];
1096 }
1097
1098 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1099 /* Store remaining information for this unprotected header */
1100 pkt->pn = packet_number;
1101 pkt->pnl = pnlen;
1102
1103 ret = 1;
1104
1105 out:
1106 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107
1108 return ret;
1109}
1110
1111/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1112 * address, with <payload_len> as payload length, <aad> as address of
1113 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1114 * context.
1115 * Returns 1 if succeeded, 0 if not.
1116 */
1117static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1118 unsigned char *aad, size_t aad_len, uint64_t pn,
1119 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1120{
1121 unsigned char iv[12];
1122 unsigned char *tx_iv = tls_ctx->tx.iv;
1123 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001124 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125
1126 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1127 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001128 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001129 }
1130
1131 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1132 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1133 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001134 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001135 }
1136
1137 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001138
1139 err:
1140 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1141 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1142 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001143}
1144
1145/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1146 * Returns 1 if succeeded, 0 if not.
1147 */
1148static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1149{
1150 int ret;
1151 unsigned char iv[12];
1152 unsigned char *rx_iv = tls_ctx->rx.iv;
1153 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1154
1155 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1156 return 0;
1157
1158 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1159 pkt->data, pkt->aad_len,
1160 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1161 if (!ret)
1162 return 0;
1163
1164 /* Update the packet length (required to parse the frames). */
1165 pkt->len = pkt->aad_len + ret;
1166
1167 return 1;
1168}
1169
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001170/* Remove from <qcs> stream the acknowledged frames.
1171 * Never fails.
1172 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001173static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001174{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001175 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001176 struct eb64_node *frm_node;
1177
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001178 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001179 frm_node = eb64_first(&qcs->tx.acked_frms);
1180 while (frm_node) {
1181 struct quic_stream *strm;
1182
1183 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1184 if (strm->offset.key != qcs->tx.ack_offset)
1185 break;
1186
1187 b_del(strm->buf, strm->len);
1188 qcs->tx.ack_offset += strm->len;
1189 frm_node = eb64_next(frm_node);
1190 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001191 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001192 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001193
1194 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001195}
1196
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001197/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001198static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001199 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001200{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001201 int stream_acked;
1202 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001203
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001204 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001205 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001206 switch (frm->type) {
1207 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1208 {
1209 struct qcs *qcs = frm->stream.qcs;
1210 struct quic_stream *strm = &frm->stream;
1211
1212 if (qcs->tx.ack_offset == strm->offset.key) {
1213 b_del(strm->buf, strm->len);
1214 qcs->tx.ack_offset += strm->len;
1215 LIST_DELETE(&frm->list);
1216 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001217 qc->qcc->flags &= ~QC_CF_MUX_MFULL;
1218 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001219 }
1220 else {
1221 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1222 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001223 stream_acked |= qcs_try_to_consume(qcs);
Amaury Denoyellecae07912021-10-08 17:57:41 +02001224
1225 if (qcs->flags & QC_SF_DETACH)
1226 tasklet_wakeup(qcs->shut_tl);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001227 }
1228 break;
1229 default:
1230 LIST_DELETE(&frm->list);
1231 pool_free(pool_head_quic_frame, frm);
1232 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001233
1234 if (stream_acked) {
1235 struct qcc *qcc = qc->qcc;
1236
1237 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1238 tasklet_wakeup(qcc->subs->tasklet);
1239 qcc->subs->events &= ~SUB_RETRY_SEND;
1240 if (!qcc->subs->events)
1241 qcc->subs = NULL;
1242 }
1243 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001244}
1245
1246/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1247 * deallocating them, and their TX frames.
1248 * Returns the last node reached to be used for the next range.
1249 * May be NULL if <largest> node could not be found.
1250 */
1251static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1252 struct list *newly_acked_pkts,
1253 struct eb64_node *largest_node,
1254 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001255 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001256{
1257 struct eb64_node *node;
1258 struct quic_tx_packet *pkt;
1259
1260 if (largest_node)
1261 node = largest_node;
1262 else {
1263 node = eb64_lookup(pkts, largest);
1264 while (!node && largest > smallest) {
1265 node = eb64_lookup(pkts, --largest);
1266 }
1267 }
1268
1269 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001270 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271
1272 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1273 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001274 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001275 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1276 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1277 qc_treat_acked_tx_frm(frm, ctx);
1278 node = eb64_prev(node);
1279 eb64_delete(&pkt->pn_node);
1280 }
1281
1282 return node;
1283}
1284
1285/* Treat <frm> frame whose packet it is attached to has just been detected as non
1286 * acknowledged.
1287 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001288static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001289 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001290 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001291{
1292 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001293 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001294 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001295}
1296
1297
1298/* Free the TX packets of <pkts> list */
1299static inline void free_quic_tx_pkts(struct list *pkts)
1300{
1301 struct quic_tx_packet *pkt, *tmp;
1302
1303 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001304 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001305 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001306 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001307 }
1308}
1309
1310/* Send a packet loss event nofification to the congestion controller
1311 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1312 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1313 * at <now_us> current time.
1314 * Always succeeds.
1315 */
1316static inline void qc_cc_loss_event(struct quic_conn *qc,
1317 unsigned int lost_bytes,
1318 unsigned int newest_time_sent,
1319 unsigned int period,
1320 unsigned int now_us)
1321{
1322 struct quic_cc_event ev = {
1323 .type = QUIC_CC_EVT_LOSS,
1324 .loss.now_ms = now_ms,
1325 .loss.max_ack_delay = qc->max_ack_delay,
1326 .loss.lost_bytes = lost_bytes,
1327 .loss.newest_time_sent = newest_time_sent,
1328 .loss.period = period,
1329 };
1330
1331 quic_cc_event(&qc->path->cc, &ev);
1332}
1333
1334/* Send a packet ack event nofication for each newly acked packet of
1335 * <newly_acked_pkts> list and free them.
1336 * Always succeeds.
1337 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001338static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001339 struct list *newly_acked_pkts)
1340{
1341 struct quic_conn *qc = ctx->conn->qc;
1342 struct quic_tx_packet *pkt, *tmp;
1343 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1344
1345 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1346 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001347 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001348 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001349 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001350 ev.ack.acked = pkt->in_flight_len;
1351 ev.ack.time_sent = pkt->time_sent;
1352 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001353 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001354 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001355 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001356 }
1357
1358}
1359
1360/* Handle <pkts> list of lost packets detected at <now_us> handling
1361 * their TX frames.
1362 * Send a packet loss event to the congestion controller if
1363 * in flight packet have been lost.
1364 * Also frees the packet in <pkts> list.
1365 * Never fails.
1366 */
1367static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001368 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001369 struct list *pkts,
1370 uint64_t now_us)
1371{
1372 struct quic_conn *qc = ctx->conn->qc;
1373 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001374 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001375 uint64_t lost_bytes;
1376
1377 lost_bytes = 0;
1378 oldest_lost = newest_lost = NULL;
1379 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1380 lost_bytes += pkt->in_flight_len;
1381 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001382 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001383 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001384 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001385 /* Treat the frames of this lost packet. */
1386 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1387 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001388 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001389 if (!oldest_lost) {
1390 oldest_lost = newest_lost = pkt;
1391 }
1392 else {
1393 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001394 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001395 newest_lost = pkt;
1396 }
1397 }
1398
1399 if (lost_bytes) {
1400 /* Sent a packet loss event to the congestion controller. */
1401 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1402 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001403 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001404 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001405 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001406 }
1407}
1408
1409/* Look for packet loss from sent packets for <qel> encryption level of a
1410 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1411 * their tree if deemed as lost or set the <loss_time> value the packet number
1412 * space if any not deemed lost.
1413 * Should be called after having received an ACK frame with newly acknowledged
1414 * packets or when the the loss detection timer has expired.
1415 * Always succeeds.
1416 */
1417static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1418 struct quic_conn *qc,
1419 struct list *lost_pkts)
1420{
1421 struct eb_root *pkts;
1422 struct eb64_node *node;
1423 struct quic_loss *ql;
1424 unsigned int loss_delay;
1425
1426 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1427 pkts = &pktns->tx.pkts;
1428 pktns->tx.loss_time = TICK_ETERNITY;
1429 if (eb_is_empty(pkts))
1430 goto out;
1431
1432 ql = &qc->path->loss;
1433 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1434 loss_delay += loss_delay >> 3;
1435 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1436
1437 node = eb64_first(pkts);
1438 while (node) {
1439 struct quic_tx_packet *pkt;
1440 int64_t largest_acked_pn;
1441 unsigned int loss_time_limit, time_sent;
1442
1443 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001444 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001445 node = eb64_next(node);
1446 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1447 break;
1448
1449 time_sent = pkt->time_sent;
1450 loss_time_limit = tick_add(time_sent, loss_delay);
1451 if (tick_is_le(time_sent, now_ms) ||
1452 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1453 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001454 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001455 }
1456 else {
1457 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1458 }
1459 }
1460
1461 out:
1462 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1463}
1464
1465/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1466 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001467 * 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 +01001468 * acked ack-eliciting packet.
1469 * Return 1, if succeeded, 0 if not.
1470 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001471static inline int qc_parse_ack_frm(struct quic_frame *frm, struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001472 struct quic_enc_level *qel,
1473 unsigned int *rtt_sample,
1474 const unsigned char **pos, const unsigned char *end)
1475{
1476 struct quic_ack *ack = &frm->ack;
1477 uint64_t smallest, largest;
1478 struct eb_root *pkts;
1479 struct eb64_node *largest_node;
1480 unsigned int time_sent, pkt_flags;
1481 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1482 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1483
1484 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1485 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1486 ctx->conn,, &ack->largest_ack);
1487 goto err;
1488 }
1489
1490 if (ack->first_ack_range > ack->largest_ack) {
1491 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1492 ctx->conn,, &ack->first_ack_range);
1493 goto err;
1494 }
1495
1496 largest = ack->largest_ack;
1497 smallest = largest - ack->first_ack_range;
1498 pkts = &qel->pktns->tx.pkts;
1499 pkt_flags = 0;
1500 largest_node = NULL;
1501 time_sent = 0;
1502
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001503 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 +01001504 largest_node = eb64_lookup(pkts, largest);
1505 if (!largest_node) {
1506 TRACE_DEVEL("Largest acked packet not found",
1507 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1508 goto err;
1509 }
1510
1511 time_sent = eb64_entry(&largest_node->node,
1512 struct quic_tx_packet, pn_node)->time_sent;
1513 }
1514
1515 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1516 ctx->conn,, &largest, &smallest);
1517 do {
1518 uint64_t gap, ack_range;
1519
1520 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1521 largest_node, largest, smallest, ctx);
1522 if (!ack->ack_range_num--)
1523 break;
1524
1525 if (!quic_dec_int(&gap, pos, end))
1526 goto err;
1527
1528 if (smallest < gap + 2) {
1529 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1530 ctx->conn,, &gap, &smallest);
1531 goto err;
1532 }
1533
1534 largest = smallest - gap - 2;
1535 if (!quic_dec_int(&ack_range, pos, end))
1536 goto err;
1537
1538 if (largest < ack_range) {
1539 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1540 ctx->conn,, &largest, &ack_range);
1541 goto err;
1542 }
1543
1544 /* Do not use this node anymore. */
1545 largest_node = NULL;
1546 /* Next range */
1547 smallest = largest - ack_range;
1548
1549 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1550 ctx->conn,, &largest, &smallest);
1551 } while (1);
1552
1553 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001554 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001555
1556 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1557 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001558 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001559 }
1560
1561 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1562 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1563 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1564 if (!LIST_ISEMPTY(&lost_pkts))
1565 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1566 }
1567 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1568 if (quic_peer_validated_addr(ctx))
1569 ctx->conn->qc->path->loss.pto_count = 0;
1570 qc_set_timer(ctx);
1571 }
1572
1573
1574 return 1;
1575
1576 err:
1577 free_quic_tx_pkts(&newly_acked_pkts);
1578 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1579 return 0;
1580}
1581
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001582/* This function gives the detail of the SSL error. It is used only
1583 * if the debug mode and the verbose mode are activated. It dump all
1584 * the SSL error until the stack was empty.
1585 */
1586static forceinline void qc_ssl_dump_errors(struct connection *conn)
1587{
1588 if (unlikely(global.mode & MODE_DEBUG)) {
1589 while (1) {
1590 unsigned long ret;
1591
1592 ret = ERR_get_error();
1593 if (!ret)
1594 return;
1595
1596 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1597 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1598 }
1599 }
1600}
1601
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001602int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx,
1603 const char **str, int *len);
1604
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1606 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001607 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608 * Return 1 if succeeded, 0 if not.
1609 */
1610static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001611 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001612 const unsigned char *data, size_t len,
1613 struct quic_rx_packet *pkt,
1614 struct quic_rx_crypto_frm *cf)
1615{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001616 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001617 struct quic_conn *qc;
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001618 const char *alpn;
1619 int alpn_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620
1621 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1622 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001623 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001624 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1625 TRACE_PROTO("SSL_provide_quic_data() error",
1626 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1627 goto err;
1628 }
1629
1630 el->rx.crypto.offset += len;
1631 TRACE_PROTO("in order CRYPTO data",
1632 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1633
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001634 state = HA_ATOMIC_LOAD(&qc->state);
1635 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001636 ssl_err = SSL_do_handshake(ctx->ssl);
1637 if (ssl_err != 1) {
1638 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1639 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1640 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001641 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001642 goto out;
1643 }
1644
1645 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001646 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001647 qc_ssl_dump_errors(ctx->conn);
1648 BUG_ON(1);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001649 goto err;
1650 }
1651
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001652 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001654 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001655 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001656 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001657 } else {
1658 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1659 if (ssl_err != 1) {
1660 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1661 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1662 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001663 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001664 goto out;
1665 }
1666
1667 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001668 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001669 goto err;
1670 }
1671
1672 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001673 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001674 }
1675
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001676 conn_get_alpn(ctx->conn, &alpn, &alpn_len);
1677 if (alpn_len >= 2 && memcmp(alpn, "h3", 2) == 0) {
1678 qc->qcc->app_ops = &h3_ops;
1679 if (!qc->qcc->app_ops->init(qc->qcc))
1680 goto err;
1681 }
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +01001682 else if (alpn_len >= 10 && memcmp(alpn, "hq-interop", 10) == 0) {
1683 qc->qcc->app_ops = &hq_interop_ops;
1684 }
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01001685 else {
1686 /* TODO RFC9001 8.1. Protocol Negotiation
1687 * must return no_application_protocol TLS alert
1688 */
1689 ABORT_NOW();
1690 }
1691
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001692 out:
1693 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1694 return 1;
1695
1696 err:
1697 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1698 return 0;
1699}
1700
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001701/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1702 * <pkt> RX packet.
1703 * Return it if succeeded, NULL if not.
1704 */
1705static inline
1706struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1707 struct quic_rx_packet *pkt)
1708{
1709 struct quic_rx_strm_frm *frm;
1710
1711 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1712 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001713 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001714 frm->len = stream_frm->len;
1715 frm->data = stream_frm->data;
1716 frm->pkt = pkt;
1717 }
1718
1719 return frm;
1720}
1721
1722/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001723 * several streams, depending on the already open ones.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001724 * Return this node if succeeded, NULL if not.
1725 */
1726static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1727{
1728 unsigned int strm_type;
1729 int64_t sub_id;
1730 struct eb64_node *strm_node;
1731
1732 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1733
1734 strm_type = id & QCS_ID_TYPE_MASK;
1735 sub_id = id >> QCS_ID_TYPE_SHIFT;
1736 strm_node = NULL;
1737 if (qc_local_stream_id(qcc, id)) {
1738 /* Local streams: this stream must be already opened. */
1739 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1740 if (!strm_node) {
1741 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1742 goto out;
1743 }
1744 }
1745 else {
1746 /* Remote streams. */
1747 struct eb_root *strms;
1748 uint64_t largest_id;
1749 enum qcs_type qcs_type;
1750
1751 strms = &qcc->streams_by_id;
1752 qcs_type = qcs_id_type(id);
1753 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1754 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1755 goto out;
1756 }
1757
1758 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1759 * correct value.
1760 */
1761 largest_id = qcc->strms[qcs_type].largest_id;
1762 if (sub_id > (int64_t)largest_id) {
1763 /* RFC: "A stream ID that is used out of order results in all streams
1764 * of that type with lower-numbered stream IDs also being opened".
1765 * So, let's "open" these streams.
1766 */
1767 int64_t i;
1768 struct qcs *qcs;
1769
1770 qcs = NULL;
1771 for (i = largest_id + 1; i <= sub_id; i++) {
1772 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1773 if (!qcs) {
1774 TRACE_PROTO("Could not allocate a new stream",
1775 QUIC_EV_CONN_PSTRM, qcc->conn);
1776 goto out;
1777 }
1778
1779 qcc->strms[qcs_type].largest_id = i;
1780 }
1781 if (qcs)
1782 strm_node = &qcs->by_id;
1783 }
1784 else {
1785 strm_node = eb64_lookup(strms, id);
1786 }
1787 }
1788
1789 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1790 return strm_node;
1791
1792 out:
1793 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1794 return NULL;
1795}
1796
1797/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1798 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1799 * to reflect the data which have been consumed.
1800 */
1801static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1802{
1803 size_t ret;
1804
1805 ret = 0;
1806 while (strm_frm->len) {
1807 size_t try;
1808
1809 try = b_contig_space(buf);
1810 if (!try)
1811 break;
1812
1813 if (try > strm_frm->len)
1814 try = strm_frm->len;
1815 memcpy(b_tail(buf), strm_frm->data, try);
1816 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001817 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001818 b_add(buf, try);
1819 ret += try;
1820 }
1821
1822 return ret;
1823}
1824
1825/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1826 * streams may be open. The data are copied to the stream RX buffer if possible.
1827 * If not, the STREAM frame is stored to be treated again later.
1828 * We rely on the flow control so that not to store too much STREAM frames.
1829 * Return 1 if succeeded, 0 if not.
1830 */
1831static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1832 struct quic_stream *strm_frm,
1833 struct quic_conn *qc)
1834{
1835 struct qcs *strm;
1836 struct eb64_node *strm_node, *frm_node;
1837 struct quic_rx_strm_frm *frm;
1838
1839 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1840 if (!strm_node) {
1841 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1842 return 0;
1843 }
1844
1845 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001846 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001847 /* FIXME: handle the case where this frame overlap others */
1848 if (frm_node) {
1849 TRACE_PROTO("Already existing stream data",
1850 QUIC_EV_CONN_PSTRM, qc->conn);
1851 goto out;
1852 }
1853
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001854 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001855 int ret;
1856
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001857 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001858 goto store_frm;
1859
1860 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
Amaury Denoyelleb9ce14e2021-11-08 09:13:42 +01001861 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) < 0) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001862 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1863 return 0;
1864 }
1865
1866 strm->rx.offset += ret;
1867 }
1868
1869 if (!strm_frm->len)
1870 goto out;
1871
1872 store_frm:
1873 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1874 if (!frm) {
1875 TRACE_PROTO("Could not alloc RX STREAM frame",
1876 QUIC_EV_CONN_PSTRM, qc->conn);
1877 return 0;
1878 }
1879
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001880 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001881 quic_rx_packet_refinc(pkt);
1882
1883 out:
1884 return 1;
1885}
1886
1887/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1888 * streams may be open. The data are copied to the stream RX buffer if possible.
1889 * If not, the STREAM frame is stored to be treated again later.
1890 * We rely on the flow control so that not to store too much STREAM frames.
1891 * Return 1 if succeeded, 0 if not.
1892 */
1893static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1894 struct quic_stream *strm_frm,
1895 struct quic_conn *qc)
1896{
1897 struct qcs *strm;
1898 struct eb64_node *strm_node, *frm_node;
1899 struct quic_rx_strm_frm *frm;
1900 size_t strm_frm_len;
1901
1902 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1903 if (!strm_node) {
1904 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1905 return 0;
1906 }
1907
1908 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001909 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001910 /* FIXME: handle the case where this frame overlap others */
1911 if (frm_node) {
1912 TRACE_PROTO("Already existing stream data",
1913 QUIC_EV_CONN_PSTRM, qc->conn);
1914 goto out;
1915 }
1916
1917 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001918 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001919 int ret;
1920
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001921 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001922 goto store_frm;
1923
1924 /* qc_strm_cpy() will modify the offset, depending on the number
1925 * of bytes copied.
1926 */
1927 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1928 /* Inform the application of the arrival of this new stream */
1929 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1930 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1931 return 0;
1932 }
1933
1934 if (ret)
1935 ruqs_notify_recv(strm);
1936
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001937 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001938 }
1939 /* Take this frame into an account for the stream flow control */
1940 strm->rx.offset += strm_frm_len;
1941 /* It all the data were provided to the application, there is no need to
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05001942 * store any more information for it.
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001943 */
1944 if (!strm_frm->len)
1945 goto out;
1946
1947 store_frm:
1948 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1949 if (!frm) {
1950 TRACE_PROTO("Could not alloc RX STREAM frame",
1951 QUIC_EV_CONN_PSTRM, qc->conn);
1952 return 0;
1953 }
1954
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001955 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001956 quic_rx_packet_refinc(pkt);
1957
1958 out:
1959 return 1;
1960}
1961
1962static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1963 struct quic_stream *strm_frm,
1964 struct quic_conn *qc)
1965{
1966 if (strm_frm->id & QCS_ID_DIR_BIT)
1967 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1968 else
1969 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1970}
1971
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001972/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1973 * as I/O handler context and <qel> as encryption level.
1974 * Returns 1 if succeeded, 0 if failed.
1975 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001976static 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 +01001977 struct quic_enc_level *qel)
1978{
1979 struct quic_frame frm;
1980 const unsigned char *pos, *end;
1981 struct quic_conn *conn = ctx->conn->qc;
1982
1983 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1984 /* Skip the AAD */
1985 pos = pkt->data + pkt->aad_len;
1986 end = pkt->data + pkt->len;
1987
1988 while (pos < end) {
1989 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1990 goto err;
1991
1992 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001993 case QUIC_FT_PADDING:
1994 if (pos != end) {
1995 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1996 goto err;
1997 }
1998 break;
1999 case QUIC_FT_PING:
2000 break;
2001 case QUIC_FT_ACK:
2002 {
2003 unsigned int rtt_sample;
2004
2005 rtt_sample = 0;
2006 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
2007 goto err;
2008
2009 if (rtt_sample) {
2010 unsigned int ack_delay;
2011
2012 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
2013 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
2014 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
2015 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002016 break;
2017 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018 case QUIC_FT_CRYPTO:
2019 if (frm.crypto.offset != qel->rx.crypto.offset) {
2020 struct quic_rx_crypto_frm *cf;
2021
2022 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
2023 if (!cf) {
2024 TRACE_DEVEL("CRYPTO frame allocation failed",
2025 QUIC_EV_CONN_PRSHPKT, ctx->conn);
2026 goto err;
2027 }
2028
2029 cf->offset_node.key = frm.crypto.offset;
2030 cf->len = frm.crypto.len;
2031 cf->data = frm.crypto.data;
2032 cf->pkt = pkt;
2033 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2034 quic_rx_packet_refinc(pkt);
2035 }
2036 else {
2037 /* XXX TO DO: <cf> is used only for the traces. */
2038 struct quic_rx_crypto_frm cf = { };
2039
2040 cf.offset_node.key = frm.crypto.offset;
2041 cf.len = frm.crypto.len;
2042 if (!qc_provide_cdata(qel, ctx,
2043 frm.crypto.data, frm.crypto.len,
2044 pkt, &cf))
2045 goto err;
2046 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002047 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002048 case QUIC_FT_STREAM_8:
2049 case QUIC_FT_STREAM_9:
2050 case QUIC_FT_STREAM_A:
2051 case QUIC_FT_STREAM_B:
2052 case QUIC_FT_STREAM_C:
2053 case QUIC_FT_STREAM_D:
2054 case QUIC_FT_STREAM_E:
2055 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002056 {
2057 struct quic_stream *stream = &frm.stream;
2058
2059 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
2060 if (objt_listener(ctx->conn->target)) {
2061 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2062 goto err;
2063 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2064 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002065
2066 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
2067 goto err;
2068
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002069 break;
2070 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002071 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002072 break;
2073 case QUIC_FT_CONNECTION_CLOSE:
2074 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle493bb1d2021-10-13 16:34:49 +02002075 conn->qcc->flags |= QC_CF_CC_RECV;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002076 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002077 case QUIC_FT_HANDSHAKE_DONE:
2078 if (objt_listener(ctx->conn->target))
2079 goto err;
2080
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002081 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002082 break;
2083 default:
2084 goto err;
2085 }
2086 }
2087
2088 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2089 * has successfully parse a Handshake packet. The Initial encryption must also
2090 * be discarded.
2091 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002092 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
2093 int state = HA_ATOMIC_LOAD(&conn->state);
2094
2095 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
2096 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2097 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2098 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2099 qc_set_timer(ctx);
2100 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
2101 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
2102 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 }
2104
2105 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2106 return 1;
2107
2108 err:
2109 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2110 return 0;
2111}
2112
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002113/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002114 * buffer. This is the responsibility of the caller to check there is enough
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002115 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2116 * This function must be called only after having built a correct datagram.
2117 * Always succeeds.
2118 */
2119static inline void qc_set_dg(struct cbuf *cbuf,
2120 uint16_t dglen, struct quic_tx_packet *pkt)
2121{
2122 write_u16(cb_wr(cbuf), dglen);
2123 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2124 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2125}
2126
2127/* Prepare as much as possible handshake packets into <qr> ring buffer for
2128 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2129 * several packets in the same datagram. A header made of two fields is added
2130 * to each datagram: the datagram length followed by the address of the first
2131 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002132 * Returns 1 if succeeded, or 0 if something wrong happened.
2133 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002134static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002135{
2136 struct quic_conn *qc;
2137 enum quic_tls_enc_level tel, next_tel;
2138 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002139 struct cbuf *cbuf;
2140 unsigned char *end_buf, *end, *pos, *spos;
2141 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2142 /* length of datagrams */
2143 uint16_t dglen;
2144 size_t total;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002145 int padding;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002146 /* Each datagram is prepended with its length followed by the
2147 * address of the first packet in the datagram.
2148 */
2149 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002150
2151 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2152 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002153 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002154 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002155 goto err;
2156 }
2157
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002158 start:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002159 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002160 total = 0;
2161 padding = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002162 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002163 cbuf = qr->cbuf;
2164 spos = pos = cb_wr(cbuf);
2165 /* Leave at least <dglen> bytes at the end of this buffer
2166 * to ensure there is enough room to mark the end of prepared
2167 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002168 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002169 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2170 first_pkt = prv_pkt = NULL;
2171 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002172 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002173 enum quic_pkt_type pkt_type;
2174
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002175 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002176 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002177 if (!prv_pkt) {
2178 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2179 do {
2180 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2181 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2182 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002183 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002184 /* Do not build any more packet if the TX secrets are not available or
2185 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002186 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002187 * and if there is no more CRYPTO data available or in flight
2188 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002189 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002190 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002191 (!ack && !nb_ptos &&
2192 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2193 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002194 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002195 /* Set the current datagram as prepared into <cbuf> if
2196 * the was already a correct packet which was previously written.
2197 */
2198 if (prv_pkt)
2199 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002200 break;
2201 }
2202
2203 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002204 if (!prv_pkt) {
2205 /* Leave room for the datagram header */
2206 pos += dg_headlen;
2207 end = pos + qc->path->mtu;
2208 }
2209
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002210 cur_pkt = qc_build_pkt(&pos, end, qel, qc, dglen, padding,
2211 pkt_type, ack, nb_ptos, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002212 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002213 if (err < 0) {
2214 if (!prv_pkt && nb_ptos)
2215 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002216 if (ack)
2217 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002218 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002219 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002220 case -2:
2221 goto err;
2222 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002223 /* If there was already a correct packet present, set the
2224 * current datagram as prepared into <cbuf>.
2225 */
2226 if (prv_pkt) {
2227 qc_set_dg(cbuf, dglen, first_pkt);
2228 goto stop_build;
2229 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002230 goto out;
2231 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002232 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2233 if (!cur_pkt)
2234 goto err;
2235
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002236 total += cur_pkt->len;
2237 /* keep trace of the first packet in the datagram */
2238 if (!first_pkt)
2239 first_pkt = cur_pkt;
2240 /* Attach the current one to the previous one */
2241 if (prv_pkt)
2242 prv_pkt->next = cur_pkt;
2243 /* Let's say we have to build a new dgram */
2244 prv_pkt = NULL;
2245 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002246 /* Discard the Initial encryption keys as soon as
2247 * a handshake packet could be built.
2248 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002249 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002250 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2251 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002252 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2254 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002255 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002256 }
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002257 /* If the data for the current encryption level have all been sent,
2258 * select the next level.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002259 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002260 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002261 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2262 (next_tel != QUIC_TLS_ENC_LEVEL_NONE && qc->els[next_tel].pktns->tx.in_flight))) {
Frédéric Lécaille4bade772021-08-23 08:54:28 +02002263 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2264 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2265 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002266 tel = next_tel;
2267 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002268 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002269 /* If there is data for the next level, do not
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002270 * consume a datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002271 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002272 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002273 }
2274 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002275 }
2276
2277 /* If we have to build a new datagram, set the current datagram as
2278 * prepared into <cbuf>.
2279 */
2280 if (!prv_pkt) {
2281 qc_set_dg(cbuf, dglen, first_pkt);
2282 first_pkt = NULL;
2283 dglen = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01002284 padding = 0;
2285 }
2286 else if (prv_pkt->type == QUIC_TLS_ENC_LEVEL_INITIAL &&
2287 (objt_server(qc->conn->target) ||
2288 prv_pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
2289 padding = 1;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002290 }
2291 }
2292
2293 stop_build:
2294 /* Reset <wr> writer index if in front of <rd> index */
2295 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2296 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2297
2298 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2299 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2300 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2301 /* Mark the end of contiguous data for the reader */
2302 write_u16(cb_wr(cbuf), 0);
2303 cb_add(cbuf, sizeof(uint16_t));
2304 }
2305 }
2306
2307 if (rd && rd <= cbuf->wr) {
2308 cb_wr_reset(cbuf);
2309 if (pos == spos) {
2310 /* Reuse the same buffer if nothing was built. */
2311 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312 }
2313 }
2314 }
2315
2316 out:
2317 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002318 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002319
2320 err:
2321 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002322 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323}
2324
2325/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002326 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002327 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002328int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002329{
2330 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002331 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002332
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002333 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002334 cbuf = qr->cbuf;
2335 while (cb_contig_data(cbuf)) {
2336 unsigned char *pos;
2337 struct buffer tmpbuf = { };
2338 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2339 uint16_t dglen;
2340 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002341 unsigned int time_sent;
2342
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002343 pos = cb_rd(cbuf);
2344 dglen = read_u16(pos);
2345 /* End of prepared datagrams.
2346 * Reset the reader index only if in front of the writer index.
2347 */
2348 if (!dglen) {
2349 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2350
2351 if (wr && wr < cbuf->rd) {
2352 cb_rd_reset(cbuf);
2353 continue;
2354 }
2355 break;
2356 }
2357
2358 pos += sizeof dglen;
2359 first_pkt = read_ptr(pos);
2360 pos += sizeof first_pkt;
2361 tmpbuf.area = (char *)pos;
2362 tmpbuf.size = tmpbuf.data = dglen;
2363
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002364 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002365 for (pkt = first_pkt; pkt; pkt = pkt->next)
2366 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002367 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002368 &tmpbuf, tmpbuf.data, 0) <= 0) {
2369 for (pkt = first_pkt; pkt; pkt = pkt->next)
2370 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002371 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002372 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002373
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002374 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002375 qc->tx.bytes += tmpbuf.data;
2376 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002377
2378 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2379 pkt->time_sent = time_sent;
2380 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2381 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002382 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002383 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002384 qc->path->in_flight += pkt->in_flight_len;
2385 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2386 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002387 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002388 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2389 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002390 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002391 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392 }
2393 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002394
2395 return 1;
2396}
2397
2398/* Build all the frames which must be sent just after the handshake have succeeded.
2399 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2400 * a HANDSHAKE_DONE frame.
2401 * Return 1 if succeeded, 0 if not.
2402 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002403static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404{
2405 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002406 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002407 struct quic_frame *frm;
2408
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002409 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002410 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002411 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002413 if (!frm)
2414 return 0;
2415
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002416 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002417 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002418 }
2419
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002420 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002421 struct quic_connection_id *cid;
2422
2423 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002424 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002425 if (!frm || !cid)
2426 goto err;
2427
2428 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002429 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002430 }
2431
2432 return 1;
2433
2434 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002435 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002436 return 0;
2437}
2438
2439/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002440void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002441{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002442 struct eb64_node *n;
2443 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002444
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002445 n = eb64_first(&arngs->root);
2446 while (n) {
2447 struct eb64_node *next;
2448
2449 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2450 next = eb64_next(n);
2451 eb64_delete(n);
2452 free(ar);
2453 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002454 }
2455}
2456
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002457/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2458 * descending order.
2459 */
2460static inline size_t sack_gap(struct quic_arng_node *p,
2461 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002462{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002463 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002464}
2465
2466
2467/* Remove the last elements of <ack_ranges> list of ack range updating its
2468 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002469 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002470 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002471static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002473 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002474
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002475 last = eb64_last(&arngs->root);
2476 while (last && arngs->enc_sz > limit) {
2477 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002478
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002479 prev = eb64_prev(last);
2480 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002481 return 0;
2482
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002483 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2484 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2485 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2486 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2487 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2488 --arngs->sz;
2489 eb64_delete(last);
2490 pool_free(pool_head_quic_arng, last);
2491 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 }
2493
2494 return 1;
2495}
2496
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002497/* Set the encoded size of <arngs> QUIC ack ranges. */
2498static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2499{
2500 struct eb64_node *node, *next;
2501 struct quic_arng_node *ar, *ar_next;
2502
2503 node = eb64_last(&arngs->root);
2504 if (!node)
2505 return;
2506
2507 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2508 arngs->enc_sz = quic_int_getsize(ar->last) +
2509 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2510
2511 while ((next = eb64_prev(node))) {
2512 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2513 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2514 quic_int_getsize(ar_next->last - ar_next->first.key);
2515 node = next;
2516 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2517 }
2518}
2519
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002520/* Insert <ar> ack range into <argns> tree of ack ranges.
2521 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002522 */
2523static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002524struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2525 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002526{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002527 struct quic_arng_node *new_ar;
2528
2529 new_ar = pool_alloc(pool_head_quic_arng);
2530 if (new_ar) {
2531 new_ar->first.key = ar->first;
2532 new_ar->last = ar->last;
2533 eb64_insert(&arngs->root, &new_ar->first);
2534 arngs->sz++;
2535 }
2536
2537 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002538}
2539
2540/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002541 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002542 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002543 *
2544 * Descending order
2545 * ------------->
2546 * range1 range2
2547 * ..........|--------|..............|--------|
2548 * ^ ^ ^ ^
2549 * | | | |
2550 * last1 first1 last2 first2
2551 * ..........+--------+--------------+--------+......
2552 * diff1 gap12 diff2
2553 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002554 * To encode the previous list of ranges we must encode integers as follows in
2555 * descending order:
2556 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002557 * with diff1 = last1 - first1
2558 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002559 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002560 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002561 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002562int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2563 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002564{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002565 struct eb64_node *le;
2566 struct quic_arng_node *new_node;
2567 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002568
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002569 new = NULL;
2570 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002571 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002572 if (!new_node)
2573 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002574
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002575 goto out;
2576 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002577
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002578 le = eb64_lookup_le(&arngs->root, ar->first);
2579 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002580 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002581 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002582 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002583 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002584 else {
2585 struct quic_arng_node *le_ar =
2586 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002588 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002589 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002590 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002591
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002592 if (le_ar->last + 1 >= ar->first) {
2593 le_ar->last = ar->last;
2594 new = le;
2595 new_node = le_ar;
2596 }
2597 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002598 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002599 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002600 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002601
2602 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002603 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002604 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002605
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002606 /* Verify that the new inserted node does not overlap the nodes
2607 * which follow it.
2608 */
2609 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002610 struct eb64_node *next;
2611 struct quic_arng_node *next_node;
2612
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002613 while ((next = eb64_next(new))) {
2614 next_node =
2615 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002616 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002617 break;
2618
2619 if (next_node->last > new_node->last)
2620 new_node->last = next_node->last;
2621 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002622 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002623 /* Decrement the size of these ranges. */
2624 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002625 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002626 }
2627
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002628 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002629 quic_arngs_set_enc_sz(arngs);
2630
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 return 1;
2632}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002633/* Remove the header protection of packets at <el> encryption level.
2634 * Always succeeds.
2635 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002636static inline void qc_rm_hp_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002637{
2638 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002639 struct quic_rx_packet *pqpkt;
2640 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002641 struct quic_enc_level *app_qel;
2642
2643 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2644 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2645 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002646 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002647 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002648 TRACE_PROTO("hp not removed (handshake not completed)",
2649 QUIC_EV_CONN_ELRMHP, ctx->conn);
2650 goto out;
2651 }
2652 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002653 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002654 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2655 pqpkt->data + pqpkt->pn_offset,
2656 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2657 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2658 /* XXX TO DO XXX */
2659 }
2660 else {
2661 /* The AAD includes the packet number field */
2662 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2663 /* Store the packet into the tree of packets to decrypt. */
2664 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002665 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002666 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2667 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002668 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002669 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2670 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002671 MT_LIST_DELETE_SAFE(pkttmp1);
2672 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 }
2674
2675 out:
2676 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2677}
2678
2679/* Process all the CRYPTO frame at <el> encryption level.
2680 * Return 1 if succeeded, 0 if not.
2681 */
2682static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002683 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002684{
2685 struct eb64_node *node;
2686
2687 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
2688 node = eb64_first(&el->rx.crypto.frms);
2689 while (node) {
2690 struct quic_rx_crypto_frm *cf;
2691
2692 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2693 if (cf->offset_node.key != el->rx.crypto.offset)
2694 break;
2695
2696 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2697 goto err;
2698
2699 node = eb64_next(node);
2700 quic_rx_packet_refdec(cf->pkt);
2701 eb64_delete(&cf->offset_node);
2702 pool_free(pool_head_quic_rx_crypto_frm, cf);
2703 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002704 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2705 return 1;
2706
2707 err:
2708 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2709 return 0;
2710}
2711
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002712/* Process all the packets at <el> and <next_el> encryption level.
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05002713 * 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 +02002714 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002715 * Return 1 if succeeded, 0 if not.
2716 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002717int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2718 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002719{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002720 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002721 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002722 struct quic_conn *qc = ctx->conn->qc;
2723 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002724
2725 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002726 qel = cur_el;
2727 next_tel:
2728 if (!qel)
2729 goto out;
2730
2731 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2732 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002733 while (node) {
2734 struct quic_rx_packet *pkt;
2735
2736 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002737 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2738 ctx->conn, pkt, NULL, ctx->ssl);
2739 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002740 /* Drop the packet */
2741 TRACE_PROTO("packet decryption failed -> dropped",
2742 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2743 }
2744 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002745 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002746 /* Drop the packet */
2747 TRACE_PROTO("packet parsing failed -> dropped",
2748 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2749 }
2750 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002751 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2752
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002753 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002754 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2755 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002756 if (pkt->pn > largest_pn)
2757 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002759 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 TRACE_DEVEL("Could not update ack range list",
2761 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002762 }
2763 }
2764 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002765 eb64_delete(&pkt->pn_node);
2766 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002768 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002770 /* Update the largest packet number. */
2771 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002772 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2773 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002774 goto err;
2775
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002776 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002777 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002778 qel = next_el;
2779 goto next_tel;
2780 }
2781
2782 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2784 return 1;
2785
2786 err:
2787 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2788 return 0;
2789}
2790
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002791/* QUIC connection packet handler task. */
2792struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002793{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002794 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002795 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002796 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002797 enum quic_tls_enc_level tel, next_tel;
2798 struct quic_enc_level *qel, *next_qel;
2799 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002800 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002801 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002802
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002803 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002804 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002805 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002806 st = HA_ATOMIC_LOAD(&qc->state);
2807 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002808 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002809 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002810 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002811 goto err;
2812
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002813 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002814 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002815
2816 next_level:
2817 tls_ctx = &qel->tls_ctx;
2818
2819 /* If the header protection key for this level has been derived,
2820 * remove the packet header protections.
2821 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002822 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2824 qc_rm_hp_pkts(qel, ctx);
2825
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002826 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002827 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2828 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2829 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002830 goto err;
2831
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002832 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002833 if (st >= QUIC_HS_ST_COMPLETE &&
2834 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002835 /* Discard the Handshake keys. */
2836 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002837 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002838 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2839 qc_set_timer(ctx);
2840 if (!quic_build_post_handshake_frames(qc))
2841 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002842 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002843 }
2844
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002845 if (!qr)
2846 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2847 ret = qc_prep_hdshk_pkts(qr, ctx);
2848 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002849 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002850 else if (ret == 0)
2851 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002853 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002854 goto err;
2855
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002856 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002857 /* Check if there is something to do for the next level.
2858 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002859 if (next_qel && next_qel != qel &&
2860 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002861 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002862 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002863 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864 goto next_level;
2865 }
2866
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002867 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002868 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002869 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870
2871 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002872 if (qr)
2873 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002874 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002875 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876}
2877
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002878/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002879static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2880{
2881 int i;
2882
2883 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2884 if (qel->tx.crypto.bufs[i]) {
2885 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2886 qel->tx.crypto.bufs[i] = NULL;
2887 }
2888 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002889 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890}
2891
2892/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002893 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002894 * Returns 1 if succeeded, 0 if not.
2895 */
2896static int quic_conn_enc_level_init(struct quic_conn *qc,
2897 enum quic_tls_enc_level level)
2898{
2899 struct quic_enc_level *qel;
2900
2901 qel = &qc->els[level];
2902 qel->level = quic_to_ssl_enc_level(level);
2903 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2904 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2905 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2906 qel->tls_ctx.rx.flags = 0;
2907 qel->tls_ctx.tx.flags = 0;
2908
2909 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002910 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002911 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002912 qel->rx.crypto.offset = 0;
2913 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002914
2915 /* Allocate only one buffer. */
2916 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2917 if (!qel->tx.crypto.bufs)
2918 goto err;
2919
2920 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2921 if (!qel->tx.crypto.bufs[0])
2922 goto err;
2923
2924 qel->tx.crypto.bufs[0]->sz = 0;
2925 qel->tx.crypto.nb_buf = 1;
2926
2927 qel->tx.crypto.sz = 0;
2928 qel->tx.crypto.offset = 0;
2929
2930 return 1;
2931
2932 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002933 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002934 return 0;
2935}
2936
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002937/* Release all the memory allocated for <conn> QUIC connection. */
2938static void quic_conn_free(struct quic_conn *conn)
2939{
2940 int i;
2941
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002942 if (!conn)
2943 return;
2944
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002945 free_quic_conn_cids(conn);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02002946
2947 /* remove the connection from receiver cids trees */
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01002948 HA_RWLOCK_WRLOCK(QUIC_LOCK, &conn->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02002949 ebmb_delete(&conn->odcid_node);
2950 ebmb_delete(&conn->scid_node);
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01002951 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &conn->li->rx.cids_lock);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02002952
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002953 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2954 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002955 if (conn->timer_task)
2956 task_destroy(conn->timer_task);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01002957 pool_free(pool_head_quic_conn_rxbuf, conn->rx.buf.area);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002958 pool_free(pool_head_quic_conn, conn);
2959}
2960
Amaury Denoyelle414cac52021-09-22 11:14:37 +02002961void quic_close(struct connection *conn, void *xprt_ctx)
2962{
2963 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
2964 struct quic_conn *qc = conn_ctx->conn->qc;
2965 quic_conn_free(qc);
2966}
2967
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002968/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002969static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002970{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002971 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002972 struct quic_conn *qc;
2973 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002974 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002975
2976 conn_ctx = task->context;
2977 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002978 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2979 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 task->expire = TICK_ETERNITY;
2981 pktns = quic_loss_pktns(qc);
2982 if (tick_isset(pktns->tx.loss_time)) {
2983 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2984
2985 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2986 if (!LIST_ISEMPTY(&lost_pkts))
2987 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2988 qc_set_timer(conn_ctx);
2989 goto out;
2990 }
2991
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002992 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002994 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995 pktns->tx.pto_probe = 1;
2996 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002997 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2999 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
3000
3001 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3002 hel->pktns->tx.pto_probe = 1;
3003 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
3004 iel->pktns->tx.pto_probe = 1;
3005 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003006 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3008 qc->path->loss.pto_count++;
3009
3010 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003011 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012
3013 return task;
3014}
3015
3016/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
3017 * connections used to identify the first Initial packets of client connecting
3018 * to listeners. This parameter must be NULL for QUIC connections attached
3019 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
3020 * <scid> is the source connection ID with <scid_len> as length.
3021 * Returns 1 if succeeded, 0 if not.
3022 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003023static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
3024 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003025 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003026{
3027 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003028 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029 /* Initial CID. */
3030 struct quic_connection_id *icid;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003031 char *buf_area;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003032
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003033 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003034 qc = pool_zalloc(pool_head_quic_conn);
3035 if (!qc) {
3036 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3037 goto err;
3038 }
3039
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003040 buf_area = pool_alloc(pool_head_quic_conn_rxbuf);
3041 if (!buf_area) {
3042 TRACE_PROTO("Could not allocate a new RX buffer", QUIC_EV_CONN_INIT);
3043 goto err;
3044 }
3045
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 qc->cids = EB_ROOT;
3047 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003048 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003049 struct listener *l = owner;
3050
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003051 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003052 /* Copy the initial DCID. */
3053 qc->odcid.len = dcid_len;
3054 if (qc->odcid.len)
3055 memcpy(qc->odcid.data, dcid, dcid_len);
3056
3057 /* Copy the SCID as our DCID for this connection. */
3058 if (scid_len)
3059 memcpy(qc->dcid.data, scid, scid_len);
3060 qc->dcid.len = scid_len;
Frédéric Lécaillec1029f62021-10-20 11:09:58 +02003061 qc->tx.qring_list = &l->rx.tx_qring_list;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003062 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003063 }
3064 /* QUIC Client (outgoing connection to servers) */
3065 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003066 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003067 if (dcid_len)
3068 memcpy(qc->dcid.data, dcid, dcid_len);
3069 qc->dcid.len = dcid_len;
3070 }
3071
3072 /* Initialize the output buffer */
3073 qc->obuf.pos = qc->obuf.data;
3074
3075 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003076 if (!icid) {
3077 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
3078 goto err;
3079 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080
3081 /* Select our SCID which is the first CID with 0 as sequence number. */
3082 qc->scid = icid->cid;
3083
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003084 /* Packet number spaces initialization. */
3085 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3086 quic_pktns_init(&qc->pktns[i]);
3087 /* QUIC encryption level context initialization. */
3088 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003089 if (!quic_conn_enc_level_init(qc, i)) {
3090 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003091 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003092 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003093 /* Initialize the packet number space. */
3094 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3095 }
3096
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003097 qc->version = version;
Frédéric Lécaillea956d152021-11-10 09:24:22 +01003098 qc->tps_tls_ext = qc->version & 0xff000000 ?
3099 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS_DRAFT:
3100 TLS_EXTENSION_QUIC_TRANSPORT_PARAMETERS;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003101 /* TX part. */
3102 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003103 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3104 qc->tx.wbuf = qc->tx.rbuf = 0;
3105 qc->tx.bytes = 0;
3106 qc->tx.nb_pto_dgrams = 0;
3107 /* RX part. */
3108 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003109 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003110 qc->rx.buf = b_make(buf_area, QUIC_CONN_RX_BUFSZ, 0, 0);
3111 HA_RWLOCK_INIT(&qc->rx.buf_rwlock);
3112 LIST_INIT(&qc->rx.pkt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003113
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114 /* XXX TO DO: Only one path at this time. */
3115 qc->path = &qc->paths[0];
3116 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3117
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003118 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003119
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003120 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003121
3122 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003123 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003124 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003125 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003126}
3127
3128/* Initialize the timer task of <qc> QUIC connection.
3129 * Returns 1 if succeeded, 0 if not.
3130 */
3131static int quic_conn_init_timer(struct quic_conn *qc)
3132{
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003133 qc->timer_task = task_new_anywhere();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003134 if (!qc->timer_task)
3135 return 0;
3136
3137 qc->timer = TICK_ETERNITY;
3138 qc->timer_task->process = process_timer;
3139 qc->timer_task->context = qc->conn->xprt_ctx;
3140
3141 return 1;
3142}
3143
3144/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3145 * past one byte of this buffer.
3146 */
3147static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3148 struct quic_rx_packet *pkt)
3149{
3150 unsigned char dcid_len, scid_len;
3151
3152 /* Version */
3153 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3154 return 0;
3155
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003156 /* Destination Connection ID Length */
3157 dcid_len = *(*buf)++;
3158 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3159 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3160 /* XXX MUST BE DROPPED */
3161 return 0;
3162
3163 if (dcid_len) {
3164 /* Check that the length of this received DCID matches the CID lengths
3165 * of our implementation for non Initials packets only.
3166 */
3167 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3168 return 0;
3169
3170 memcpy(pkt->dcid.data, *buf, dcid_len);
3171 }
3172
3173 pkt->dcid.len = dcid_len;
3174 *buf += dcid_len;
3175
3176 /* Source Connection ID Length */
3177 scid_len = *(*buf)++;
3178 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3179 /* XXX MUST BE DROPPED */
3180 return 0;
3181
3182 if (scid_len)
3183 memcpy(pkt->scid.data, *buf, scid_len);
3184 pkt->scid.len = scid_len;
3185 *buf += scid_len;
3186
3187 return 1;
3188}
3189
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003190/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3191 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3192 * encryption level matching with the packet type. <*qel> may be null if not found.
3193 * Note that <ctx> may be null (for Initial packets).
3194 */
3195static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003196 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003197 struct quic_enc_level **qel)
3198{
3199 enum quic_tls_enc_level tel;
3200
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05003201 /* Special case without connection context (first Initial packets) */
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003202 if (!ctx) {
3203 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3204 return 1;
3205 }
3206
3207 tel = quic_packet_type_enc_level(pkt->type);
3208 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3209 *qel = NULL;
3210 return 0;
3211 }
3212
3213 *qel = &qc->els[tel];
3214 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3215 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3216 return 0;
3217 }
3218
3219 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003220 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3221 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003222 return 1;
3223
3224 return 0;
3225}
3226
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003227/* Insert <pkt> RX packet in its <qel> RX packets tree */
3228static void qc_pkt_insert(struct quic_rx_packet *pkt, struct quic_enc_level *qel)
3229{
3230 pkt->pn_node.key = pkt->pn;
3231 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3232 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3233 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
3234 quic_rx_packet_refinc(pkt);
3235}
3236
3237/* Try to remove the header protection of <pkt> QUIC packet attached to <qc>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003238 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3239 * byte past the end of the buffer containing this packet and <beg> the address of
3240 * the packet first byte.
3241 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3242 * Returns 1 if succeeded, 0 if not.
3243 */
3244static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3245 unsigned char **buf, unsigned char *beg,
3246 const unsigned char *end,
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003247 struct quic_conn *qc, struct quic_enc_level **el,
3248 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003249{
3250 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003251 struct quic_enc_level *qel;
3252 /* Only for traces. */
3253 struct quic_rx_packet *qpkt_trace;
3254
3255 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003256 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003257 /* The packet number is here. This is also the start minus
3258 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3259 * protection.
3260 */
3261 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003262 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003263 /* Note that the following function enables us to unprotect the packet
3264 * number and its length subsequently used to decrypt the entire
3265 * packets.
3266 */
3267 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3268 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003269 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003270 goto err;
3271 }
3272
3273 /* The AAD includes the packet number field found at <pn>. */
3274 pkt->aad_len = pn - beg + pkt->pnl;
3275 qpkt_trace = pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003276 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003277 else if (qel) {
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003278 if (qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3279 /* If the packet number space has been discarded, this packet
3280 * will be not parsed.
3281 */
3282 TRACE_PROTO("Discarded pktns", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
3283 goto out;
3284 }
3285
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003286 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003287 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003288 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3289 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003290 }
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003291 else {
3292 TRACE_PROTO("Unknown packet type", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
3293 goto err;
3294 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003295
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003296 *el = qel;
3297 /* No reference counter incrementation here!!! */
3298 LIST_APPEND(&qc->rx.pkt_list, &pkt->qc_rx_pkt_list);
3299 memcpy(b_tail(&qc->rx.buf), beg, pkt->len);
3300 pkt->data = (unsigned char *)b_tail(&qc->rx.buf);
3301 b_add(&qc->rx.buf, pkt->len);
3302 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003303 /* Updtate the offset of <*buf> for the next QUIC packet. */
3304 *buf = beg + pkt->len;
3305
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003306 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003307 return 1;
3308
3309 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003310 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003311 return 0;
3312}
3313
3314/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3315 * Also set <*long_header> to 1 if this form is long, 0 if not.
3316 */
3317static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3318 unsigned char byte0, int *long_header)
3319{
3320 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3321 pkt->type =
3322 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3323 *long_header = 1;
3324 }
3325 else {
3326 pkt->type = QUIC_PACKET_TYPE_SHORT;
3327 *long_header = 0;
3328 }
3329}
3330
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003331/*
3332 * Check if the QUIC version in packet <pkt> is supported. Returns a boolean.
3333 */
3334static inline int qc_pkt_is_supported_version(struct quic_rx_packet *pkt)
3335{
3336 int j = 0, version;
3337
3338 do {
3339 version = quic_supported_version[j];
3340 if (version == pkt->version)
3341 return 1;
3342
3343 version = quic_supported_version[++j];
3344 } while(version);
3345
3346 return 0;
3347}
3348
Frédéric Lécaillec5c69a02021-10-20 17:24:42 +02003349__attribute__((unused))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3351 struct quic_rx_packet *pkt,
3352 struct quic_dgram_ctx *dgram_ctx,
3353 struct sockaddr_storage *saddr)
3354{
3355 unsigned char *beg;
3356 uint64_t len;
3357 struct quic_conn *qc;
3358 struct eb_root *cids;
3359 struct ebmb_node *node;
3360 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003361 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003362 int long_header;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003363 size_t b_cspace;
3364 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365
3366 qc = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003367 qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003368 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3369 if (end <= *buf)
3370 goto err;
3371
3372 /* Fixed bit */
3373 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3374 /* XXX TO BE DISCARDED */
3375 goto err;
3376
3377 srv_conn = dgram_ctx->owner;
3378 beg = *buf;
3379 /* Header form */
3380 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3381 if (long_header) {
3382 size_t cid_lookup_len;
3383
3384 if (!quic_packet_read_long_header(buf, end, pkt))
3385 goto err;
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003386
3387 /* unsupported QUIC version */
3388 if (!qc_pkt_is_supported_version(pkt)) {
3389 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3390 goto err;
3391 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003392
3393 /* For Initial packets, and for servers (QUIC clients connections),
3394 * there is no Initial connection IDs storage.
3395 */
3396 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3397 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3398 cid_lookup_len = pkt->dcid.len;
3399 }
3400 else {
3401 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3402 cid_lookup_len = QUIC_CID_LEN;
3403 }
3404
3405 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3406 if (!node)
3407 goto err;
3408
3409 qc = ebmb_entry(node, struct quic_conn, scid_node);
3410
3411 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3412 qc->dcid.len = pkt->scid.len;
3413 if (pkt->scid.len)
3414 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3415 }
3416
3417 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3418 uint64_t token_len;
3419
3420 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3421 goto err;
3422
3423 /* XXX TO DO XXX 0 value means "the token is not present".
3424 * A server which sends an Initial packet must not set the token.
3425 * So, a client which receives an Initial packet with a token
3426 * MUST discard the packet or generate a connection error with
3427 * PROTOCOL_VIOLATION as type.
3428 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3429 */
3430 pkt->token_len = token_len;
3431 }
3432 }
3433 else {
3434 /* XXX TO DO: Short header XXX */
3435 if (end - *buf < QUIC_CID_LEN)
3436 goto err;
3437
3438 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3439 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3440 if (!node)
3441 goto err;
3442
3443 qc = ebmb_entry(node, struct quic_conn, scid_node);
3444 *buf += QUIC_CID_LEN;
3445 }
3446 /* Store the DCID used for this packet to check the packet which
3447 * come in this UDP datagram match with it.
3448 */
3449 if (!dgram_ctx->dcid_node)
3450 dgram_ctx->dcid_node = node;
3451 /* Only packets packets with long headers and not RETRY or VERSION as type
3452 * have a length field.
3453 */
3454 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3455 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3456 goto err;
3457
3458 pkt->len = len;
3459 }
3460 else if (!long_header) {
3461 /* A short packet is the last one of an UDP datagram. */
3462 pkt->len = end - *buf;
3463 }
3464
3465 conn_ctx = qc->conn->xprt_ctx;
3466
3467 /* Increase the total length of this packet by the header length. */
3468 pkt->len += *buf - beg;
3469 /* Do not check the DCID node before the length. */
3470 if (dgram_ctx->dcid_node != node) {
3471 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3472 goto err;
3473 }
3474
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003475 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3476 b_cspace = b_contig_space(&qc->rx.buf);
3477 if (b_cspace < pkt->len) {
3478 /* Let us consume the remaining contiguous space. */
3479 b_add(&qc->rx.buf, b_cspace);
3480 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3481 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3482 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3483 goto err;
3484 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003485 }
3486
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003487 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
3488 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3489 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490 goto err;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003491 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003492
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003493 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3494 if (pkt->aad_len)
3495 qc_pkt_insert(pkt, qel);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003496 /* Wake the tasklet of the QUIC connection packet handler. */
3497 if (conn_ctx)
3498 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3499
3500 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3501
3502 return pkt->len;
3503
3504 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003505 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003506 return -1;
3507}
3508
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003509/*
3510 * Send a Version Negotiation packet on response to <pkt> on socket <fd> to
3511 * address <addr>.
3512 * Implementation of RFC9000 6. Version Negotiation
3513 *
3514 * TODO implement a rate-limiting sending of Version Negotiation packets
3515 *
3516 * Returns 0 on success else non-zero
3517 */
3518static int qc_send_version_negotiation(int fd, struct sockaddr_storage *addr,
3519 struct quic_rx_packet *pkt)
3520{
3521 char buf[256];
3522 int i = 0, j, version;
3523 const socklen_t addrlen = get_addr_len(addr);
3524
3525 /*
3526 * header form
3527 * long header, fixed bit to 0 for Version Negotiation
3528 */
3529 buf[i++] = '\x80';
3530
3531 /* null version for Version Negotiation */
3532 buf[i++] = '\x00';
3533 buf[i++] = '\x00';
3534 buf[i++] = '\x00';
3535 buf[i++] = '\x00';
3536
3537 /* source connection id */
3538 buf[i++] = pkt->scid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003539 memcpy(&buf[i], pkt->scid.data, pkt->scid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003540 i += pkt->scid.len;
3541
3542 /* destination connection id */
3543 buf[i++] = pkt->dcid.len;
Amaury Denoyelle10eed8e2021-11-18 13:48:57 +01003544 memcpy(&buf[i], pkt->dcid.data, pkt->dcid.len);
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003545 i += pkt->dcid.len;
3546
3547 /* supported version */
3548 j = 0;
3549 do {
3550 version = htonl(quic_supported_version[j]);
3551 memcpy(&buf[i], &version, sizeof(version));
3552 i += sizeof(version);
3553
3554 version = quic_supported_version[++j];
3555 } while (version);
3556
3557 if (sendto(fd, buf, i, 0, (struct sockaddr *)addr, addrlen) < 0)
3558 return 1;
3559
3560 return 0;
3561}
3562
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003563static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3564 struct quic_rx_packet *pkt,
3565 struct quic_dgram_ctx *dgram_ctx,
3566 struct sockaddr_storage *saddr)
3567{
3568 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003569 struct quic_conn *qc;
3570 struct eb_root *cids;
3571 struct ebmb_node *node;
3572 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003573 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003574 int long_header = 0;
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003575 size_t b_cspace;
3576 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003577
3578 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003579 conn_ctx = NULL;
Frédéric Lécaillec4becf52021-11-08 11:23:17 +01003580 qel = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003581 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003582 if (end <= *buf)
3583 goto err;
3584
3585 /* Fixed bit */
3586 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3587 /* XXX TO BE DISCARDED */
3588 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3589 goto err;
3590 }
3591
3592 l = dgram_ctx->owner;
3593 beg = *buf;
3594 /* Header form */
3595 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3596 if (long_header) {
3597 unsigned char dcid_len;
3598
3599 if (!quic_packet_read_long_header(buf, end, pkt)) {
3600 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3601 goto err;
3602 }
3603
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003604 /* RFC9000 6. Version Negotiation */
3605 if (!qc_pkt_is_supported_version(pkt)) {
3606 /* do not send Version Negotiation in response to a
3607 * Version Negotiation packet.
3608 */
3609 if (!pkt->version) {
3610 TRACE_PROTO("Null QUIC version, packet dropped", QUIC_EV_CONN_LPKT);
3611 goto err;
3612 }
3613
3614 /* unsupported version, send Negotiation packet */
3615 if (qc_send_version_negotiation(l->rx.fd, saddr, pkt)) {
3616 TRACE_PROTO("Error on Version Negotiation sending", QUIC_EV_CONN_LPKT);
3617 goto err;
3618 }
3619
3620 TRACE_PROTO("Unsupported QUIC version, send Version Negotiation packet", QUIC_EV_CONN_LPKT);
3621 goto out;
3622 }
3623
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003624 dcid_len = pkt->dcid.len;
3625 /* For Initial packets, and for servers (QUIC clients connections),
3626 * there is no Initial connection IDs storage.
3627 */
3628 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003629 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003630 /* DCIDs of first packets coming from clients may have the same values.
3631 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3632 */
3633 quic_cid_saddr_cat(&pkt->dcid, saddr);
3634 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003635
3636 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3637 end - *buf < token_len) {
3638 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3639 goto err;
3640 }
3641
3642 /* XXX TO DO XXX 0 value means "the token is not present".
3643 * A server which sends an Initial packet must not set the token.
3644 * So, a client which receives an Initial packet with a token
3645 * MUST discard the packet or generate a connection error with
3646 * PROTOCOL_VIOLATION as type.
3647 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3648 */
3649 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003650 }
3651 else {
3652 if (pkt->dcid.len != QUIC_CID_LEN) {
3653 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3654 goto err;
3655 }
3656
3657 cids = &l->rx.cids;
3658 }
3659
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003660 /* Only packets packets with long headers and not RETRY or VERSION as type
3661 * have a length field.
3662 */
3663 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3664 uint64_t len;
3665
3666 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3667 end - *buf < len) {
3668 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3669 goto err;
3670 }
3671
3672 pkt->len = len;
3673 }
3674
3675
3676 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3678 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3679 cids == &l->rx.odcids) {
3680 /* Switch to the definitive tree ->cids containing the final CIDs. */
3681 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3682 if (node) {
3683 /* If found, signal this with NULL as special value for <cids>. */
3684 pkt->dcid.len = dcid_len;
3685 cids = NULL;
3686 }
3687 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003688 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689
3690 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003691 int ipv4;
3692 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003693 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003694 const unsigned char *salt = initial_salt_v1;
3695 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003696
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003697 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3698 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3699 goto err;
3700 }
3701
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003702 pkt->saddr = *saddr;
3703 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3704 * of <saddr>.
3705 */
3706 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003707 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003708 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003709 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003710 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003711 goto err;
3712
3713 odcid = &qc->rx.params.original_destination_connection_id;
3714 /* Copy the transport parameters. */
3715 qc->rx.params = l->bind_conf->quic_params;
3716 /* Copy original_destination_connection_id transport parameter. */
3717 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3718 odcid->len = pkt->odcid_len;
3719 /* Copy the initial source connection ID. */
3720 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3721 qc->enc_params_len =
3722 quic_transport_params_encode(qc->enc_params,
3723 qc->enc_params + sizeof qc->enc_params,
3724 &qc->rx.params, 1);
3725 if (!qc->enc_params_len)
3726 goto err;
3727
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003728 /* NOTE: the socket address has been concatenated to the destination ID
3729 * chosen by the client for Initial packets.
3730 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003731 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3732 salt = initial_salt_draft_29;
3733 salt_len = sizeof initial_salt_draft_29;
3734 }
3735 if (!qc_new_isecs(qc, salt, salt_len,
3736 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003737 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3738 goto err;
3739 }
3740
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01003741 HA_RWLOCK_WRLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003742 /* Insert the DCID the QUIC client has chosen (only for listeners) */
Frédéric Lécaille8370c932021-11-08 17:01:46 +01003743 n = ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3744 if (n == &qc->odcid_node) {
3745 /* Insert our SCID, the connection ID for the QUIC client. */
3746 ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3747 }
Frédéric Lécailleb0006ee2021-11-03 19:01:31 +01003748 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &l->rx.cids_lock);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01003749
3750 pkt->qc = qc;
3751 if (n == &qc->odcid_node) {
3752 /* Enqueue this packet. */
3753 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3754 /* Try to accept a new connection. */
3755 listener_accept(l);
3756 }
3757 else {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003758 quic_conn_free(qc);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01003759 qc = ebmb_entry(n, struct quic_conn, odcid_node);
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003760 }
Frédéric Lécaille8370c932021-11-08 17:01:46 +01003761
3762 /* This is the DCID node sent in this packet by the client. */
3763 node = &qc->odcid_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003764 }
3765 else {
3766 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3767 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3768 else
3769 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécaille8370c932021-11-08 17:01:46 +01003770 pkt->qc = qc;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003771 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003772 }
3773 }
3774 else {
3775 if (end - *buf < QUIC_CID_LEN) {
3776 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3777 goto err;
3778 }
3779
3780 cids = &l->rx.cids;
3781 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3782 if (!node) {
3783 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3784 goto err;
3785 }
3786
3787 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003788 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789 *buf += QUIC_CID_LEN;
Frédéric Lécaille8370c932021-11-08 17:01:46 +01003790 pkt->qc = qc;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003791 /* A short packet is the last one of an UDP datagram. */
3792 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003793 }
3794
3795 /* Store the DCID used for this packet to check the packet which
3796 * come in this UDP datagram match with it.
3797 */
3798 if (!dgram_ctx->dcid_node) {
3799 dgram_ctx->dcid_node = node;
3800 dgram_ctx->qc = qc;
3801 }
3802
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003803 /* Increase the total length of this packet by the header length. */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003804 pkt->raw_len = pkt->len += *buf - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003805 /* Do not check the DCID node before the length. */
3806 if (dgram_ctx->dcid_node != node) {
3807 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3808 goto err;
3809 }
3810
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003811 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3812 b_cspace = b_contig_space(&qc->rx.buf);
3813 if (b_cspace < pkt->len) {
3814 /* Let us consume the remaining contiguous space. */
3815 b_add(&qc->rx.buf, b_cspace);
3816 if (b_contig_space(&qc->rx.buf) < pkt->len) {
3817 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
3818 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3819 goto err;
3820 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003821 }
3822
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003823 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, &qel, conn_ctx)) {
3824 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003825 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3826 goto err;
3827 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003828
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003829 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qc->rx.buf_rwlock);
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003830 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01003831 if (pkt->aad_len)
3832 qc_pkt_insert(pkt, qel);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003833 /* Wake up the connection packet handler task from here only if all
3834 * the contexts have been initialized, especially the mux context
3835 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3836 * will start it if these contexts for the connection are not already
3837 * initialized.
3838 */
3839 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003840 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003841
Amaury Denoyellea22d8602021-11-10 15:17:56 +01003842 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003843 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3844
3845 return pkt->len;
3846
3847 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003848 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003849 qc ? qc->conn : NULL, pkt);
3850 return -1;
3851}
3852
3853/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3854 * in advance. This is the reponsability of the caller to check there is enough room in this
3855 * buffer to build a long header.
3856 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3857 */
3858static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3859 int type, size_t pn_len, struct quic_conn *conn)
3860{
3861 if (type > QUIC_PACKET_TYPE_RETRY)
3862 return 0;
3863
3864 /* #0 byte flags */
3865 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3866 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3867 /* Version */
3868 quic_write_uint32(buf, end, conn->version);
3869 *(*buf)++ = conn->dcid.len;
3870 /* Destination connection ID */
3871 if (conn->dcid.len) {
3872 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3873 *buf += conn->dcid.len;
3874 }
3875 /* Source connection ID */
3876 *(*buf)++ = conn->scid.len;
3877 if (conn->scid.len) {
3878 memcpy(*buf, conn->scid.data, conn->scid.len);
3879 *buf += conn->scid.len;
3880 }
3881
3882 return 1;
3883}
3884
3885/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3886 * in advance. This is the reponsability of the caller to check there is enough room in this
3887 * buffer to build a long header.
3888 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3889 */
3890static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3891 size_t pn_len, struct quic_conn *conn)
3892{
3893 /* #0 byte flags */
3894 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3895 /* Destination connection ID */
3896 if (conn->dcid.len) {
3897 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3898 *buf += conn->dcid.len;
3899 }
3900
3901 return 1;
3902}
3903
3904/* Apply QUIC header protection to the packet with <buf> as first byte address,
3905 * <pn> as address of the Packet number field, <pnlen> being this field length
3906 * with <aead> as AEAD cipher and <key> as secret key.
3907 * Returns 1 if succeeded or 0 if failed.
3908 */
3909static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3910 const EVP_CIPHER *aead, const unsigned char *key)
3911{
3912 int i, ret, outlen;
3913 EVP_CIPHER_CTX *ctx;
3914 /* We need an IV of at least 5 bytes: one byte for bytes #0
3915 * and at most 4 bytes for the packet number
3916 */
3917 unsigned char mask[5] = {0};
3918
3919 ret = 0;
3920 ctx = EVP_CIPHER_CTX_new();
3921 if (!ctx)
3922 return 0;
3923
3924 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3925 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3926 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3927 goto out;
3928
3929 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3930 for (i = 0; i < pnlen; i++)
3931 pn[i] ^= mask[i + 1];
3932
3933 ret = 1;
3934
3935 out:
3936 EVP_CIPHER_CTX_free(ctx);
3937
3938 return ret;
3939}
3940
3941/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3942 * ACK ranges if needed to a value below <limit> in bytes.
3943 * Return 1 if succeeded, 0 if not.
3944 */
3945static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3946{
3947 size_t room, ack_delay_sz;
3948
3949 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3950 /* A frame is made of 1 byte for the frame type. */
3951 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003952 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003953 return 0;
3954
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003955 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003956}
3957
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003958/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3959 * 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 +01003960 * and <*len> the packet Length field initialized with the number of bytes already present
3961 * 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 +02003962 * <headlen> is the number of bytes already present in this packet before building frames.
3963 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003964 * Update consequently <*len> to reflect the size of these frames built
3965 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003966 * Return 1 if succeeded, 0 if not.
3967 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003968static inline int qc_build_frms(struct quic_tx_packet *pkt,
3969 size_t room, size_t *len, size_t headlen,
3970 struct quic_enc_level *qel,
3971 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003972{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003973 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003974 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003975 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003976 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003977
Frédéric Lécailleea604992020-12-24 13:01:37 +01003978 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003979 if (*len > room || headlen > remain)
3980 return 0;
3981
Frédéric Lécailleea604992020-12-24 13:01:37 +01003982 /* If we are not probing we must take into an account the congestion
3983 * control window.
3984 */
3985 if (!conn->tx.nb_pto_dgrams)
3986 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003987 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003988 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003989 mt_list_for_each_entry_safe(cf, &qel->pktns->tx.frms, mt_list, tmp1, tmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003990 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003991 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003992
Frédéric Lécailleea604992020-12-24 13:01:37 +01003993 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003994 break;
3995
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003996 switch (cf->type) {
3997 case QUIC_FT_CRYPTO:
3998 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3999 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
4000 /* Compute the length of this CRYPTO frame header */
4001 hlen = 1 + quic_int_getsize(cf->crypto.offset);
4002 /* Compute the data length of this CRyPTO frame. */
4003 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
4004 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
4005 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
4006 if (!dlen)
4007 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004008
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004009 pkt->cdata_len += dlen;
4010 /* CRYPTO frame length. */
4011 flen = hlen + quic_int_getsize(dlen) + dlen;
4012 TRACE_PROTO(" CRYPTO frame length (flen)",
4013 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
4014 /* Add the CRYPTO data length and its encoded length to the packet
4015 * length and the length of this length.
4016 */
4017 *len += flen;
4018 room -= flen;
4019 if (dlen == cf->crypto.len) {
4020 /* <cf> CRYPTO data have been consumed. */
4021 MT_LIST_DELETE_SAFE(tmp1);
4022 LIST_APPEND(&pkt->frms, &cf->list);
4023 }
4024 else {
4025 struct quic_frame *new_cf;
4026
4027 new_cf = pool_alloc(pool_head_quic_frame);
4028 if (!new_cf) {
4029 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
4030 return 0;
4031 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004032
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004033 new_cf->type = QUIC_FT_CRYPTO;
4034 new_cf->crypto.len = dlen;
4035 new_cf->crypto.offset = cf->crypto.offset;
4036 new_cf->crypto.qel = qel;
4037 LIST_APPEND(&pkt->frms, &new_cf->list);
4038 /* Consume <dlen> bytes of the current frame. */
4039 cf->crypto.len -= dlen;
4040 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004041 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004042 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004043
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004044 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004045 /* Note that these frames are accepted in short packets only without
4046 * "Length" packet field. Here, <*len> is used only to compute the
4047 * sum of the lengths of the already built frames for this packet.
4048 */
4049 TRACE_PROTO(" New STREAM frame build (room, len)",
4050 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
4051 /* Compute the length of this STREAM frame "header" made a all the field
4052 * excepting the variable ones. Note that +1 is for the type of this frame.
4053 */
4054 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004055 ((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 +02004056 /* Compute the data length of this STREAM frame. */
4057 avail_room = room - hlen - *len;
4058 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004059 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004060
4061 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
4062 dlen = max_available_room(avail_room, &dlen_sz);
4063 if (dlen > cf->stream.len) {
4064 dlen = cf->stream.len;
4065 }
4066 dlen_sz = quic_int_getsize(dlen);
4067 flen = hlen + dlen_sz + dlen;
4068 }
4069 else {
4070 dlen = QUIC_MIN(avail_room, cf->stream.len);
4071 flen = hlen + dlen;
4072 }
4073 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
4074 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
4075 TRACE_PROTO(" STREAM frame length (flen)",
4076 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
4077 /* Add the STREAM data length and its encoded length to the packet
4078 * length and the length of this length.
4079 */
4080 *len += flen;
4081 room -= flen;
4082 if (dlen == cf->stream.len) {
4083 /* <cf> STREAM data have been consumed. */
4084 MT_LIST_DELETE_SAFE(tmp1);
4085 LIST_APPEND(&pkt->frms, &cf->list);
4086 }
4087 else {
4088 struct quic_frame *new_cf;
4089
4090 new_cf = pool_zalloc(pool_head_quic_frame);
4091 if (!new_cf) {
4092 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
4093 return 0;
4094 }
4095
4096 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004097 new_cf->stream.qcs = cf->stream.qcs;
4098 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004099 new_cf->stream.id = cf->stream.id;
4100 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
4101 new_cf->stream.offset = cf->stream.offset;
4102 new_cf->stream.len = dlen;
4103 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
4104 /* FIN bit reset */
4105 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
4106 new_cf->stream.data = cf->stream.data;
4107 LIST_APPEND(&pkt->frms, &new_cf->list);
4108 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
4109 /* Consume <dlen> bytes of the current frame. */
4110 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02004111 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02004112 cf->stream.data += dlen;
4113 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02004114 break;
4115
4116 default:
4117 flen = qc_frm_len(cf);
4118 BUG_ON(!flen);
4119 if (flen > room)
4120 continue;
4121
4122 *len += flen;
4123 room -= flen;
4124 MT_LIST_DELETE_SAFE(tmp1);
4125 LIST_APPEND(&pkt->frms, &cf->list);
4126 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004127 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01004128 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004129 }
4130
Frédéric Lécailleea604992020-12-24 13:01:37 +01004131 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004132}
4133
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004134/* This function evaluates if <pkt> packet may be built into a buffer with
4135 * <room> as available room. A valid packet should at least contain a valid
4136 * header and at least a frame.
4137 * To estimate the minimal space to build a packet, we consider the worst case:
4138 - there is not enough space to build ack-eliciting frames from
4139 qel->pktns->tx.frms. This is safe to consider this because when we build
4140 a packet we first build the ACK frames, then the ack-eliciting frames
4141 from qel->pktns->tx.frms only if there is enough space for these
4142 ack-eliciting frames, finally PING and PADDING frames if needed,
4143 - we have to ensure there is enough space to build an ACK frame if required,
4144 and a PING frame, even if we do not have to probe,
4145 - we also have to verify there is enough space to build a PADDING frame
4146 if needed, especially if there is no need to send an ACK frame.
4147 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
4148 * a valid packet).
4149 */
4150static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
4151 int ack, int nb_pto_dgrams,
4152 struct quic_enc_level *qel, struct quic_conn *conn)
4153{
4154 size_t minlen, token_fields_len;
4155 /* XXX FIXME XXX : ack delay not supported */
4156 uint64_t ack_delay = 0;
4157 size_t ack_frm_len = 0;
4158
4159 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
4160 conn->conn, NULL, NULL, &room);
4161 /* When we do not have to probe nor send acks either, we must take into
4162 * an account the data which have already been prepared and limit
4163 * the size of this packet. We will potentially build an ack-eliciting
4164 * packet.
4165 */
4166 if (!nb_pto_dgrams && !ack) {
4167 size_t path_room;
4168
4169 path_room = quic_path_prep_data(conn->path);
4170 if (room > path_room)
4171 room = path_room;
4172 }
4173
4174 if (ack)
4175 /* A frame is made of 1 byte for the frame type. */
4176 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
4177
4178 /* XXX FIXME XXX : token not supported */
4179 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
4180 /* Check there is enough room to build the header followed by a token,
4181 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
4182 * encryption tag is also taken into an account. Note that we have no
4183 * knowledge of the packet number for this packet. It must be atomically
4184 * incremented each time a packet is built. But before building a packet
4185 * we must estimate if it may be built if we do not want to consume a packet
4186 * number for nothing! Note that we add 1 byte more to
4187 * <minlen> to be able to build an ack-eliciting packet when probing without
4188 * ack-eliciting frames to send. In this case we need to add a 1-byte length
4189 * PING frame.
4190 */
4191 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
4192 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
4193 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
4194 + token_fields_len;
4195 else
4196 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
4197
4198 /* Consider any PADDING frame to add */
4199 if (objt_server(conn->conn->target) &&
4200 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
4201 minlen < QUIC_INITIAL_PACKET_MINLEN) {
4202 /* Pad too short client Initial packet */
4203 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
4204 }
4205 else if (!ack) {
4206 /* Consider we will have to add the longest short PADDING frame to
4207 * protect a 1-byte length packet number.
4208 */
4209 minlen += QUIC_PACKET_PN_MAXLEN - 1;
4210 }
4211
4212 if (room < minlen) {
4213 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
4214 conn->conn, NULL, NULL, &room);
4215 return 0;
4216 }
4217
4218 return 1;
4219}
4220
4221/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004222 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4223 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4224 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225 * 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 +02004226 * 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 +02004227 * having returned from this function.
4228 * 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 +01004229 * number field in this packet. <pn_len> will also have the packet number
4230 * length as value.
4231 *
Ilya Shipitsinbd6b4be2021-10-15 16:18:21 +05004232 * Always succeeds: this is the responsibility of the caller to ensure there is
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004233 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004234 */
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004235static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end, int padding,
4236 size_t dglen, struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004237 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4238 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004239{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004240 unsigned char *beg;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004241 size_t len, len_sz, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004242 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4243 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004244 size_t ack_frm_len, head_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004245 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004246 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004247
Frédéric Lécailleea604992020-12-24 13:01:37 +01004248 /* Length field value with CRYPTO frames if present. */
4249 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004250 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004251 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004252 * the congestion controller window. So, we do not limit the size of this
4253 * packet if we have an ACK frame to send because an ACK frame is not
4254 * ack-eliciting. This size will be limited if we have ack-eliciting
4255 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004256 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004257 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004258 size_t path_room;
4259
4260 path_room = quic_path_prep_data(conn->path);
4261 if (end - beg > path_room)
4262 end = beg + path_room;
4263 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004264
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004265 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004266 /* packet number length */
4267 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004268 /* Build the header */
4269 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004270 quic_build_packet_short_header(&pos, end, *pn_len, conn);
4271 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004272 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
4273 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4274 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004275 *pos++ = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004276 head_len = pos - beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004277 /* Ensure there is enough room for the TLS encryption tag */
4278 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004279 /* Build an ACK frame if required. */
4280 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004281 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004282 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004283 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004284 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4285 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4286 * that from here, we do not know if we will have to send a PING frame.
4287 * This will be decided after having computed the ack-eliciting frames
4288 * to be added to this packet.
4289 */
4290 ack_frm_len = quic_ack_frm_reduce_sz(&ack_frm, end - 1 - *pn_len - pos);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004291 if (!ack_frm_len) {
4292 ssize_t room = end - pos;
4293 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4294 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004295 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004296 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004297 }
4298
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004299 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004300 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004301 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004302 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004303
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004304 /* Initialize the length of the frames built below to <len>.
4305 * If any frame could be successfully built by qc_build_frms(),
4306 * we will have len_frms > len.
4307 */
4308 len_frms = len;
4309 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004310 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4311 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004312 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004313
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004314 /* Length (of the remaining data). Must not fail because, the buffer size
4315 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
4316 * for the encryption tag. It must be taken into an account for the length
4317 * of this packet.
4318 */
4319 if (len_frms)
4320 len = len_frms + QUIC_TLS_TAG_LEN;
4321 else
4322 len += QUIC_TLS_TAG_LEN;
4323
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004324 add_ping_frm = 0;
4325 padding_len = 0;
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004326 len_sz = quic_int_getsize(len);
4327 /* Add this packet size to <dglen> */
4328 dglen += head_len + len_sz + len;
4329 if (padding && dglen < QUIC_INITIAL_PACKET_MINLEN) {
4330 /* This is a maximum padding size */
4331 padding_len = QUIC_INITIAL_PACKET_MINLEN - dglen;
4332 /* The length field value is of this packet is <len> + <padding_len>
4333 * the size of which may be greater than the initial computed size
4334 * <len_sz>. So, let's deduce the difference betwen these to packet
4335 * sizes from <padding_len>.
4336 */
4337 padding_len -= quic_int_getsize(len + padding_len) - len_sz;
4338 len += padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004339 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004340 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004341 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004342 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004343 add_ping_frm = 1;
4344 len += 1;
4345 }
4346 /* If there is no frame at all to follow, add at least a PADDING frame. */
4347 if (!ack_frm_len)
4348 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4349 }
4350
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004351 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004352 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004353
4354 /* Packet number field address. */
4355 *buf_pn = pos;
4356
4357 /* Packet number encoding. */
4358 quic_packet_number_encode(&pos, end, pn, *pn_len);
4359
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004360 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4361 ssize_t room = end - pos;
4362 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4363 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004364 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004365 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004366
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004367 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004368 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004369 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004370
4371 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004372 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004373 ssize_t room = end - pos;
4374 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4375 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004376 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004377 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004378 }
4379 }
4380
4381 /* Build a PING frame if needed. */
4382 if (add_ping_frm) {
4383 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004384 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4385 ssize_t room = end - pos;
4386 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4387 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004388 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004389 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004390 }
4391
4392 /* Build a PADDING frame if needed. */
4393 if (padding_len) {
4394 frm.type = QUIC_FT_PADDING;
4395 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004396 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4397 ssize_t room = end - pos;
4398 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4399 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004400 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004401 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004402 }
4403
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004404 /* Always reset this variable as this function has no idea
4405 * if it was set. It is handle by the loss detection timer.
4406 */
4407 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004408 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004409}
4410
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004411static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004412{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004413 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004414 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004415 pkt->cdata_len = 0;
4416 pkt->in_flight_len = 0;
4417 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004418 pkt->next = NULL;
4419 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004420}
4421
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004422/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004423static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4424{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004425 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004426
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004427 if (!pkt)
4428 return;
4429
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004430 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004431 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004432 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004433 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004434 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004435}
4436
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004437/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4438 * type for <qc> QUIC connection from <qel> encryption level.
4439 * Return -2 if the packet could not be allocated or encrypted for any reason,
4440 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004441 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004442static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4443 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004444 struct quic_enc_level *qel,
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004445 struct quic_conn *qc, size_t dglen, int padding,
4446 int pkt_type, int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004447{
4448 /* The pointer to the packet number field. */
4449 unsigned char *buf_pn;
4450 unsigned char *beg, *end, *payload;
4451 int64_t pn;
4452 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453 struct quic_tls_ctx *tls_ctx;
4454 struct quic_tx_packet *pkt;
4455
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004456 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004457 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004458 pkt = pool_alloc(pool_head_quic_tx_packet);
4459 if (!pkt) {
4460 TRACE_DEVEL("Not enough memory for a new packet", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004461 *err = -2;
4462 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004463 }
4464
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004465 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004466 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467 pn_len = 0;
4468 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004469 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004470 *err = -1;
4471 goto err;
4472 }
4473
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004474 /* Consume a packet number. */
4475 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille28f51fa2021-11-09 14:12:12 +01004476 qc_do_build_pkt(*pos, buf_end, padding, dglen, pkt, ack, nb_pto_dgrams,
4477 pn, &pn_len, &buf_pn, qel, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004478
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004479 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004480 payload = buf_pn + pn_len;
4481 payload_len = end - payload;
4482 aad_len = payload - beg;
4483
4484 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004485 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4486 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004487 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004488 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004489
4490 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004491 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004492 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4493 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4494 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004495 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004496 goto err;
4497 }
4498
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004499 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4500 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004501 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004502 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004503 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004504 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004505 pkt->in_flight_len = pkt->len;
4506 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004507 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004508 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004509 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4510
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004511 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004512
4513 err:
4514 free_quic_tx_packet(pkt);
4515 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004516 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004517}
4518
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004519/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4520 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004521 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004522static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4523 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004524{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004525 size_t try, done = 0;
4526
4527 if (!conn_ctrl_ready(conn))
4528 return 0;
4529
4530 if (!fd_recv_ready(conn->handle.fd))
4531 return 0;
4532
4533 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004534
4535 /* read the largest possible block. For this, we perform only one call
4536 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004537 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004538 */
4539 while (count > 0) {
4540 try = b_contig_space(buf);
4541 if (!try)
4542 break;
4543
4544 if (try > count)
4545 try = count;
4546
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004547 b_add(buf, try);
4548 done += try;
4549 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004550 }
4551
4552 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4553 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4554
4555 leave:
4556 return done;
4557
4558 read0:
4559 conn_sock_read0(conn);
4560 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4561
4562 /* Now a final check for a possible asynchronous low-level error
4563 * report. This can happen when a connection receives a reset
4564 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4565 * we might have come from there by just checking POLL_HUP instead
4566 * of recv()'s return value 0, so we have no way to tell there was
4567 * an error without checking.
4568 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004569 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004570 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4571 goto leave;
4572}
4573
4574
4575/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4576 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4577 * other pending data for example, but this flag is ignored at the moment.
4578 * Only one call to send() is performed, unless the buffer wraps, in which case
4579 * a second call may be performed. The connection's flags are updated with
4580 * whatever special event is detected (error, empty). The caller is responsible
4581 * for taking care of those events and avoiding the call if inappropriate. The
4582 * function does not call the connection's polling update function, so the caller
4583 * is responsible for this. It's up to the caller to update the buffer's contents
4584 * based on the return value.
4585 */
4586static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4587{
4588 ssize_t ret;
4589 size_t try, done;
4590 int send_flag;
4591
4592 if (!conn_ctrl_ready(conn))
4593 return 0;
4594
4595 if (!fd_send_ready(conn->handle.fd))
4596 return 0;
4597
4598 done = 0;
4599 /* send the largest possible block. For this we perform only one call
4600 * to send() unless the buffer wraps and we exactly fill the first hunk,
4601 * in which case we accept to do it once again.
4602 */
4603 while (count) {
4604 try = b_contig_data(buf, done);
4605 if (try > count)
4606 try = count;
4607
4608 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4609 if (try < count || flags & CO_SFL_MSG_MORE)
4610 send_flag |= MSG_MORE;
4611
4612 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4613 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4614 if (ret > 0) {
4615 count -= ret;
4616 done += ret;
4617
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004618 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004619 conn->flags |= CO_FL_WAIT_L4L6;
4620 /* if the system buffer is full, don't insist */
4621 if (ret < try)
4622 break;
4623 }
4624 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4625 /* nothing written, we need to poll for write first */
4626 fd_cant_send(conn->handle.fd);
4627 break;
4628 }
4629 else if (errno != EINTR) {
4630 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4631 break;
4632 }
4633 }
4634 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4635 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4636
4637 if (done > 0) {
4638 /* we count the total bytes sent, and the send rate for 32-byte
4639 * blocks. The reason for the latter is that freq_ctr are
4640 * limited to 4GB and that it's not enough per second.
4641 */
4642 _HA_ATOMIC_ADD(&global.out_bytes, done);
4643 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4644 }
4645 return done;
4646}
4647
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004648/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4649 * event subscriber <es> is not allowed to change from a previous call as long
4650 * as at least one event is still subscribed. The <event_type> must only be a
4651 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4652 */
4653static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4654{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004655 struct qcc *qcc = conn->qc->qcc;
4656
4657 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4658 BUG_ON(qcc->subs && qcc->subs != es);
4659
4660 es->events |= event_type;
4661 qcc->subs = es;
4662
4663 if (event_type & SUB_RETRY_RECV)
4664 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc);
4665
4666 if (event_type & SUB_RETRY_SEND)
4667 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc);
4668
4669 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004670}
4671
4672/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4673 * The <es> pointer is not allowed to differ from the one passed to the
4674 * subscribe() call. It always returns zero.
4675 */
4676static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4677{
4678 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4679}
4680
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004681/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4682 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4683 * parameters of this session.
4684 * This is the responsibility of the caller to check the validity of all the
4685 * pointers passed as parameter to this function.
4686 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4687 * CO_ER_SSL_NO_MEM.
4688 */
4689static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4690 unsigned char *params, size_t params_len)
4691{
4692 int retry;
4693
4694 retry = 1;
4695 retry:
4696 *ssl = SSL_new(ssl_ctx);
4697 if (!*ssl) {
4698 if (!retry--)
4699 goto err;
4700
4701 pool_gc(NULL);
4702 goto retry;
4703 }
4704
4705 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4706 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
4707 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4708 goto err;
4709
4710 SSL_free(*ssl);
4711 *ssl = NULL;
4712 if (!retry--)
4713 goto err;
4714
4715 pool_gc(NULL);
4716 goto retry;
4717 }
4718
4719 return 0;
4720
4721 err:
4722 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4723 return -1;
4724}
4725
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004726/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4727 * connection with <xprt_ctx> as address of the xprt context.
4728 * Returns 1 if succeeded, 0 if not.
4729 */
4730static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4731{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004732 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004733
4734 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4735
4736 if (*xprt_ctx)
4737 goto out;
4738
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004739 ctx = pool_alloc(pool_head_quic_conn_ctx);
4740 if (!ctx) {
4741 conn->err_code = CO_ER_SYS_MEMLIM;
4742 goto err;
4743 }
4744
4745 ctx->wait_event.tasklet = tasklet_new();
4746 if (!ctx->wait_event.tasklet) {
4747 conn->err_code = CO_ER_SYS_MEMLIM;
4748 goto err;
4749 }
4750
4751 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4752 ctx->wait_event.tasklet->context = ctx;
4753 ctx->wait_event.events = 0;
4754 ctx->conn = conn;
4755 ctx->subs = NULL;
4756 ctx->xprt_ctx = NULL;
4757
4758 ctx->xprt = xprt_get(XPRT_QUIC);
4759 if (objt_server(conn->target)) {
4760 /* Server */
4761 struct server *srv = __objt_server(conn->target);
4762 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004763 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004764 int ssl_err, ipv4;
4765
4766 ssl_err = SSL_ERROR_NONE;
4767 if (RAND_bytes(dcid, sizeof dcid) != 1)
4768 goto err;
4769
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004770 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004771 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004772 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004773 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004774 goto err;
4775
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004776 /* Insert our SCID, the connection ID for the QUIC client. */
4777 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4778
4779 conn->qc = qc;
4780 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004781 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4782 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004783 goto err;
4784
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004785 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004786 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004787 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4788 qc->enc_params_len =
4789 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4790 &qc->rx.params, 0);
4791 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004792 goto err;
4793
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004794 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
4795 qc->enc_params, qc->enc_params_len) == -1)
4796 goto err;
4797
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004798 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004799 SSL_set_connect_state(ctx->ssl);
4800 ssl_err = SSL_do_handshake(ctx->ssl);
4801 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004802 int st;
4803
4804 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004805 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4806 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004807 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004808 }
4809 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004810 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004811 goto err;
4812 }
4813 }
4814 }
4815 else if (objt_listener(conn->target)) {
4816 /* Listener */
4817 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004818 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004819
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004820 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004821 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4822 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004823 goto err;
4824
4825 SSL_set_accept_state(ctx->ssl);
4826 }
4827
4828 *xprt_ctx = ctx;
4829
4830 /* Leave init state and start handshake */
4831 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004832
4833 out:
4834 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4835
4836 return 0;
4837
4838 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004839 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004840 tasklet_free(ctx->wait_event.tasklet);
4841 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004842 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004843 return -1;
4844}
4845
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004846/* Start the QUIC transport layer */
4847static int qc_xprt_start(struct connection *conn, void *ctx)
4848{
4849 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004850 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004851
4852 qc = conn->qc;
4853 if (!quic_conn_init_timer(qc)) {
4854 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4855 return 0;
4856 }
4857
4858 tasklet_wakeup(qctx->wait_event.tasklet);
4859 return 1;
4860}
4861
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004862/* transport-layer operations for QUIC connections. */
4863static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004864 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004865 .snd_buf = quic_conn_from_buf,
4866 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004867 .subscribe = quic_conn_subscribe,
4868 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004869 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004870 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004871 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4872 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01004873 .get_alpn = ssl_sock_get_alpn,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004874 .name = "QUIC",
4875};
4876
4877__attribute__((constructor))
4878static void __quic_conn_init(void)
4879{
4880 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4881 xprt_register(XPRT_QUIC, &ssl_quic);
4882}
4883
4884__attribute__((destructor))
4885static void __quic_conn_deinit(void)
4886{
4887 BIO_meth_free(ha_quic_meth);
4888}
4889
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004890/* Read all the QUIC packets found in <buf> from QUIC connection with <owner>
4891 * as owner calling <func> function.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004892 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004893 */
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004894static ssize_t quic_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004895 struct sockaddr_storage *saddr, qpkt_read_func *func)
4896{
4897 unsigned char *pos;
4898 const unsigned char *end;
4899 struct quic_dgram_ctx dgram_ctx = {
4900 .dcid_node = NULL,
4901 .owner = owner,
4902 };
4903
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004904 pos = (unsigned char *)b_head(buf);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004905 end = pos + len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004906 do {
4907 int ret;
4908 struct quic_rx_packet *pkt;
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004909 size_t pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004910
Willy Tarreaue4498932021-03-22 21:13:05 +01004911 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004912 if (!pkt)
4913 goto err;
4914
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004915 quic_rx_packet_refinc(pkt);
4916 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004917 pkt_len = pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02004918 quic_rx_packet_refdec(pkt);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004919 if (ret == -1 && !pkt_len)
4920 /* If the packet length could not be found, we cannot continue. */
4921 break;
4922
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004923 } while (pos < end);
4924
4925 /* Increasing the received bytes counter by the UDP datagram length
4926 * if this datagram could be associated to a connection.
4927 */
4928 if (dgram_ctx.qc)
4929 dgram_ctx.qc->rx.bytes += len;
4930
4931 return pos - (unsigned char *)buf;
4932
4933 err:
4934 return -1;
4935}
4936
Frédéric Lécaille324ecda2021-11-02 10:14:44 +01004937ssize_t quic_lstnr_dgram_read(struct buffer *buf, size_t len, void *owner,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004938 struct sockaddr_storage *saddr)
4939{
4940 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4941}
4942
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004943/*
4944 * Local variables:
4945 * c-indent-level: 8
4946 * c-basic-offset: 8
4947 * End:
4948 */