blob: 2d79d9a2f4b315426e4443bbf889e8bc09c1e2f3 [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
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020045#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010046#include <haproxy/quic_tls.h>
47#include <haproxy/ssl_sock.h>
48#include <haproxy/stream_interface.h>
49#include <haproxy/task.h>
50#include <haproxy/trace.h>
51#include <haproxy/xprt_quic.h>
52
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020053/* This is the values of some QUIC transport parameters when absent.
54 * Should be used to initialize any transport parameters (local or remote)
55 * before updating them with customized values.
56 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010057struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020058 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
59 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
60 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020061 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010062};
63
64/* trace source and events */
65static void quic_trace(enum trace_level level, uint64_t mask, \
66 const struct trace_source *src,
67 const struct ist where, const struct ist func,
68 const void *a1, const void *a2, const void *a3, const void *a4);
69
70static const struct trace_event quic_trace_events[] = {
71 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
72 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
73 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
74 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
75 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
76 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
77 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050078 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010079 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
80 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
81 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
82 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
83 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
84 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
85 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
86 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
87 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
88 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
89 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
90 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
91 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
92 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
93 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
94 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
95 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
96 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
97 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010098 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
99 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
100 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
101 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
102 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
103 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100104 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100105 { /* end */ }
106};
107
108static const struct name_desc quic_trace_lockon_args[4] = {
109 /* arg1 */ { /* already used by the connection */ },
110 /* arg2 */ { .name="quic", .desc="QUIC transport" },
111 /* arg3 */ { },
112 /* arg4 */ { }
113};
114
115static const struct name_desc quic_trace_decoding[] = {
116#define QUIC_VERB_CLEAN 1
117 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
118 { /* end */ }
119};
120
121
122struct trace_source trace_quic = {
123 .name = IST("quic"),
124 .desc = "QUIC xprt",
125 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
126 .default_cb = quic_trace,
127 .known_events = quic_trace_events,
128 .lockon_args = quic_trace_lockon_args,
129 .decoding = quic_trace_decoding,
130 .report_events = ~0, /* report everything by default */
131};
132
133#define TRACE_SOURCE &trace_quic
134INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
135
136static BIO_METHOD *ha_quic_meth;
137
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200138DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100139DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200140 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100141DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142DECLARE_POOL(pool_head_quic_connection_id,
143 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144DECLARE_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 +0100145DECLARE_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 +0100146DECLARE_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 +0100147DECLARE_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 +0100148DECLARE_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 +0200149DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100150DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100151
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200152static 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 +0200153 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200154 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200155 int ack, int nb_pto_dgrams, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100156
157/* Add traces to <buf> depending on <frm> TX frame type. */
158static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200159 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100160{
161 switch (frm->type) {
162 case QUIC_FT_CRYPTO:
163 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
164 (unsigned long long)frm->crypto.offset,
165 (unsigned long long)frm->crypto.len);
166 break;
167 default:
168 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
169 }
170}
171
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100172/* Only for debug purpose */
173struct enc_debug_info {
174 unsigned char *payload;
175 size_t payload_len;
176 unsigned char *aad;
177 size_t aad_len;
178 uint64_t pn;
179};
180
181/* Initializes a enc_debug_info struct (only for debug purpose) */
182static inline void enc_debug_info_init(struct enc_debug_info *edi,
183 unsigned char *payload, size_t payload_len,
184 unsigned char *aad, size_t aad_len, uint64_t pn)
185{
186 edi->payload = payload;
187 edi->payload_len = payload_len;
188 edi->aad = aad;
189 edi->aad_len = aad_len;
190 edi->pn = pn;
191}
192
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100193/* Trace callback for QUIC.
194 * These traces always expect that arg1, if non-null, is of type connection.
195 */
196static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
197 const struct ist where, const struct ist func,
198 const void *a1, const void *a2, const void *a3, const void *a4)
199{
200 const struct connection *conn = a1;
201
202 if (conn) {
203 struct quic_tls_secrets *secs;
204 struct quic_conn *qc;
205
206 qc = conn->qc;
207 chunk_appendf(&trace_buf, " : conn@%p", conn);
208 if ((mask & QUIC_EV_CONN_INIT) && qc) {
209 chunk_appendf(&trace_buf, "\n odcid");
210 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100211 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100212 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100213 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 }
216
217 if (mask & QUIC_EV_CONN_ADDDATA) {
218 const enum ssl_encryption_level_t *level = a2;
219 const size_t *len = a3;
220
221 if (level) {
222 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
223
224 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
225 }
226 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100227 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100228 }
229 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
230 /* Initial read & write secrets. */
231 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
232 const unsigned char *rx_sec = a2;
233 const unsigned char *tx_sec = a3;
234
235 secs = &qc->els[level].tls_ctx.rx;
236 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
237 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
238 if (rx_sec)
239 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
240 quic_tls_keys_hexdump(&trace_buf, secs);
241 }
242 secs = &qc->els[level].tls_ctx.tx;
243 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
244 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
245 if (tx_sec)
246 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
247 quic_tls_keys_hexdump(&trace_buf, secs);
248 }
249 }
250 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
251 const enum ssl_encryption_level_t *level = a2;
252 const unsigned char *secret = a3;
253 const size_t *secret_len = a4;
254
255 if (level) {
256 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
257
258 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
259 if (secret && secret_len)
260 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
261 secs = &qc->els[lvl].tls_ctx.rx;
262 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
263 quic_tls_keys_hexdump(&trace_buf, secs);
264 }
265 }
266
267 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
268 const enum ssl_encryption_level_t *level = a2;
269 const unsigned char *secret = a3;
270 const size_t *secret_len = a4;
271
272 if (level) {
273 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
274
275 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
276 if (secret && secret_len)
277 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
278 secs = &qc->els[lvl].tls_ctx.tx;
279 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
280 quic_tls_keys_hexdump(&trace_buf, secs);
281 }
282
283 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100284
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100285 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100286 const struct quic_tx_packet *pkt = a2;
287 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100288 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289
290 if (qel) {
291 struct quic_pktns *pktns;
292
293 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100294 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
295 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100296 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100297 (unsigned long long)qc->path->cwnd,
298 (unsigned long long)qc->path->prep_in_flight,
299 (unsigned long long)qc->path->in_flight,
300 (unsigned long long)pktns->tx.in_flight,
301 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100302 }
303 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200304 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100305 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
306 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
307 list_for_each_entry(frm, &pkt->frms, list)
308 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100309 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
310 }
311
312 if (room) {
313 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
314 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
315 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100316 }
317 }
318
319 if (mask & QUIC_EV_CONN_HDSHK) {
320 const enum quic_handshake_state *state = a2;
321 const int *err = a3;
322
323 if (state)
324 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
325 if (err)
326 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
327 }
328
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100329 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100330 const struct quic_rx_packet *pkt = a2;
331 const unsigned long *pktlen = a3;
332 const SSL *ssl = a4;
333
334 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100335 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100336 pkt, quic_packet_type_enc_level_char(pkt->type));
337 if (pkt->pnl)
338 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
339 (unsigned long long)pkt->pn);
340 if (pkt->token_len)
341 chunk_appendf(&trace_buf, " toklen=%llu",
342 (unsigned long long)pkt->token_len);
343 if (pkt->aad_len)
344 chunk_appendf(&trace_buf, " aadlen=%llu",
345 (unsigned long long)pkt->aad_len);
346 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
347 pkt->flags, (unsigned long long)pkt->len);
348 }
349 if (pktlen)
350 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
351 if (ssl) {
352 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
353 chunk_appendf(&trace_buf, " el=%c",
354 quic_enc_level_char(ssl_to_quic_enc_level(level)));
355 }
356 }
357
358 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
359 const struct quic_rx_packet *pkt = a2;
360 const struct quic_rx_crypto_frm *cf = a3;
361 const SSL *ssl = a4;
362
363 if (pkt)
364 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
365 quic_packet_type_enc_level_char(pkt->type),
366 (unsigned long long)pkt->pn);
367 if (cf)
368 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
369 (unsigned long long)cf->offset_node.key,
370 (unsigned long long)cf->len);
371 if (ssl) {
372 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
373 chunk_appendf(&trace_buf, " el=%c",
374 quic_enc_level_char(ssl_to_quic_enc_level(level)));
375 }
376 }
377
378 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
379 const struct quic_frame *frm = a2;
380
381 if (frm)
382 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
383 }
384
385 if (mask & QUIC_EV_CONN_PHPKTS) {
386 const struct quic_enc_level *qel = a2;
387
388 if (qel) {
389 struct quic_pktns *pktns;
390
391 pktns = qc->pktns;
392 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200393 " 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 +0100394 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200395 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille2766e782021-08-30 17:16:07 +0200396 !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100397 (unsigned long long)qc->path->cwnd,
398 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100399 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100400 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
401 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100402 }
403 }
404
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100405 if (mask & QUIC_EV_CONN_ENCPKT) {
406 const struct enc_debug_info *edi = a2;
407
408 if (edi)
409 chunk_appendf(&trace_buf,
410 " payload=@%p payload_len=%llu"
411 " aad=@%p aad_len=%llu pn=%llu",
412 edi->payload, (unsigned long long)edi->payload_len,
413 edi->aad, (unsigned long long)edi->aad_len,
414 (unsigned long long)edi->pn);
415 }
416
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100417 if (mask & QUIC_EV_CONN_RMHP) {
418 const struct quic_rx_packet *pkt = a2;
419
420 if (pkt) {
421 const int *ret = a3;
422
423 chunk_appendf(&trace_buf, " pkt@%p", pkt);
424 if (ret && *ret)
425 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
426 pkt->pnl, (unsigned long long)pkt->pn);
427 }
428 }
429
430 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200431 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100432 const unsigned long *val1 = a3;
433 const unsigned long *val2 = a4;
434
435 if (frm)
436 chunk_tx_frm_appendf(&trace_buf, frm);
437 if (val1)
438 chunk_appendf(&trace_buf, " %lu", *val1);
439 if (val2)
440 chunk_appendf(&trace_buf, "..%lu", *val2);
441 }
442
443 if (mask & QUIC_EV_CONN_RTTUPDT) {
444 const unsigned int *rtt_sample = a2;
445 const unsigned int *ack_delay = a3;
446 const struct quic_loss *ql = a4;
447
448 if (rtt_sample)
449 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
450 if (ack_delay)
451 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
452 if (ql)
453 chunk_appendf(&trace_buf,
454 " srtt=%ums rttvar=%ums min_rtt=%ums",
455 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
456 }
457 if (mask & QUIC_EV_CONN_CC) {
458 const struct quic_cc_event *ev = a2;
459 const struct quic_cc *cc = a3;
460
461 if (a2)
462 quic_cc_event_trace(&trace_buf, ev);
463 if (a3)
464 quic_cc_state_trace(&trace_buf, cc);
465 }
466
467 if (mask & QUIC_EV_CONN_PKTLOSS) {
468 const struct quic_pktns *pktns = a2;
469 const struct list *lost_pkts = a3;
470 struct quic_conn *qc = conn->qc;
471
472 if (pktns) {
473 chunk_appendf(&trace_buf, " pktns=%s",
474 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
475 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
476 if (pktns->tx.loss_time)
477 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100478 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100479 }
480 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
481 struct quic_tx_packet *pkt;
482
483 chunk_appendf(&trace_buf, " lost_pkts:");
484 list_for_each_entry(pkt, lost_pkts, list)
485 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
486 }
487 }
488
489 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
490 struct quic_conn *qc = conn->qc;
491 const struct quic_pktns *pktns = a2;
492 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100493 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100494
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100495 if (ifae_pkts)
496 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
497 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100498 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100499 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100501 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
502 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 if (mask & QUIC_EV_CONN_STIMER) {
504 if (pktns->tx.loss_time)
505 chunk_appendf(&trace_buf, " loss_time=%dms",
506 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
507 }
508 if (mask & QUIC_EV_CONN_SPTO) {
509 if (pktns->tx.time_of_last_eliciting)
510 chunk_appendf(&trace_buf, " tole=%dms",
511 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
512 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100513 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100514 }
515 }
516
517 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
518 chunk_appendf(&trace_buf,
519 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
520 }
521 }
522
523 if (mask & QUIC_EV_CONN_SPPKTS) {
524 const struct quic_tx_packet *pkt = a2;
525
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100526 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
527 (unsigned long long)qc->path->cwnd,
528 (unsigned long long)qc->path->prep_in_flight,
529 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100530 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100531 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 (unsigned long)pkt->pn_node.key,
533 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
534 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100535 (unsigned long long)pkt->in_flight_len,
536 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100537 }
538 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100539
540 if (mask & QUIC_EV_CONN_SSLALERT) {
541 const uint8_t *alert = a2;
542 const enum ssl_encryption_level_t *level = a3;
543
544 if (alert)
545 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
546 if (level)
547 chunk_appendf(&trace_buf, " el=%c",
548 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
549 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100550
551 if (mask & QUIC_EV_CONN_BCFRMS) {
552 const size_t *sz1 = a2;
553 const size_t *sz2 = a3;
554 const size_t *sz3 = a4;
555
556 if (sz1)
557 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
558 if (sz2)
559 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
560 if (sz3)
561 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
562 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100563
564 if (mask & QUIC_EV_CONN_PSTRM) {
565 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100566
567 if (a2) {
568 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100569
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100570 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
571 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
572 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
573 (unsigned long long)s->id,
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200574 (unsigned long long)s->offset.key,
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100575 (unsigned long long)s->len);
576 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100577 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100578 }
579 if (mask & QUIC_EV_CONN_LPKT) {
580 const struct quic_rx_packet *pkt = a2;
581
582 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200583 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100584 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200585 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
586 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587 }
588
589}
590
591/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200592static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593{
594 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200595 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100596
597 qc = ctx->conn->qc;
598 if (objt_server(qc->conn->target))
599 return 1;
600
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200601 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
602 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
603 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
604 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200605 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100606 return 1;
607
608 return 0;
609}
610
611/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
612 * both loss detection and PTO and schedule the task assiated to this timer if needed.
613 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200614static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615{
616 struct quic_conn *qc;
617 struct quic_pktns *pktns;
618 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200619 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100621 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
622 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100623 qc = ctx->conn->qc;
624 pktns = quic_loss_pktns(qc);
625 if (tick_isset(pktns->tx.loss_time)) {
626 qc->timer = pktns->tx.loss_time;
627 goto out;
628 }
629
630 /* XXX TODO: anti-amplification: the timer must be
631 * cancelled for a server which reached the anti-amplification limit.
632 */
633
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100634 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
635 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100636 /* Timer cancellation. */
637 qc->timer = TICK_ETERNITY;
638 goto out;
639 }
640
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200641 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
642 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100643 if (tick_isset(pto))
644 qc->timer = pto;
645 out:
646 task_schedule(qc->timer_task, qc->timer);
647 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
648}
649
650#ifndef OPENSSL_IS_BORINGSSL
651int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
652 const uint8_t *read_secret,
653 const uint8_t *write_secret, size_t secret_len)
654{
655 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
656 struct quic_tls_ctx *tls_ctx =
657 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
658 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
659
660 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
661 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
662 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
663 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
664
665 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
666 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
667 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
668 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
669 read_secret, secret_len)) {
670 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
671 return 0;
672 }
673
674 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
675 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
676 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
677 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
678 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
679 write_secret, secret_len)) {
680 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
681 return 0;
682 }
683
684 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
685 if (objt_server(conn->target) && level == ssl_encryption_application) {
686 const unsigned char *buf;
687 size_t buflen;
688
689 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
690 if (!buflen)
691 return 0;
692
693 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
694 return 0;
695 }
696 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
697
698 return 1;
699}
700#else
701/* ->set_read_secret callback to derive the RX secrets at <level> encryption
702 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500703 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100704 */
705int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
706 const SSL_CIPHER *cipher,
707 const uint8_t *secret, size_t secret_len)
708{
709 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
710 struct quic_tls_ctx *tls_ctx =
711 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
712
713 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
714 tls_ctx->rx.aead = tls_aead(cipher);
715 tls_ctx->rx.md = tls_md(cipher);
716 tls_ctx->rx.hp = tls_hp(cipher);
717
718 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
719 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
720 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
721 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
722 secret, secret_len)) {
723 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
724 goto err;
725 }
726
727 if (objt_server(conn->target) && level == ssl_encryption_application) {
728 const unsigned char *buf;
729 size_t buflen;
730
731 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
732 if (!buflen)
733 goto err;
734
735 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
736 goto err;
737 }
738
739 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
740 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
741
742 return 1;
743
744 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100745 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100746 return 0;
747}
748
749/* ->set_write_secret callback to derive the TX secrets at <level>
750 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500751 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100752 */
753int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
754 const SSL_CIPHER *cipher,
755 const uint8_t *secret, size_t secret_len)
756{
757 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
758 struct quic_tls_ctx *tls_ctx =
759 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
760
761 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
762 tls_ctx->tx.aead = tls_aead(cipher);
763 tls_ctx->tx.md = tls_md(cipher);
764 tls_ctx->tx.hp = tls_hp(cipher);
765
766 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
767 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
768 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
769 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
770 secret, secret_len)) {
771 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
772 goto err;
773 }
774
775 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
776 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
777
778 return 1;
779
780 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100781 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100782 return 0;
783}
784#endif
785
786/* This function copies the CRYPTO data provided by the TLS stack found at <data>
787 * with <len> as size in CRYPTO buffers dedicated to store the information about
788 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
789 * It fails only if it could not managed to allocate enough CRYPTO buffers to
790 * store all the data.
791 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
792 */
793static int quic_crypto_data_cpy(struct quic_enc_level *qel,
794 const unsigned char *data, size_t len)
795{
796 struct quic_crypto_buf **qcb;
797 /* The remaining byte to store in CRYPTO buffers. */
798 size_t cf_offset, cf_len, *nb_buf;
799 unsigned char *pos;
800
801 nb_buf = &qel->tx.crypto.nb_buf;
802 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
803 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
804 cf_len = len;
805
806 while (len) {
807 size_t to_copy, room;
808
809 pos = (*qcb)->data + (*qcb)->sz;
810 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
811 to_copy = len > room ? room : len;
812 if (to_copy) {
813 memcpy(pos, data, to_copy);
814 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
815 qel->tx.crypto.sz += to_copy;
816 (*qcb)->sz += to_copy;
817 pos += to_copy;
818 len -= to_copy;
819 data += to_copy;
820 }
821 else {
822 struct quic_crypto_buf **tmp;
823
824 tmp = realloc(qel->tx.crypto.bufs,
825 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
826 if (tmp) {
827 qel->tx.crypto.bufs = tmp;
828 qcb = &qel->tx.crypto.bufs[*nb_buf];
829 *qcb = pool_alloc(pool_head_quic_crypto_buf);
830 if (!*qcb)
831 return 0;
832
833 (*qcb)->sz = 0;
834 ++*nb_buf;
835 }
836 else {
837 break;
838 }
839 }
840 }
841
842 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
843 * have been buffered.
844 */
845 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200846 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100847
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200848 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100849 if (!frm)
850 return 0;
851
852 frm->type = QUIC_FT_CRYPTO;
853 frm->crypto.offset = cf_offset;
854 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200855 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200856 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100857 }
858
859 return len == 0;
860}
861
862
863/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
864 * wants to provide the QUIC layer with CRYPTO data.
865 * Returns 1 if succeeded, 0 if not.
866 */
867int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
868 const uint8_t *data, size_t len)
869{
870 struct connection *conn;
871 enum quic_tls_enc_level tel;
872 struct quic_enc_level *qel;
873
874 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
875 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
876 tel = ssl_to_quic_enc_level(level);
877 qel = &conn->qc->els[tel];
878
879 if (tel == -1) {
880 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
881 goto err;
882 }
883
884 if (!quic_crypto_data_cpy(qel, data, len)) {
885 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
886 goto err;
887 }
888
889 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
890 conn, &level, &len);
891
892 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
893 return 1;
894
895 err:
896 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
897 return 0;
898}
899
900int ha_quic_flush_flight(SSL *ssl)
901{
902 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
903
904 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
905 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
906
907 return 1;
908}
909
910int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
911{
912 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
913
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100914 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100915 return 1;
916}
917
918/* QUIC TLS methods */
919static SSL_QUIC_METHOD ha_quic_method = {
920#ifdef OPENSSL_IS_BORINGSSL
921 .set_read_secret = ha_set_rsec,
922 .set_write_secret = ha_set_wsec,
923#else
924 .set_encryption_secrets = ha_quic_set_encryption_secrets,
925#endif
926 .add_handshake_data = ha_quic_add_handshake_data,
927 .flush_flight = ha_quic_flush_flight,
928 .send_alert = ha_quic_send_alert,
929};
930
931/* Initialize the TLS context of a listener with <bind_conf> as configuration.
932 * Returns an error count.
933 */
934int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
935{
936 struct proxy *curproxy = bind_conf->frontend;
937 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
938 int cfgerr = 0;
939
940#if 0
941 /* XXX Did not manage to use this. */
942 const char *ciphers =
943 "TLS_AES_128_GCM_SHA256:"
944 "TLS_AES_256_GCM_SHA384:"
945 "TLS_CHACHA20_POLY1305_SHA256:"
946 "TLS_AES_128_CCM_SHA256";
947#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200948 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100949 long options =
950 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
951 SSL_OP_SINGLE_ECDH_USE |
952 SSL_OP_CIPHER_SERVER_PREFERENCE;
953 SSL_CTX *ctx;
954
955 ctx = SSL_CTX_new(TLS_server_method());
956 bind_conf->initial_ctx = ctx;
957
958 SSL_CTX_set_options(ctx, options);
959#if 0
960 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
961 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
962 "for bind '%s' at [%s:%d].\n",
963 curproxy->id, ciphers,
964 bind_conf->arg, bind_conf->file, bind_conf->line);
965 cfgerr++;
966 }
967#endif
968
969 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
970 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
971 "for bind '%s' at [%s:%d].\n",
972 curproxy->id, groups,
973 bind_conf->arg, bind_conf->file, bind_conf->line);
974 cfgerr++;
975 }
976
977 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
978 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
979 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
980 SSL_CTX_set_default_verify_paths(ctx);
981
982#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
983#ifdef OPENSSL_IS_BORINGSSL
984 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
985 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
986#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
987 if (bind_conf->ssl_conf.early_data) {
988 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
989 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
990 }
991 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
992 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
993#else
994 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
995#endif
996 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
997#endif
998 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
999
1000 return cfgerr;
1001}
1002
1003/* Decode an expected packet number from <truncated_on> its truncated value,
1004 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1005 * the number of bits used to encode this packet number (its length in bytes * 8).
1006 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1007 */
1008static uint64_t decode_packet_number(uint64_t largest_pn,
1009 uint32_t truncated_pn, unsigned int pn_nbits)
1010{
1011 uint64_t expected_pn = largest_pn + 1;
1012 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1013 uint64_t pn_hwin = pn_win / 2;
1014 uint64_t pn_mask = pn_win - 1;
1015 uint64_t candidate_pn;
1016
1017
1018 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1019 /* Note that <pn_win> > <pn_hwin>. */
1020 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1021 candidate_pn + pn_hwin <= expected_pn)
1022 return candidate_pn + pn_win;
1023
1024 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1025 return candidate_pn - pn_win;
1026
1027 return candidate_pn;
1028}
1029
1030/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1031 * cryptographic context.
1032 * <largest_pn> is the largest received packet number and <pn> the address of
1033 * the packet number field for this packet with <byte0> address of its first byte.
1034 * <end> points to one byte past the end of this packet.
1035 * Returns 1 if succeeded, 0 if not.
1036 */
1037static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1038 int64_t largest_pn, unsigned char *pn,
1039 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001040 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001041{
1042 int ret, outlen, i, pnlen;
1043 uint64_t packet_number;
1044 uint32_t truncated_pn = 0;
1045 unsigned char mask[5] = {0};
1046 unsigned char *sample;
1047 EVP_CIPHER_CTX *cctx;
1048 unsigned char *hp_key;
1049
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001050 /* Check there is enough data in this packet. */
1051 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1052 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1053 return 0;
1054 }
1055
1056 cctx = EVP_CIPHER_CTX_new();
1057 if (!cctx) {
1058 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1059 return 0;
1060 }
1061
1062 ret = 0;
1063 sample = pn + QUIC_PACKET_PN_MAXLEN;
1064
1065 hp_key = tls_ctx->rx.hp_key;
1066 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1067 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1068 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1069 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1070 goto out;
1071 }
1072
1073 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1074 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1075 for (i = 0; i < pnlen; i++) {
1076 pn[i] ^= mask[i + 1];
1077 truncated_pn = (truncated_pn << 8) | pn[i];
1078 }
1079
1080 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1081 /* Store remaining information for this unprotected header */
1082 pkt->pn = packet_number;
1083 pkt->pnl = pnlen;
1084
1085 ret = 1;
1086
1087 out:
1088 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001089
1090 return ret;
1091}
1092
1093/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1094 * address, with <payload_len> as payload length, <aad> as address of
1095 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1096 * context.
1097 * Returns 1 if succeeded, 0 if not.
1098 */
1099static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1100 unsigned char *aad, size_t aad_len, uint64_t pn,
1101 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1102{
1103 unsigned char iv[12];
1104 unsigned char *tx_iv = tls_ctx->tx.iv;
1105 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001106 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001107
1108 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1109 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001110 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001111 }
1112
1113 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1114 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1115 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001116 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117 }
1118
1119 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001120
1121 err:
1122 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1123 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1124 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001125}
1126
1127/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1128 * Returns 1 if succeeded, 0 if not.
1129 */
1130static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1131{
1132 int ret;
1133 unsigned char iv[12];
1134 unsigned char *rx_iv = tls_ctx->rx.iv;
1135 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1136
1137 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1138 return 0;
1139
1140 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1141 pkt->data, pkt->aad_len,
1142 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1143 if (!ret)
1144 return 0;
1145
1146 /* Update the packet length (required to parse the frames). */
1147 pkt->len = pkt->aad_len + ret;
1148
1149 return 1;
1150}
1151
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001152/* Remove from <qcs> stream the acknowledged frames.
1153 * Never fails.
1154 */
1155static void qcs_try_to_consume(struct qcs *qcs)
1156{
1157 struct eb64_node *frm_node;
1158
1159 frm_node = eb64_first(&qcs->tx.acked_frms);
1160 while (frm_node) {
1161 struct quic_stream *strm;
1162
1163 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1164 if (strm->offset.key != qcs->tx.ack_offset)
1165 break;
1166
1167 b_del(strm->buf, strm->len);
1168 qcs->tx.ack_offset += strm->len;
1169 frm_node = eb64_next(frm_node);
1170 eb64_delete(&strm->offset);
1171 }
1172}
1173
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001174/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001175static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001176 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001177{
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001178
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001179 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001180 switch (frm->type) {
1181 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1182 {
1183 struct qcs *qcs = frm->stream.qcs;
1184 struct quic_stream *strm = &frm->stream;
1185
1186 if (qcs->tx.ack_offset == strm->offset.key) {
1187 b_del(strm->buf, strm->len);
1188 qcs->tx.ack_offset += strm->len;
1189 LIST_DELETE(&frm->list);
1190 pool_free(pool_head_quic_frame, frm);
1191 }
1192 else {
1193 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1194 }
1195 qcs_try_to_consume(qcs);
1196 }
1197 break;
1198 default:
1199 LIST_DELETE(&frm->list);
1200 pool_free(pool_head_quic_frame, frm);
1201 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202}
1203
1204/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1205 * deallocating them, and their TX frames.
1206 * Returns the last node reached to be used for the next range.
1207 * May be NULL if <largest> node could not be found.
1208 */
1209static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1210 struct list *newly_acked_pkts,
1211 struct eb64_node *largest_node,
1212 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001213 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001214{
1215 struct eb64_node *node;
1216 struct quic_tx_packet *pkt;
1217
1218 if (largest_node)
1219 node = largest_node;
1220 else {
1221 node = eb64_lookup(pkts, largest);
1222 while (!node && largest > smallest) {
1223 node = eb64_lookup(pkts, --largest);
1224 }
1225 }
1226
1227 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001228 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001229
1230 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1231 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001232 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001233 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1234 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1235 qc_treat_acked_tx_frm(frm, ctx);
1236 node = eb64_prev(node);
1237 eb64_delete(&pkt->pn_node);
1238 }
1239
1240 return node;
1241}
1242
1243/* Treat <frm> frame whose packet it is attached to has just been detected as non
1244 * acknowledged.
1245 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001246static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001247 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001248 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001249{
1250 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001251 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001252 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001253}
1254
1255
1256/* Free the TX packets of <pkts> list */
1257static inline void free_quic_tx_pkts(struct list *pkts)
1258{
1259 struct quic_tx_packet *pkt, *tmp;
1260
1261 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001262 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001263 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001264 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 }
1266}
1267
1268/* Send a packet loss event nofification to the congestion controller
1269 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1270 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1271 * at <now_us> current time.
1272 * Always succeeds.
1273 */
1274static inline void qc_cc_loss_event(struct quic_conn *qc,
1275 unsigned int lost_bytes,
1276 unsigned int newest_time_sent,
1277 unsigned int period,
1278 unsigned int now_us)
1279{
1280 struct quic_cc_event ev = {
1281 .type = QUIC_CC_EVT_LOSS,
1282 .loss.now_ms = now_ms,
1283 .loss.max_ack_delay = qc->max_ack_delay,
1284 .loss.lost_bytes = lost_bytes,
1285 .loss.newest_time_sent = newest_time_sent,
1286 .loss.period = period,
1287 };
1288
1289 quic_cc_event(&qc->path->cc, &ev);
1290}
1291
1292/* Send a packet ack event nofication for each newly acked packet of
1293 * <newly_acked_pkts> list and free them.
1294 * Always succeeds.
1295 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001296static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001297 struct list *newly_acked_pkts)
1298{
1299 struct quic_conn *qc = ctx->conn->qc;
1300 struct quic_tx_packet *pkt, *tmp;
1301 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1302
1303 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1304 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001305 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001306 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001307 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001308 ev.ack.acked = pkt->in_flight_len;
1309 ev.ack.time_sent = pkt->time_sent;
1310 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001311 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001312 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001313 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001314 }
1315
1316}
1317
1318/* Handle <pkts> list of lost packets detected at <now_us> handling
1319 * their TX frames.
1320 * Send a packet loss event to the congestion controller if
1321 * in flight packet have been lost.
1322 * Also frees the packet in <pkts> list.
1323 * Never fails.
1324 */
1325static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001326 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001327 struct list *pkts,
1328 uint64_t now_us)
1329{
1330 struct quic_conn *qc = ctx->conn->qc;
1331 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001332 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001333 uint64_t lost_bytes;
1334
1335 lost_bytes = 0;
1336 oldest_lost = newest_lost = NULL;
1337 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1338 lost_bytes += pkt->in_flight_len;
1339 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001340 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001341 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001342 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001343 /* Treat the frames of this lost packet. */
1344 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1345 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001346 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001347 if (!oldest_lost) {
1348 oldest_lost = newest_lost = pkt;
1349 }
1350 else {
1351 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001352 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001353 newest_lost = pkt;
1354 }
1355 }
1356
1357 if (lost_bytes) {
1358 /* Sent a packet loss event to the congestion controller. */
1359 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1360 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001361 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001362 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001363 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001364 }
1365}
1366
1367/* Look for packet loss from sent packets for <qel> encryption level of a
1368 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1369 * their tree if deemed as lost or set the <loss_time> value the packet number
1370 * space if any not deemed lost.
1371 * Should be called after having received an ACK frame with newly acknowledged
1372 * packets or when the the loss detection timer has expired.
1373 * Always succeeds.
1374 */
1375static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1376 struct quic_conn *qc,
1377 struct list *lost_pkts)
1378{
1379 struct eb_root *pkts;
1380 struct eb64_node *node;
1381 struct quic_loss *ql;
1382 unsigned int loss_delay;
1383
1384 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1385 pkts = &pktns->tx.pkts;
1386 pktns->tx.loss_time = TICK_ETERNITY;
1387 if (eb_is_empty(pkts))
1388 goto out;
1389
1390 ql = &qc->path->loss;
1391 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1392 loss_delay += loss_delay >> 3;
1393 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1394
1395 node = eb64_first(pkts);
1396 while (node) {
1397 struct quic_tx_packet *pkt;
1398 int64_t largest_acked_pn;
1399 unsigned int loss_time_limit, time_sent;
1400
1401 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001402 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001403 node = eb64_next(node);
1404 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1405 break;
1406
1407 time_sent = pkt->time_sent;
1408 loss_time_limit = tick_add(time_sent, loss_delay);
1409 if (tick_is_le(time_sent, now_ms) ||
1410 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1411 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001412 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001413 }
1414 else {
1415 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1416 }
1417 }
1418
1419 out:
1420 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1421}
1422
1423/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1424 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001425 * 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 +01001426 * acked ack-eliciting packet.
1427 * Return 1, if succeeded, 0 if not.
1428 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001429static 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 +01001430 struct quic_enc_level *qel,
1431 unsigned int *rtt_sample,
1432 const unsigned char **pos, const unsigned char *end)
1433{
1434 struct quic_ack *ack = &frm->ack;
1435 uint64_t smallest, largest;
1436 struct eb_root *pkts;
1437 struct eb64_node *largest_node;
1438 unsigned int time_sent, pkt_flags;
1439 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1440 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1441
1442 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1443 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1444 ctx->conn,, &ack->largest_ack);
1445 goto err;
1446 }
1447
1448 if (ack->first_ack_range > ack->largest_ack) {
1449 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1450 ctx->conn,, &ack->first_ack_range);
1451 goto err;
1452 }
1453
1454 largest = ack->largest_ack;
1455 smallest = largest - ack->first_ack_range;
1456 pkts = &qel->pktns->tx.pkts;
1457 pkt_flags = 0;
1458 largest_node = NULL;
1459 time_sent = 0;
1460
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001461 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 +01001462 largest_node = eb64_lookup(pkts, largest);
1463 if (!largest_node) {
1464 TRACE_DEVEL("Largest acked packet not found",
1465 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1466 goto err;
1467 }
1468
1469 time_sent = eb64_entry(&largest_node->node,
1470 struct quic_tx_packet, pn_node)->time_sent;
1471 }
1472
1473 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1474 ctx->conn,, &largest, &smallest);
1475 do {
1476 uint64_t gap, ack_range;
1477
1478 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1479 largest_node, largest, smallest, ctx);
1480 if (!ack->ack_range_num--)
1481 break;
1482
1483 if (!quic_dec_int(&gap, pos, end))
1484 goto err;
1485
1486 if (smallest < gap + 2) {
1487 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1488 ctx->conn,, &gap, &smallest);
1489 goto err;
1490 }
1491
1492 largest = smallest - gap - 2;
1493 if (!quic_dec_int(&ack_range, pos, end))
1494 goto err;
1495
1496 if (largest < ack_range) {
1497 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1498 ctx->conn,, &largest, &ack_range);
1499 goto err;
1500 }
1501
1502 /* Do not use this node anymore. */
1503 largest_node = NULL;
1504 /* Next range */
1505 smallest = largest - ack_range;
1506
1507 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1508 ctx->conn,, &largest, &smallest);
1509 } while (1);
1510
1511 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001512 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001513
1514 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1515 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001516 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001517 }
1518
1519 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1520 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1521 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1522 if (!LIST_ISEMPTY(&lost_pkts))
1523 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1524 }
1525 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1526 if (quic_peer_validated_addr(ctx))
1527 ctx->conn->qc->path->loss.pto_count = 0;
1528 qc_set_timer(ctx);
1529 }
1530
1531
1532 return 1;
1533
1534 err:
1535 free_quic_tx_pkts(&newly_acked_pkts);
1536 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1537 return 0;
1538}
1539
1540/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1541 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001542 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001543 * Return 1 if succeeded, 0 if not.
1544 */
1545static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001546 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001547 const unsigned char *data, size_t len,
1548 struct quic_rx_packet *pkt,
1549 struct quic_rx_crypto_frm *cf)
1550{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001551 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001552 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001553
1554 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1555 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001556 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001557 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1558 TRACE_PROTO("SSL_provide_quic_data() error",
1559 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1560 goto err;
1561 }
1562
1563 el->rx.crypto.offset += len;
1564 TRACE_PROTO("in order CRYPTO data",
1565 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1566
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001567 state = HA_ATOMIC_LOAD(&qc->state);
1568 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 ssl_err = SSL_do_handshake(ctx->ssl);
1570 if (ssl_err != 1) {
1571 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1572 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1573 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001574 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001575 goto out;
1576 }
1577
1578 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001579 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 goto err;
1581 }
1582
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001583 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001584 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001585 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001586 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001587 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001588 } else {
1589 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1590 if (ssl_err != 1) {
1591 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1592 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1593 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001594 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001595 goto out;
1596 }
1597
1598 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001599 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001600 goto err;
1601 }
1602
1603 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001604 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001605 }
1606
1607 out:
1608 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1609 return 1;
1610
1611 err:
1612 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1613 return 0;
1614}
1615
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001616/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1617 * <pkt> RX packet.
1618 * Return it if succeeded, NULL if not.
1619 */
1620static inline
1621struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1622 struct quic_rx_packet *pkt)
1623{
1624 struct quic_rx_strm_frm *frm;
1625
1626 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1627 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001628 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001629 frm->len = stream_frm->len;
1630 frm->data = stream_frm->data;
1631 frm->pkt = pkt;
1632 }
1633
1634 return frm;
1635}
1636
1637/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1638 * several streams, depending on the already open onces.
1639 * Return this node if succeeded, NULL if not.
1640 */
1641static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1642{
1643 unsigned int strm_type;
1644 int64_t sub_id;
1645 struct eb64_node *strm_node;
1646
1647 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1648
1649 strm_type = id & QCS_ID_TYPE_MASK;
1650 sub_id = id >> QCS_ID_TYPE_SHIFT;
1651 strm_node = NULL;
1652 if (qc_local_stream_id(qcc, id)) {
1653 /* Local streams: this stream must be already opened. */
1654 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1655 if (!strm_node) {
1656 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1657 goto out;
1658 }
1659 }
1660 else {
1661 /* Remote streams. */
1662 struct eb_root *strms;
1663 uint64_t largest_id;
1664 enum qcs_type qcs_type;
1665
1666 strms = &qcc->streams_by_id;
1667 qcs_type = qcs_id_type(id);
1668 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1669 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1670 goto out;
1671 }
1672
1673 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1674 * correct value.
1675 */
1676 largest_id = qcc->strms[qcs_type].largest_id;
1677 if (sub_id > (int64_t)largest_id) {
1678 /* RFC: "A stream ID that is used out of order results in all streams
1679 * of that type with lower-numbered stream IDs also being opened".
1680 * So, let's "open" these streams.
1681 */
1682 int64_t i;
1683 struct qcs *qcs;
1684
1685 qcs = NULL;
1686 for (i = largest_id + 1; i <= sub_id; i++) {
1687 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1688 if (!qcs) {
1689 TRACE_PROTO("Could not allocate a new stream",
1690 QUIC_EV_CONN_PSTRM, qcc->conn);
1691 goto out;
1692 }
1693
1694 qcc->strms[qcs_type].largest_id = i;
1695 }
1696 if (qcs)
1697 strm_node = &qcs->by_id;
1698 }
1699 else {
1700 strm_node = eb64_lookup(strms, id);
1701 }
1702 }
1703
1704 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1705 return strm_node;
1706
1707 out:
1708 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1709 return NULL;
1710}
1711
1712/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1713 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1714 * to reflect the data which have been consumed.
1715 */
1716static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1717{
1718 size_t ret;
1719
1720 ret = 0;
1721 while (strm_frm->len) {
1722 size_t try;
1723
1724 try = b_contig_space(buf);
1725 if (!try)
1726 break;
1727
1728 if (try > strm_frm->len)
1729 try = strm_frm->len;
1730 memcpy(b_tail(buf), strm_frm->data, try);
1731 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001732 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001733 b_add(buf, try);
1734 ret += try;
1735 }
1736
1737 return ret;
1738}
1739
1740/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1741 * streams may be open. The data are copied to the stream RX buffer if possible.
1742 * If not, the STREAM frame is stored to be treated again later.
1743 * We rely on the flow control so that not to store too much STREAM frames.
1744 * Return 1 if succeeded, 0 if not.
1745 */
1746static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1747 struct quic_stream *strm_frm,
1748 struct quic_conn *qc)
1749{
1750 struct qcs *strm;
1751 struct eb64_node *strm_node, *frm_node;
1752 struct quic_rx_strm_frm *frm;
1753
1754 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1755 if (!strm_node) {
1756 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1757 return 0;
1758 }
1759
1760 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001761 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001762 /* FIXME: handle the case where this frame overlap others */
1763 if (frm_node) {
1764 TRACE_PROTO("Already existing stream data",
1765 QUIC_EV_CONN_PSTRM, qc->conn);
1766 goto out;
1767 }
1768
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001769 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001770 int ret;
1771
1772 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1773 goto store_frm;
1774
1775 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1776 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1777 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1778 return 0;
1779 }
1780
1781 strm->rx.offset += ret;
1782 }
1783
1784 if (!strm_frm->len)
1785 goto out;
1786
1787 store_frm:
1788 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1789 if (!frm) {
1790 TRACE_PROTO("Could not alloc RX STREAM frame",
1791 QUIC_EV_CONN_PSTRM, qc->conn);
1792 return 0;
1793 }
1794
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001795 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001796 quic_rx_packet_refinc(pkt);
1797
1798 out:
1799 return 1;
1800}
1801
1802/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1803 * streams may be open. The data are copied to the stream RX buffer if possible.
1804 * If not, the STREAM frame is stored to be treated again later.
1805 * We rely on the flow control so that not to store too much STREAM frames.
1806 * Return 1 if succeeded, 0 if not.
1807 */
1808static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1809 struct quic_stream *strm_frm,
1810 struct quic_conn *qc)
1811{
1812 struct qcs *strm;
1813 struct eb64_node *strm_node, *frm_node;
1814 struct quic_rx_strm_frm *frm;
1815 size_t strm_frm_len;
1816
1817 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1818 if (!strm_node) {
1819 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1820 return 0;
1821 }
1822
1823 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001824 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001825 /* FIXME: handle the case where this frame overlap others */
1826 if (frm_node) {
1827 TRACE_PROTO("Already existing stream data",
1828 QUIC_EV_CONN_PSTRM, qc->conn);
1829 goto out;
1830 }
1831
1832 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001833 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001834 int ret;
1835
1836 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1837 goto store_frm;
1838
1839 /* qc_strm_cpy() will modify the offset, depending on the number
1840 * of bytes copied.
1841 */
1842 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1843 /* Inform the application of the arrival of this new stream */
1844 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1845 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1846 return 0;
1847 }
1848
1849 if (ret)
1850 ruqs_notify_recv(strm);
1851
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001852 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001853 }
1854 /* Take this frame into an account for the stream flow control */
1855 strm->rx.offset += strm_frm_len;
1856 /* It all the data were provided to the application, there is no need to
1857 * store any more inforamtion for it.
1858 */
1859 if (!strm_frm->len)
1860 goto out;
1861
1862 store_frm:
1863 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1864 if (!frm) {
1865 TRACE_PROTO("Could not alloc RX STREAM frame",
1866 QUIC_EV_CONN_PSTRM, qc->conn);
1867 return 0;
1868 }
1869
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001870 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001871 quic_rx_packet_refinc(pkt);
1872
1873 out:
1874 return 1;
1875}
1876
1877static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1878 struct quic_stream *strm_frm,
1879 struct quic_conn *qc)
1880{
1881 if (strm_frm->id & QCS_ID_DIR_BIT)
1882 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1883 else
1884 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1885}
1886
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001887/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1888 * as I/O handler context and <qel> as encryption level.
1889 * Returns 1 if succeeded, 0 if failed.
1890 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001891static 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 +01001892 struct quic_enc_level *qel)
1893{
1894 struct quic_frame frm;
1895 const unsigned char *pos, *end;
1896 struct quic_conn *conn = ctx->conn->qc;
1897
1898 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1899 /* Skip the AAD */
1900 pos = pkt->data + pkt->aad_len;
1901 end = pkt->data + pkt->len;
1902
1903 while (pos < end) {
1904 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1905 goto err;
1906
1907 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001908 case QUIC_FT_PADDING:
1909 if (pos != end) {
1910 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1911 goto err;
1912 }
1913 break;
1914 case QUIC_FT_PING:
1915 break;
1916 case QUIC_FT_ACK:
1917 {
1918 unsigned int rtt_sample;
1919
1920 rtt_sample = 0;
1921 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1922 goto err;
1923
1924 if (rtt_sample) {
1925 unsigned int ack_delay;
1926
1927 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1928 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1929 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1930 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001931 break;
1932 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001933 case QUIC_FT_CRYPTO:
1934 if (frm.crypto.offset != qel->rx.crypto.offset) {
1935 struct quic_rx_crypto_frm *cf;
1936
1937 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1938 if (!cf) {
1939 TRACE_DEVEL("CRYPTO frame allocation failed",
1940 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1941 goto err;
1942 }
1943
1944 cf->offset_node.key = frm.crypto.offset;
1945 cf->len = frm.crypto.len;
1946 cf->data = frm.crypto.data;
1947 cf->pkt = pkt;
1948 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1949 quic_rx_packet_refinc(pkt);
1950 }
1951 else {
1952 /* XXX TO DO: <cf> is used only for the traces. */
1953 struct quic_rx_crypto_frm cf = { };
1954
1955 cf.offset_node.key = frm.crypto.offset;
1956 cf.len = frm.crypto.len;
1957 if (!qc_provide_cdata(qel, ctx,
1958 frm.crypto.data, frm.crypto.len,
1959 pkt, &cf))
1960 goto err;
1961 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001962 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001963 case QUIC_FT_STREAM_8:
1964 case QUIC_FT_STREAM_9:
1965 case QUIC_FT_STREAM_A:
1966 case QUIC_FT_STREAM_B:
1967 case QUIC_FT_STREAM_C:
1968 case QUIC_FT_STREAM_D:
1969 case QUIC_FT_STREAM_E:
1970 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001971 {
1972 struct quic_stream *stream = &frm.stream;
1973
1974 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1975 if (objt_listener(ctx->conn->target)) {
1976 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1977 goto err;
1978 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1979 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001980
1981 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1982 goto err;
1983
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001984 break;
1985 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001986 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001987 break;
1988 case QUIC_FT_CONNECTION_CLOSE:
1989 case QUIC_FT_CONNECTION_CLOSE_APP:
1990 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001991 case QUIC_FT_HANDSHAKE_DONE:
1992 if (objt_listener(ctx->conn->target))
1993 goto err;
1994
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001995 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001996 break;
1997 default:
1998 goto err;
1999 }
2000 }
2001
2002 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2003 * has successfully parse a Handshake packet. The Initial encryption must also
2004 * be discarded.
2005 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002006 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
2007 int state = HA_ATOMIC_LOAD(&conn->state);
2008
2009 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
2010 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2011 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2012 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2013 qc_set_timer(ctx);
2014 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
2015 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
2016 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002017 }
2018
2019 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2020 return 1;
2021
2022 err:
2023 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2024 return 0;
2025}
2026
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002027/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
2028 * buffer. This is the responsability of the caller to check there is enough
2029 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2030 * This function must be called only after having built a correct datagram.
2031 * Always succeeds.
2032 */
2033static inline void qc_set_dg(struct cbuf *cbuf,
2034 uint16_t dglen, struct quic_tx_packet *pkt)
2035{
2036 write_u16(cb_wr(cbuf), dglen);
2037 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2038 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2039}
2040
2041/* Prepare as much as possible handshake packets into <qr> ring buffer for
2042 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2043 * several packets in the same datagram. A header made of two fields is added
2044 * to each datagram: the datagram length followed by the address of the first
2045 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002046 * Returns 1 if succeeded, or 0 if something wrong happened.
2047 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002048static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002049{
2050 struct quic_conn *qc;
2051 enum quic_tls_enc_level tel, next_tel;
2052 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002053 struct cbuf *cbuf;
2054 unsigned char *end_buf, *end, *pos, *spos;
2055 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2056 /* length of datagrams */
2057 uint16_t dglen;
2058 size_t total;
2059 /* Each datagram is prepended with its length followed by the
2060 * address of the first packet in the datagram.
2061 */
2062 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002063
2064 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2065 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002066 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002067 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002068 goto err;
2069 }
2070
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002071 start:
2072 total = 0;
2073 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002074 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002075 cbuf = qr->cbuf;
2076 spos = pos = cb_wr(cbuf);
2077 /* Leave at least <dglen> bytes at the end of this buffer
2078 * to ensure there is enough room to mark the end of prepared
2079 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002080 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002081 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2082 first_pkt = prv_pkt = NULL;
2083 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002084 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002085 enum quic_pkt_type pkt_type;
2086
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002087 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002088 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002089 if (!prv_pkt) {
2090 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2091 do {
2092 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2093 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2094 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002095 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002096 /* Do not build any more packet if the TX secrets are not available or
2097 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002098 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002099 * and if there is no more CRYPTO data available or in flight
2100 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002101 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002102 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002103 (!ack && !nb_ptos &&
2104 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2105 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002106 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002107 /* Set the current datagram as prepared into <cbuf> if
2108 * the was already a correct packet which was previously written.
2109 */
2110 if (prv_pkt)
2111 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002112 break;
2113 }
2114
2115 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002116 if (!prv_pkt) {
2117 /* Leave room for the datagram header */
2118 pos += dg_headlen;
2119 end = pos + qc->path->mtu;
2120 }
2121
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002122 cur_pkt = qc_build_pkt(&pos, end, qel, qc, pkt_type, ack, nb_ptos, &err);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002123 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002124 if (err < 0) {
2125 if (!prv_pkt && nb_ptos)
2126 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002127 if (ack)
2128 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002129 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002130 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002131 case -2:
2132 goto err;
2133 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002134 /* If there was already a correct packet present, set the
2135 * current datagram as prepared into <cbuf>.
2136 */
2137 if (prv_pkt) {
2138 qc_set_dg(cbuf, dglen, first_pkt);
2139 goto stop_build;
2140 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002141 goto out;
2142 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002143 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2144 if (!cur_pkt)
2145 goto err;
2146
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002147 total += cur_pkt->len;
2148 /* keep trace of the first packet in the datagram */
2149 if (!first_pkt)
2150 first_pkt = cur_pkt;
2151 /* Attach the current one to the previous one */
2152 if (prv_pkt)
2153 prv_pkt->next = cur_pkt;
2154 /* Let's say we have to build a new dgram */
2155 prv_pkt = NULL;
2156 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002157 /* Discard the Initial encryption keys as soon as
2158 * a handshake packet could be built.
2159 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002160 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002161 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2162 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002163 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002164 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2165 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002166 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002167 }
2168 /* Special case for Initial packets: when they have all
2169 * been sent, select the next level.
2170 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002171 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002172 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2173 (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 +02002174 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2175 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2176 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002177 tel = next_tel;
2178 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002179 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002180 /* If there is data for the next level, do not
2181 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002182 * which sends only one Initial packet, then wait
2183 * for additional CRYPTO data from the server to enter the
2184 * next level.
2185 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002186 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002187 }
2188 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002189 }
2190
2191 /* If we have to build a new datagram, set the current datagram as
2192 * prepared into <cbuf>.
2193 */
2194 if (!prv_pkt) {
2195 qc_set_dg(cbuf, dglen, first_pkt);
2196 first_pkt = NULL;
2197 dglen = 0;
2198 }
2199 }
2200
2201 stop_build:
2202 /* Reset <wr> writer index if in front of <rd> index */
2203 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2204 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2205
2206 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2207 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2208 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2209 /* Mark the end of contiguous data for the reader */
2210 write_u16(cb_wr(cbuf), 0);
2211 cb_add(cbuf, sizeof(uint16_t));
2212 }
2213 }
2214
2215 if (rd && rd <= cbuf->wr) {
2216 cb_wr_reset(cbuf);
2217 if (pos == spos) {
2218 /* Reuse the same buffer if nothing was built. */
2219 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002220 }
2221 }
2222 }
2223
2224 out:
2225 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002226 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002227
2228 err:
2229 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002230 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002231}
2232
2233/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002234 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002235 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002236int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002237{
2238 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002239 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002240
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002241 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002242 cbuf = qr->cbuf;
2243 while (cb_contig_data(cbuf)) {
2244 unsigned char *pos;
2245 struct buffer tmpbuf = { };
2246 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2247 uint16_t dglen;
2248 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 unsigned int time_sent;
2250
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002251 pos = cb_rd(cbuf);
2252 dglen = read_u16(pos);
2253 /* End of prepared datagrams.
2254 * Reset the reader index only if in front of the writer index.
2255 */
2256 if (!dglen) {
2257 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2258
2259 if (wr && wr < cbuf->rd) {
2260 cb_rd_reset(cbuf);
2261 continue;
2262 }
2263 break;
2264 }
2265
2266 pos += sizeof dglen;
2267 first_pkt = read_ptr(pos);
2268 pos += sizeof first_pkt;
2269 tmpbuf.area = (char *)pos;
2270 tmpbuf.size = tmpbuf.data = dglen;
2271
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002272 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002273 for (pkt = first_pkt; pkt; pkt = pkt->next)
2274 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002275 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002276 &tmpbuf, tmpbuf.data, 0) <= 0) {
2277 for (pkt = first_pkt; pkt; pkt = pkt->next)
2278 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002279 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002280 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002281
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002282 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002283 qc->tx.bytes += tmpbuf.data;
2284 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002285
2286 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2287 pkt->time_sent = time_sent;
2288 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2289 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002290 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002291 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002292 qc->path->in_flight += pkt->in_flight_len;
2293 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2294 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002295 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002296 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2297 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002298 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002299 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002300 }
2301 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002302
2303 return 1;
2304}
2305
2306/* Build all the frames which must be sent just after the handshake have succeeded.
2307 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2308 * a HANDSHAKE_DONE frame.
2309 * Return 1 if succeeded, 0 if not.
2310 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002311static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002312{
2313 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002314 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002315 struct quic_frame *frm;
2316
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002317 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002319 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002320 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002321 if (!frm)
2322 return 0;
2323
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002324 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002325 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002326 }
2327
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002328 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002329 struct quic_connection_id *cid;
2330
2331 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002332 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002333 if (!frm || !cid)
2334 goto err;
2335
2336 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002337 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002338 }
2339
2340 return 1;
2341
2342 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002343 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002344 return 0;
2345}
2346
2347/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002348void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002350 struct eb64_node *n;
2351 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002352
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002353 n = eb64_first(&arngs->root);
2354 while (n) {
2355 struct eb64_node *next;
2356
2357 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2358 next = eb64_next(n);
2359 eb64_delete(n);
2360 free(ar);
2361 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002362 }
2363}
2364
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002365/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2366 * descending order.
2367 */
2368static inline size_t sack_gap(struct quic_arng_node *p,
2369 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002370{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002371 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002372}
2373
2374
2375/* Remove the last elements of <ack_ranges> list of ack range updating its
2376 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002377 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002378 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002379static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002380{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002381 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002382
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002383 last = eb64_last(&arngs->root);
2384 while (last && arngs->enc_sz > limit) {
2385 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002386
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002387 prev = eb64_prev(last);
2388 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002389 return 0;
2390
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002391 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2392 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2393 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2394 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2395 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2396 --arngs->sz;
2397 eb64_delete(last);
2398 pool_free(pool_head_quic_arng, last);
2399 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002400 }
2401
2402 return 1;
2403}
2404
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002405/* Set the encoded size of <arngs> QUIC ack ranges. */
2406static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2407{
2408 struct eb64_node *node, *next;
2409 struct quic_arng_node *ar, *ar_next;
2410
2411 node = eb64_last(&arngs->root);
2412 if (!node)
2413 return;
2414
2415 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2416 arngs->enc_sz = quic_int_getsize(ar->last) +
2417 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2418
2419 while ((next = eb64_prev(node))) {
2420 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2421 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2422 quic_int_getsize(ar_next->last - ar_next->first.key);
2423 node = next;
2424 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2425 }
2426}
2427
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002428/* Insert <ar> ack range into <argns> tree of ack ranges.
2429 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002430 */
2431static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002432struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2433 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002434{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002435 struct quic_arng_node *new_ar;
2436
2437 new_ar = pool_alloc(pool_head_quic_arng);
2438 if (new_ar) {
2439 new_ar->first.key = ar->first;
2440 new_ar->last = ar->last;
2441 eb64_insert(&arngs->root, &new_ar->first);
2442 arngs->sz++;
2443 }
2444
2445 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002446}
2447
2448/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002449 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002450 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002451 *
2452 * Descending order
2453 * ------------->
2454 * range1 range2
2455 * ..........|--------|..............|--------|
2456 * ^ ^ ^ ^
2457 * | | | |
2458 * last1 first1 last2 first2
2459 * ..........+--------+--------------+--------+......
2460 * diff1 gap12 diff2
2461 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002462 * To encode the previous list of ranges we must encode integers as follows in
2463 * descending order:
2464 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002465 * with diff1 = last1 - first1
2466 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002467 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002469 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002470int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2471 struct quic_arng *ar)
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 *le;
2474 struct quic_arng_node *new_node;
2475 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002476
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002477 new = NULL;
2478 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002479 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002480 if (!new_node)
2481 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002482
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002483 goto out;
2484 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002485
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002486 le = eb64_lookup_le(&arngs->root, ar->first);
2487 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002488 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002489 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002491 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002492 else {
2493 struct quic_arng_node *le_ar =
2494 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002495
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002496 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002497 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002498 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002499
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002500 if (le_ar->last + 1 >= ar->first) {
2501 le_ar->last = ar->last;
2502 new = le;
2503 new_node = le_ar;
2504 }
2505 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002506 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002507 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002508 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002509
2510 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002511 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002512 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002513
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002514 /* Verify that the new inserted node does not overlap the nodes
2515 * which follow it.
2516 */
2517 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002518 struct eb64_node *next;
2519 struct quic_arng_node *next_node;
2520
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002521 while ((next = eb64_next(new))) {
2522 next_node =
2523 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002524 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002525 break;
2526
2527 if (next_node->last > new_node->last)
2528 new_node->last = next_node->last;
2529 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002530 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002531 /* Decrement the size of these ranges. */
2532 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 }
2535
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002536 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002537 quic_arngs_set_enc_sz(arngs);
2538
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002539 return 1;
2540}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002541/* Remove the header protection of packets at <el> encryption level.
2542 * Always succeeds.
2543 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002544static 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 +01002545{
2546 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002547 struct quic_rx_packet *pqpkt;
2548 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002549 struct quic_enc_level *app_qel;
2550
2551 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2552 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2553 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002554 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002555 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002556 TRACE_PROTO("hp not removed (handshake not completed)",
2557 QUIC_EV_CONN_ELRMHP, ctx->conn);
2558 goto out;
2559 }
2560 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002561 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2563 pqpkt->data + pqpkt->pn_offset,
2564 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2565 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2566 /* XXX TO DO XXX */
2567 }
2568 else {
2569 /* The AAD includes the packet number field */
2570 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2571 /* Store the packet into the tree of packets to decrypt. */
2572 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002573 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002574 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002575 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002576 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2577 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002578 MT_LIST_DELETE_SAFE(pkttmp1);
2579 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002580 }
2581
2582 out:
2583 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2584}
2585
2586/* Process all the CRYPTO frame at <el> encryption level.
2587 * Return 1 if succeeded, 0 if not.
2588 */
2589static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002590 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002591{
2592 struct eb64_node *node;
2593
2594 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002595 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002596 node = eb64_first(&el->rx.crypto.frms);
2597 while (node) {
2598 struct quic_rx_crypto_frm *cf;
2599
2600 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2601 if (cf->offset_node.key != el->rx.crypto.offset)
2602 break;
2603
2604 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2605 goto err;
2606
2607 node = eb64_next(node);
2608 quic_rx_packet_refdec(cf->pkt);
2609 eb64_delete(&cf->offset_node);
2610 pool_free(pool_head_quic_rx_crypto_frm, cf);
2611 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002612 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002613 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2614 return 1;
2615
2616 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002617 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002618 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2619 return 0;
2620}
2621
2622/* Process all the packets at <el> encryption level.
2623 * Return 1 if succeeded, 0 if not.
2624 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002625int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2626 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002627{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002628 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002629 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002630 struct quic_conn *qc = ctx->conn->qc;
2631 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002632
2633 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002634 qel = cur_el;
2635 next_tel:
2636 if (!qel)
2637 goto out;
2638
2639 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2640 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002641 while (node) {
2642 struct quic_rx_packet *pkt;
2643
2644 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002645 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2646 ctx->conn, pkt, NULL, ctx->ssl);
2647 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002648 /* Drop the packet */
2649 TRACE_PROTO("packet decryption failed -> dropped",
2650 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2651 }
2652 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002653 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002654 /* Drop the packet */
2655 TRACE_PROTO("packet parsing failed -> dropped",
2656 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2657 }
2658 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002659 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2660
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002661 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002662 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2663 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002664 if (pkt->pn > largest_pn)
2665 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002666 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002667 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 TRACE_DEVEL("Could not update ack range list",
2669 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002670 }
2671 }
2672 node = eb64_next(node);
2673 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002674 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002675 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002676
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002677 /* Update the largest packet number. */
2678 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002679 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2680 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002681 goto err;
2682
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002683 if (qel == cur_el) {
2684 qel = next_el;
2685 goto next_tel;
2686 }
2687
2688 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002689 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2690 return 1;
2691
2692 err:
2693 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2694 return 0;
2695}
2696
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002697/* QUIC connection packet handler task. */
2698struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002699{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002700 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002701 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002702 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002703 enum quic_tls_enc_level tel, next_tel;
2704 struct quic_enc_level *qel, *next_qel;
2705 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002706 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002707 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002709 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002710 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002711 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002712 st = HA_ATOMIC_LOAD(&qc->state);
2713 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002714 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002715 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002716 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002717 goto err;
2718
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002719 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002720 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002721
2722 next_level:
2723 tls_ctx = &qel->tls_ctx;
2724
2725 /* If the header protection key for this level has been derived,
2726 * remove the packet header protections.
2727 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002728 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002729 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2730 qc_rm_hp_pkts(qel, ctx);
2731
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002732 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002733 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2734 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2735 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002736 goto err;
2737
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002738 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002739 if (st >= QUIC_HS_ST_COMPLETE &&
2740 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002741 /* Discard the Handshake keys. */
2742 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002743 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002744 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2745 qc_set_timer(ctx);
2746 if (!quic_build_post_handshake_frames(qc))
2747 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002748 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002749 }
2750
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002751 if (!qr)
2752 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2753 ret = qc_prep_hdshk_pkts(qr, ctx);
2754 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002755 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002756 else if (ret == 0)
2757 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002759 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 goto err;
2761
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002762 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002763 /* Check if there is something to do for the next level.
2764 */
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002765 if (next_qel && (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002766 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767 qel = next_qel;
2768 goto next_level;
2769 }
2770
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002771 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002772 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002773 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002774
2775 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002776 if (qr)
2777 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002778 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002779 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780}
2781
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002782/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2784{
2785 int i;
2786
2787 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2788 if (qel->tx.crypto.bufs[i]) {
2789 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2790 qel->tx.crypto.bufs[i] = NULL;
2791 }
2792 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002793 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794}
2795
2796/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002797 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 * Returns 1 if succeeded, 0 if not.
2799 */
2800static int quic_conn_enc_level_init(struct quic_conn *qc,
2801 enum quic_tls_enc_level level)
2802{
2803 struct quic_enc_level *qel;
2804
2805 qel = &qc->els[level];
2806 qel->level = quic_to_ssl_enc_level(level);
2807 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2808 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2809 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2810 qel->tls_ctx.rx.flags = 0;
2811 qel->tls_ctx.tx.flags = 0;
2812
2813 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002814 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002815 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002816 qel->rx.crypto.offset = 0;
2817 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2818 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002819
2820 /* Allocate only one buffer. */
2821 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2822 if (!qel->tx.crypto.bufs)
2823 goto err;
2824
2825 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2826 if (!qel->tx.crypto.bufs[0])
2827 goto err;
2828
2829 qel->tx.crypto.bufs[0]->sz = 0;
2830 qel->tx.crypto.nb_buf = 1;
2831
2832 qel->tx.crypto.sz = 0;
2833 qel->tx.crypto.offset = 0;
2834
2835 return 1;
2836
2837 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002838 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002839 return 0;
2840}
2841
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002842/* Release all the memory allocated for <conn> QUIC connection. */
2843static void quic_conn_free(struct quic_conn *conn)
2844{
2845 int i;
2846
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002847 if (!conn)
2848 return;
2849
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002850 free_quic_conn_cids(conn);
2851 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2852 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002853 if (conn->timer_task)
2854 task_destroy(conn->timer_task);
2855 pool_free(pool_head_quic_conn, conn);
2856}
2857
2858/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002859static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002860{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002861 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002862 struct quic_conn *qc;
2863 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002864 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865
2866 conn_ctx = task->context;
2867 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002868 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2869 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002870 task->expire = TICK_ETERNITY;
2871 pktns = quic_loss_pktns(qc);
2872 if (tick_isset(pktns->tx.loss_time)) {
2873 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2874
2875 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2876 if (!LIST_ISEMPTY(&lost_pkts))
2877 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2878 qc_set_timer(conn_ctx);
2879 goto out;
2880 }
2881
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002882 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002884 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002885 pktns->tx.pto_probe = 1;
2886 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002887 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2889 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2890
2891 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2892 hel->pktns->tx.pto_probe = 1;
2893 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2894 iel->pktns->tx.pto_probe = 1;
2895 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002896 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002897 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2898 qc->path->loss.pto_count++;
2899
2900 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002901 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002902
2903 return task;
2904}
2905
2906/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2907 * connections used to identify the first Initial packets of client connecting
2908 * to listeners. This parameter must be NULL for QUIC connections attached
2909 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2910 * <scid> is the source connection ID with <scid_len> as length.
2911 * Returns 1 if succeeded, 0 if not.
2912 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002913static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2914 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002915 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002916{
2917 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002918 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002919 /* Initial CID. */
2920 struct quic_connection_id *icid;
2921
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002922 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002923 qc = pool_zalloc(pool_head_quic_conn);
2924 if (!qc) {
2925 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2926 goto err;
2927 }
2928
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002929 qc->cids = EB_ROOT;
2930 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002931 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002932 struct listener *l = owner;
2933
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002934 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002935 /* Copy the initial DCID. */
2936 qc->odcid.len = dcid_len;
2937 if (qc->odcid.len)
2938 memcpy(qc->odcid.data, dcid, dcid_len);
2939
2940 /* Copy the SCID as our DCID for this connection. */
2941 if (scid_len)
2942 memcpy(qc->dcid.data, scid, scid_len);
2943 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002944 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002945 }
2946 /* QUIC Client (outgoing connection to servers) */
2947 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002948 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002949 if (dcid_len)
2950 memcpy(qc->dcid.data, dcid, dcid_len);
2951 qc->dcid.len = dcid_len;
2952 }
2953
2954 /* Initialize the output buffer */
2955 qc->obuf.pos = qc->obuf.data;
2956
2957 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002958 if (!icid) {
2959 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2960 goto err;
2961 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002962
2963 /* Select our SCID which is the first CID with 0 as sequence number. */
2964 qc->scid = icid->cid;
2965
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002966 /* Packet number spaces initialization. */
2967 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2968 quic_pktns_init(&qc->pktns[i]);
2969 /* QUIC encryption level context initialization. */
2970 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002971 if (!quic_conn_enc_level_init(qc, i)) {
2972 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002974 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002975 /* Initialize the packet number space. */
2976 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2977 }
2978
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002979 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 /* TX part. */
2981 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002982 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2983 qc->tx.wbuf = qc->tx.rbuf = 0;
2984 qc->tx.bytes = 0;
2985 qc->tx.nb_pto_dgrams = 0;
2986 /* RX part. */
2987 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002988 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002990 /* XXX TO DO: Only one path at this time. */
2991 qc->path = &qc->paths[0];
2992 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2993
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002994 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002996 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002997
2998 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002999 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003000 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003001 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003002}
3003
3004/* Initialize the timer task of <qc> QUIC connection.
3005 * Returns 1 if succeeded, 0 if not.
3006 */
3007static int quic_conn_init_timer(struct quic_conn *qc)
3008{
3009 qc->timer_task = task_new(MAX_THREADS_MASK);
3010 if (!qc->timer_task)
3011 return 0;
3012
3013 qc->timer = TICK_ETERNITY;
3014 qc->timer_task->process = process_timer;
3015 qc->timer_task->context = qc->conn->xprt_ctx;
3016
3017 return 1;
3018}
3019
3020/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3021 * past one byte of this buffer.
3022 */
3023static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3024 struct quic_rx_packet *pkt)
3025{
3026 unsigned char dcid_len, scid_len;
3027
3028 /* Version */
3029 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3030 return 0;
3031
3032 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
3033
3034 /* Destination Connection ID Length */
3035 dcid_len = *(*buf)++;
3036 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3037 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3038 /* XXX MUST BE DROPPED */
3039 return 0;
3040
3041 if (dcid_len) {
3042 /* Check that the length of this received DCID matches the CID lengths
3043 * of our implementation for non Initials packets only.
3044 */
3045 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3046 return 0;
3047
3048 memcpy(pkt->dcid.data, *buf, dcid_len);
3049 }
3050
3051 pkt->dcid.len = dcid_len;
3052 *buf += dcid_len;
3053
3054 /* Source Connection ID Length */
3055 scid_len = *(*buf)++;
3056 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3057 /* XXX MUST BE DROPPED */
3058 return 0;
3059
3060 if (scid_len)
3061 memcpy(pkt->scid.data, *buf, scid_len);
3062 pkt->scid.len = scid_len;
3063 *buf += scid_len;
3064
3065 return 1;
3066}
3067
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003068/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3069 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3070 * encryption level matching with the packet type. <*qel> may be null if not found.
3071 * Note that <ctx> may be null (for Initial packets).
3072 */
3073static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003074 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003075 struct quic_enc_level **qel)
3076{
3077 enum quic_tls_enc_level tel;
3078
3079 /* Special case without connection context (firt Initial packets) */
3080 if (!ctx) {
3081 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3082 return 1;
3083 }
3084
3085 tel = quic_packet_type_enc_level(pkt->type);
3086 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3087 *qel = NULL;
3088 return 0;
3089 }
3090
3091 *qel = &qc->els[tel];
3092 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3093 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3094 return 0;
3095 }
3096
3097 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003098 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3099 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003100 return 1;
3101
3102 return 0;
3103}
3104
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3106 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3107 * byte past the end of the buffer containing this packet and <beg> the address of
3108 * the packet first byte.
3109 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3110 * Returns 1 if succeeded, 0 if not.
3111 */
3112static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3113 unsigned char **buf, unsigned char *beg,
3114 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003115 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003116{
3117 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003118 struct quic_enc_level *qel;
3119 /* Only for traces. */
3120 struct quic_rx_packet *qpkt_trace;
3121
3122 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003123 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003124 /* The packet number is here. This is also the start minus
3125 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3126 * protection.
3127 */
3128 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003129 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130 /* Note that the following function enables us to unprotect the packet
3131 * number and its length subsequently used to decrypt the entire
3132 * packets.
3133 */
3134 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3135 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003136 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003137 goto err;
3138 }
3139
3140 /* The AAD includes the packet number field found at <pn>. */
3141 pkt->aad_len = pn - beg + pkt->pnl;
3142 qpkt_trace = pkt;
3143 /* Store the packet */
3144 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003145 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003147 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003148 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003149 else if (qel) {
3150 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003151 pkt->pn_offset = pn - beg;
3152 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3153 }
3154
3155 memcpy(pkt->data, beg, pkt->len);
3156 /* Updtate the offset of <*buf> for the next QUIC packet. */
3157 *buf = beg + pkt->len;
3158
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003159 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003160 return 1;
3161
3162 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003163 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 +01003164 return 0;
3165}
3166
3167/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3168 * Also set <*long_header> to 1 if this form is long, 0 if not.
3169 */
3170static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3171 unsigned char byte0, int *long_header)
3172{
3173 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3174 pkt->type =
3175 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3176 *long_header = 1;
3177 }
3178 else {
3179 pkt->type = QUIC_PACKET_TYPE_SHORT;
3180 *long_header = 0;
3181 }
3182}
3183
3184static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3185 struct quic_rx_packet *pkt,
3186 struct quic_dgram_ctx *dgram_ctx,
3187 struct sockaddr_storage *saddr)
3188{
3189 unsigned char *beg;
3190 uint64_t len;
3191 struct quic_conn *qc;
3192 struct eb_root *cids;
3193 struct ebmb_node *node;
3194 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003195 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003196 int long_header;
3197
3198 qc = NULL;
3199 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3200 if (end <= *buf)
3201 goto err;
3202
3203 /* Fixed bit */
3204 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3205 /* XXX TO BE DISCARDED */
3206 goto err;
3207
3208 srv_conn = dgram_ctx->owner;
3209 beg = *buf;
3210 /* Header form */
3211 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3212 if (long_header) {
3213 size_t cid_lookup_len;
3214
3215 if (!quic_packet_read_long_header(buf, end, pkt))
3216 goto err;
3217
3218 /* For Initial packets, and for servers (QUIC clients connections),
3219 * there is no Initial connection IDs storage.
3220 */
3221 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3222 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3223 cid_lookup_len = pkt->dcid.len;
3224 }
3225 else {
3226 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3227 cid_lookup_len = QUIC_CID_LEN;
3228 }
3229
3230 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3231 if (!node)
3232 goto err;
3233
3234 qc = ebmb_entry(node, struct quic_conn, scid_node);
3235
3236 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3237 qc->dcid.len = pkt->scid.len;
3238 if (pkt->scid.len)
3239 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3240 }
3241
3242 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3243 uint64_t token_len;
3244
3245 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3246 goto err;
3247
3248 /* XXX TO DO XXX 0 value means "the token is not present".
3249 * A server which sends an Initial packet must not set the token.
3250 * So, a client which receives an Initial packet with a token
3251 * MUST discard the packet or generate a connection error with
3252 * PROTOCOL_VIOLATION as type.
3253 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3254 */
3255 pkt->token_len = token_len;
3256 }
3257 }
3258 else {
3259 /* XXX TO DO: Short header XXX */
3260 if (end - *buf < QUIC_CID_LEN)
3261 goto err;
3262
3263 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3264 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3265 if (!node)
3266 goto err;
3267
3268 qc = ebmb_entry(node, struct quic_conn, scid_node);
3269 *buf += QUIC_CID_LEN;
3270 }
3271 /* Store the DCID used for this packet to check the packet which
3272 * come in this UDP datagram match with it.
3273 */
3274 if (!dgram_ctx->dcid_node)
3275 dgram_ctx->dcid_node = node;
3276 /* Only packets packets with long headers and not RETRY or VERSION as type
3277 * have a length field.
3278 */
3279 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3280 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3281 goto err;
3282
3283 pkt->len = len;
3284 }
3285 else if (!long_header) {
3286 /* A short packet is the last one of an UDP datagram. */
3287 pkt->len = end - *buf;
3288 }
3289
3290 conn_ctx = qc->conn->xprt_ctx;
3291
3292 /* Increase the total length of this packet by the header length. */
3293 pkt->len += *buf - beg;
3294 /* Do not check the DCID node before the length. */
3295 if (dgram_ctx->dcid_node != node) {
3296 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3297 goto err;
3298 }
3299
3300 if (pkt->len > sizeof pkt->data) {
3301 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3302 goto err;
3303 }
3304
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003305 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003306 goto err;
3307
3308 /* Wake the tasklet of the QUIC connection packet handler. */
3309 if (conn_ctx)
3310 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3311
3312 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3313
3314 return pkt->len;
3315
3316 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003317 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003318 return -1;
3319}
3320
3321static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3322 struct quic_rx_packet *pkt,
3323 struct quic_dgram_ctx *dgram_ctx,
3324 struct sockaddr_storage *saddr)
3325{
3326 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003327 struct quic_conn *qc;
3328 struct eb_root *cids;
3329 struct ebmb_node *node;
3330 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003331 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003332 int long_header = 0;
3333
3334 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003335 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003336 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003337 if (end <= *buf)
3338 goto err;
3339
3340 /* Fixed bit */
3341 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3342 /* XXX TO BE DISCARDED */
3343 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3344 goto err;
3345 }
3346
3347 l = dgram_ctx->owner;
3348 beg = *buf;
3349 /* Header form */
3350 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3351 if (long_header) {
3352 unsigned char dcid_len;
3353
3354 if (!quic_packet_read_long_header(buf, end, pkt)) {
3355 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3356 goto err;
3357 }
3358
3359 dcid_len = pkt->dcid.len;
3360 /* For Initial packets, and for servers (QUIC clients connections),
3361 * there is no Initial connection IDs storage.
3362 */
3363 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003364 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003365 /* DCIDs of first packets coming from clients may have the same values.
3366 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3367 */
3368 quic_cid_saddr_cat(&pkt->dcid, saddr);
3369 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003370
3371 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3372 end - *buf < token_len) {
3373 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3374 goto err;
3375 }
3376
3377 /* XXX TO DO XXX 0 value means "the token is not present".
3378 * A server which sends an Initial packet must not set the token.
3379 * So, a client which receives an Initial packet with a token
3380 * MUST discard the packet or generate a connection error with
3381 * PROTOCOL_VIOLATION as type.
3382 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3383 */
3384 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003385 }
3386 else {
3387 if (pkt->dcid.len != QUIC_CID_LEN) {
3388 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3389 goto err;
3390 }
3391
3392 cids = &l->rx.cids;
3393 }
3394
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003395 /* Only packets packets with long headers and not RETRY or VERSION as type
3396 * have a length field.
3397 */
3398 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3399 uint64_t len;
3400
3401 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3402 end - *buf < len) {
3403 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3404 goto err;
3405 }
3406
3407 pkt->len = len;
3408 }
3409
3410
3411 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3413 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3414 cids == &l->rx.odcids) {
3415 /* Switch to the definitive tree ->cids containing the final CIDs. */
3416 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3417 if (node) {
3418 /* If found, signal this with NULL as special value for <cids>. */
3419 pkt->dcid.len = dcid_len;
3420 cids = NULL;
3421 }
3422 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003423 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003424
3425 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003426 int ipv4;
3427 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003428 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003429 const unsigned char *salt = initial_salt_v1;
3430 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003431
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003432 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3433 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3434 goto err;
3435 }
3436
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003437 pkt->saddr = *saddr;
3438 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3439 * of <saddr>.
3440 */
3441 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003442 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003443 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003444 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003445 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003446 goto err;
3447
3448 odcid = &qc->rx.params.original_destination_connection_id;
3449 /* Copy the transport parameters. */
3450 qc->rx.params = l->bind_conf->quic_params;
3451 /* Copy original_destination_connection_id transport parameter. */
3452 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3453 odcid->len = pkt->odcid_len;
3454 /* Copy the initial source connection ID. */
3455 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3456 qc->enc_params_len =
3457 quic_transport_params_encode(qc->enc_params,
3458 qc->enc_params + sizeof qc->enc_params,
3459 &qc->rx.params, 1);
3460 if (!qc->enc_params_len)
3461 goto err;
3462
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003463 /* NOTE: the socket address has been concatenated to the destination ID
3464 * chosen by the client for Initial packets.
3465 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003466 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3467 salt = initial_salt_draft_29;
3468 salt_len = sizeof initial_salt_draft_29;
3469 }
3470 if (!qc_new_isecs(qc, salt, salt_len,
3471 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003472 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3473 goto err;
3474 }
3475
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003476 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003477 /* This is the DCID node sent in this packet by the client. */
3478 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003479 /* Enqueue this packet. */
3480 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3481 /* Try to accept a new connection. */
3482 listener_accept(l);
3483
3484 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3485 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3486 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3487 /* Insert our SCID, the connection ID for the QUIC client. */
3488 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3489 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3490 if (n != &qc->scid_node) {
3491 quic_conn_free(qc);
3492 qc = ebmb_entry(n, struct quic_conn, scid_node);
3493 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003494 }
3495 else {
3496 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3497 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3498 else
3499 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003500 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003501 }
3502 }
3503 else {
3504 if (end - *buf < QUIC_CID_LEN) {
3505 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3506 goto err;
3507 }
3508
3509 cids = &l->rx.cids;
3510 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3511 if (!node) {
3512 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3513 goto err;
3514 }
3515
3516 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003517 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003518 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003519 /* A short packet is the last one of an UDP datagram. */
3520 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 }
3522
3523 /* Store the DCID used for this packet to check the packet which
3524 * come in this UDP datagram match with it.
3525 */
3526 if (!dgram_ctx->dcid_node) {
3527 dgram_ctx->dcid_node = node;
3528 dgram_ctx->qc = qc;
3529 }
3530
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003531 /* Increase the total length of this packet by the header length. */
3532 pkt->len += *buf - beg;
3533 /* Do not check the DCID node before the length. */
3534 if (dgram_ctx->dcid_node != node) {
3535 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3536 goto err;
3537 }
3538
3539 if (pkt->len > sizeof pkt->data) {
3540 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3541 goto err;
3542 }
3543
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003544 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3545 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3546 goto err;
3547 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003549
3550 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003551 /* Wake up the connection packet handler task from here only if all
3552 * the contexts have been initialized, especially the mux context
3553 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3554 * will start it if these contexts for the connection are not already
3555 * initialized.
3556 */
3557 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003559
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3561
3562 return pkt->len;
3563
3564 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003565 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003566 qc ? qc->conn : NULL, pkt);
3567 return -1;
3568}
3569
3570/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3571 * in advance. This is the reponsability of the caller to check there is enough room in this
3572 * buffer to build a long header.
3573 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3574 */
3575static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3576 int type, size_t pn_len, struct quic_conn *conn)
3577{
3578 if (type > QUIC_PACKET_TYPE_RETRY)
3579 return 0;
3580
3581 /* #0 byte flags */
3582 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3583 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3584 /* Version */
3585 quic_write_uint32(buf, end, conn->version);
3586 *(*buf)++ = conn->dcid.len;
3587 /* Destination connection ID */
3588 if (conn->dcid.len) {
3589 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3590 *buf += conn->dcid.len;
3591 }
3592 /* Source connection ID */
3593 *(*buf)++ = conn->scid.len;
3594 if (conn->scid.len) {
3595 memcpy(*buf, conn->scid.data, conn->scid.len);
3596 *buf += conn->scid.len;
3597 }
3598
3599 return 1;
3600}
3601
3602/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3603 * in advance. This is the reponsability of the caller to check there is enough room in this
3604 * buffer to build a long header.
3605 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3606 */
3607static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3608 size_t pn_len, struct quic_conn *conn)
3609{
3610 /* #0 byte flags */
3611 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3612 /* Destination connection ID */
3613 if (conn->dcid.len) {
3614 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3615 *buf += conn->dcid.len;
3616 }
3617
3618 return 1;
3619}
3620
3621/* Apply QUIC header protection to the packet with <buf> as first byte address,
3622 * <pn> as address of the Packet number field, <pnlen> being this field length
3623 * with <aead> as AEAD cipher and <key> as secret key.
3624 * Returns 1 if succeeded or 0 if failed.
3625 */
3626static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3627 const EVP_CIPHER *aead, const unsigned char *key)
3628{
3629 int i, ret, outlen;
3630 EVP_CIPHER_CTX *ctx;
3631 /* We need an IV of at least 5 bytes: one byte for bytes #0
3632 * and at most 4 bytes for the packet number
3633 */
3634 unsigned char mask[5] = {0};
3635
3636 ret = 0;
3637 ctx = EVP_CIPHER_CTX_new();
3638 if (!ctx)
3639 return 0;
3640
3641 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3642 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3643 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3644 goto out;
3645
3646 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3647 for (i = 0; i < pnlen; i++)
3648 pn[i] ^= mask[i + 1];
3649
3650 ret = 1;
3651
3652 out:
3653 EVP_CIPHER_CTX_free(ctx);
3654
3655 return ret;
3656}
3657
3658/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3659 * ACK ranges if needed to a value below <limit> in bytes.
3660 * Return 1 if succeeded, 0 if not.
3661 */
3662static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3663{
3664 size_t room, ack_delay_sz;
3665
3666 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3667 /* A frame is made of 1 byte for the frame type. */
3668 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003669 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003670 return 0;
3671
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003672 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003673}
3674
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003675/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3676 * 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 +01003677 * and <*len> the packet Length field initialized with the number of bytes already present
3678 * 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 +02003679 * <headlen> is the number of bytes already present in this packet before building frames.
3680 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003681 * Update consequently <*len> to reflect the size of these frames built
3682 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003683 * Return 1 if succeeded, 0 if not.
3684 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003685static inline int qc_build_frms(struct quic_tx_packet *pkt,
3686 size_t room, size_t *len, size_t headlen,
3687 struct quic_enc_level *qel,
3688 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003689{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003690 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003691 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003692 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003693 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003694
Frédéric Lécailleea604992020-12-24 13:01:37 +01003695 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003696 if (*len > room || headlen > remain)
3697 return 0;
3698
Frédéric Lécailleea604992020-12-24 13:01:37 +01003699 /* If we are not probing we must take into an account the congestion
3700 * control window.
3701 */
3702 if (!conn->tx.nb_pto_dgrams)
3703 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003704 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003705 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003706 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 +01003707 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003708 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003709
Frédéric Lécailleea604992020-12-24 13:01:37 +01003710 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003711 break;
3712
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003713 switch (cf->type) {
3714 case QUIC_FT_CRYPTO:
3715 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3716 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3717 /* Compute the length of this CRYPTO frame header */
3718 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3719 /* Compute the data length of this CRyPTO frame. */
3720 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3721 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3722 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3723 if (!dlen)
3724 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003726 pkt->cdata_len += dlen;
3727 /* CRYPTO frame length. */
3728 flen = hlen + quic_int_getsize(dlen) + dlen;
3729 TRACE_PROTO(" CRYPTO frame length (flen)",
3730 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3731 /* Add the CRYPTO data length and its encoded length to the packet
3732 * length and the length of this length.
3733 */
3734 *len += flen;
3735 room -= flen;
3736 if (dlen == cf->crypto.len) {
3737 /* <cf> CRYPTO data have been consumed. */
3738 MT_LIST_DELETE_SAFE(tmp1);
3739 LIST_APPEND(&pkt->frms, &cf->list);
3740 }
3741 else {
3742 struct quic_frame *new_cf;
3743
3744 new_cf = pool_alloc(pool_head_quic_frame);
3745 if (!new_cf) {
3746 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3747 return 0;
3748 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003749
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003750 new_cf->type = QUIC_FT_CRYPTO;
3751 new_cf->crypto.len = dlen;
3752 new_cf->crypto.offset = cf->crypto.offset;
3753 new_cf->crypto.qel = qel;
3754 LIST_APPEND(&pkt->frms, &new_cf->list);
3755 /* Consume <dlen> bytes of the current frame. */
3756 cf->crypto.len -= dlen;
3757 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003758 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003759 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003760
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003761 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003762 /* Note that these frames are accepted in short packets only without
3763 * "Length" packet field. Here, <*len> is used only to compute the
3764 * sum of the lengths of the already built frames for this packet.
3765 */
3766 TRACE_PROTO(" New STREAM frame build (room, len)",
3767 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3768 /* Compute the length of this STREAM frame "header" made a all the field
3769 * excepting the variable ones. Note that +1 is for the type of this frame.
3770 */
3771 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003772 ((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 +02003773 /* Compute the data length of this STREAM frame. */
3774 avail_room = room - hlen - *len;
3775 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003776 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003777
3778 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3779 dlen = max_available_room(avail_room, &dlen_sz);
3780 if (dlen > cf->stream.len) {
3781 dlen = cf->stream.len;
3782 }
3783 dlen_sz = quic_int_getsize(dlen);
3784 flen = hlen + dlen_sz + dlen;
3785 }
3786 else {
3787 dlen = QUIC_MIN(avail_room, cf->stream.len);
3788 flen = hlen + dlen;
3789 }
3790 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3791 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3792 TRACE_PROTO(" STREAM frame length (flen)",
3793 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3794 /* Add the STREAM data length and its encoded length to the packet
3795 * length and the length of this length.
3796 */
3797 *len += flen;
3798 room -= flen;
3799 if (dlen == cf->stream.len) {
3800 /* <cf> STREAM data have been consumed. */
3801 MT_LIST_DELETE_SAFE(tmp1);
3802 LIST_APPEND(&pkt->frms, &cf->list);
3803 }
3804 else {
3805 struct quic_frame *new_cf;
3806
3807 new_cf = pool_zalloc(pool_head_quic_frame);
3808 if (!new_cf) {
3809 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3810 return 0;
3811 }
3812
3813 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003814 new_cf->stream.qcs = cf->stream.qcs;
3815 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003816 new_cf->stream.id = cf->stream.id;
3817 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3818 new_cf->stream.offset = cf->stream.offset;
3819 new_cf->stream.len = dlen;
3820 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3821 /* FIN bit reset */
3822 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3823 new_cf->stream.data = cf->stream.data;
3824 LIST_APPEND(&pkt->frms, &new_cf->list);
3825 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3826 /* Consume <dlen> bytes of the current frame. */
3827 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003828 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003829 cf->stream.data += dlen;
3830 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003831 break;
3832
3833 default:
3834 flen = qc_frm_len(cf);
3835 BUG_ON(!flen);
3836 if (flen > room)
3837 continue;
3838
3839 *len += flen;
3840 room -= flen;
3841 MT_LIST_DELETE_SAFE(tmp1);
3842 LIST_APPEND(&pkt->frms, &cf->list);
3843 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003845 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003846 }
3847
Frédéric Lécailleea604992020-12-24 13:01:37 +01003848 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003849}
3850
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003851/* This function evaluates if <pkt> packet may be built into a buffer with
3852 * <room> as available room. A valid packet should at least contain a valid
3853 * header and at least a frame.
3854 * To estimate the minimal space to build a packet, we consider the worst case:
3855 - there is not enough space to build ack-eliciting frames from
3856 qel->pktns->tx.frms. This is safe to consider this because when we build
3857 a packet we first build the ACK frames, then the ack-eliciting frames
3858 from qel->pktns->tx.frms only if there is enough space for these
3859 ack-eliciting frames, finally PING and PADDING frames if needed,
3860 - we have to ensure there is enough space to build an ACK frame if required,
3861 and a PING frame, even if we do not have to probe,
3862 - we also have to verify there is enough space to build a PADDING frame
3863 if needed, especially if there is no need to send an ACK frame.
3864 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3865 * a valid packet).
3866 */
3867static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3868 int ack, int nb_pto_dgrams,
3869 struct quic_enc_level *qel, struct quic_conn *conn)
3870{
3871 size_t minlen, token_fields_len;
3872 /* XXX FIXME XXX : ack delay not supported */
3873 uint64_t ack_delay = 0;
3874 size_t ack_frm_len = 0;
3875
3876 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3877 conn->conn, NULL, NULL, &room);
3878 /* When we do not have to probe nor send acks either, we must take into
3879 * an account the data which have already been prepared and limit
3880 * the size of this packet. We will potentially build an ack-eliciting
3881 * packet.
3882 */
3883 if (!nb_pto_dgrams && !ack) {
3884 size_t path_room;
3885
3886 path_room = quic_path_prep_data(conn->path);
3887 if (room > path_room)
3888 room = path_room;
3889 }
3890
3891 if (ack)
3892 /* A frame is made of 1 byte for the frame type. */
3893 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3894
3895 /* XXX FIXME XXX : token not supported */
3896 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3897 /* Check there is enough room to build the header followed by a token,
3898 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3899 * encryption tag is also taken into an account. Note that we have no
3900 * knowledge of the packet number for this packet. It must be atomically
3901 * incremented each time a packet is built. But before building a packet
3902 * we must estimate if it may be built if we do not want to consume a packet
3903 * number for nothing! Note that we add 1 byte more to
3904 * <minlen> to be able to build an ack-eliciting packet when probing without
3905 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3906 * PING frame.
3907 */
3908 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3909 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3910 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3911 + token_fields_len;
3912 else
3913 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3914
3915 /* Consider any PADDING frame to add */
3916 if (objt_server(conn->conn->target) &&
3917 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3918 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3919 /* Pad too short client Initial packet */
3920 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3921 }
3922 else if (!ack) {
3923 /* Consider we will have to add the longest short PADDING frame to
3924 * protect a 1-byte length packet number.
3925 */
3926 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3927 }
3928
3929 if (room < minlen) {
3930 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3931 conn->conn, NULL, NULL, &room);
3932 return 0;
3933 }
3934
3935 return 1;
3936}
3937
3938/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003939 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3940 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3941 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003942 * 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 +02003943 * 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 +02003944 * having returned from this function.
3945 * 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 +01003946 * number field in this packet. <pn_len> will also have the packet number
3947 * length as value.
3948 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003949 * Always succeeds: this is the responsability of the caller to ensure there is
3950 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003951 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003952static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003953 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003954 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3955 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003956{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003957 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003958 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3960 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003961 size_t ack_frm_len;
3962 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003963 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003964
Frédéric Lécailleea604992020-12-24 13:01:37 +01003965 /* Length field value with CRYPTO frames if present. */
3966 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003967 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003968 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003969 * the congestion controller window. So, we do not limit the size of this
3970 * packet if we have an ACK frame to send because an ACK frame is not
3971 * ack-eliciting. This size will be limited if we have ack-eliciting
3972 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003973 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003974 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003975 size_t path_room;
3976
3977 path_room = quic_path_prep_data(conn->path);
3978 if (end - beg > path_room)
3979 end = beg + path_room;
3980 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003981
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003982 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003983 /* packet number length */
3984 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003985 /* Build the header */
3986 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003987 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3988 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003989 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
3990 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
3991 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003992 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003993 /* Ensure there is enough room for the TLS encryption tag */
3994 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003995 /* Build an ACK frame if required. */
3996 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003997 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003998 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003999 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004000 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4001 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4002 * that from here, we do not know if we will have to send a PING frame.
4003 * This will be decided after having computed the ack-eliciting frames
4004 * to be added to this packet.
4005 */
4006 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 +01004007 if (!ack_frm_len) {
4008 ssize_t room = end - pos;
4009 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4010 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004011 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004012 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004013 }
4014
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004015 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004016 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004017 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004018 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004019
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004020 /* Initialize the length of the frames built below to <len>.
4021 * If any frame could be successfully built by qc_build_frms(),
4022 * we will have len_frms > len.
4023 */
4024 len_frms = len;
4025 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004026 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4027 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004028 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004029
4030 add_ping_frm = 0;
4031 padding_len = 0;
4032 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004033 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004034 len < QUIC_INITIAL_PACKET_MINLEN) {
4035 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4036 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004037 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004038 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004039 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004040 add_ping_frm = 1;
4041 len += 1;
4042 }
4043 /* If there is no frame at all to follow, add at least a PADDING frame. */
4044 if (!ack_frm_len)
4045 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4046 }
4047
4048 /* Length (of the remaining data). Must not fail because, the buffer size
4049 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004050 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004051 * of this packet.
4052 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004053 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004054 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004055 else
4056 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004057 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004058 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004059
4060 /* Packet number field address. */
4061 *buf_pn = pos;
4062
4063 /* Packet number encoding. */
4064 quic_packet_number_encode(&pos, end, pn, *pn_len);
4065
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004066 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4067 ssize_t room = end - pos;
4068 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4069 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004070 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004071 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004072
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004073 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004074 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004075 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004076
4077 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004078 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004079 ssize_t room = end - pos;
4080 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4081 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004082 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004083 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004084 }
4085 }
4086
4087 /* Build a PING frame if needed. */
4088 if (add_ping_frm) {
4089 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004090 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4091 ssize_t room = end - pos;
4092 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4093 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004094 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004095 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004096 }
4097
4098 /* Build a PADDING frame if needed. */
4099 if (padding_len) {
4100 frm.type = QUIC_FT_PADDING;
4101 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004102 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4103 ssize_t room = end - pos;
4104 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4105 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004106 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004107 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004108 }
4109
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004110 /* Always reset this variable as this function has no idea
4111 * if it was set. It is handle by the loss detection timer.
4112 */
4113 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004114 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004115}
4116
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004117static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004118{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004119 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004120 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004121 pkt->cdata_len = 0;
4122 pkt->in_flight_len = 0;
4123 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004124 pkt->next = NULL;
4125 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004126}
4127
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004128/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004129static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4130{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004131 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004132
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004133 if (!pkt)
4134 return;
4135
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004136 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004137 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004138 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004139 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004140 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004141}
4142
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004143/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4144 * type for <qc> QUIC connection from <qel> encryption level.
4145 * Return -2 if the packet could not be allocated or encrypted for any reason,
4146 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004147 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004148static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4149 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004150 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004151 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004152 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004153{
4154 /* The pointer to the packet number field. */
4155 unsigned char *buf_pn;
4156 unsigned char *beg, *end, *payload;
4157 int64_t pn;
4158 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004159 struct quic_tls_ctx *tls_ctx;
4160 struct quic_tx_packet *pkt;
4161
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004162 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004163 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004164 pkt = pool_alloc(pool_head_quic_tx_packet);
4165 if (!pkt) {
4166 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 +02004167 *err = -2;
4168 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004169 }
4170
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004171 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004172 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004173 pn_len = 0;
4174 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004175 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004176 *err = -1;
4177 goto err;
4178 }
4179
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004180 /* Consume a packet number. */
4181 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004182 qc_do_build_pkt(*pos, buf_end, pkt, ack, nb_pto_dgrams, pn, &pn_len, &buf_pn, qel, qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004184 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004185 payload = buf_pn + pn_len;
4186 payload_len = end - payload;
4187 aad_len = payload - beg;
4188
4189 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004190 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4191 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004192 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004193 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004194
4195 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004196 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004197 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4198 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4199 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004200 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004201 goto err;
4202 }
4203
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004204 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4205 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004206 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004207 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004208 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004209 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004210 pkt->in_flight_len = pkt->len;
4211 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004212 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004213 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004214 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4215
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004216 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004217
4218 err:
4219 free_quic_tx_packet(pkt);
4220 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004221 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004222}
4223
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004224/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4225 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004226 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004227static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4228 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004230 size_t try, done = 0;
4231
4232 if (!conn_ctrl_ready(conn))
4233 return 0;
4234
4235 if (!fd_recv_ready(conn->handle.fd))
4236 return 0;
4237
4238 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004239
4240 /* read the largest possible block. For this, we perform only one call
4241 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004242 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004243 */
4244 while (count > 0) {
4245 try = b_contig_space(buf);
4246 if (!try)
4247 break;
4248
4249 if (try > count)
4250 try = count;
4251
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004252 b_add(buf, try);
4253 done += try;
4254 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004255 }
4256
4257 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4258 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4259
4260 leave:
4261 return done;
4262
4263 read0:
4264 conn_sock_read0(conn);
4265 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4266
4267 /* Now a final check for a possible asynchronous low-level error
4268 * report. This can happen when a connection receives a reset
4269 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4270 * we might have come from there by just checking POLL_HUP instead
4271 * of recv()'s return value 0, so we have no way to tell there was
4272 * an error without checking.
4273 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004274 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004275 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4276 goto leave;
4277}
4278
4279
4280/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4281 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4282 * other pending data for example, but this flag is ignored at the moment.
4283 * Only one call to send() is performed, unless the buffer wraps, in which case
4284 * a second call may be performed. The connection's flags are updated with
4285 * whatever special event is detected (error, empty). The caller is responsible
4286 * for taking care of those events and avoiding the call if inappropriate. The
4287 * function does not call the connection's polling update function, so the caller
4288 * is responsible for this. It's up to the caller to update the buffer's contents
4289 * based on the return value.
4290 */
4291static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4292{
4293 ssize_t ret;
4294 size_t try, done;
4295 int send_flag;
4296
4297 if (!conn_ctrl_ready(conn))
4298 return 0;
4299
4300 if (!fd_send_ready(conn->handle.fd))
4301 return 0;
4302
4303 done = 0;
4304 /* send the largest possible block. For this we perform only one call
4305 * to send() unless the buffer wraps and we exactly fill the first hunk,
4306 * in which case we accept to do it once again.
4307 */
4308 while (count) {
4309 try = b_contig_data(buf, done);
4310 if (try > count)
4311 try = count;
4312
4313 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4314 if (try < count || flags & CO_SFL_MSG_MORE)
4315 send_flag |= MSG_MORE;
4316
4317 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4318 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4319 if (ret > 0) {
4320 count -= ret;
4321 done += ret;
4322
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004323 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004324 conn->flags |= CO_FL_WAIT_L4L6;
4325 /* if the system buffer is full, don't insist */
4326 if (ret < try)
4327 break;
4328 }
4329 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4330 /* nothing written, we need to poll for write first */
4331 fd_cant_send(conn->handle.fd);
4332 break;
4333 }
4334 else if (errno != EINTR) {
4335 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4336 break;
4337 }
4338 }
4339 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4340 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4341
4342 if (done > 0) {
4343 /* we count the total bytes sent, and the send rate for 32-byte
4344 * blocks. The reason for the latter is that freq_ctr are
4345 * limited to 4GB and that it's not enough per second.
4346 */
4347 _HA_ATOMIC_ADD(&global.out_bytes, done);
4348 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4349 }
4350 return done;
4351}
4352
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004353/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4354 * event subscriber <es> is not allowed to change from a previous call as long
4355 * as at least one event is still subscribed. The <event_type> must only be a
4356 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4357 */
4358static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4359{
4360 return conn_subscribe(conn, xprt_ctx, event_type, es);
4361}
4362
4363/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4364 * The <es> pointer is not allowed to differ from the one passed to the
4365 * subscribe() call. It always returns zero.
4366 */
4367static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4368{
4369 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4370}
4371
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004372/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4373 * connection with <xprt_ctx> as address of the xprt context.
4374 * Returns 1 if succeeded, 0 if not.
4375 */
4376static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4377{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004378 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004379
4380 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4381
4382 if (*xprt_ctx)
4383 goto out;
4384
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004385 ctx = pool_alloc(pool_head_quic_conn_ctx);
4386 if (!ctx) {
4387 conn->err_code = CO_ER_SYS_MEMLIM;
4388 goto err;
4389 }
4390
4391 ctx->wait_event.tasklet = tasklet_new();
4392 if (!ctx->wait_event.tasklet) {
4393 conn->err_code = CO_ER_SYS_MEMLIM;
4394 goto err;
4395 }
4396
4397 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4398 ctx->wait_event.tasklet->context = ctx;
4399 ctx->wait_event.events = 0;
4400 ctx->conn = conn;
4401 ctx->subs = NULL;
4402 ctx->xprt_ctx = NULL;
4403
4404 ctx->xprt = xprt_get(XPRT_QUIC);
4405 if (objt_server(conn->target)) {
4406 /* Server */
4407 struct server *srv = __objt_server(conn->target);
4408 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004409 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004410 int ssl_err, ipv4;
4411
4412 ssl_err = SSL_ERROR_NONE;
4413 if (RAND_bytes(dcid, sizeof dcid) != 1)
4414 goto err;
4415
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004416 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004417 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004418 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004419 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004420 goto err;
4421
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004422 /* Insert our SCID, the connection ID for the QUIC client. */
4423 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4424
4425 conn->qc = qc;
4426 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004427 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4428 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004429 goto err;
4430
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004431 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4432 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4433 goto err;
4434
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004435 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004436 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004437 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4438 qc->enc_params_len =
4439 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4440 &qc->rx.params, 0);
4441 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004442 goto err;
4443
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004444 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004445 SSL_set_connect_state(ctx->ssl);
4446 ssl_err = SSL_do_handshake(ctx->ssl);
4447 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004448 int st;
4449
4450 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004451 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4452 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004453 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004454 }
4455 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004456 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457 goto err;
4458 }
4459 }
4460 }
4461 else if (objt_listener(conn->target)) {
4462 /* Listener */
4463 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004464 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004465
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004466 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004467 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4468 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4469 goto err;
4470
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004471 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004472 SSL_set_accept_state(ctx->ssl);
4473 }
4474
4475 *xprt_ctx = ctx;
4476
4477 /* Leave init state and start handshake */
4478 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004479
4480 out:
4481 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4482
4483 return 0;
4484
4485 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004486 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004487 tasklet_free(ctx->wait_event.tasklet);
4488 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004489 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004490 return -1;
4491}
4492
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004493/* Start the QUIC transport layer */
4494static int qc_xprt_start(struct connection *conn, void *ctx)
4495{
4496 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004497 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004498
4499 qc = conn->qc;
4500 if (!quic_conn_init_timer(qc)) {
4501 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4502 return 0;
4503 }
4504
4505 tasklet_wakeup(qctx->wait_event.tasklet);
4506 return 1;
4507}
4508
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004509/* transport-layer operations for QUIC connections. */
4510static struct xprt_ops ssl_quic = {
4511 .snd_buf = quic_conn_from_buf,
4512 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004513 .subscribe = quic_conn_subscribe,
4514 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004515 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004516 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004517 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4518 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4519 .name = "QUIC",
4520};
4521
4522__attribute__((constructor))
4523static void __quic_conn_init(void)
4524{
4525 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4526 xprt_register(XPRT_QUIC, &ssl_quic);
4527}
4528
4529__attribute__((destructor))
4530static void __quic_conn_deinit(void)
4531{
4532 BIO_meth_free(ha_quic_meth);
4533}
4534
4535/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4536 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4537 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004538 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004539 */
4540static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4541 struct sockaddr_storage *saddr, qpkt_read_func *func)
4542{
4543 unsigned char *pos;
4544 const unsigned char *end;
4545 struct quic_dgram_ctx dgram_ctx = {
4546 .dcid_node = NULL,
4547 .owner = owner,
4548 };
4549
4550 pos = (unsigned char *)buf;
4551 end = pos + len;
4552
4553 do {
4554 int ret;
4555 struct quic_rx_packet *pkt;
4556
Willy Tarreaue4498932021-03-22 21:13:05 +01004557 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004558 if (!pkt)
4559 goto err;
4560
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004561 quic_rx_packet_refinc(pkt);
4562 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4563 if (ret == -1) {
4564 size_t pkt_len;
4565
4566 pkt_len = pkt->len;
4567 free_quic_rx_packet(pkt);
4568 /* If the packet length could not be found, we cannot continue. */
4569 if (!pkt_len)
4570 break;
4571 }
4572 } while (pos < end);
4573
4574 /* Increasing the received bytes counter by the UDP datagram length
4575 * if this datagram could be associated to a connection.
4576 */
4577 if (dgram_ctx.qc)
4578 dgram_ctx.qc->rx.bytes += len;
4579
4580 return pos - (unsigned char *)buf;
4581
4582 err:
4583 return -1;
4584}
4585
4586ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4587 struct sockaddr_storage *saddr)
4588{
4589 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4590}
4591
4592ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4593 struct sockaddr_storage *saddr)
4594{
4595 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4596}
4597
4598/* QUIC I/O handler for connection to local listeners or remove servers
4599 * depending on <listener> boolean value, with <fd> as socket file
4600 * descriptor and <ctx> as context.
4601 */
4602static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4603{
4604 ssize_t ret;
4605 size_t done = 0;
4606 struct buffer *buf = get_trash_chunk();
4607 /* Source address */
4608 struct sockaddr_storage saddr = {0};
4609 socklen_t saddrlen = sizeof saddr;
4610
4611 if (!fd_recv_ready(fd))
4612 return 0;
4613
4614 do {
4615 ret = recvfrom(fd, buf->area, buf->size, 0,
4616 (struct sockaddr *)&saddr, &saddrlen);
4617 if (ret < 0) {
4618 if (errno == EINTR)
4619 continue;
4620 if (errno == EAGAIN)
4621 fd_cant_recv(fd);
4622 goto out;
4623 }
4624 } while (0);
4625
4626 done = buf->data = ret;
4627 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4628
4629 out:
4630 return done;
4631}
4632
4633/* QUIC I/O handler for connections to local listeners with <fd> as socket
4634 * file descriptor.
4635 */
4636void quic_fd_handler(int fd)
4637{
Willy Tarreauf5090652021-04-06 17:23:40 +02004638 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004639 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4640}
4641
4642/* QUIC I/O handler for connections to remote servers with <fd> as socket
4643 * file descriptor.
4644 */
4645void quic_conn_fd_handler(int fd)
4646{
Willy Tarreauf5090652021-04-06 17:23:40 +02004647 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004648 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4649}
4650
4651/*
4652 * Local variables:
4653 * c-indent-level: 8
4654 * c-basic-offset: 8
4655 * End:
4656 */