blob: 0fdd45d9ccdb00a6c98b21e44ab77afea1e88dbb [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,
574 (unsigned long long)s->offset,
575 (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
1152/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001153static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001154 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001155{
1156 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001157 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001158 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001159}
1160
1161/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1162 * deallocating them, and their TX frames.
1163 * Returns the last node reached to be used for the next range.
1164 * May be NULL if <largest> node could not be found.
1165 */
1166static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1167 struct list *newly_acked_pkts,
1168 struct eb64_node *largest_node,
1169 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001170 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001171{
1172 struct eb64_node *node;
1173 struct quic_tx_packet *pkt;
1174
1175 if (largest_node)
1176 node = largest_node;
1177 else {
1178 node = eb64_lookup(pkts, largest);
1179 while (!node && largest > smallest) {
1180 node = eb64_lookup(pkts, --largest);
1181 }
1182 }
1183
1184 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001185 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001186
1187 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1188 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001189 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001190 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1191 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1192 qc_treat_acked_tx_frm(frm, ctx);
1193 node = eb64_prev(node);
1194 eb64_delete(&pkt->pn_node);
1195 }
1196
1197 return node;
1198}
1199
1200/* Treat <frm> frame whose packet it is attached to has just been detected as non
1201 * acknowledged.
1202 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001203static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001204 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001205 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001206{
1207 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001208 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001209 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001210}
1211
1212
1213/* Free the TX packets of <pkts> list */
1214static inline void free_quic_tx_pkts(struct list *pkts)
1215{
1216 struct quic_tx_packet *pkt, *tmp;
1217
1218 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001219 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001220 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001221 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001222 }
1223}
1224
1225/* Send a packet loss event nofification to the congestion controller
1226 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1227 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1228 * at <now_us> current time.
1229 * Always succeeds.
1230 */
1231static inline void qc_cc_loss_event(struct quic_conn *qc,
1232 unsigned int lost_bytes,
1233 unsigned int newest_time_sent,
1234 unsigned int period,
1235 unsigned int now_us)
1236{
1237 struct quic_cc_event ev = {
1238 .type = QUIC_CC_EVT_LOSS,
1239 .loss.now_ms = now_ms,
1240 .loss.max_ack_delay = qc->max_ack_delay,
1241 .loss.lost_bytes = lost_bytes,
1242 .loss.newest_time_sent = newest_time_sent,
1243 .loss.period = period,
1244 };
1245
1246 quic_cc_event(&qc->path->cc, &ev);
1247}
1248
1249/* Send a packet ack event nofication for each newly acked packet of
1250 * <newly_acked_pkts> list and free them.
1251 * Always succeeds.
1252 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001253static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001254 struct list *newly_acked_pkts)
1255{
1256 struct quic_conn *qc = ctx->conn->qc;
1257 struct quic_tx_packet *pkt, *tmp;
1258 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1259
1260 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1261 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001262 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001263 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001264 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001265 ev.ack.acked = pkt->in_flight_len;
1266 ev.ack.time_sent = pkt->time_sent;
1267 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001268 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001269 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001270 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001271 }
1272
1273}
1274
1275/* Handle <pkts> list of lost packets detected at <now_us> handling
1276 * their TX frames.
1277 * Send a packet loss event to the congestion controller if
1278 * in flight packet have been lost.
1279 * Also frees the packet in <pkts> list.
1280 * Never fails.
1281 */
1282static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001283 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001284 struct list *pkts,
1285 uint64_t now_us)
1286{
1287 struct quic_conn *qc = ctx->conn->qc;
1288 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001289 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001290 uint64_t lost_bytes;
1291
1292 lost_bytes = 0;
1293 oldest_lost = newest_lost = NULL;
1294 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1295 lost_bytes += pkt->in_flight_len;
1296 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001297 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001298 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001299 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001300 /* Treat the frames of this lost packet. */
1301 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1302 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001303 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001304 if (!oldest_lost) {
1305 oldest_lost = newest_lost = pkt;
1306 }
1307 else {
1308 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001309 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001310 newest_lost = pkt;
1311 }
1312 }
1313
1314 if (lost_bytes) {
1315 /* Sent a packet loss event to the congestion controller. */
1316 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1317 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001318 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001319 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001320 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001321 }
1322}
1323
1324/* Look for packet loss from sent packets for <qel> encryption level of a
1325 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1326 * their tree if deemed as lost or set the <loss_time> value the packet number
1327 * space if any not deemed lost.
1328 * Should be called after having received an ACK frame with newly acknowledged
1329 * packets or when the the loss detection timer has expired.
1330 * Always succeeds.
1331 */
1332static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1333 struct quic_conn *qc,
1334 struct list *lost_pkts)
1335{
1336 struct eb_root *pkts;
1337 struct eb64_node *node;
1338 struct quic_loss *ql;
1339 unsigned int loss_delay;
1340
1341 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1342 pkts = &pktns->tx.pkts;
1343 pktns->tx.loss_time = TICK_ETERNITY;
1344 if (eb_is_empty(pkts))
1345 goto out;
1346
1347 ql = &qc->path->loss;
1348 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1349 loss_delay += loss_delay >> 3;
1350 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1351
1352 node = eb64_first(pkts);
1353 while (node) {
1354 struct quic_tx_packet *pkt;
1355 int64_t largest_acked_pn;
1356 unsigned int loss_time_limit, time_sent;
1357
1358 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001359 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001360 node = eb64_next(node);
1361 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1362 break;
1363
1364 time_sent = pkt->time_sent;
1365 loss_time_limit = tick_add(time_sent, loss_delay);
1366 if (tick_is_le(time_sent, now_ms) ||
1367 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1368 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001369 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001370 }
1371 else {
1372 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1373 }
1374 }
1375
1376 out:
1377 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1378}
1379
1380/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1381 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001382 * 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 +01001383 * acked ack-eliciting packet.
1384 * Return 1, if succeeded, 0 if not.
1385 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001386static 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 +01001387 struct quic_enc_level *qel,
1388 unsigned int *rtt_sample,
1389 const unsigned char **pos, const unsigned char *end)
1390{
1391 struct quic_ack *ack = &frm->ack;
1392 uint64_t smallest, largest;
1393 struct eb_root *pkts;
1394 struct eb64_node *largest_node;
1395 unsigned int time_sent, pkt_flags;
1396 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1397 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1398
1399 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1400 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1401 ctx->conn,, &ack->largest_ack);
1402 goto err;
1403 }
1404
1405 if (ack->first_ack_range > ack->largest_ack) {
1406 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1407 ctx->conn,, &ack->first_ack_range);
1408 goto err;
1409 }
1410
1411 largest = ack->largest_ack;
1412 smallest = largest - ack->first_ack_range;
1413 pkts = &qel->pktns->tx.pkts;
1414 pkt_flags = 0;
1415 largest_node = NULL;
1416 time_sent = 0;
1417
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001418 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 +01001419 largest_node = eb64_lookup(pkts, largest);
1420 if (!largest_node) {
1421 TRACE_DEVEL("Largest acked packet not found",
1422 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1423 goto err;
1424 }
1425
1426 time_sent = eb64_entry(&largest_node->node,
1427 struct quic_tx_packet, pn_node)->time_sent;
1428 }
1429
1430 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1431 ctx->conn,, &largest, &smallest);
1432 do {
1433 uint64_t gap, ack_range;
1434
1435 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1436 largest_node, largest, smallest, ctx);
1437 if (!ack->ack_range_num--)
1438 break;
1439
1440 if (!quic_dec_int(&gap, pos, end))
1441 goto err;
1442
1443 if (smallest < gap + 2) {
1444 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1445 ctx->conn,, &gap, &smallest);
1446 goto err;
1447 }
1448
1449 largest = smallest - gap - 2;
1450 if (!quic_dec_int(&ack_range, pos, end))
1451 goto err;
1452
1453 if (largest < ack_range) {
1454 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1455 ctx->conn,, &largest, &ack_range);
1456 goto err;
1457 }
1458
1459 /* Do not use this node anymore. */
1460 largest_node = NULL;
1461 /* Next range */
1462 smallest = largest - ack_range;
1463
1464 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1465 ctx->conn,, &largest, &smallest);
1466 } while (1);
1467
1468 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001469 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001470
1471 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1472 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001473 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001474 }
1475
1476 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1477 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1478 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1479 if (!LIST_ISEMPTY(&lost_pkts))
1480 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1481 }
1482 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1483 if (quic_peer_validated_addr(ctx))
1484 ctx->conn->qc->path->loss.pto_count = 0;
1485 qc_set_timer(ctx);
1486 }
1487
1488
1489 return 1;
1490
1491 err:
1492 free_quic_tx_pkts(&newly_acked_pkts);
1493 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1494 return 0;
1495}
1496
1497/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1498 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001499 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001500 * Return 1 if succeeded, 0 if not.
1501 */
1502static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001503 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001504 const unsigned char *data, size_t len,
1505 struct quic_rx_packet *pkt,
1506 struct quic_rx_crypto_frm *cf)
1507{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001508 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001509 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001510
1511 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1512 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001513 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001514 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1515 TRACE_PROTO("SSL_provide_quic_data() error",
1516 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1517 goto err;
1518 }
1519
1520 el->rx.crypto.offset += len;
1521 TRACE_PROTO("in order CRYPTO data",
1522 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1523
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001524 state = HA_ATOMIC_LOAD(&qc->state);
1525 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001526 ssl_err = SSL_do_handshake(ctx->ssl);
1527 if (ssl_err != 1) {
1528 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1529 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1530 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001531 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001532 goto out;
1533 }
1534
1535 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001536 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001537 goto err;
1538 }
1539
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001540 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001541 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001542 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001543 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001544 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001545 } else {
1546 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1547 if (ssl_err != 1) {
1548 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1549 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1550 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001551 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001552 goto out;
1553 }
1554
1555 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001556 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001557 goto err;
1558 }
1559
1560 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001561 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001562 }
1563
1564 out:
1565 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1566 return 1;
1567
1568 err:
1569 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1570 return 0;
1571}
1572
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001573/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1574 * <pkt> RX packet.
1575 * Return it if succeeded, NULL if not.
1576 */
1577static inline
1578struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1579 struct quic_rx_packet *pkt)
1580{
1581 struct quic_rx_strm_frm *frm;
1582
1583 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1584 if (frm) {
1585 frm->offset_node.key = stream_frm->offset;
1586 frm->len = stream_frm->len;
1587 frm->data = stream_frm->data;
1588 frm->pkt = pkt;
1589 }
1590
1591 return frm;
1592}
1593
1594/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1595 * several streams, depending on the already open onces.
1596 * Return this node if succeeded, NULL if not.
1597 */
1598static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1599{
1600 unsigned int strm_type;
1601 int64_t sub_id;
1602 struct eb64_node *strm_node;
1603
1604 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1605
1606 strm_type = id & QCS_ID_TYPE_MASK;
1607 sub_id = id >> QCS_ID_TYPE_SHIFT;
1608 strm_node = NULL;
1609 if (qc_local_stream_id(qcc, id)) {
1610 /* Local streams: this stream must be already opened. */
1611 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1612 if (!strm_node) {
1613 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1614 goto out;
1615 }
1616 }
1617 else {
1618 /* Remote streams. */
1619 struct eb_root *strms;
1620 uint64_t largest_id;
1621 enum qcs_type qcs_type;
1622
1623 strms = &qcc->streams_by_id;
1624 qcs_type = qcs_id_type(id);
1625 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1626 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1627 goto out;
1628 }
1629
1630 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1631 * correct value.
1632 */
1633 largest_id = qcc->strms[qcs_type].largest_id;
1634 if (sub_id > (int64_t)largest_id) {
1635 /* RFC: "A stream ID that is used out of order results in all streams
1636 * of that type with lower-numbered stream IDs also being opened".
1637 * So, let's "open" these streams.
1638 */
1639 int64_t i;
1640 struct qcs *qcs;
1641
1642 qcs = NULL;
1643 for (i = largest_id + 1; i <= sub_id; i++) {
1644 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1645 if (!qcs) {
1646 TRACE_PROTO("Could not allocate a new stream",
1647 QUIC_EV_CONN_PSTRM, qcc->conn);
1648 goto out;
1649 }
1650
1651 qcc->strms[qcs_type].largest_id = i;
1652 }
1653 if (qcs)
1654 strm_node = &qcs->by_id;
1655 }
1656 else {
1657 strm_node = eb64_lookup(strms, id);
1658 }
1659 }
1660
1661 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1662 return strm_node;
1663
1664 out:
1665 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1666 return NULL;
1667}
1668
1669/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1670 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1671 * to reflect the data which have been consumed.
1672 */
1673static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1674{
1675 size_t ret;
1676
1677 ret = 0;
1678 while (strm_frm->len) {
1679 size_t try;
1680
1681 try = b_contig_space(buf);
1682 if (!try)
1683 break;
1684
1685 if (try > strm_frm->len)
1686 try = strm_frm->len;
1687 memcpy(b_tail(buf), strm_frm->data, try);
1688 strm_frm->len -= try;
1689 strm_frm->offset += try;
1690 b_add(buf, try);
1691 ret += try;
1692 }
1693
1694 return ret;
1695}
1696
1697/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1698 * streams may be open. The data are copied to the stream RX buffer if possible.
1699 * If not, the STREAM frame is stored to be treated again later.
1700 * We rely on the flow control so that not to store too much STREAM frames.
1701 * Return 1 if succeeded, 0 if not.
1702 */
1703static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1704 struct quic_stream *strm_frm,
1705 struct quic_conn *qc)
1706{
1707 struct qcs *strm;
1708 struct eb64_node *strm_node, *frm_node;
1709 struct quic_rx_strm_frm *frm;
1710
1711 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1712 if (!strm_node) {
1713 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1714 return 0;
1715 }
1716
1717 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1718 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1719 /* FIXME: handle the case where this frame overlap others */
1720 if (frm_node) {
1721 TRACE_PROTO("Already existing stream data",
1722 QUIC_EV_CONN_PSTRM, qc->conn);
1723 goto out;
1724 }
1725
1726 if (strm_frm->offset == strm->rx.offset) {
1727 int ret;
1728
1729 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1730 goto store_frm;
1731
1732 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1733 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1734 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1735 return 0;
1736 }
1737
1738 strm->rx.offset += ret;
1739 }
1740
1741 if (!strm_frm->len)
1742 goto out;
1743
1744 store_frm:
1745 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1746 if (!frm) {
1747 TRACE_PROTO("Could not alloc RX STREAM frame",
1748 QUIC_EV_CONN_PSTRM, qc->conn);
1749 return 0;
1750 }
1751
1752 eb64_insert(&strm->frms, &frm->offset_node);
1753 quic_rx_packet_refinc(pkt);
1754
1755 out:
1756 return 1;
1757}
1758
1759/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1760 * streams may be open. The data are copied to the stream RX buffer if possible.
1761 * If not, the STREAM frame is stored to be treated again later.
1762 * We rely on the flow control so that not to store too much STREAM frames.
1763 * Return 1 if succeeded, 0 if not.
1764 */
1765static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1766 struct quic_stream *strm_frm,
1767 struct quic_conn *qc)
1768{
1769 struct qcs *strm;
1770 struct eb64_node *strm_node, *frm_node;
1771 struct quic_rx_strm_frm *frm;
1772 size_t strm_frm_len;
1773
1774 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1775 if (!strm_node) {
1776 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1777 return 0;
1778 }
1779
1780 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1781 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1782 /* FIXME: handle the case where this frame overlap others */
1783 if (frm_node) {
1784 TRACE_PROTO("Already existing stream data",
1785 QUIC_EV_CONN_PSTRM, qc->conn);
1786 goto out;
1787 }
1788
1789 strm_frm_len = strm_frm->len;
1790 if (strm_frm->offset == strm->rx.offset) {
1791 int ret;
1792
1793 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1794 goto store_frm;
1795
1796 /* qc_strm_cpy() will modify the offset, depending on the number
1797 * of bytes copied.
1798 */
1799 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1800 /* Inform the application of the arrival of this new stream */
1801 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1802 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1803 return 0;
1804 }
1805
1806 if (ret)
1807 ruqs_notify_recv(strm);
1808
1809 strm_frm->offset += ret;
1810 }
1811 /* Take this frame into an account for the stream flow control */
1812 strm->rx.offset += strm_frm_len;
1813 /* It all the data were provided to the application, there is no need to
1814 * store any more inforamtion for it.
1815 */
1816 if (!strm_frm->len)
1817 goto out;
1818
1819 store_frm:
1820 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1821 if (!frm) {
1822 TRACE_PROTO("Could not alloc RX STREAM frame",
1823 QUIC_EV_CONN_PSTRM, qc->conn);
1824 return 0;
1825 }
1826
1827 eb64_insert(&strm->frms, &frm->offset_node);
1828 quic_rx_packet_refinc(pkt);
1829
1830 out:
1831 return 1;
1832}
1833
1834static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1835 struct quic_stream *strm_frm,
1836 struct quic_conn *qc)
1837{
1838 if (strm_frm->id & QCS_ID_DIR_BIT)
1839 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1840 else
1841 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1842}
1843
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001844/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1845 * as I/O handler context and <qel> as encryption level.
1846 * Returns 1 if succeeded, 0 if failed.
1847 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001848static 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 +01001849 struct quic_enc_level *qel)
1850{
1851 struct quic_frame frm;
1852 const unsigned char *pos, *end;
1853 struct quic_conn *conn = ctx->conn->qc;
1854
1855 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1856 /* Skip the AAD */
1857 pos = pkt->data + pkt->aad_len;
1858 end = pkt->data + pkt->len;
1859
1860 while (pos < end) {
1861 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1862 goto err;
1863
1864 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001865 case QUIC_FT_PADDING:
1866 if (pos != end) {
1867 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1868 goto err;
1869 }
1870 break;
1871 case QUIC_FT_PING:
1872 break;
1873 case QUIC_FT_ACK:
1874 {
1875 unsigned int rtt_sample;
1876
1877 rtt_sample = 0;
1878 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1879 goto err;
1880
1881 if (rtt_sample) {
1882 unsigned int ack_delay;
1883
1884 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1885 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1886 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1887 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001888 break;
1889 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001890 case QUIC_FT_CRYPTO:
1891 if (frm.crypto.offset != qel->rx.crypto.offset) {
1892 struct quic_rx_crypto_frm *cf;
1893
1894 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1895 if (!cf) {
1896 TRACE_DEVEL("CRYPTO frame allocation failed",
1897 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1898 goto err;
1899 }
1900
1901 cf->offset_node.key = frm.crypto.offset;
1902 cf->len = frm.crypto.len;
1903 cf->data = frm.crypto.data;
1904 cf->pkt = pkt;
1905 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1906 quic_rx_packet_refinc(pkt);
1907 }
1908 else {
1909 /* XXX TO DO: <cf> is used only for the traces. */
1910 struct quic_rx_crypto_frm cf = { };
1911
1912 cf.offset_node.key = frm.crypto.offset;
1913 cf.len = frm.crypto.len;
1914 if (!qc_provide_cdata(qel, ctx,
1915 frm.crypto.data, frm.crypto.len,
1916 pkt, &cf))
1917 goto err;
1918 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001919 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001920 case QUIC_FT_STREAM_8:
1921 case QUIC_FT_STREAM_9:
1922 case QUIC_FT_STREAM_A:
1923 case QUIC_FT_STREAM_B:
1924 case QUIC_FT_STREAM_C:
1925 case QUIC_FT_STREAM_D:
1926 case QUIC_FT_STREAM_E:
1927 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001928 {
1929 struct quic_stream *stream = &frm.stream;
1930
1931 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1932 if (objt_listener(ctx->conn->target)) {
1933 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1934 goto err;
1935 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1936 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001937
1938 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1939 goto err;
1940
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001941 break;
1942 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001943 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001944 break;
1945 case QUIC_FT_CONNECTION_CLOSE:
1946 case QUIC_FT_CONNECTION_CLOSE_APP:
1947 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001948 case QUIC_FT_HANDSHAKE_DONE:
1949 if (objt_listener(ctx->conn->target))
1950 goto err;
1951
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001952 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001953 break;
1954 default:
1955 goto err;
1956 }
1957 }
1958
1959 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1960 * has successfully parse a Handshake packet. The Initial encryption must also
1961 * be discarded.
1962 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001963 if (HA_ATOMIC_LOAD(&conn->state) == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001964 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1965 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1966 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1967 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001968 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001969 }
1970
1971 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1972 return 1;
1973
1974 err:
1975 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1976 return 0;
1977}
1978
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001979/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
1980 * buffer. This is the responsability of the caller to check there is enough
1981 * room in <cbuf>. Also increase the <cbuf> write index consequently.
1982 * This function must be called only after having built a correct datagram.
1983 * Always succeeds.
1984 */
1985static inline void qc_set_dg(struct cbuf *cbuf,
1986 uint16_t dglen, struct quic_tx_packet *pkt)
1987{
1988 write_u16(cb_wr(cbuf), dglen);
1989 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
1990 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
1991}
1992
1993/* Prepare as much as possible handshake packets into <qr> ring buffer for
1994 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
1995 * several packets in the same datagram. A header made of two fields is added
1996 * to each datagram: the datagram length followed by the address of the first
1997 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001998 * Returns 1 if succeeded, or 0 if something wrong happened.
1999 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002000static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002001{
2002 struct quic_conn *qc;
2003 enum quic_tls_enc_level tel, next_tel;
2004 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002005 struct cbuf *cbuf;
2006 unsigned char *end_buf, *end, *pos, *spos;
2007 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2008 /* length of datagrams */
2009 uint16_t dglen;
2010 size_t total;
2011 /* Each datagram is prepended with its length followed by the
2012 * address of the first packet in the datagram.
2013 */
2014 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002015
2016 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2017 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002018 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002019 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002020 goto err;
2021 }
2022
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002023 start:
2024 total = 0;
2025 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002026 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002027 cbuf = qr->cbuf;
2028 spos = pos = cb_wr(cbuf);
2029 /* Leave at least <dglen> bytes at the end of this buffer
2030 * to ensure there is enough room to mark the end of prepared
2031 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002032 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002033 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2034 first_pkt = prv_pkt = NULL;
2035 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002036 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002037 enum quic_pkt_type pkt_type;
2038
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002039 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002040 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002041 if (!prv_pkt) {
2042 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2043 do {
2044 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2045 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2046 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002047 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002048 /* Do not build any more packet if the TX secrets are not available or
2049 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002050 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002051 * and if there is no more CRYPTO data available or in flight
2052 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002053 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002054 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002055 (!ack && !nb_ptos &&
2056 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2057 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002058 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002059 /* Set the current datagram as prepared into <cbuf> if
2060 * the was already a correct packet which was previously written.
2061 */
2062 if (prv_pkt)
2063 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002064 break;
2065 }
2066
2067 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002068 if (!prv_pkt) {
2069 /* Leave room for the datagram header */
2070 pos += dg_headlen;
2071 end = pos + qc->path->mtu;
2072 }
2073
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002074 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 +02002075 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002076 if (err < 0) {
2077 if (!prv_pkt && nb_ptos)
2078 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002079 if (ack)
2080 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002081 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002082 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002083 case -2:
2084 goto err;
2085 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002086 /* If there was already a correct packet present, set the
2087 * current datagram as prepared into <cbuf>.
2088 */
2089 if (prv_pkt) {
2090 qc_set_dg(cbuf, dglen, first_pkt);
2091 goto stop_build;
2092 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002093 goto out;
2094 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002095 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2096 if (!cur_pkt)
2097 goto err;
2098
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002099 total += cur_pkt->len;
2100 /* keep trace of the first packet in the datagram */
2101 if (!first_pkt)
2102 first_pkt = cur_pkt;
2103 /* Attach the current one to the previous one */
2104 if (prv_pkt)
2105 prv_pkt->next = cur_pkt;
2106 /* Let's say we have to build a new dgram */
2107 prv_pkt = NULL;
2108 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002109 /* Discard the Initial encryption keys as soon as
2110 * a handshake packet could be built.
2111 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002112 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002113 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2114 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2115 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2116 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002117 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002118 }
2119 /* Special case for Initial packets: when they have all
2120 * been sent, select the next level.
2121 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002122 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002123 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2124 (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 +02002125 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2126 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2127 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002128 tel = next_tel;
2129 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002130 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002131 /* If there is data for the next level, do not
2132 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002133 * which sends only one Initial packet, then wait
2134 * for additional CRYPTO data from the server to enter the
2135 * next level.
2136 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002137 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002138 }
2139 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002140 }
2141
2142 /* If we have to build a new datagram, set the current datagram as
2143 * prepared into <cbuf>.
2144 */
2145 if (!prv_pkt) {
2146 qc_set_dg(cbuf, dglen, first_pkt);
2147 first_pkt = NULL;
2148 dglen = 0;
2149 }
2150 }
2151
2152 stop_build:
2153 /* Reset <wr> writer index if in front of <rd> index */
2154 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2155 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2156
2157 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2158 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2159 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2160 /* Mark the end of contiguous data for the reader */
2161 write_u16(cb_wr(cbuf), 0);
2162 cb_add(cbuf, sizeof(uint16_t));
2163 }
2164 }
2165
2166 if (rd && rd <= cbuf->wr) {
2167 cb_wr_reset(cbuf);
2168 if (pos == spos) {
2169 /* Reuse the same buffer if nothing was built. */
2170 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002171 }
2172 }
2173 }
2174
2175 out:
2176 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002177 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002178
2179 err:
2180 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002181 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002182}
2183
2184/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002185 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002186 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002187int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002188{
2189 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002190 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002191
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002192 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002193 cbuf = qr->cbuf;
2194 while (cb_contig_data(cbuf)) {
2195 unsigned char *pos;
2196 struct buffer tmpbuf = { };
2197 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2198 uint16_t dglen;
2199 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002200 unsigned int time_sent;
2201
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002202 pos = cb_rd(cbuf);
2203 dglen = read_u16(pos);
2204 /* End of prepared datagrams.
2205 * Reset the reader index only if in front of the writer index.
2206 */
2207 if (!dglen) {
2208 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2209
2210 if (wr && wr < cbuf->rd) {
2211 cb_rd_reset(cbuf);
2212 continue;
2213 }
2214 break;
2215 }
2216
2217 pos += sizeof dglen;
2218 first_pkt = read_ptr(pos);
2219 pos += sizeof first_pkt;
2220 tmpbuf.area = (char *)pos;
2221 tmpbuf.size = tmpbuf.data = dglen;
2222
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002223 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002224 for (pkt = first_pkt; pkt; pkt = pkt->next)
2225 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002226 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002227 &tmpbuf, tmpbuf.data, 0) <= 0) {
2228 for (pkt = first_pkt; pkt; pkt = pkt->next)
2229 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002230 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002231 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002232
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002233 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002234 qc->tx.bytes += tmpbuf.data;
2235 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002236
2237 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2238 pkt->time_sent = time_sent;
2239 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2240 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002241 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002242 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002243 qc->path->in_flight += pkt->in_flight_len;
2244 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2245 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002246 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002247 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2248 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002249 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002250 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002251 }
2252 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253
2254 return 1;
2255}
2256
2257/* Build all the frames which must be sent just after the handshake have succeeded.
2258 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2259 * a HANDSHAKE_DONE frame.
2260 * Return 1 if succeeded, 0 if not.
2261 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002262static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002263{
2264 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002265 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002266 struct quic_frame *frm;
2267
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002268 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002269 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002270 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002271 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002272 if (!frm)
2273 return 0;
2274
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002275 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002276 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002277 }
2278
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002279 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002280 struct quic_connection_id *cid;
2281
2282 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002283 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002284 if (!frm || !cid)
2285 goto err;
2286
2287 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002288 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002289 }
2290
2291 return 1;
2292
2293 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002294 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002295 return 0;
2296}
2297
2298/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002299void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002300{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002301 struct eb64_node *n;
2302 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002303
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002304 n = eb64_first(&arngs->root);
2305 while (n) {
2306 struct eb64_node *next;
2307
2308 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2309 next = eb64_next(n);
2310 eb64_delete(n);
2311 free(ar);
2312 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002313 }
2314}
2315
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002316/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2317 * descending order.
2318 */
2319static inline size_t sack_gap(struct quic_arng_node *p,
2320 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002321{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002322 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323}
2324
2325
2326/* Remove the last elements of <ack_ranges> list of ack range updating its
2327 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002328 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002329 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002330static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002331{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002332 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002333
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002334 last = eb64_last(&arngs->root);
2335 while (last && arngs->enc_sz > limit) {
2336 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002338 prev = eb64_prev(last);
2339 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002340 return 0;
2341
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002342 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2343 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2344 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2345 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2346 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2347 --arngs->sz;
2348 eb64_delete(last);
2349 pool_free(pool_head_quic_arng, last);
2350 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002351 }
2352
2353 return 1;
2354}
2355
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002356/* Set the encoded size of <arngs> QUIC ack ranges. */
2357static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2358{
2359 struct eb64_node *node, *next;
2360 struct quic_arng_node *ar, *ar_next;
2361
2362 node = eb64_last(&arngs->root);
2363 if (!node)
2364 return;
2365
2366 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2367 arngs->enc_sz = quic_int_getsize(ar->last) +
2368 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2369
2370 while ((next = eb64_prev(node))) {
2371 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2372 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2373 quic_int_getsize(ar_next->last - ar_next->first.key);
2374 node = next;
2375 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2376 }
2377}
2378
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002379/* Insert <ar> ack range into <argns> tree of ack ranges.
2380 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002381 */
2382static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002383struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2384 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002385{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002386 struct quic_arng_node *new_ar;
2387
2388 new_ar = pool_alloc(pool_head_quic_arng);
2389 if (new_ar) {
2390 new_ar->first.key = ar->first;
2391 new_ar->last = ar->last;
2392 eb64_insert(&arngs->root, &new_ar->first);
2393 arngs->sz++;
2394 }
2395
2396 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002397}
2398
2399/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002400 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002401 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002402 *
2403 * Descending order
2404 * ------------->
2405 * range1 range2
2406 * ..........|--------|..............|--------|
2407 * ^ ^ ^ ^
2408 * | | | |
2409 * last1 first1 last2 first2
2410 * ..........+--------+--------------+--------+......
2411 * diff1 gap12 diff2
2412 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002413 * To encode the previous list of ranges we must encode integers as follows in
2414 * descending order:
2415 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002416 * with diff1 = last1 - first1
2417 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002418 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002419 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002420 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002421int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2422 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002423{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002424 struct eb64_node *le;
2425 struct quic_arng_node *new_node;
2426 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002427
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002428 new = NULL;
2429 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002430 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002431 if (!new_node)
2432 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002433
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002434 goto out;
2435 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002436
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002437 le = eb64_lookup_le(&arngs->root, ar->first);
2438 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002439 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002440 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002441 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002443 else {
2444 struct quic_arng_node *le_ar =
2445 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002446
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002447 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002448 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002449 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002450
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002451 if (le_ar->last + 1 >= ar->first) {
2452 le_ar->last = ar->last;
2453 new = le;
2454 new_node = le_ar;
2455 }
2456 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002457 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002458 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002459 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002460
2461 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002462 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002463 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002464
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002465 /* Verify that the new inserted node does not overlap the nodes
2466 * which follow it.
2467 */
2468 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002469 struct eb64_node *next;
2470 struct quic_arng_node *next_node;
2471
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002472 while ((next = eb64_next(new))) {
2473 next_node =
2474 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002475 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002476 break;
2477
2478 if (next_node->last > new_node->last)
2479 new_node->last = next_node->last;
2480 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002481 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002482 /* Decrement the size of these ranges. */
2483 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002484 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002485 }
2486
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002487 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002488 quic_arngs_set_enc_sz(arngs);
2489
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002490 return 1;
2491}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492/* Remove the header protection of packets at <el> encryption level.
2493 * Always succeeds.
2494 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002495static 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 +01002496{
2497 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002498 struct quic_rx_packet *pqpkt;
2499 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002500 struct quic_enc_level *app_qel;
2501
2502 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2503 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2504 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002505 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002506 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002507 TRACE_PROTO("hp not removed (handshake not completed)",
2508 QUIC_EV_CONN_ELRMHP, ctx->conn);
2509 goto out;
2510 }
2511 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002512 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002513 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2514 pqpkt->data + pqpkt->pn_offset,
2515 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2516 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2517 /* XXX TO DO XXX */
2518 }
2519 else {
2520 /* The AAD includes the packet number field */
2521 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2522 /* Store the packet into the tree of packets to decrypt. */
2523 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002524 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002526 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002527 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2528 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002529 MT_LIST_DELETE_SAFE(pkttmp1);
2530 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002531 }
2532
2533 out:
2534 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2535}
2536
2537/* Process all the CRYPTO frame at <el> encryption level.
2538 * Return 1 if succeeded, 0 if not.
2539 */
2540static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002541 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002542{
2543 struct eb64_node *node;
2544
2545 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002546 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002547 node = eb64_first(&el->rx.crypto.frms);
2548 while (node) {
2549 struct quic_rx_crypto_frm *cf;
2550
2551 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2552 if (cf->offset_node.key != el->rx.crypto.offset)
2553 break;
2554
2555 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2556 goto err;
2557
2558 node = eb64_next(node);
2559 quic_rx_packet_refdec(cf->pkt);
2560 eb64_delete(&cf->offset_node);
2561 pool_free(pool_head_quic_rx_crypto_frm, cf);
2562 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002563 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002564 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2565 return 1;
2566
2567 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002568 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002569 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2570 return 0;
2571}
2572
2573/* Process all the packets at <el> encryption level.
2574 * Return 1 if succeeded, 0 if not.
2575 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002576int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2577 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002578{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002579 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002580 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002581 struct quic_conn *qc = ctx->conn->qc;
2582 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002583
2584 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002585 qel = cur_el;
2586 next_tel:
2587 if (!qel)
2588 goto out;
2589
2590 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2591 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002592 while (node) {
2593 struct quic_rx_packet *pkt;
2594
2595 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002596 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2597 ctx->conn, pkt, NULL, ctx->ssl);
2598 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002599 /* Drop the packet */
2600 TRACE_PROTO("packet decryption failed -> dropped",
2601 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2602 }
2603 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002604 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002605 /* Drop the packet */
2606 TRACE_PROTO("packet parsing failed -> dropped",
2607 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2608 }
2609 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002610 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2611
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002612 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002613 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2614 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002615 if (pkt->pn > largest_pn)
2616 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002617 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002618 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002619 TRACE_DEVEL("Could not update ack range list",
2620 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002621 }
2622 }
2623 node = eb64_next(node);
2624 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002625 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002626 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002627
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002628 /* Update the largest packet number. */
2629 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002630 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2631 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002632 goto err;
2633
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002634 if (qel == cur_el) {
2635 qel = next_el;
2636 goto next_tel;
2637 }
2638
2639 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2641 return 1;
2642
2643 err:
2644 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2645 return 0;
2646}
2647
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002648/* QUIC connection packet handler task. */
2649struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002651 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002652 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002653 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002654 enum quic_tls_enc_level tel, next_tel;
2655 struct quic_enc_level *qel, *next_qel;
2656 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002657 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002658 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002659
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002660 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002661 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002662 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002663 st = HA_ATOMIC_LOAD(&qc->state);
2664 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002666 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002667 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 goto err;
2669
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002670 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002671 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002672
2673 next_level:
2674 tls_ctx = &qel->tls_ctx;
2675
2676 /* If the header protection key for this level has been derived,
2677 * remove the packet header protections.
2678 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002679 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002680 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2681 qc_rm_hp_pkts(qel, ctx);
2682
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002683 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002684 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2685 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2686 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002687 goto err;
2688
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002689 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002690 if (st >= QUIC_HS_ST_COMPLETE &&
2691 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002692 /* Discard the Handshake keys. */
2693 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2694 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2695 qc_set_timer(ctx);
2696 if (!quic_build_post_handshake_frames(qc))
2697 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002698 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002699 }
2700
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002701 if (!qr)
2702 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2703 ret = qc_prep_hdshk_pkts(qr, ctx);
2704 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002705 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002706 else if (ret == 0)
2707 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002709 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002710 goto err;
2711
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002712 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002713 /* Check if there is something to do for the next level.
2714 */
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002715 if (next_qel && (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002716 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002717 qel = next_qel;
2718 goto next_level;
2719 }
2720
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002721 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002722 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002723 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002724
2725 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002726 if (qr)
2727 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002728 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002729 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002730}
2731
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002732/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002733static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2734{
2735 int i;
2736
2737 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2738 if (qel->tx.crypto.bufs[i]) {
2739 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2740 qel->tx.crypto.bufs[i] = NULL;
2741 }
2742 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002743 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002744}
2745
2746/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002747 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748 * Returns 1 if succeeded, 0 if not.
2749 */
2750static int quic_conn_enc_level_init(struct quic_conn *qc,
2751 enum quic_tls_enc_level level)
2752{
2753 struct quic_enc_level *qel;
2754
2755 qel = &qc->els[level];
2756 qel->level = quic_to_ssl_enc_level(level);
2757 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2758 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2759 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2760 qel->tls_ctx.rx.flags = 0;
2761 qel->tls_ctx.tx.flags = 0;
2762
2763 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002764 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002765 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002766 qel->rx.crypto.offset = 0;
2767 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2768 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002769
2770 /* Allocate only one buffer. */
2771 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2772 if (!qel->tx.crypto.bufs)
2773 goto err;
2774
2775 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2776 if (!qel->tx.crypto.bufs[0])
2777 goto err;
2778
2779 qel->tx.crypto.bufs[0]->sz = 0;
2780 qel->tx.crypto.nb_buf = 1;
2781
2782 qel->tx.crypto.sz = 0;
2783 qel->tx.crypto.offset = 0;
2784
2785 return 1;
2786
2787 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002788 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002789 return 0;
2790}
2791
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792/* Release all the memory allocated for <conn> QUIC connection. */
2793static void quic_conn_free(struct quic_conn *conn)
2794{
2795 int i;
2796
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002797 if (!conn)
2798 return;
2799
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002800 free_quic_conn_cids(conn);
2801 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2802 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002803 if (conn->timer_task)
2804 task_destroy(conn->timer_task);
2805 pool_free(pool_head_quic_conn, conn);
2806}
2807
2808/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002809static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002810{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002811 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002812 struct quic_conn *qc;
2813 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002814 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002815
2816 conn_ctx = task->context;
2817 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002818 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2819 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002820 task->expire = TICK_ETERNITY;
2821 pktns = quic_loss_pktns(qc);
2822 if (tick_isset(pktns->tx.loss_time)) {
2823 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2824
2825 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2826 if (!LIST_ISEMPTY(&lost_pkts))
2827 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2828 qc_set_timer(conn_ctx);
2829 goto out;
2830 }
2831
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002832 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002833 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002834 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002835 pktns->tx.pto_probe = 1;
2836 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002837 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002838 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2839 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2840
2841 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2842 hel->pktns->tx.pto_probe = 1;
2843 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2844 iel->pktns->tx.pto_probe = 1;
2845 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002846 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002847 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2848 qc->path->loss.pto_count++;
2849
2850 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002851 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852
2853 return task;
2854}
2855
2856/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2857 * connections used to identify the first Initial packets of client connecting
2858 * to listeners. This parameter must be NULL for QUIC connections attached
2859 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2860 * <scid> is the source connection ID with <scid_len> as length.
2861 * Returns 1 if succeeded, 0 if not.
2862 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002863static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2864 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002865 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002866{
2867 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002868 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002869 /* Initial CID. */
2870 struct quic_connection_id *icid;
2871
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002872 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002873 qc = pool_zalloc(pool_head_quic_conn);
2874 if (!qc) {
2875 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2876 goto err;
2877 }
2878
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002879 qc->cids = EB_ROOT;
2880 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002881 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002882 struct listener *l = owner;
2883
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002884 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002885 /* Copy the initial DCID. */
2886 qc->odcid.len = dcid_len;
2887 if (qc->odcid.len)
2888 memcpy(qc->odcid.data, dcid, dcid_len);
2889
2890 /* Copy the SCID as our DCID for this connection. */
2891 if (scid_len)
2892 memcpy(qc->dcid.data, scid, scid_len);
2893 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002894 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895 }
2896 /* QUIC Client (outgoing connection to servers) */
2897 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002898 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899 if (dcid_len)
2900 memcpy(qc->dcid.data, dcid, dcid_len);
2901 qc->dcid.len = dcid_len;
2902 }
2903
2904 /* Initialize the output buffer */
2905 qc->obuf.pos = qc->obuf.data;
2906
2907 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002908 if (!icid) {
2909 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2910 goto err;
2911 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002912
2913 /* Select our SCID which is the first CID with 0 as sequence number. */
2914 qc->scid = icid->cid;
2915
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002916 /* Packet number spaces initialization. */
2917 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2918 quic_pktns_init(&qc->pktns[i]);
2919 /* QUIC encryption level context initialization. */
2920 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002921 if (!quic_conn_enc_level_init(qc, i)) {
2922 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002923 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002924 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002925 /* Initialize the packet number space. */
2926 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2927 }
2928
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002929 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002930 /* TX part. */
2931 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002932 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2933 qc->tx.wbuf = qc->tx.rbuf = 0;
2934 qc->tx.bytes = 0;
2935 qc->tx.nb_pto_dgrams = 0;
2936 /* RX part. */
2937 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002938 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002939
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002940 /* XXX TO DO: Only one path at this time. */
2941 qc->path = &qc->paths[0];
2942 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2943
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002944 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002945
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002946 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002947
2948 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002949 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002950 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002951 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002952}
2953
2954/* Initialize the timer task of <qc> QUIC connection.
2955 * Returns 1 if succeeded, 0 if not.
2956 */
2957static int quic_conn_init_timer(struct quic_conn *qc)
2958{
2959 qc->timer_task = task_new(MAX_THREADS_MASK);
2960 if (!qc->timer_task)
2961 return 0;
2962
2963 qc->timer = TICK_ETERNITY;
2964 qc->timer_task->process = process_timer;
2965 qc->timer_task->context = qc->conn->xprt_ctx;
2966
2967 return 1;
2968}
2969
2970/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2971 * past one byte of this buffer.
2972 */
2973static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2974 struct quic_rx_packet *pkt)
2975{
2976 unsigned char dcid_len, scid_len;
2977
2978 /* Version */
2979 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2980 return 0;
2981
2982 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2983
2984 /* Destination Connection ID Length */
2985 dcid_len = *(*buf)++;
2986 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2987 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2988 /* XXX MUST BE DROPPED */
2989 return 0;
2990
2991 if (dcid_len) {
2992 /* Check that the length of this received DCID matches the CID lengths
2993 * of our implementation for non Initials packets only.
2994 */
2995 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2996 return 0;
2997
2998 memcpy(pkt->dcid.data, *buf, dcid_len);
2999 }
3000
3001 pkt->dcid.len = dcid_len;
3002 *buf += dcid_len;
3003
3004 /* Source Connection ID Length */
3005 scid_len = *(*buf)++;
3006 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3007 /* XXX MUST BE DROPPED */
3008 return 0;
3009
3010 if (scid_len)
3011 memcpy(pkt->scid.data, *buf, scid_len);
3012 pkt->scid.len = scid_len;
3013 *buf += scid_len;
3014
3015 return 1;
3016}
3017
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003018/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3019 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3020 * encryption level matching with the packet type. <*qel> may be null if not found.
3021 * Note that <ctx> may be null (for Initial packets).
3022 */
3023static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003024 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003025 struct quic_enc_level **qel)
3026{
3027 enum quic_tls_enc_level tel;
3028
3029 /* Special case without connection context (firt Initial packets) */
3030 if (!ctx) {
3031 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3032 return 1;
3033 }
3034
3035 tel = quic_packet_type_enc_level(pkt->type);
3036 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3037 *qel = NULL;
3038 return 0;
3039 }
3040
3041 *qel = &qc->els[tel];
3042 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3043 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3044 return 0;
3045 }
3046
3047 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003048 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3049 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003050 return 1;
3051
3052 return 0;
3053}
3054
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3056 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3057 * byte past the end of the buffer containing this packet and <beg> the address of
3058 * the packet first byte.
3059 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3060 * Returns 1 if succeeded, 0 if not.
3061 */
3062static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3063 unsigned char **buf, unsigned char *beg,
3064 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003065 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003066{
3067 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003068 struct quic_enc_level *qel;
3069 /* Only for traces. */
3070 struct quic_rx_packet *qpkt_trace;
3071
3072 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003073 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003074 /* The packet number is here. This is also the start minus
3075 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3076 * protection.
3077 */
3078 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003079 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003080 /* Note that the following function enables us to unprotect the packet
3081 * number and its length subsequently used to decrypt the entire
3082 * packets.
3083 */
3084 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3085 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003086 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003087 goto err;
3088 }
3089
3090 /* The AAD includes the packet number field found at <pn>. */
3091 pkt->aad_len = pn - beg + pkt->pnl;
3092 qpkt_trace = pkt;
3093 /* Store the packet */
3094 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003095 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003096 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003097 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003098 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003099 else if (qel) {
3100 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003101 pkt->pn_offset = pn - beg;
3102 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3103 }
3104
3105 memcpy(pkt->data, beg, pkt->len);
3106 /* Updtate the offset of <*buf> for the next QUIC packet. */
3107 *buf = beg + pkt->len;
3108
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003109 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003110 return 1;
3111
3112 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003113 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 +01003114 return 0;
3115}
3116
3117/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3118 * Also set <*long_header> to 1 if this form is long, 0 if not.
3119 */
3120static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3121 unsigned char byte0, int *long_header)
3122{
3123 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3124 pkt->type =
3125 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3126 *long_header = 1;
3127 }
3128 else {
3129 pkt->type = QUIC_PACKET_TYPE_SHORT;
3130 *long_header = 0;
3131 }
3132}
3133
3134static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3135 struct quic_rx_packet *pkt,
3136 struct quic_dgram_ctx *dgram_ctx,
3137 struct sockaddr_storage *saddr)
3138{
3139 unsigned char *beg;
3140 uint64_t len;
3141 struct quic_conn *qc;
3142 struct eb_root *cids;
3143 struct ebmb_node *node;
3144 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003145 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003146 int long_header;
3147
3148 qc = NULL;
3149 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3150 if (end <= *buf)
3151 goto err;
3152
3153 /* Fixed bit */
3154 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3155 /* XXX TO BE DISCARDED */
3156 goto err;
3157
3158 srv_conn = dgram_ctx->owner;
3159 beg = *buf;
3160 /* Header form */
3161 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3162 if (long_header) {
3163 size_t cid_lookup_len;
3164
3165 if (!quic_packet_read_long_header(buf, end, pkt))
3166 goto err;
3167
3168 /* For Initial packets, and for servers (QUIC clients connections),
3169 * there is no Initial connection IDs storage.
3170 */
3171 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3172 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3173 cid_lookup_len = pkt->dcid.len;
3174 }
3175 else {
3176 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3177 cid_lookup_len = QUIC_CID_LEN;
3178 }
3179
3180 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3181 if (!node)
3182 goto err;
3183
3184 qc = ebmb_entry(node, struct quic_conn, scid_node);
3185
3186 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3187 qc->dcid.len = pkt->scid.len;
3188 if (pkt->scid.len)
3189 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3190 }
3191
3192 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3193 uint64_t token_len;
3194
3195 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3196 goto err;
3197
3198 /* XXX TO DO XXX 0 value means "the token is not present".
3199 * A server which sends an Initial packet must not set the token.
3200 * So, a client which receives an Initial packet with a token
3201 * MUST discard the packet or generate a connection error with
3202 * PROTOCOL_VIOLATION as type.
3203 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3204 */
3205 pkt->token_len = token_len;
3206 }
3207 }
3208 else {
3209 /* XXX TO DO: Short header XXX */
3210 if (end - *buf < QUIC_CID_LEN)
3211 goto err;
3212
3213 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3214 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3215 if (!node)
3216 goto err;
3217
3218 qc = ebmb_entry(node, struct quic_conn, scid_node);
3219 *buf += QUIC_CID_LEN;
3220 }
3221 /* Store the DCID used for this packet to check the packet which
3222 * come in this UDP datagram match with it.
3223 */
3224 if (!dgram_ctx->dcid_node)
3225 dgram_ctx->dcid_node = node;
3226 /* Only packets packets with long headers and not RETRY or VERSION as type
3227 * have a length field.
3228 */
3229 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3230 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3231 goto err;
3232
3233 pkt->len = len;
3234 }
3235 else if (!long_header) {
3236 /* A short packet is the last one of an UDP datagram. */
3237 pkt->len = end - *buf;
3238 }
3239
3240 conn_ctx = qc->conn->xprt_ctx;
3241
3242 /* Increase the total length of this packet by the header length. */
3243 pkt->len += *buf - beg;
3244 /* Do not check the DCID node before the length. */
3245 if (dgram_ctx->dcid_node != node) {
3246 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3247 goto err;
3248 }
3249
3250 if (pkt->len > sizeof pkt->data) {
3251 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3252 goto err;
3253 }
3254
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003255 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003256 goto err;
3257
3258 /* Wake the tasklet of the QUIC connection packet handler. */
3259 if (conn_ctx)
3260 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3261
3262 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3263
3264 return pkt->len;
3265
3266 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003267 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003268 return -1;
3269}
3270
3271static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3272 struct quic_rx_packet *pkt,
3273 struct quic_dgram_ctx *dgram_ctx,
3274 struct sockaddr_storage *saddr)
3275{
3276 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003277 struct quic_conn *qc;
3278 struct eb_root *cids;
3279 struct ebmb_node *node;
3280 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003281 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003282 int long_header = 0;
3283
3284 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003285 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003286 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003287 if (end <= *buf)
3288 goto err;
3289
3290 /* Fixed bit */
3291 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3292 /* XXX TO BE DISCARDED */
3293 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3294 goto err;
3295 }
3296
3297 l = dgram_ctx->owner;
3298 beg = *buf;
3299 /* Header form */
3300 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3301 if (long_header) {
3302 unsigned char dcid_len;
3303
3304 if (!quic_packet_read_long_header(buf, end, pkt)) {
3305 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3306 goto err;
3307 }
3308
3309 dcid_len = pkt->dcid.len;
3310 /* For Initial packets, and for servers (QUIC clients connections),
3311 * there is no Initial connection IDs storage.
3312 */
3313 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003314 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003315 /* DCIDs of first packets coming from clients may have the same values.
3316 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3317 */
3318 quic_cid_saddr_cat(&pkt->dcid, saddr);
3319 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003320
3321 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3322 end - *buf < token_len) {
3323 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3324 goto err;
3325 }
3326
3327 /* XXX TO DO XXX 0 value means "the token is not present".
3328 * A server which sends an Initial packet must not set the token.
3329 * So, a client which receives an Initial packet with a token
3330 * MUST discard the packet or generate a connection error with
3331 * PROTOCOL_VIOLATION as type.
3332 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3333 */
3334 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003335 }
3336 else {
3337 if (pkt->dcid.len != QUIC_CID_LEN) {
3338 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3339 goto err;
3340 }
3341
3342 cids = &l->rx.cids;
3343 }
3344
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003345 /* Only packets packets with long headers and not RETRY or VERSION as type
3346 * have a length field.
3347 */
3348 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3349 uint64_t len;
3350
3351 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3352 end - *buf < len) {
3353 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3354 goto err;
3355 }
3356
3357 pkt->len = len;
3358 }
3359
3360
3361 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003362 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3363 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3364 cids == &l->rx.odcids) {
3365 /* Switch to the definitive tree ->cids containing the final CIDs. */
3366 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3367 if (node) {
3368 /* If found, signal this with NULL as special value for <cids>. */
3369 pkt->dcid.len = dcid_len;
3370 cids = NULL;
3371 }
3372 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003373 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003374
3375 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003376 int ipv4;
3377 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003378 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003379 const unsigned char *salt = initial_salt_v1;
3380 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003381
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003382 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3383 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3384 goto err;
3385 }
3386
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003387 pkt->saddr = *saddr;
3388 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3389 * of <saddr>.
3390 */
3391 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003392 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003393 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003394 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003395 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003396 goto err;
3397
3398 odcid = &qc->rx.params.original_destination_connection_id;
3399 /* Copy the transport parameters. */
3400 qc->rx.params = l->bind_conf->quic_params;
3401 /* Copy original_destination_connection_id transport parameter. */
3402 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3403 odcid->len = pkt->odcid_len;
3404 /* Copy the initial source connection ID. */
3405 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3406 qc->enc_params_len =
3407 quic_transport_params_encode(qc->enc_params,
3408 qc->enc_params + sizeof qc->enc_params,
3409 &qc->rx.params, 1);
3410 if (!qc->enc_params_len)
3411 goto err;
3412
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003413 /* NOTE: the socket address has been concatenated to the destination ID
3414 * chosen by the client for Initial packets.
3415 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003416 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3417 salt = initial_salt_draft_29;
3418 salt_len = sizeof initial_salt_draft_29;
3419 }
3420 if (!qc_new_isecs(qc, salt, salt_len,
3421 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003422 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3423 goto err;
3424 }
3425
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003426 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003427 /* This is the DCID node sent in this packet by the client. */
3428 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003429 /* Enqueue this packet. */
3430 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3431 /* Try to accept a new connection. */
3432 listener_accept(l);
3433
3434 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3435 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3436 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3437 /* Insert our SCID, the connection ID for the QUIC client. */
3438 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3439 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3440 if (n != &qc->scid_node) {
3441 quic_conn_free(qc);
3442 qc = ebmb_entry(n, struct quic_conn, scid_node);
3443 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003444 }
3445 else {
3446 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3447 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3448 else
3449 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003450 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003451 }
3452 }
3453 else {
3454 if (end - *buf < QUIC_CID_LEN) {
3455 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3456 goto err;
3457 }
3458
3459 cids = &l->rx.cids;
3460 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3461 if (!node) {
3462 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3463 goto err;
3464 }
3465
3466 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003467 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003468 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003469 /* A short packet is the last one of an UDP datagram. */
3470 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003471 }
3472
3473 /* Store the DCID used for this packet to check the packet which
3474 * come in this UDP datagram match with it.
3475 */
3476 if (!dgram_ctx->dcid_node) {
3477 dgram_ctx->dcid_node = node;
3478 dgram_ctx->qc = qc;
3479 }
3480
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003481 /* Increase the total length of this packet by the header length. */
3482 pkt->len += *buf - beg;
3483 /* Do not check the DCID node before the length. */
3484 if (dgram_ctx->dcid_node != node) {
3485 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3486 goto err;
3487 }
3488
3489 if (pkt->len > sizeof pkt->data) {
3490 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3491 goto err;
3492 }
3493
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003494 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3495 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3496 goto err;
3497 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003498
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003499
3500 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003501 /* Wake up the connection packet handler task from here only if all
3502 * the contexts have been initialized, especially the mux context
3503 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3504 * will start it if these contexts for the connection are not already
3505 * initialized.
3506 */
3507 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003508 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003509
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3511
3512 return pkt->len;
3513
3514 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003515 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003516 qc ? qc->conn : NULL, pkt);
3517 return -1;
3518}
3519
3520/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3521 * in advance. This is the reponsability of the caller to check there is enough room in this
3522 * buffer to build a long header.
3523 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3524 */
3525static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3526 int type, size_t pn_len, struct quic_conn *conn)
3527{
3528 if (type > QUIC_PACKET_TYPE_RETRY)
3529 return 0;
3530
3531 /* #0 byte flags */
3532 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3533 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3534 /* Version */
3535 quic_write_uint32(buf, end, conn->version);
3536 *(*buf)++ = conn->dcid.len;
3537 /* Destination connection ID */
3538 if (conn->dcid.len) {
3539 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3540 *buf += conn->dcid.len;
3541 }
3542 /* Source connection ID */
3543 *(*buf)++ = conn->scid.len;
3544 if (conn->scid.len) {
3545 memcpy(*buf, conn->scid.data, conn->scid.len);
3546 *buf += conn->scid.len;
3547 }
3548
3549 return 1;
3550}
3551
3552/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3553 * in advance. This is the reponsability of the caller to check there is enough room in this
3554 * buffer to build a long header.
3555 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3556 */
3557static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3558 size_t pn_len, struct quic_conn *conn)
3559{
3560 /* #0 byte flags */
3561 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3562 /* Destination connection ID */
3563 if (conn->dcid.len) {
3564 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3565 *buf += conn->dcid.len;
3566 }
3567
3568 return 1;
3569}
3570
3571/* Apply QUIC header protection to the packet with <buf> as first byte address,
3572 * <pn> as address of the Packet number field, <pnlen> being this field length
3573 * with <aead> as AEAD cipher and <key> as secret key.
3574 * Returns 1 if succeeded or 0 if failed.
3575 */
3576static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3577 const EVP_CIPHER *aead, const unsigned char *key)
3578{
3579 int i, ret, outlen;
3580 EVP_CIPHER_CTX *ctx;
3581 /* We need an IV of at least 5 bytes: one byte for bytes #0
3582 * and at most 4 bytes for the packet number
3583 */
3584 unsigned char mask[5] = {0};
3585
3586 ret = 0;
3587 ctx = EVP_CIPHER_CTX_new();
3588 if (!ctx)
3589 return 0;
3590
3591 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3592 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3593 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3594 goto out;
3595
3596 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3597 for (i = 0; i < pnlen; i++)
3598 pn[i] ^= mask[i + 1];
3599
3600 ret = 1;
3601
3602 out:
3603 EVP_CIPHER_CTX_free(ctx);
3604
3605 return ret;
3606}
3607
3608/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3609 * ACK ranges if needed to a value below <limit> in bytes.
3610 * Return 1 if succeeded, 0 if not.
3611 */
3612static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3613{
3614 size_t room, ack_delay_sz;
3615
3616 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3617 /* A frame is made of 1 byte for the frame type. */
3618 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003619 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003620 return 0;
3621
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003622 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003623}
3624
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003625/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3626 * 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 +01003627 * and <*len> the packet Length field initialized with the number of bytes already present
3628 * 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 +02003629 * <headlen> is the number of bytes already present in this packet before building frames.
3630 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003631 * Update consequently <*len> to reflect the size of these frames built
3632 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003633 * Return 1 if succeeded, 0 if not.
3634 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003635static inline int qc_build_frms(struct quic_tx_packet *pkt,
3636 size_t room, size_t *len, size_t headlen,
3637 struct quic_enc_level *qel,
3638 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003639{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003640 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003641 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003642 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003643 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003644
Frédéric Lécailleea604992020-12-24 13:01:37 +01003645 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003646 if (*len > room || headlen > remain)
3647 return 0;
3648
Frédéric Lécailleea604992020-12-24 13:01:37 +01003649 /* If we are not probing we must take into an account the congestion
3650 * control window.
3651 */
3652 if (!conn->tx.nb_pto_dgrams)
3653 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003654 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003655 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003656 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 +01003657 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003658 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003659
Frédéric Lécailleea604992020-12-24 13:01:37 +01003660 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003661 break;
3662
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003663 switch (cf->type) {
3664 case QUIC_FT_CRYPTO:
3665 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3666 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3667 /* Compute the length of this CRYPTO frame header */
3668 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3669 /* Compute the data length of this CRyPTO frame. */
3670 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3671 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3672 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3673 if (!dlen)
3674 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003675
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003676 pkt->cdata_len += dlen;
3677 /* CRYPTO frame length. */
3678 flen = hlen + quic_int_getsize(dlen) + dlen;
3679 TRACE_PROTO(" CRYPTO frame length (flen)",
3680 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3681 /* Add the CRYPTO data length and its encoded length to the packet
3682 * length and the length of this length.
3683 */
3684 *len += flen;
3685 room -= flen;
3686 if (dlen == cf->crypto.len) {
3687 /* <cf> CRYPTO data have been consumed. */
3688 MT_LIST_DELETE_SAFE(tmp1);
3689 LIST_APPEND(&pkt->frms, &cf->list);
3690 }
3691 else {
3692 struct quic_frame *new_cf;
3693
3694 new_cf = pool_alloc(pool_head_quic_frame);
3695 if (!new_cf) {
3696 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3697 return 0;
3698 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003699
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003700 new_cf->type = QUIC_FT_CRYPTO;
3701 new_cf->crypto.len = dlen;
3702 new_cf->crypto.offset = cf->crypto.offset;
3703 new_cf->crypto.qel = qel;
3704 LIST_APPEND(&pkt->frms, &new_cf->list);
3705 /* Consume <dlen> bytes of the current frame. */
3706 cf->crypto.len -= dlen;
3707 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003708 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003709 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003710
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003711 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003712 /* Note that these frames are accepted in short packets only without
3713 * "Length" packet field. Here, <*len> is used only to compute the
3714 * sum of the lengths of the already built frames for this packet.
3715 */
3716 TRACE_PROTO(" New STREAM frame build (room, len)",
3717 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3718 /* Compute the length of this STREAM frame "header" made a all the field
3719 * excepting the variable ones. Note that +1 is for the type of this frame.
3720 */
3721 hlen = 1 + quic_int_getsize(cf->stream.id) +
3722 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset) : 0);
3723 /* Compute the data length of this STREAM frame. */
3724 avail_room = room - hlen - *len;
3725 if ((ssize_t)avail_room <= 0)
3726 continue;
3727
3728 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3729 dlen = max_available_room(avail_room, &dlen_sz);
3730 if (dlen > cf->stream.len) {
3731 dlen = cf->stream.len;
3732 }
3733 dlen_sz = quic_int_getsize(dlen);
3734 flen = hlen + dlen_sz + dlen;
3735 }
3736 else {
3737 dlen = QUIC_MIN(avail_room, cf->stream.len);
3738 flen = hlen + dlen;
3739 }
3740 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3741 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3742 TRACE_PROTO(" STREAM frame length (flen)",
3743 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3744 /* Add the STREAM data length and its encoded length to the packet
3745 * length and the length of this length.
3746 */
3747 *len += flen;
3748 room -= flen;
3749 if (dlen == cf->stream.len) {
3750 /* <cf> STREAM data have been consumed. */
3751 MT_LIST_DELETE_SAFE(tmp1);
3752 LIST_APPEND(&pkt->frms, &cf->list);
3753 }
3754 else {
3755 struct quic_frame *new_cf;
3756
3757 new_cf = pool_zalloc(pool_head_quic_frame);
3758 if (!new_cf) {
3759 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3760 return 0;
3761 }
3762
3763 new_cf->type = cf->type;
3764 new_cf->stream.id = cf->stream.id;
3765 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3766 new_cf->stream.offset = cf->stream.offset;
3767 new_cf->stream.len = dlen;
3768 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3769 /* FIN bit reset */
3770 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3771 new_cf->stream.data = cf->stream.data;
3772 LIST_APPEND(&pkt->frms, &new_cf->list);
3773 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3774 /* Consume <dlen> bytes of the current frame. */
3775 cf->stream.len -= dlen;
3776 cf->stream.offset += dlen;
3777 cf->stream.data += dlen;
3778 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003779 break;
3780
3781 default:
3782 flen = qc_frm_len(cf);
3783 BUG_ON(!flen);
3784 if (flen > room)
3785 continue;
3786
3787 *len += flen;
3788 room -= flen;
3789 MT_LIST_DELETE_SAFE(tmp1);
3790 LIST_APPEND(&pkt->frms, &cf->list);
3791 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003793 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794 }
3795
Frédéric Lécailleea604992020-12-24 13:01:37 +01003796 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797}
3798
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003799/* This function evaluates if <pkt> packet may be built into a buffer with
3800 * <room> as available room. A valid packet should at least contain a valid
3801 * header and at least a frame.
3802 * To estimate the minimal space to build a packet, we consider the worst case:
3803 - there is not enough space to build ack-eliciting frames from
3804 qel->pktns->tx.frms. This is safe to consider this because when we build
3805 a packet we first build the ACK frames, then the ack-eliciting frames
3806 from qel->pktns->tx.frms only if there is enough space for these
3807 ack-eliciting frames, finally PING and PADDING frames if needed,
3808 - we have to ensure there is enough space to build an ACK frame if required,
3809 and a PING frame, even if we do not have to probe,
3810 - we also have to verify there is enough space to build a PADDING frame
3811 if needed, especially if there is no need to send an ACK frame.
3812 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3813 * a valid packet).
3814 */
3815static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3816 int ack, int nb_pto_dgrams,
3817 struct quic_enc_level *qel, struct quic_conn *conn)
3818{
3819 size_t minlen, token_fields_len;
3820 /* XXX FIXME XXX : ack delay not supported */
3821 uint64_t ack_delay = 0;
3822 size_t ack_frm_len = 0;
3823
3824 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3825 conn->conn, NULL, NULL, &room);
3826 /* When we do not have to probe nor send acks either, we must take into
3827 * an account the data which have already been prepared and limit
3828 * the size of this packet. We will potentially build an ack-eliciting
3829 * packet.
3830 */
3831 if (!nb_pto_dgrams && !ack) {
3832 size_t path_room;
3833
3834 path_room = quic_path_prep_data(conn->path);
3835 if (room > path_room)
3836 room = path_room;
3837 }
3838
3839 if (ack)
3840 /* A frame is made of 1 byte for the frame type. */
3841 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3842
3843 /* XXX FIXME XXX : token not supported */
3844 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3845 /* Check there is enough room to build the header followed by a token,
3846 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3847 * encryption tag is also taken into an account. Note that we have no
3848 * knowledge of the packet number for this packet. It must be atomically
3849 * incremented each time a packet is built. But before building a packet
3850 * we must estimate if it may be built if we do not want to consume a packet
3851 * number for nothing! Note that we add 1 byte more to
3852 * <minlen> to be able to build an ack-eliciting packet when probing without
3853 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3854 * PING frame.
3855 */
3856 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3857 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3858 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3859 + token_fields_len;
3860 else
3861 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3862
3863 /* Consider any PADDING frame to add */
3864 if (objt_server(conn->conn->target) &&
3865 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3866 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3867 /* Pad too short client Initial packet */
3868 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3869 }
3870 else if (!ack) {
3871 /* Consider we will have to add the longest short PADDING frame to
3872 * protect a 1-byte length packet number.
3873 */
3874 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3875 }
3876
3877 if (room < minlen) {
3878 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3879 conn->conn, NULL, NULL, &room);
3880 return 0;
3881 }
3882
3883 return 1;
3884}
3885
3886/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003887 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3888 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3889 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003890 * 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 +02003891 * 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 +02003892 * having returned from this function.
3893 * 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 +01003894 * number field in this packet. <pn_len> will also have the packet number
3895 * length as value.
3896 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003897 * Always succeeds: this is the responsability of the caller to ensure there is
3898 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003899 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003900static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003901 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003902 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3903 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003904{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003905 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003906 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003907 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3908 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003909 size_t ack_frm_len;
3910 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003911 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003912
Frédéric Lécailleea604992020-12-24 13:01:37 +01003913 /* Length field value with CRYPTO frames if present. */
3914 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003915 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003916 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003917 * the congestion controller window. So, we do not limit the size of this
3918 * packet if we have an ACK frame to send because an ACK frame is not
3919 * ack-eliciting. This size will be limited if we have ack-eliciting
3920 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003921 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003922 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003923 size_t path_room;
3924
3925 path_room = quic_path_prep_data(conn->path);
3926 if (end - beg > path_room)
3927 end = beg + path_room;
3928 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003929
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003930 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003931 /* packet number length */
3932 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003933 /* Build the header */
3934 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003935 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3936 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003937 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
3938 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
3939 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003940 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003941 /* Ensure there is enough room for the TLS encryption tag */
3942 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003943 /* Build an ACK frame if required. */
3944 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003945 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003946 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003947 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003948 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
3949 * smallest frame (PING) and <*pn_len> more for the packet number. Note
3950 * that from here, we do not know if we will have to send a PING frame.
3951 * This will be decided after having computed the ack-eliciting frames
3952 * to be added to this packet.
3953 */
3954 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 +01003955 if (!ack_frm_len) {
3956 ssize_t room = end - pos;
3957 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3958 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003959 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003960 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003961 }
3962
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003963 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003964 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003965 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003966 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003967
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003968 /* Initialize the length of the frames built below to <len>.
3969 * If any frame could be successfully built by qc_build_frms(),
3970 * we will have len_frms > len.
3971 */
3972 len_frms = len;
3973 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01003974 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3975 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003976 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003977
3978 add_ping_frm = 0;
3979 padding_len = 0;
3980 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003981 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003982 len < QUIC_INITIAL_PACKET_MINLEN) {
3983 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3984 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003985 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003987 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003988 add_ping_frm = 1;
3989 len += 1;
3990 }
3991 /* If there is no frame at all to follow, add at least a PADDING frame. */
3992 if (!ack_frm_len)
3993 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3994 }
3995
3996 /* Length (of the remaining data). Must not fail because, the buffer size
3997 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003998 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003999 * of this packet.
4000 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004001 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004002 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004003 else
4004 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004005 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004006 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004007
4008 /* Packet number field address. */
4009 *buf_pn = pos;
4010
4011 /* Packet number encoding. */
4012 quic_packet_number_encode(&pos, end, pn, *pn_len);
4013
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004014 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4015 ssize_t room = end - pos;
4016 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4017 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004018 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004019 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004020
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004021 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004022 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004023 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004024
4025 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004026 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004027 ssize_t room = end - pos;
4028 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4029 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004030 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004031 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004032 }
4033 }
4034
4035 /* Build a PING frame if needed. */
4036 if (add_ping_frm) {
4037 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004038 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4039 ssize_t room = end - pos;
4040 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4041 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004042 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004043 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004044 }
4045
4046 /* Build a PADDING frame if needed. */
4047 if (padding_len) {
4048 frm.type = QUIC_FT_PADDING;
4049 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004050 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4051 ssize_t room = end - pos;
4052 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4053 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004054 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004055 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004056 }
4057
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004058 /* Always reset this variable as this function has no idea
4059 * if it was set. It is handle by the loss detection timer.
4060 */
4061 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004062 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004063}
4064
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004065static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004066{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004067 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004068 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004069 pkt->cdata_len = 0;
4070 pkt->in_flight_len = 0;
4071 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004072 pkt->next = NULL;
4073 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004074}
4075
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004076/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004077static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4078{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004079 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004080
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004081 if (!pkt)
4082 return;
4083
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004084 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004085 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004086 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004087 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004088 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004089}
4090
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004091/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4092 * type for <qc> QUIC connection from <qel> encryption level.
4093 * Return -2 if the packet could not be allocated or encrypted for any reason,
4094 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004095 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004096static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4097 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004098 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004099 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004100 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004101{
4102 /* The pointer to the packet number field. */
4103 unsigned char *buf_pn;
4104 unsigned char *beg, *end, *payload;
4105 int64_t pn;
4106 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004107 struct quic_tls_ctx *tls_ctx;
4108 struct quic_tx_packet *pkt;
4109
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004110 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004111 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004112 pkt = pool_alloc(pool_head_quic_tx_packet);
4113 if (!pkt) {
4114 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 +02004115 *err = -2;
4116 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004117 }
4118
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004119 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004120 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004121 pn_len = 0;
4122 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004123 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004124 *err = -1;
4125 goto err;
4126 }
4127
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004128 /* Consume a packet number. */
4129 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004130 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 +01004131
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004132 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004133 payload = buf_pn + pn_len;
4134 payload_len = end - payload;
4135 aad_len = payload - beg;
4136
4137 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004138 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4139 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004140 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004141 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004142
4143 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004144 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004145 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4146 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4147 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004148 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004149 goto err;
4150 }
4151
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004152 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4153 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004154 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004155 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004156 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004157 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004158 pkt->in_flight_len = pkt->len;
4159 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004160 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004161 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004162 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4163
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004164 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004165
4166 err:
4167 free_quic_tx_packet(pkt);
4168 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004169 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004170}
4171
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004172/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4173 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004174 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004175static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4176 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004177{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004178 size_t try, done = 0;
4179
4180 if (!conn_ctrl_ready(conn))
4181 return 0;
4182
4183 if (!fd_recv_ready(conn->handle.fd))
4184 return 0;
4185
4186 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004187
4188 /* read the largest possible block. For this, we perform only one call
4189 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004190 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191 */
4192 while (count > 0) {
4193 try = b_contig_space(buf);
4194 if (!try)
4195 break;
4196
4197 if (try > count)
4198 try = count;
4199
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004200 b_add(buf, try);
4201 done += try;
4202 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004203 }
4204
4205 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4206 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4207
4208 leave:
4209 return done;
4210
4211 read0:
4212 conn_sock_read0(conn);
4213 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4214
4215 /* Now a final check for a possible asynchronous low-level error
4216 * report. This can happen when a connection receives a reset
4217 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4218 * we might have come from there by just checking POLL_HUP instead
4219 * of recv()'s return value 0, so we have no way to tell there was
4220 * an error without checking.
4221 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004222 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004223 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4224 goto leave;
4225}
4226
4227
4228/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4229 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4230 * other pending data for example, but this flag is ignored at the moment.
4231 * Only one call to send() is performed, unless the buffer wraps, in which case
4232 * a second call may be performed. The connection's flags are updated with
4233 * whatever special event is detected (error, empty). The caller is responsible
4234 * for taking care of those events and avoiding the call if inappropriate. The
4235 * function does not call the connection's polling update function, so the caller
4236 * is responsible for this. It's up to the caller to update the buffer's contents
4237 * based on the return value.
4238 */
4239static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4240{
4241 ssize_t ret;
4242 size_t try, done;
4243 int send_flag;
4244
4245 if (!conn_ctrl_ready(conn))
4246 return 0;
4247
4248 if (!fd_send_ready(conn->handle.fd))
4249 return 0;
4250
4251 done = 0;
4252 /* send the largest possible block. For this we perform only one call
4253 * to send() unless the buffer wraps and we exactly fill the first hunk,
4254 * in which case we accept to do it once again.
4255 */
4256 while (count) {
4257 try = b_contig_data(buf, done);
4258 if (try > count)
4259 try = count;
4260
4261 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4262 if (try < count || flags & CO_SFL_MSG_MORE)
4263 send_flag |= MSG_MORE;
4264
4265 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4266 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4267 if (ret > 0) {
4268 count -= ret;
4269 done += ret;
4270
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004271 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004272 conn->flags |= CO_FL_WAIT_L4L6;
4273 /* if the system buffer is full, don't insist */
4274 if (ret < try)
4275 break;
4276 }
4277 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4278 /* nothing written, we need to poll for write first */
4279 fd_cant_send(conn->handle.fd);
4280 break;
4281 }
4282 else if (errno != EINTR) {
4283 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4284 break;
4285 }
4286 }
4287 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4288 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4289
4290 if (done > 0) {
4291 /* we count the total bytes sent, and the send rate for 32-byte
4292 * blocks. The reason for the latter is that freq_ctr are
4293 * limited to 4GB and that it's not enough per second.
4294 */
4295 _HA_ATOMIC_ADD(&global.out_bytes, done);
4296 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4297 }
4298 return done;
4299}
4300
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004301/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4302 * event subscriber <es> is not allowed to change from a previous call as long
4303 * as at least one event is still subscribed. The <event_type> must only be a
4304 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4305 */
4306static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4307{
4308 return conn_subscribe(conn, xprt_ctx, event_type, es);
4309}
4310
4311/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4312 * The <es> pointer is not allowed to differ from the one passed to the
4313 * subscribe() call. It always returns zero.
4314 */
4315static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4316{
4317 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4318}
4319
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004320/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4321 * connection with <xprt_ctx> as address of the xprt context.
4322 * Returns 1 if succeeded, 0 if not.
4323 */
4324static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4325{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004326 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004327
4328 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4329
4330 if (*xprt_ctx)
4331 goto out;
4332
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004333 ctx = pool_alloc(pool_head_quic_conn_ctx);
4334 if (!ctx) {
4335 conn->err_code = CO_ER_SYS_MEMLIM;
4336 goto err;
4337 }
4338
4339 ctx->wait_event.tasklet = tasklet_new();
4340 if (!ctx->wait_event.tasklet) {
4341 conn->err_code = CO_ER_SYS_MEMLIM;
4342 goto err;
4343 }
4344
4345 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4346 ctx->wait_event.tasklet->context = ctx;
4347 ctx->wait_event.events = 0;
4348 ctx->conn = conn;
4349 ctx->subs = NULL;
4350 ctx->xprt_ctx = NULL;
4351
4352 ctx->xprt = xprt_get(XPRT_QUIC);
4353 if (objt_server(conn->target)) {
4354 /* Server */
4355 struct server *srv = __objt_server(conn->target);
4356 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004357 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004358 int ssl_err, ipv4;
4359
4360 ssl_err = SSL_ERROR_NONE;
4361 if (RAND_bytes(dcid, sizeof dcid) != 1)
4362 goto err;
4363
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004364 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004365 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004366 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004367 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004368 goto err;
4369
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004370 /* Insert our SCID, the connection ID for the QUIC client. */
4371 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4372
4373 conn->qc = qc;
4374 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004375 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4376 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004377 goto err;
4378
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004379 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4380 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4381 goto err;
4382
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004383 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004384 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004385 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4386 qc->enc_params_len =
4387 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4388 &qc->rx.params, 0);
4389 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004390 goto err;
4391
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004392 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004393 SSL_set_connect_state(ctx->ssl);
4394 ssl_err = SSL_do_handshake(ctx->ssl);
4395 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004396 int st;
4397
4398 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004399 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4400 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004401 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004402 }
4403 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004404 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004405 goto err;
4406 }
4407 }
4408 }
4409 else if (objt_listener(conn->target)) {
4410 /* Listener */
4411 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004412 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004413
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004414 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004415 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4416 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4417 goto err;
4418
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004419 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004420 SSL_set_accept_state(ctx->ssl);
4421 }
4422
4423 *xprt_ctx = ctx;
4424
4425 /* Leave init state and start handshake */
4426 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004427
4428 out:
4429 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4430
4431 return 0;
4432
4433 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004434 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004435 tasklet_free(ctx->wait_event.tasklet);
4436 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004437 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004438 return -1;
4439}
4440
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004441/* Start the QUIC transport layer */
4442static int qc_xprt_start(struct connection *conn, void *ctx)
4443{
4444 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004445 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004446
4447 qc = conn->qc;
4448 if (!quic_conn_init_timer(qc)) {
4449 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4450 return 0;
4451 }
4452
4453 tasklet_wakeup(qctx->wait_event.tasklet);
4454 return 1;
4455}
4456
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457/* transport-layer operations for QUIC connections. */
4458static struct xprt_ops ssl_quic = {
4459 .snd_buf = quic_conn_from_buf,
4460 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004461 .subscribe = quic_conn_subscribe,
4462 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004463 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004464 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004465 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4466 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4467 .name = "QUIC",
4468};
4469
4470__attribute__((constructor))
4471static void __quic_conn_init(void)
4472{
4473 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4474 xprt_register(XPRT_QUIC, &ssl_quic);
4475}
4476
4477__attribute__((destructor))
4478static void __quic_conn_deinit(void)
4479{
4480 BIO_meth_free(ha_quic_meth);
4481}
4482
4483/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4484 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4485 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004486 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004487 */
4488static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4489 struct sockaddr_storage *saddr, qpkt_read_func *func)
4490{
4491 unsigned char *pos;
4492 const unsigned char *end;
4493 struct quic_dgram_ctx dgram_ctx = {
4494 .dcid_node = NULL,
4495 .owner = owner,
4496 };
4497
4498 pos = (unsigned char *)buf;
4499 end = pos + len;
4500
4501 do {
4502 int ret;
4503 struct quic_rx_packet *pkt;
4504
Willy Tarreaue4498932021-03-22 21:13:05 +01004505 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004506 if (!pkt)
4507 goto err;
4508
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004509 quic_rx_packet_refinc(pkt);
4510 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4511 if (ret == -1) {
4512 size_t pkt_len;
4513
4514 pkt_len = pkt->len;
4515 free_quic_rx_packet(pkt);
4516 /* If the packet length could not be found, we cannot continue. */
4517 if (!pkt_len)
4518 break;
4519 }
4520 } while (pos < end);
4521
4522 /* Increasing the received bytes counter by the UDP datagram length
4523 * if this datagram could be associated to a connection.
4524 */
4525 if (dgram_ctx.qc)
4526 dgram_ctx.qc->rx.bytes += len;
4527
4528 return pos - (unsigned char *)buf;
4529
4530 err:
4531 return -1;
4532}
4533
4534ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4535 struct sockaddr_storage *saddr)
4536{
4537 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4538}
4539
4540ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4541 struct sockaddr_storage *saddr)
4542{
4543 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4544}
4545
4546/* QUIC I/O handler for connection to local listeners or remove servers
4547 * depending on <listener> boolean value, with <fd> as socket file
4548 * descriptor and <ctx> as context.
4549 */
4550static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4551{
4552 ssize_t ret;
4553 size_t done = 0;
4554 struct buffer *buf = get_trash_chunk();
4555 /* Source address */
4556 struct sockaddr_storage saddr = {0};
4557 socklen_t saddrlen = sizeof saddr;
4558
4559 if (!fd_recv_ready(fd))
4560 return 0;
4561
4562 do {
4563 ret = recvfrom(fd, buf->area, buf->size, 0,
4564 (struct sockaddr *)&saddr, &saddrlen);
4565 if (ret < 0) {
4566 if (errno == EINTR)
4567 continue;
4568 if (errno == EAGAIN)
4569 fd_cant_recv(fd);
4570 goto out;
4571 }
4572 } while (0);
4573
4574 done = buf->data = ret;
4575 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4576
4577 out:
4578 return done;
4579}
4580
4581/* QUIC I/O handler for connections to local listeners with <fd> as socket
4582 * file descriptor.
4583 */
4584void quic_fd_handler(int fd)
4585{
Willy Tarreauf5090652021-04-06 17:23:40 +02004586 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004587 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4588}
4589
4590/* QUIC I/O handler for connections to remote servers with <fd> as socket
4591 * file descriptor.
4592 */
4593void quic_conn_fd_handler(int fd)
4594{
Willy Tarreauf5090652021-04-06 17:23:40 +02004595 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004596 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4597}
4598
4599/*
4600 * Local variables:
4601 * c-indent-level: 8
4602 * c-basic-offset: 8
4603 * End:
4604 */