blob: 75ba788daef36abaec416ef1b2714b869cfb9f7a [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]);
2163 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2164 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002165 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002166 }
2167 /* Special case for Initial packets: when they have all
2168 * been sent, select the next level.
2169 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002170 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002171 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2172 (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 +02002173 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2174 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2175 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002176 tel = next_tel;
2177 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002178 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002179 /* If there is data for the next level, do not
2180 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002181 * which sends only one Initial packet, then wait
2182 * for additional CRYPTO data from the server to enter the
2183 * next level.
2184 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002185 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002186 }
2187 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002188 }
2189
2190 /* If we have to build a new datagram, set the current datagram as
2191 * prepared into <cbuf>.
2192 */
2193 if (!prv_pkt) {
2194 qc_set_dg(cbuf, dglen, first_pkt);
2195 first_pkt = NULL;
2196 dglen = 0;
2197 }
2198 }
2199
2200 stop_build:
2201 /* Reset <wr> writer index if in front of <rd> index */
2202 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2203 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2204
2205 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2206 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2207 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2208 /* Mark the end of contiguous data for the reader */
2209 write_u16(cb_wr(cbuf), 0);
2210 cb_add(cbuf, sizeof(uint16_t));
2211 }
2212 }
2213
2214 if (rd && rd <= cbuf->wr) {
2215 cb_wr_reset(cbuf);
2216 if (pos == spos) {
2217 /* Reuse the same buffer if nothing was built. */
2218 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002219 }
2220 }
2221 }
2222
2223 out:
2224 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002225 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002226
2227 err:
2228 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002229 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002230}
2231
2232/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002233 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002234 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002235int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002236{
2237 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002238 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002239
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002240 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002241 cbuf = qr->cbuf;
2242 while (cb_contig_data(cbuf)) {
2243 unsigned char *pos;
2244 struct buffer tmpbuf = { };
2245 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2246 uint16_t dglen;
2247 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002248 unsigned int time_sent;
2249
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002250 pos = cb_rd(cbuf);
2251 dglen = read_u16(pos);
2252 /* End of prepared datagrams.
2253 * Reset the reader index only if in front of the writer index.
2254 */
2255 if (!dglen) {
2256 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2257
2258 if (wr && wr < cbuf->rd) {
2259 cb_rd_reset(cbuf);
2260 continue;
2261 }
2262 break;
2263 }
2264
2265 pos += sizeof dglen;
2266 first_pkt = read_ptr(pos);
2267 pos += sizeof first_pkt;
2268 tmpbuf.area = (char *)pos;
2269 tmpbuf.size = tmpbuf.data = dglen;
2270
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002271 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002272 for (pkt = first_pkt; pkt; pkt = pkt->next)
2273 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002274 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002275 &tmpbuf, tmpbuf.data, 0) <= 0) {
2276 for (pkt = first_pkt; pkt; pkt = pkt->next)
2277 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002278 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002279 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002280
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002281 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002282 qc->tx.bytes += tmpbuf.data;
2283 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002284
2285 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2286 pkt->time_sent = time_sent;
2287 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2288 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002289 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002290 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002291 qc->path->in_flight += pkt->in_flight_len;
2292 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2293 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002294 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002295 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2296 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002297 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002298 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002299 }
2300 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002301
2302 return 1;
2303}
2304
2305/* Build all the frames which must be sent just after the handshake have succeeded.
2306 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2307 * a HANDSHAKE_DONE frame.
2308 * Return 1 if succeeded, 0 if not.
2309 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002310static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002311{
2312 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002313 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002314 struct quic_frame *frm;
2315
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002316 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002318 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002319 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002320 if (!frm)
2321 return 0;
2322
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002324 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002325 }
2326
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002327 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002328 struct quic_connection_id *cid;
2329
2330 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002331 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002332 if (!frm || !cid)
2333 goto err;
2334
2335 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002336 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337 }
2338
2339 return 1;
2340
2341 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002342 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002343 return 0;
2344}
2345
2346/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002347void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002348{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002349 struct eb64_node *n;
2350 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002351
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002352 n = eb64_first(&arngs->root);
2353 while (n) {
2354 struct eb64_node *next;
2355
2356 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2357 next = eb64_next(n);
2358 eb64_delete(n);
2359 free(ar);
2360 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002361 }
2362}
2363
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002364/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2365 * descending order.
2366 */
2367static inline size_t sack_gap(struct quic_arng_node *p,
2368 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002369{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002370 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002371}
2372
2373
2374/* Remove the last elements of <ack_ranges> list of ack range updating its
2375 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002376 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002377 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002378static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002379{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002380 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002381
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002382 last = eb64_last(&arngs->root);
2383 while (last && arngs->enc_sz > limit) {
2384 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002385
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002386 prev = eb64_prev(last);
2387 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002388 return 0;
2389
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002390 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2391 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2392 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2393 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2394 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2395 --arngs->sz;
2396 eb64_delete(last);
2397 pool_free(pool_head_quic_arng, last);
2398 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002399 }
2400
2401 return 1;
2402}
2403
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002404/* Set the encoded size of <arngs> QUIC ack ranges. */
2405static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2406{
2407 struct eb64_node *node, *next;
2408 struct quic_arng_node *ar, *ar_next;
2409
2410 node = eb64_last(&arngs->root);
2411 if (!node)
2412 return;
2413
2414 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2415 arngs->enc_sz = quic_int_getsize(ar->last) +
2416 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2417
2418 while ((next = eb64_prev(node))) {
2419 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2420 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2421 quic_int_getsize(ar_next->last - ar_next->first.key);
2422 node = next;
2423 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2424 }
2425}
2426
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002427/* Insert <ar> ack range into <argns> tree of ack ranges.
2428 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002429 */
2430static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002431struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2432 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002433{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002434 struct quic_arng_node *new_ar;
2435
2436 new_ar = pool_alloc(pool_head_quic_arng);
2437 if (new_ar) {
2438 new_ar->first.key = ar->first;
2439 new_ar->last = ar->last;
2440 eb64_insert(&arngs->root, &new_ar->first);
2441 arngs->sz++;
2442 }
2443
2444 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002445}
2446
2447/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002448 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002449 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450 *
2451 * Descending order
2452 * ------------->
2453 * range1 range2
2454 * ..........|--------|..............|--------|
2455 * ^ ^ ^ ^
2456 * | | | |
2457 * last1 first1 last2 first2
2458 * ..........+--------+--------------+--------+......
2459 * diff1 gap12 diff2
2460 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002461 * To encode the previous list of ranges we must encode integers as follows in
2462 * descending order:
2463 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002464 * with diff1 = last1 - first1
2465 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002466 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002467 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002469int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2470 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002471{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002472 struct eb64_node *le;
2473 struct quic_arng_node *new_node;
2474 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002475
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002476 new = NULL;
2477 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002478 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002479 if (!new_node)
2480 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002481
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002482 goto out;
2483 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002484
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002485 le = eb64_lookup_le(&arngs->root, ar->first);
2486 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002487 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002488 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002489 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002491 else {
2492 struct quic_arng_node *le_ar =
2493 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002495 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002496 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002497 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002498
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002499 if (le_ar->last + 1 >= ar->first) {
2500 le_ar->last = ar->last;
2501 new = le;
2502 new_node = le_ar;
2503 }
2504 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002505 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002506 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002507 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002508
2509 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002510 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002511 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002512
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002513 /* Verify that the new inserted node does not overlap the nodes
2514 * which follow it.
2515 */
2516 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002517 struct eb64_node *next;
2518 struct quic_arng_node *next_node;
2519
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002520 while ((next = eb64_next(new))) {
2521 next_node =
2522 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002523 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002524 break;
2525
2526 if (next_node->last > new_node->last)
2527 new_node->last = next_node->last;
2528 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002529 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002530 /* Decrement the size of these ranges. */
2531 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002532 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002533 }
2534
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002535 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002536 quic_arngs_set_enc_sz(arngs);
2537
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002538 return 1;
2539}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002540/* Remove the header protection of packets at <el> encryption level.
2541 * Always succeeds.
2542 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002543static 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 +01002544{
2545 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002546 struct quic_rx_packet *pqpkt;
2547 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002548 struct quic_enc_level *app_qel;
2549
2550 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2551 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2552 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002553 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002554 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002555 TRACE_PROTO("hp not removed (handshake not completed)",
2556 QUIC_EV_CONN_ELRMHP, ctx->conn);
2557 goto out;
2558 }
2559 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002560 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002561 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2562 pqpkt->data + pqpkt->pn_offset,
2563 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2564 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2565 /* XXX TO DO XXX */
2566 }
2567 else {
2568 /* The AAD includes the packet number field */
2569 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2570 /* Store the packet into the tree of packets to decrypt. */
2571 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002572 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002573 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002574 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2576 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002577 MT_LIST_DELETE_SAFE(pkttmp1);
2578 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002579 }
2580
2581 out:
2582 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2583}
2584
2585/* Process all the CRYPTO frame at <el> encryption level.
2586 * Return 1 if succeeded, 0 if not.
2587 */
2588static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002589 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002590{
2591 struct eb64_node *node;
2592
2593 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002594 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595 node = eb64_first(&el->rx.crypto.frms);
2596 while (node) {
2597 struct quic_rx_crypto_frm *cf;
2598
2599 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2600 if (cf->offset_node.key != el->rx.crypto.offset)
2601 break;
2602
2603 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2604 goto err;
2605
2606 node = eb64_next(node);
2607 quic_rx_packet_refdec(cf->pkt);
2608 eb64_delete(&cf->offset_node);
2609 pool_free(pool_head_quic_rx_crypto_frm, cf);
2610 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002611 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002612 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2613 return 1;
2614
2615 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002616 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002617 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2618 return 0;
2619}
2620
2621/* Process all the packets at <el> encryption level.
2622 * Return 1 if succeeded, 0 if not.
2623 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002624int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2625 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002626{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002627 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002628 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002629 struct quic_conn *qc = ctx->conn->qc;
2630 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631
2632 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002633 qel = cur_el;
2634 next_tel:
2635 if (!qel)
2636 goto out;
2637
2638 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2639 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640 while (node) {
2641 struct quic_rx_packet *pkt;
2642
2643 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002644 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2645 ctx->conn, pkt, NULL, ctx->ssl);
2646 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002647 /* Drop the packet */
2648 TRACE_PROTO("packet decryption failed -> dropped",
2649 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2650 }
2651 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002652 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002653 /* Drop the packet */
2654 TRACE_PROTO("packet parsing failed -> dropped",
2655 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2656 }
2657 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002658 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2659
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002660 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002661 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2662 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002663 if (pkt->pn > largest_pn)
2664 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002666 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002667 TRACE_DEVEL("Could not update ack range list",
2668 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002669 }
2670 }
2671 node = eb64_next(node);
2672 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002674 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002675
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002676 /* Update the largest packet number. */
2677 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002678 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2679 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002680 goto err;
2681
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002682 if (qel == cur_el) {
2683 qel = next_el;
2684 goto next_tel;
2685 }
2686
2687 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002688 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2689 return 1;
2690
2691 err:
2692 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2693 return 0;
2694}
2695
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002696/* QUIC connection packet handler task. */
2697struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002698{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002699 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002700 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002701 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002702 enum quic_tls_enc_level tel, next_tel;
2703 struct quic_enc_level *qel, *next_qel;
2704 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002705 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002706 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002707
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002708 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002709 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002710 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002711 st = HA_ATOMIC_LOAD(&qc->state);
2712 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002714 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002715 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002716 goto err;
2717
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002718 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002719 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002720
2721 next_level:
2722 tls_ctx = &qel->tls_ctx;
2723
2724 /* If the header protection key for this level has been derived,
2725 * remove the packet header protections.
2726 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002727 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002728 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2729 qc_rm_hp_pkts(qel, ctx);
2730
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002731 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002732 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2733 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2734 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002735 goto err;
2736
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002737 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002738 if (st >= QUIC_HS_ST_COMPLETE &&
2739 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002740 /* Discard the Handshake keys. */
2741 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2742 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2743 qc_set_timer(ctx);
2744 if (!quic_build_post_handshake_frames(qc))
2745 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002746 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002747 }
2748
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002749 if (!qr)
2750 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2751 ret = qc_prep_hdshk_pkts(qr, ctx);
2752 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002753 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002754 else if (ret == 0)
2755 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002756
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002757 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758 goto err;
2759
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002760 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761 /* Check if there is something to do for the next level.
2762 */
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002763 if (next_qel && (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002764 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002765 qel = next_qel;
2766 goto next_level;
2767 }
2768
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002769 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002770 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002771 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772
2773 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002774 if (qr)
2775 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002776 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002777 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002778}
2779
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002780/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002781static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2782{
2783 int i;
2784
2785 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2786 if (qel->tx.crypto.bufs[i]) {
2787 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2788 qel->tx.crypto.bufs[i] = NULL;
2789 }
2790 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002791 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792}
2793
2794/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002795 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002796 * Returns 1 if succeeded, 0 if not.
2797 */
2798static int quic_conn_enc_level_init(struct quic_conn *qc,
2799 enum quic_tls_enc_level level)
2800{
2801 struct quic_enc_level *qel;
2802
2803 qel = &qc->els[level];
2804 qel->level = quic_to_ssl_enc_level(level);
2805 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2806 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2807 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2808 qel->tls_ctx.rx.flags = 0;
2809 qel->tls_ctx.tx.flags = 0;
2810
2811 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002812 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002813 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002814 qel->rx.crypto.offset = 0;
2815 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2816 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817
2818 /* Allocate only one buffer. */
2819 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2820 if (!qel->tx.crypto.bufs)
2821 goto err;
2822
2823 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2824 if (!qel->tx.crypto.bufs[0])
2825 goto err;
2826
2827 qel->tx.crypto.bufs[0]->sz = 0;
2828 qel->tx.crypto.nb_buf = 1;
2829
2830 qel->tx.crypto.sz = 0;
2831 qel->tx.crypto.offset = 0;
2832
2833 return 1;
2834
2835 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002836 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002837 return 0;
2838}
2839
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002840/* Release all the memory allocated for <conn> QUIC connection. */
2841static void quic_conn_free(struct quic_conn *conn)
2842{
2843 int i;
2844
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002845 if (!conn)
2846 return;
2847
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002848 free_quic_conn_cids(conn);
2849 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2850 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002851 if (conn->timer_task)
2852 task_destroy(conn->timer_task);
2853 pool_free(pool_head_quic_conn, conn);
2854}
2855
2856/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002857static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002858{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002859 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002860 struct quic_conn *qc;
2861 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002862 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002863
2864 conn_ctx = task->context;
2865 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002866 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2867 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002868 task->expire = TICK_ETERNITY;
2869 pktns = quic_loss_pktns(qc);
2870 if (tick_isset(pktns->tx.loss_time)) {
2871 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2872
2873 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2874 if (!LIST_ISEMPTY(&lost_pkts))
2875 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2876 qc_set_timer(conn_ctx);
2877 goto out;
2878 }
2879
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002880 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002881 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002882 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883 pktns->tx.pto_probe = 1;
2884 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002885 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2887 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2888
2889 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2890 hel->pktns->tx.pto_probe = 1;
2891 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2892 iel->pktns->tx.pto_probe = 1;
2893 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002894 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2896 qc->path->loss.pto_count++;
2897
2898 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002899 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002900
2901 return task;
2902}
2903
2904/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2905 * connections used to identify the first Initial packets of client connecting
2906 * to listeners. This parameter must be NULL for QUIC connections attached
2907 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2908 * <scid> is the source connection ID with <scid_len> as length.
2909 * Returns 1 if succeeded, 0 if not.
2910 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002911static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2912 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002913 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002914{
2915 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002916 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002917 /* Initial CID. */
2918 struct quic_connection_id *icid;
2919
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002920 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002921 qc = pool_zalloc(pool_head_quic_conn);
2922 if (!qc) {
2923 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2924 goto err;
2925 }
2926
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002927 qc->cids = EB_ROOT;
2928 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002929 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002930 struct listener *l = owner;
2931
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002932 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002933 /* Copy the initial DCID. */
2934 qc->odcid.len = dcid_len;
2935 if (qc->odcid.len)
2936 memcpy(qc->odcid.data, dcid, dcid_len);
2937
2938 /* Copy the SCID as our DCID for this connection. */
2939 if (scid_len)
2940 memcpy(qc->dcid.data, scid, scid_len);
2941 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002942 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002943 }
2944 /* QUIC Client (outgoing connection to servers) */
2945 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002946 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002947 if (dcid_len)
2948 memcpy(qc->dcid.data, dcid, dcid_len);
2949 qc->dcid.len = dcid_len;
2950 }
2951
2952 /* Initialize the output buffer */
2953 qc->obuf.pos = qc->obuf.data;
2954
2955 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002956 if (!icid) {
2957 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2958 goto err;
2959 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002960
2961 /* Select our SCID which is the first CID with 0 as sequence number. */
2962 qc->scid = icid->cid;
2963
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002964 /* Packet number spaces initialization. */
2965 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2966 quic_pktns_init(&qc->pktns[i]);
2967 /* QUIC encryption level context initialization. */
2968 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002969 if (!quic_conn_enc_level_init(qc, i)) {
2970 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002971 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002972 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002973 /* Initialize the packet number space. */
2974 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2975 }
2976
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002977 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002978 /* TX part. */
2979 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002980 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2981 qc->tx.wbuf = qc->tx.rbuf = 0;
2982 qc->tx.bytes = 0;
2983 qc->tx.nb_pto_dgrams = 0;
2984 /* RX part. */
2985 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002986 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002988 /* XXX TO DO: Only one path at this time. */
2989 qc->path = &qc->paths[0];
2990 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2991
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002992 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002993
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002994 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002995
2996 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002997 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002999 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003000}
3001
3002/* Initialize the timer task of <qc> QUIC connection.
3003 * Returns 1 if succeeded, 0 if not.
3004 */
3005static int quic_conn_init_timer(struct quic_conn *qc)
3006{
3007 qc->timer_task = task_new(MAX_THREADS_MASK);
3008 if (!qc->timer_task)
3009 return 0;
3010
3011 qc->timer = TICK_ETERNITY;
3012 qc->timer_task->process = process_timer;
3013 qc->timer_task->context = qc->conn->xprt_ctx;
3014
3015 return 1;
3016}
3017
3018/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3019 * past one byte of this buffer.
3020 */
3021static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3022 struct quic_rx_packet *pkt)
3023{
3024 unsigned char dcid_len, scid_len;
3025
3026 /* Version */
3027 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3028 return 0;
3029
3030 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
3031
3032 /* Destination Connection ID Length */
3033 dcid_len = *(*buf)++;
3034 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3035 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3036 /* XXX MUST BE DROPPED */
3037 return 0;
3038
3039 if (dcid_len) {
3040 /* Check that the length of this received DCID matches the CID lengths
3041 * of our implementation for non Initials packets only.
3042 */
3043 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3044 return 0;
3045
3046 memcpy(pkt->dcid.data, *buf, dcid_len);
3047 }
3048
3049 pkt->dcid.len = dcid_len;
3050 *buf += dcid_len;
3051
3052 /* Source Connection ID Length */
3053 scid_len = *(*buf)++;
3054 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3055 /* XXX MUST BE DROPPED */
3056 return 0;
3057
3058 if (scid_len)
3059 memcpy(pkt->scid.data, *buf, scid_len);
3060 pkt->scid.len = scid_len;
3061 *buf += scid_len;
3062
3063 return 1;
3064}
3065
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003066/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3067 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3068 * encryption level matching with the packet type. <*qel> may be null if not found.
3069 * Note that <ctx> may be null (for Initial packets).
3070 */
3071static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003072 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003073 struct quic_enc_level **qel)
3074{
3075 enum quic_tls_enc_level tel;
3076
3077 /* Special case without connection context (firt Initial packets) */
3078 if (!ctx) {
3079 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3080 return 1;
3081 }
3082
3083 tel = quic_packet_type_enc_level(pkt->type);
3084 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3085 *qel = NULL;
3086 return 0;
3087 }
3088
3089 *qel = &qc->els[tel];
3090 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3091 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3092 return 0;
3093 }
3094
3095 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003096 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3097 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003098 return 1;
3099
3100 return 0;
3101}
3102
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003103/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3104 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3105 * byte past the end of the buffer containing this packet and <beg> the address of
3106 * the packet first byte.
3107 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3108 * Returns 1 if succeeded, 0 if not.
3109 */
3110static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3111 unsigned char **buf, unsigned char *beg,
3112 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003113 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003114{
3115 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003116 struct quic_enc_level *qel;
3117 /* Only for traces. */
3118 struct quic_rx_packet *qpkt_trace;
3119
3120 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003121 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003122 /* The packet number is here. This is also the start minus
3123 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3124 * protection.
3125 */
3126 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003127 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003128 /* Note that the following function enables us to unprotect the packet
3129 * number and its length subsequently used to decrypt the entire
3130 * packets.
3131 */
3132 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3133 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003134 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003135 goto err;
3136 }
3137
3138 /* The AAD includes the packet number field found at <pn>. */
3139 pkt->aad_len = pn - beg + pkt->pnl;
3140 qpkt_trace = pkt;
3141 /* Store the packet */
3142 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003143 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003144 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003145 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003147 else if (qel) {
3148 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149 pkt->pn_offset = pn - beg;
3150 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3151 }
3152
3153 memcpy(pkt->data, beg, pkt->len);
3154 /* Updtate the offset of <*buf> for the next QUIC packet. */
3155 *buf = beg + pkt->len;
3156
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003157 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003158 return 1;
3159
3160 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003161 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 +01003162 return 0;
3163}
3164
3165/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3166 * Also set <*long_header> to 1 if this form is long, 0 if not.
3167 */
3168static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3169 unsigned char byte0, int *long_header)
3170{
3171 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3172 pkt->type =
3173 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3174 *long_header = 1;
3175 }
3176 else {
3177 pkt->type = QUIC_PACKET_TYPE_SHORT;
3178 *long_header = 0;
3179 }
3180}
3181
3182static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3183 struct quic_rx_packet *pkt,
3184 struct quic_dgram_ctx *dgram_ctx,
3185 struct sockaddr_storage *saddr)
3186{
3187 unsigned char *beg;
3188 uint64_t len;
3189 struct quic_conn *qc;
3190 struct eb_root *cids;
3191 struct ebmb_node *node;
3192 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003193 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003194 int long_header;
3195
3196 qc = NULL;
3197 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3198 if (end <= *buf)
3199 goto err;
3200
3201 /* Fixed bit */
3202 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3203 /* XXX TO BE DISCARDED */
3204 goto err;
3205
3206 srv_conn = dgram_ctx->owner;
3207 beg = *buf;
3208 /* Header form */
3209 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3210 if (long_header) {
3211 size_t cid_lookup_len;
3212
3213 if (!quic_packet_read_long_header(buf, end, pkt))
3214 goto err;
3215
3216 /* For Initial packets, and for servers (QUIC clients connections),
3217 * there is no Initial connection IDs storage.
3218 */
3219 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3220 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3221 cid_lookup_len = pkt->dcid.len;
3222 }
3223 else {
3224 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3225 cid_lookup_len = QUIC_CID_LEN;
3226 }
3227
3228 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3229 if (!node)
3230 goto err;
3231
3232 qc = ebmb_entry(node, struct quic_conn, scid_node);
3233
3234 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3235 qc->dcid.len = pkt->scid.len;
3236 if (pkt->scid.len)
3237 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3238 }
3239
3240 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3241 uint64_t token_len;
3242
3243 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3244 goto err;
3245
3246 /* XXX TO DO XXX 0 value means "the token is not present".
3247 * A server which sends an Initial packet must not set the token.
3248 * So, a client which receives an Initial packet with a token
3249 * MUST discard the packet or generate a connection error with
3250 * PROTOCOL_VIOLATION as type.
3251 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3252 */
3253 pkt->token_len = token_len;
3254 }
3255 }
3256 else {
3257 /* XXX TO DO: Short header XXX */
3258 if (end - *buf < QUIC_CID_LEN)
3259 goto err;
3260
3261 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3262 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3263 if (!node)
3264 goto err;
3265
3266 qc = ebmb_entry(node, struct quic_conn, scid_node);
3267 *buf += QUIC_CID_LEN;
3268 }
3269 /* Store the DCID used for this packet to check the packet which
3270 * come in this UDP datagram match with it.
3271 */
3272 if (!dgram_ctx->dcid_node)
3273 dgram_ctx->dcid_node = node;
3274 /* Only packets packets with long headers and not RETRY or VERSION as type
3275 * have a length field.
3276 */
3277 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3278 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3279 goto err;
3280
3281 pkt->len = len;
3282 }
3283 else if (!long_header) {
3284 /* A short packet is the last one of an UDP datagram. */
3285 pkt->len = end - *buf;
3286 }
3287
3288 conn_ctx = qc->conn->xprt_ctx;
3289
3290 /* Increase the total length of this packet by the header length. */
3291 pkt->len += *buf - beg;
3292 /* Do not check the DCID node before the length. */
3293 if (dgram_ctx->dcid_node != node) {
3294 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3295 goto err;
3296 }
3297
3298 if (pkt->len > sizeof pkt->data) {
3299 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3300 goto err;
3301 }
3302
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003303 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003304 goto err;
3305
3306 /* Wake the tasklet of the QUIC connection packet handler. */
3307 if (conn_ctx)
3308 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3309
3310 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3311
3312 return pkt->len;
3313
3314 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003315 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003316 return -1;
3317}
3318
3319static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3320 struct quic_rx_packet *pkt,
3321 struct quic_dgram_ctx *dgram_ctx,
3322 struct sockaddr_storage *saddr)
3323{
3324 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003325 struct quic_conn *qc;
3326 struct eb_root *cids;
3327 struct ebmb_node *node;
3328 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003329 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003330 int long_header = 0;
3331
3332 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003333 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003334 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003335 if (end <= *buf)
3336 goto err;
3337
3338 /* Fixed bit */
3339 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3340 /* XXX TO BE DISCARDED */
3341 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3342 goto err;
3343 }
3344
3345 l = dgram_ctx->owner;
3346 beg = *buf;
3347 /* Header form */
3348 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3349 if (long_header) {
3350 unsigned char dcid_len;
3351
3352 if (!quic_packet_read_long_header(buf, end, pkt)) {
3353 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3354 goto err;
3355 }
3356
3357 dcid_len = pkt->dcid.len;
3358 /* For Initial packets, and for servers (QUIC clients connections),
3359 * there is no Initial connection IDs storage.
3360 */
3361 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003362 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003363 /* DCIDs of first packets coming from clients may have the same values.
3364 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3365 */
3366 quic_cid_saddr_cat(&pkt->dcid, saddr);
3367 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003368
3369 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3370 end - *buf < token_len) {
3371 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3372 goto err;
3373 }
3374
3375 /* XXX TO DO XXX 0 value means "the token is not present".
3376 * A server which sends an Initial packet must not set the token.
3377 * So, a client which receives an Initial packet with a token
3378 * MUST discard the packet or generate a connection error with
3379 * PROTOCOL_VIOLATION as type.
3380 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3381 */
3382 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003383 }
3384 else {
3385 if (pkt->dcid.len != QUIC_CID_LEN) {
3386 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3387 goto err;
3388 }
3389
3390 cids = &l->rx.cids;
3391 }
3392
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003393 /* Only packets packets with long headers and not RETRY or VERSION as type
3394 * have a length field.
3395 */
3396 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3397 uint64_t len;
3398
3399 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3400 end - *buf < len) {
3401 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3402 goto err;
3403 }
3404
3405 pkt->len = len;
3406 }
3407
3408
3409 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003410 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3411 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3412 cids == &l->rx.odcids) {
3413 /* Switch to the definitive tree ->cids containing the final CIDs. */
3414 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3415 if (node) {
3416 /* If found, signal this with NULL as special value for <cids>. */
3417 pkt->dcid.len = dcid_len;
3418 cids = NULL;
3419 }
3420 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003421 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003422
3423 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003424 int ipv4;
3425 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003426 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003427 const unsigned char *salt = initial_salt_v1;
3428 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003429
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003430 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3431 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3432 goto err;
3433 }
3434
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003435 pkt->saddr = *saddr;
3436 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3437 * of <saddr>.
3438 */
3439 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003440 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003441 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003442 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003443 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003444 goto err;
3445
3446 odcid = &qc->rx.params.original_destination_connection_id;
3447 /* Copy the transport parameters. */
3448 qc->rx.params = l->bind_conf->quic_params;
3449 /* Copy original_destination_connection_id transport parameter. */
3450 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3451 odcid->len = pkt->odcid_len;
3452 /* Copy the initial source connection ID. */
3453 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3454 qc->enc_params_len =
3455 quic_transport_params_encode(qc->enc_params,
3456 qc->enc_params + sizeof qc->enc_params,
3457 &qc->rx.params, 1);
3458 if (!qc->enc_params_len)
3459 goto err;
3460
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003461 /* NOTE: the socket address has been concatenated to the destination ID
3462 * chosen by the client for Initial packets.
3463 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003464 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3465 salt = initial_salt_draft_29;
3466 salt_len = sizeof initial_salt_draft_29;
3467 }
3468 if (!qc_new_isecs(qc, salt, salt_len,
3469 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003470 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3471 goto err;
3472 }
3473
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003474 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003475 /* This is the DCID node sent in this packet by the client. */
3476 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003477 /* Enqueue this packet. */
3478 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3479 /* Try to accept a new connection. */
3480 listener_accept(l);
3481
3482 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3483 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3484 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3485 /* Insert our SCID, the connection ID for the QUIC client. */
3486 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3487 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3488 if (n != &qc->scid_node) {
3489 quic_conn_free(qc);
3490 qc = ebmb_entry(n, struct quic_conn, scid_node);
3491 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003492 }
3493 else {
3494 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3495 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3496 else
3497 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003498 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003499 }
3500 }
3501 else {
3502 if (end - *buf < QUIC_CID_LEN) {
3503 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3504 goto err;
3505 }
3506
3507 cids = &l->rx.cids;
3508 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3509 if (!node) {
3510 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3511 goto err;
3512 }
3513
3514 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003515 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003517 /* A short packet is the last one of an UDP datagram. */
3518 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003519 }
3520
3521 /* Store the DCID used for this packet to check the packet which
3522 * come in this UDP datagram match with it.
3523 */
3524 if (!dgram_ctx->dcid_node) {
3525 dgram_ctx->dcid_node = node;
3526 dgram_ctx->qc = qc;
3527 }
3528
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003529 /* Increase the total length of this packet by the header length. */
3530 pkt->len += *buf - beg;
3531 /* Do not check the DCID node before the length. */
3532 if (dgram_ctx->dcid_node != node) {
3533 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3534 goto err;
3535 }
3536
3537 if (pkt->len > sizeof pkt->data) {
3538 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3539 goto err;
3540 }
3541
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003542 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3543 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3544 goto err;
3545 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003546
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003547
3548 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003549 /* Wake up the connection packet handler task from here only if all
3550 * the contexts have been initialized, especially the mux context
3551 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3552 * will start it if these contexts for the connection are not already
3553 * initialized.
3554 */
3555 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003556 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003557
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3559
3560 return pkt->len;
3561
3562 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003563 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003564 qc ? qc->conn : NULL, pkt);
3565 return -1;
3566}
3567
3568/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3569 * in advance. This is the reponsability of the caller to check there is enough room in this
3570 * buffer to build a long header.
3571 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3572 */
3573static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3574 int type, size_t pn_len, struct quic_conn *conn)
3575{
3576 if (type > QUIC_PACKET_TYPE_RETRY)
3577 return 0;
3578
3579 /* #0 byte flags */
3580 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3581 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3582 /* Version */
3583 quic_write_uint32(buf, end, conn->version);
3584 *(*buf)++ = conn->dcid.len;
3585 /* Destination connection ID */
3586 if (conn->dcid.len) {
3587 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3588 *buf += conn->dcid.len;
3589 }
3590 /* Source connection ID */
3591 *(*buf)++ = conn->scid.len;
3592 if (conn->scid.len) {
3593 memcpy(*buf, conn->scid.data, conn->scid.len);
3594 *buf += conn->scid.len;
3595 }
3596
3597 return 1;
3598}
3599
3600/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3601 * in advance. This is the reponsability of the caller to check there is enough room in this
3602 * buffer to build a long header.
3603 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3604 */
3605static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3606 size_t pn_len, struct quic_conn *conn)
3607{
3608 /* #0 byte flags */
3609 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3610 /* Destination connection ID */
3611 if (conn->dcid.len) {
3612 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3613 *buf += conn->dcid.len;
3614 }
3615
3616 return 1;
3617}
3618
3619/* Apply QUIC header protection to the packet with <buf> as first byte address,
3620 * <pn> as address of the Packet number field, <pnlen> being this field length
3621 * with <aead> as AEAD cipher and <key> as secret key.
3622 * Returns 1 if succeeded or 0 if failed.
3623 */
3624static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3625 const EVP_CIPHER *aead, const unsigned char *key)
3626{
3627 int i, ret, outlen;
3628 EVP_CIPHER_CTX *ctx;
3629 /* We need an IV of at least 5 bytes: one byte for bytes #0
3630 * and at most 4 bytes for the packet number
3631 */
3632 unsigned char mask[5] = {0};
3633
3634 ret = 0;
3635 ctx = EVP_CIPHER_CTX_new();
3636 if (!ctx)
3637 return 0;
3638
3639 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3640 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3641 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3642 goto out;
3643
3644 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3645 for (i = 0; i < pnlen; i++)
3646 pn[i] ^= mask[i + 1];
3647
3648 ret = 1;
3649
3650 out:
3651 EVP_CIPHER_CTX_free(ctx);
3652
3653 return ret;
3654}
3655
3656/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3657 * ACK ranges if needed to a value below <limit> in bytes.
3658 * Return 1 if succeeded, 0 if not.
3659 */
3660static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3661{
3662 size_t room, ack_delay_sz;
3663
3664 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3665 /* A frame is made of 1 byte for the frame type. */
3666 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003667 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003668 return 0;
3669
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003670 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003671}
3672
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003673/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3674 * 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 +01003675 * and <*len> the packet Length field initialized with the number of bytes already present
3676 * 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 +02003677 * <headlen> is the number of bytes already present in this packet before building frames.
3678 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003679 * Update consequently <*len> to reflect the size of these frames built
3680 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003681 * Return 1 if succeeded, 0 if not.
3682 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003683static inline int qc_build_frms(struct quic_tx_packet *pkt,
3684 size_t room, size_t *len, size_t headlen,
3685 struct quic_enc_level *qel,
3686 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003687{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003688 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003689 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003690 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003691 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003692
Frédéric Lécailleea604992020-12-24 13:01:37 +01003693 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003694 if (*len > room || headlen > remain)
3695 return 0;
3696
Frédéric Lécailleea604992020-12-24 13:01:37 +01003697 /* If we are not probing we must take into an account the congestion
3698 * control window.
3699 */
3700 if (!conn->tx.nb_pto_dgrams)
3701 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003702 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003703 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003704 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 +01003705 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003706 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003707
Frédéric Lécailleea604992020-12-24 13:01:37 +01003708 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003709 break;
3710
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003711 switch (cf->type) {
3712 case QUIC_FT_CRYPTO:
3713 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3714 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3715 /* Compute the length of this CRYPTO frame header */
3716 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3717 /* Compute the data length of this CRyPTO frame. */
3718 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3719 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3720 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3721 if (!dlen)
3722 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003723
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003724 pkt->cdata_len += dlen;
3725 /* CRYPTO frame length. */
3726 flen = hlen + quic_int_getsize(dlen) + dlen;
3727 TRACE_PROTO(" CRYPTO frame length (flen)",
3728 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3729 /* Add the CRYPTO data length and its encoded length to the packet
3730 * length and the length of this length.
3731 */
3732 *len += flen;
3733 room -= flen;
3734 if (dlen == cf->crypto.len) {
3735 /* <cf> CRYPTO data have been consumed. */
3736 MT_LIST_DELETE_SAFE(tmp1);
3737 LIST_APPEND(&pkt->frms, &cf->list);
3738 }
3739 else {
3740 struct quic_frame *new_cf;
3741
3742 new_cf = pool_alloc(pool_head_quic_frame);
3743 if (!new_cf) {
3744 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3745 return 0;
3746 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003747
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003748 new_cf->type = QUIC_FT_CRYPTO;
3749 new_cf->crypto.len = dlen;
3750 new_cf->crypto.offset = cf->crypto.offset;
3751 new_cf->crypto.qel = qel;
3752 LIST_APPEND(&pkt->frms, &new_cf->list);
3753 /* Consume <dlen> bytes of the current frame. */
3754 cf->crypto.len -= dlen;
3755 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003756 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003757 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003758
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003759 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003760 /* Note that these frames are accepted in short packets only without
3761 * "Length" packet field. Here, <*len> is used only to compute the
3762 * sum of the lengths of the already built frames for this packet.
3763 */
3764 TRACE_PROTO(" New STREAM frame build (room, len)",
3765 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3766 /* Compute the length of this STREAM frame "header" made a all the field
3767 * excepting the variable ones. Note that +1 is for the type of this frame.
3768 */
3769 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003770 ((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 +02003771 /* Compute the data length of this STREAM frame. */
3772 avail_room = room - hlen - *len;
3773 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003774 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003775
3776 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3777 dlen = max_available_room(avail_room, &dlen_sz);
3778 if (dlen > cf->stream.len) {
3779 dlen = cf->stream.len;
3780 }
3781 dlen_sz = quic_int_getsize(dlen);
3782 flen = hlen + dlen_sz + dlen;
3783 }
3784 else {
3785 dlen = QUIC_MIN(avail_room, cf->stream.len);
3786 flen = hlen + dlen;
3787 }
3788 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3789 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3790 TRACE_PROTO(" STREAM frame length (flen)",
3791 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3792 /* Add the STREAM data length and its encoded length to the packet
3793 * length and the length of this length.
3794 */
3795 *len += flen;
3796 room -= flen;
3797 if (dlen == cf->stream.len) {
3798 /* <cf> STREAM data have been consumed. */
3799 MT_LIST_DELETE_SAFE(tmp1);
3800 LIST_APPEND(&pkt->frms, &cf->list);
3801 }
3802 else {
3803 struct quic_frame *new_cf;
3804
3805 new_cf = pool_zalloc(pool_head_quic_frame);
3806 if (!new_cf) {
3807 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3808 return 0;
3809 }
3810
3811 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003812 new_cf->stream.qcs = cf->stream.qcs;
3813 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003814 new_cf->stream.id = cf->stream.id;
3815 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3816 new_cf->stream.offset = cf->stream.offset;
3817 new_cf->stream.len = dlen;
3818 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3819 /* FIN bit reset */
3820 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3821 new_cf->stream.data = cf->stream.data;
3822 LIST_APPEND(&pkt->frms, &new_cf->list);
3823 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3824 /* Consume <dlen> bytes of the current frame. */
3825 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003826 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003827 cf->stream.data += dlen;
3828 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003829 break;
3830
3831 default:
3832 flen = qc_frm_len(cf);
3833 BUG_ON(!flen);
3834 if (flen > room)
3835 continue;
3836
3837 *len += flen;
3838 room -= flen;
3839 MT_LIST_DELETE_SAFE(tmp1);
3840 LIST_APPEND(&pkt->frms, &cf->list);
3841 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003842 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003843 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003844 }
3845
Frédéric Lécailleea604992020-12-24 13:01:37 +01003846 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003847}
3848
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003849/* This function evaluates if <pkt> packet may be built into a buffer with
3850 * <room> as available room. A valid packet should at least contain a valid
3851 * header and at least a frame.
3852 * To estimate the minimal space to build a packet, we consider the worst case:
3853 - there is not enough space to build ack-eliciting frames from
3854 qel->pktns->tx.frms. This is safe to consider this because when we build
3855 a packet we first build the ACK frames, then the ack-eliciting frames
3856 from qel->pktns->tx.frms only if there is enough space for these
3857 ack-eliciting frames, finally PING and PADDING frames if needed,
3858 - we have to ensure there is enough space to build an ACK frame if required,
3859 and a PING frame, even if we do not have to probe,
3860 - we also have to verify there is enough space to build a PADDING frame
3861 if needed, especially if there is no need to send an ACK frame.
3862 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3863 * a valid packet).
3864 */
3865static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3866 int ack, int nb_pto_dgrams,
3867 struct quic_enc_level *qel, struct quic_conn *conn)
3868{
3869 size_t minlen, token_fields_len;
3870 /* XXX FIXME XXX : ack delay not supported */
3871 uint64_t ack_delay = 0;
3872 size_t ack_frm_len = 0;
3873
3874 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3875 conn->conn, NULL, NULL, &room);
3876 /* When we do not have to probe nor send acks either, we must take into
3877 * an account the data which have already been prepared and limit
3878 * the size of this packet. We will potentially build an ack-eliciting
3879 * packet.
3880 */
3881 if (!nb_pto_dgrams && !ack) {
3882 size_t path_room;
3883
3884 path_room = quic_path_prep_data(conn->path);
3885 if (room > path_room)
3886 room = path_room;
3887 }
3888
3889 if (ack)
3890 /* A frame is made of 1 byte for the frame type. */
3891 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3892
3893 /* XXX FIXME XXX : token not supported */
3894 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3895 /* Check there is enough room to build the header followed by a token,
3896 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3897 * encryption tag is also taken into an account. Note that we have no
3898 * knowledge of the packet number for this packet. It must be atomically
3899 * incremented each time a packet is built. But before building a packet
3900 * we must estimate if it may be built if we do not want to consume a packet
3901 * number for nothing! Note that we add 1 byte more to
3902 * <minlen> to be able to build an ack-eliciting packet when probing without
3903 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3904 * PING frame.
3905 */
3906 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3907 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3908 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3909 + token_fields_len;
3910 else
3911 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3912
3913 /* Consider any PADDING frame to add */
3914 if (objt_server(conn->conn->target) &&
3915 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3916 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3917 /* Pad too short client Initial packet */
3918 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3919 }
3920 else if (!ack) {
3921 /* Consider we will have to add the longest short PADDING frame to
3922 * protect a 1-byte length packet number.
3923 */
3924 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3925 }
3926
3927 if (room < minlen) {
3928 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3929 conn->conn, NULL, NULL, &room);
3930 return 0;
3931 }
3932
3933 return 1;
3934}
3935
3936/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003937 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3938 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3939 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003940 * 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 +02003941 * 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 +02003942 * having returned from this function.
3943 * 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 +01003944 * number field in this packet. <pn_len> will also have the packet number
3945 * length as value.
3946 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003947 * Always succeeds: this is the responsability of the caller to ensure there is
3948 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003949 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003950static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003951 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003952 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3953 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003955 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003956 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003957 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3958 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959 size_t ack_frm_len;
3960 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003961 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003962
Frédéric Lécailleea604992020-12-24 13:01:37 +01003963 /* Length field value with CRYPTO frames if present. */
3964 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003965 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003966 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003967 * the congestion controller window. So, we do not limit the size of this
3968 * packet if we have an ACK frame to send because an ACK frame is not
3969 * ack-eliciting. This size will be limited if we have ack-eliciting
3970 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003971 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003972 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003973 size_t path_room;
3974
3975 path_room = quic_path_prep_data(conn->path);
3976 if (end - beg > path_room)
3977 end = beg + path_room;
3978 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003979
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003980 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003981 /* packet number length */
3982 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003983 /* Build the header */
3984 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003985 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3986 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003987 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
3988 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
3989 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003990 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003991 /* Ensure there is enough room for the TLS encryption tag */
3992 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003993 /* Build an ACK frame if required. */
3994 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003995 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003996 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003997 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003998 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
3999 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4000 * that from here, we do not know if we will have to send a PING frame.
4001 * This will be decided after having computed the ack-eliciting frames
4002 * to be added to this packet.
4003 */
4004 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 +01004005 if (!ack_frm_len) {
4006 ssize_t room = end - pos;
4007 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4008 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004009 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004010 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004011 }
4012
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004013 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004014 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004015 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004016 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004017
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004018 /* Initialize the length of the frames built below to <len>.
4019 * If any frame could be successfully built by qc_build_frms(),
4020 * we will have len_frms > len.
4021 */
4022 len_frms = len;
4023 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004024 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4025 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004026 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004027
4028 add_ping_frm = 0;
4029 padding_len = 0;
4030 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004031 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004032 len < QUIC_INITIAL_PACKET_MINLEN) {
4033 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4034 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004035 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004036 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004037 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004038 add_ping_frm = 1;
4039 len += 1;
4040 }
4041 /* If there is no frame at all to follow, add at least a PADDING frame. */
4042 if (!ack_frm_len)
4043 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4044 }
4045
4046 /* Length (of the remaining data). Must not fail because, the buffer size
4047 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004048 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004049 * of this packet.
4050 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004051 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004052 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004053 else
4054 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004055 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004056 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004057
4058 /* Packet number field address. */
4059 *buf_pn = pos;
4060
4061 /* Packet number encoding. */
4062 quic_packet_number_encode(&pos, end, pn, *pn_len);
4063
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004064 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4065 ssize_t room = end - pos;
4066 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4067 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004068 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004069 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004070
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004071 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004072 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004073 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004074
4075 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004076 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004077 ssize_t room = end - pos;
4078 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4079 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004080 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004081 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082 }
4083 }
4084
4085 /* Build a PING frame if needed. */
4086 if (add_ping_frm) {
4087 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004088 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4089 ssize_t room = end - pos;
4090 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4091 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004092 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004093 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004094 }
4095
4096 /* Build a PADDING frame if needed. */
4097 if (padding_len) {
4098 frm.type = QUIC_FT_PADDING;
4099 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004100 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4101 ssize_t room = end - pos;
4102 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4103 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004104 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004105 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004106 }
4107
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004108 /* Always reset this variable as this function has no idea
4109 * if it was set. It is handle by the loss detection timer.
4110 */
4111 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004112 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004113}
4114
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004115static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004116{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004117 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004118 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004119 pkt->cdata_len = 0;
4120 pkt->in_flight_len = 0;
4121 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004122 pkt->next = NULL;
4123 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004124}
4125
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004126/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004127static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4128{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004129 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004130
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004131 if (!pkt)
4132 return;
4133
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004134 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004135 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004136 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004137 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004138 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004139}
4140
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004141/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4142 * type for <qc> QUIC connection from <qel> encryption level.
4143 * Return -2 if the packet could not be allocated or encrypted for any reason,
4144 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004145 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004146static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4147 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004148 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004149 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004150 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004151{
4152 /* The pointer to the packet number field. */
4153 unsigned char *buf_pn;
4154 unsigned char *beg, *end, *payload;
4155 int64_t pn;
4156 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004157 struct quic_tls_ctx *tls_ctx;
4158 struct quic_tx_packet *pkt;
4159
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004160 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004161 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004162 pkt = pool_alloc(pool_head_quic_tx_packet);
4163 if (!pkt) {
4164 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 +02004165 *err = -2;
4166 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004167 }
4168
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004169 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004170 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004171 pn_len = 0;
4172 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004173 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004174 *err = -1;
4175 goto err;
4176 }
4177
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004178 /* Consume a packet number. */
4179 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004180 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 +01004181
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004182 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183 payload = buf_pn + pn_len;
4184 payload_len = end - payload;
4185 aad_len = payload - beg;
4186
4187 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004188 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4189 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004190 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004191 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004192
4193 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004194 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004195 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4196 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4197 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004198 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199 goto err;
4200 }
4201
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004202 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4203 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004204 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004205 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004206 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004207 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004208 pkt->in_flight_len = pkt->len;
4209 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004210 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004211 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4213
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004214 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215
4216 err:
4217 free_quic_tx_packet(pkt);
4218 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004219 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004220}
4221
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004222/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4223 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004224 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004225static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4226 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004227{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004228 size_t try, done = 0;
4229
4230 if (!conn_ctrl_ready(conn))
4231 return 0;
4232
4233 if (!fd_recv_ready(conn->handle.fd))
4234 return 0;
4235
4236 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004237
4238 /* read the largest possible block. For this, we perform only one call
4239 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004240 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004241 */
4242 while (count > 0) {
4243 try = b_contig_space(buf);
4244 if (!try)
4245 break;
4246
4247 if (try > count)
4248 try = count;
4249
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004250 b_add(buf, try);
4251 done += try;
4252 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253 }
4254
4255 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4256 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4257
4258 leave:
4259 return done;
4260
4261 read0:
4262 conn_sock_read0(conn);
4263 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4264
4265 /* Now a final check for a possible asynchronous low-level error
4266 * report. This can happen when a connection receives a reset
4267 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4268 * we might have come from there by just checking POLL_HUP instead
4269 * of recv()'s return value 0, so we have no way to tell there was
4270 * an error without checking.
4271 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004272 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004273 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4274 goto leave;
4275}
4276
4277
4278/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4279 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4280 * other pending data for example, but this flag is ignored at the moment.
4281 * Only one call to send() is performed, unless the buffer wraps, in which case
4282 * a second call may be performed. The connection's flags are updated with
4283 * whatever special event is detected (error, empty). The caller is responsible
4284 * for taking care of those events and avoiding the call if inappropriate. The
4285 * function does not call the connection's polling update function, so the caller
4286 * is responsible for this. It's up to the caller to update the buffer's contents
4287 * based on the return value.
4288 */
4289static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4290{
4291 ssize_t ret;
4292 size_t try, done;
4293 int send_flag;
4294
4295 if (!conn_ctrl_ready(conn))
4296 return 0;
4297
4298 if (!fd_send_ready(conn->handle.fd))
4299 return 0;
4300
4301 done = 0;
4302 /* send the largest possible block. For this we perform only one call
4303 * to send() unless the buffer wraps and we exactly fill the first hunk,
4304 * in which case we accept to do it once again.
4305 */
4306 while (count) {
4307 try = b_contig_data(buf, done);
4308 if (try > count)
4309 try = count;
4310
4311 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4312 if (try < count || flags & CO_SFL_MSG_MORE)
4313 send_flag |= MSG_MORE;
4314
4315 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4316 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4317 if (ret > 0) {
4318 count -= ret;
4319 done += ret;
4320
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004321 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322 conn->flags |= CO_FL_WAIT_L4L6;
4323 /* if the system buffer is full, don't insist */
4324 if (ret < try)
4325 break;
4326 }
4327 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4328 /* nothing written, we need to poll for write first */
4329 fd_cant_send(conn->handle.fd);
4330 break;
4331 }
4332 else if (errno != EINTR) {
4333 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4334 break;
4335 }
4336 }
4337 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4338 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4339
4340 if (done > 0) {
4341 /* we count the total bytes sent, and the send rate for 32-byte
4342 * blocks. The reason for the latter is that freq_ctr are
4343 * limited to 4GB and that it's not enough per second.
4344 */
4345 _HA_ATOMIC_ADD(&global.out_bytes, done);
4346 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4347 }
4348 return done;
4349}
4350
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004351/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4352 * event subscriber <es> is not allowed to change from a previous call as long
4353 * as at least one event is still subscribed. The <event_type> must only be a
4354 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4355 */
4356static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4357{
4358 return conn_subscribe(conn, xprt_ctx, event_type, es);
4359}
4360
4361/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4362 * The <es> pointer is not allowed to differ from the one passed to the
4363 * subscribe() call. It always returns zero.
4364 */
4365static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4366{
4367 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4368}
4369
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004370/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4371 * connection with <xprt_ctx> as address of the xprt context.
4372 * Returns 1 if succeeded, 0 if not.
4373 */
4374static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4375{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004376 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004377
4378 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4379
4380 if (*xprt_ctx)
4381 goto out;
4382
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004383 ctx = pool_alloc(pool_head_quic_conn_ctx);
4384 if (!ctx) {
4385 conn->err_code = CO_ER_SYS_MEMLIM;
4386 goto err;
4387 }
4388
4389 ctx->wait_event.tasklet = tasklet_new();
4390 if (!ctx->wait_event.tasklet) {
4391 conn->err_code = CO_ER_SYS_MEMLIM;
4392 goto err;
4393 }
4394
4395 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4396 ctx->wait_event.tasklet->context = ctx;
4397 ctx->wait_event.events = 0;
4398 ctx->conn = conn;
4399 ctx->subs = NULL;
4400 ctx->xprt_ctx = NULL;
4401
4402 ctx->xprt = xprt_get(XPRT_QUIC);
4403 if (objt_server(conn->target)) {
4404 /* Server */
4405 struct server *srv = __objt_server(conn->target);
4406 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004407 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004408 int ssl_err, ipv4;
4409
4410 ssl_err = SSL_ERROR_NONE;
4411 if (RAND_bytes(dcid, sizeof dcid) != 1)
4412 goto err;
4413
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004414 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004415 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004416 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004417 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004418 goto err;
4419
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004420 /* Insert our SCID, the connection ID for the QUIC client. */
4421 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4422
4423 conn->qc = qc;
4424 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004425 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4426 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004427 goto err;
4428
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004429 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4430 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4431 goto err;
4432
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004433 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004434 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004435 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4436 qc->enc_params_len =
4437 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4438 &qc->rx.params, 0);
4439 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004440 goto err;
4441
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004442 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004443 SSL_set_connect_state(ctx->ssl);
4444 ssl_err = SSL_do_handshake(ctx->ssl);
4445 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004446 int st;
4447
4448 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004449 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4450 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004451 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004452 }
4453 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004454 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004455 goto err;
4456 }
4457 }
4458 }
4459 else if (objt_listener(conn->target)) {
4460 /* Listener */
4461 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004462 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004463
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004464 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004465 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4466 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4467 goto err;
4468
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004469 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004470 SSL_set_accept_state(ctx->ssl);
4471 }
4472
4473 *xprt_ctx = ctx;
4474
4475 /* Leave init state and start handshake */
4476 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477
4478 out:
4479 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4480
4481 return 0;
4482
4483 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004484 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004485 tasklet_free(ctx->wait_event.tasklet);
4486 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004487 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004488 return -1;
4489}
4490
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004491/* Start the QUIC transport layer */
4492static int qc_xprt_start(struct connection *conn, void *ctx)
4493{
4494 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004495 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004496
4497 qc = conn->qc;
4498 if (!quic_conn_init_timer(qc)) {
4499 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4500 return 0;
4501 }
4502
4503 tasklet_wakeup(qctx->wait_event.tasklet);
4504 return 1;
4505}
4506
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004507/* transport-layer operations for QUIC connections. */
4508static struct xprt_ops ssl_quic = {
4509 .snd_buf = quic_conn_from_buf,
4510 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004511 .subscribe = quic_conn_subscribe,
4512 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004513 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004514 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004515 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4516 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4517 .name = "QUIC",
4518};
4519
4520__attribute__((constructor))
4521static void __quic_conn_init(void)
4522{
4523 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4524 xprt_register(XPRT_QUIC, &ssl_quic);
4525}
4526
4527__attribute__((destructor))
4528static void __quic_conn_deinit(void)
4529{
4530 BIO_meth_free(ha_quic_meth);
4531}
4532
4533/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4534 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4535 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004536 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004537 */
4538static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4539 struct sockaddr_storage *saddr, qpkt_read_func *func)
4540{
4541 unsigned char *pos;
4542 const unsigned char *end;
4543 struct quic_dgram_ctx dgram_ctx = {
4544 .dcid_node = NULL,
4545 .owner = owner,
4546 };
4547
4548 pos = (unsigned char *)buf;
4549 end = pos + len;
4550
4551 do {
4552 int ret;
4553 struct quic_rx_packet *pkt;
4554
Willy Tarreaue4498932021-03-22 21:13:05 +01004555 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004556 if (!pkt)
4557 goto err;
4558
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004559 quic_rx_packet_refinc(pkt);
4560 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4561 if (ret == -1) {
4562 size_t pkt_len;
4563
4564 pkt_len = pkt->len;
4565 free_quic_rx_packet(pkt);
4566 /* If the packet length could not be found, we cannot continue. */
4567 if (!pkt_len)
4568 break;
4569 }
4570 } while (pos < end);
4571
4572 /* Increasing the received bytes counter by the UDP datagram length
4573 * if this datagram could be associated to a connection.
4574 */
4575 if (dgram_ctx.qc)
4576 dgram_ctx.qc->rx.bytes += len;
4577
4578 return pos - (unsigned char *)buf;
4579
4580 err:
4581 return -1;
4582}
4583
4584ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4585 struct sockaddr_storage *saddr)
4586{
4587 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4588}
4589
4590ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4591 struct sockaddr_storage *saddr)
4592{
4593 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4594}
4595
4596/* QUIC I/O handler for connection to local listeners or remove servers
4597 * depending on <listener> boolean value, with <fd> as socket file
4598 * descriptor and <ctx> as context.
4599 */
4600static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4601{
4602 ssize_t ret;
4603 size_t done = 0;
4604 struct buffer *buf = get_trash_chunk();
4605 /* Source address */
4606 struct sockaddr_storage saddr = {0};
4607 socklen_t saddrlen = sizeof saddr;
4608
4609 if (!fd_recv_ready(fd))
4610 return 0;
4611
4612 do {
4613 ret = recvfrom(fd, buf->area, buf->size, 0,
4614 (struct sockaddr *)&saddr, &saddrlen);
4615 if (ret < 0) {
4616 if (errno == EINTR)
4617 continue;
4618 if (errno == EAGAIN)
4619 fd_cant_recv(fd);
4620 goto out;
4621 }
4622 } while (0);
4623
4624 done = buf->data = ret;
4625 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4626
4627 out:
4628 return done;
4629}
4630
4631/* QUIC I/O handler for connections to local listeners with <fd> as socket
4632 * file descriptor.
4633 */
4634void quic_fd_handler(int fd)
4635{
Willy Tarreauf5090652021-04-06 17:23:40 +02004636 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004637 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4638}
4639
4640/* QUIC I/O handler for connections to remote servers with <fd> as socket
4641 * file descriptor.
4642 */
4643void quic_conn_fd_handler(int fd)
4644{
Willy Tarreauf5090652021-04-06 17:23:40 +02004645 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004646 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4647}
4648
4649/*
4650 * Local variables:
4651 * c-indent-level: 8
4652 * c-basic-offset: 8
4653 * End:
4654 */