blob: eed8b2fe6e0dcfbd55e4328a8c97cee0d0321d0e [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
Amaury Denoyelleeb01f592021-10-07 16:44:05 +020025#include <import/ebmbtree.h>
26
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010027#include <haproxy/buf-t.h>
28#include <haproxy/compat.h>
29#include <haproxy/api.h>
30#include <haproxy/debug.h>
31#include <haproxy/tools.h>
32#include <haproxy/ticks.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010033
34#include <haproxy/connection.h>
35#include <haproxy/fd.h>
36#include <haproxy/freq_ctr.h>
37#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010038#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010039#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010040#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010041#include <haproxy/pipe.h>
42#include <haproxy/proxy.h>
43#include <haproxy/quic_cc.h>
44#include <haproxy/quic_frame.h>
45#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020046#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010047#include <haproxy/quic_tls.h>
48#include <haproxy/ssl_sock.h>
49#include <haproxy/stream_interface.h>
50#include <haproxy/task.h>
51#include <haproxy/trace.h>
52#include <haproxy/xprt_quic.h>
53
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020054/* This is the values of some QUIC transport parameters when absent.
55 * Should be used to initialize any transport parameters (local or remote)
56 * before updating them with customized values.
57 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010058struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020059 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
60 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
61 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020062 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010063};
64
65/* trace source and events */
66static void quic_trace(enum trace_level level, uint64_t mask, \
67 const struct trace_source *src,
68 const struct ist where, const struct ist func,
69 const void *a1, const void *a2, const void *a3, const void *a4);
70
71static const struct trace_event quic_trace_events[] = {
72 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
73 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
74 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
75 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
76 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
77 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
78 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050079 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010080 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
81 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
82 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
83 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
84 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
85 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
86 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
87 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
88 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
89 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
90 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
91 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
92 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
93 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
94 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
95 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
96 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
97 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
98 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010099 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
100 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
101 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
102 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
103 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
104 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100105 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200106 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
107 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100108 { /* end */ }
109};
110
111static const struct name_desc quic_trace_lockon_args[4] = {
112 /* arg1 */ { /* already used by the connection */ },
113 /* arg2 */ { .name="quic", .desc="QUIC transport" },
114 /* arg3 */ { },
115 /* arg4 */ { }
116};
117
118static const struct name_desc quic_trace_decoding[] = {
119#define QUIC_VERB_CLEAN 1
120 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
121 { /* end */ }
122};
123
124
125struct trace_source trace_quic = {
126 .name = IST("quic"),
127 .desc = "QUIC xprt",
128 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
129 .default_cb = quic_trace,
130 .known_events = quic_trace_events,
131 .lockon_args = quic_trace_lockon_args,
132 .decoding = quic_trace_decoding,
133 .report_events = ~0, /* report everything by default */
134};
135
136#define TRACE_SOURCE &trace_quic
137INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
138
139static BIO_METHOD *ha_quic_meth;
140
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200141DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100142DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200143 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100145DECLARE_POOL(pool_head_quic_connection_id,
146 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100147DECLARE_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 +0100148DECLARE_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 +0100149DECLARE_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 +0100150DECLARE_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 +0100151DECLARE_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 +0200152DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100153DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100154
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200155static 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 +0200156 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200157 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200158 int ack, int nb_pto_dgrams, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100159
160/* Add traces to <buf> depending on <frm> TX frame type. */
161static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200162 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100163{
164 switch (frm->type) {
165 case QUIC_FT_CRYPTO:
166 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
167 (unsigned long long)frm->crypto.offset,
168 (unsigned long long)frm->crypto.len);
169 break;
170 default:
171 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
172 }
173}
174
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100175/* Only for debug purpose */
176struct enc_debug_info {
177 unsigned char *payload;
178 size_t payload_len;
179 unsigned char *aad;
180 size_t aad_len;
181 uint64_t pn;
182};
183
184/* Initializes a enc_debug_info struct (only for debug purpose) */
185static inline void enc_debug_info_init(struct enc_debug_info *edi,
186 unsigned char *payload, size_t payload_len,
187 unsigned char *aad, size_t aad_len, uint64_t pn)
188{
189 edi->payload = payload;
190 edi->payload_len = payload_len;
191 edi->aad = aad;
192 edi->aad_len = aad_len;
193 edi->pn = pn;
194}
195
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100196/* Trace callback for QUIC.
197 * These traces always expect that arg1, if non-null, is of type connection.
198 */
199static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
200 const struct ist where, const struct ist func,
201 const void *a1, const void *a2, const void *a3, const void *a4)
202{
203 const struct connection *conn = a1;
204
205 if (conn) {
206 struct quic_tls_secrets *secs;
207 struct quic_conn *qc;
208
209 qc = conn->qc;
210 chunk_appendf(&trace_buf, " : conn@%p", conn);
211 if ((mask & QUIC_EV_CONN_INIT) && qc) {
212 chunk_appendf(&trace_buf, "\n odcid");
213 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100214 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100215 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100216 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100217 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100218 }
219
220 if (mask & QUIC_EV_CONN_ADDDATA) {
221 const enum ssl_encryption_level_t *level = a2;
222 const size_t *len = a3;
223
224 if (level) {
225 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
226
227 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
228 }
229 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100230 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100231 }
232 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
233 /* Initial read & write secrets. */
234 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
235 const unsigned char *rx_sec = a2;
236 const unsigned char *tx_sec = a3;
237
238 secs = &qc->els[level].tls_ctx.rx;
239 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
240 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
241 if (rx_sec)
242 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
243 quic_tls_keys_hexdump(&trace_buf, secs);
244 }
245 secs = &qc->els[level].tls_ctx.tx;
246 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
247 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
248 if (tx_sec)
249 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
250 quic_tls_keys_hexdump(&trace_buf, secs);
251 }
252 }
253 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
254 const enum ssl_encryption_level_t *level = a2;
255 const unsigned char *secret = a3;
256 const size_t *secret_len = a4;
257
258 if (level) {
259 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
260
261 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
262 if (secret && secret_len)
263 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
264 secs = &qc->els[lvl].tls_ctx.rx;
265 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
266 quic_tls_keys_hexdump(&trace_buf, secs);
267 }
268 }
269
270 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
271 const enum ssl_encryption_level_t *level = a2;
272 const unsigned char *secret = a3;
273 const size_t *secret_len = a4;
274
275 if (level) {
276 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
277
278 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
279 if (secret && secret_len)
280 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
281 secs = &qc->els[lvl].tls_ctx.tx;
282 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
283 quic_tls_keys_hexdump(&trace_buf, secs);
284 }
285
286 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100287
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100288 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100289 const struct quic_tx_packet *pkt = a2;
290 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100291 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100292
293 if (qel) {
294 struct quic_pktns *pktns;
295
296 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100297 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
298 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100299 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100300 (unsigned long long)qc->path->cwnd,
301 (unsigned long long)qc->path->prep_in_flight,
302 (unsigned long long)qc->path->in_flight,
303 (unsigned long long)pktns->tx.in_flight,
304 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100305 }
306 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200307 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100308 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
309 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
310 list_for_each_entry(frm, &pkt->frms, list)
311 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100312 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
313 }
314
315 if (room) {
316 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
317 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
318 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100319 }
320 }
321
322 if (mask & QUIC_EV_CONN_HDSHK) {
323 const enum quic_handshake_state *state = a2;
324 const int *err = a3;
325
326 if (state)
327 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
328 if (err)
329 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
330 }
331
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100332 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100333 const struct quic_rx_packet *pkt = a2;
334 const unsigned long *pktlen = a3;
335 const SSL *ssl = a4;
336
337 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100338 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100339 pkt, quic_packet_type_enc_level_char(pkt->type));
340 if (pkt->pnl)
341 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
342 (unsigned long long)pkt->pn);
343 if (pkt->token_len)
344 chunk_appendf(&trace_buf, " toklen=%llu",
345 (unsigned long long)pkt->token_len);
346 if (pkt->aad_len)
347 chunk_appendf(&trace_buf, " aadlen=%llu",
348 (unsigned long long)pkt->aad_len);
349 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
350 pkt->flags, (unsigned long long)pkt->len);
351 }
352 if (pktlen)
353 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
354 if (ssl) {
355 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
356 chunk_appendf(&trace_buf, " el=%c",
357 quic_enc_level_char(ssl_to_quic_enc_level(level)));
358 }
359 }
360
361 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
362 const struct quic_rx_packet *pkt = a2;
363 const struct quic_rx_crypto_frm *cf = a3;
364 const SSL *ssl = a4;
365
366 if (pkt)
367 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
368 quic_packet_type_enc_level_char(pkt->type),
369 (unsigned long long)pkt->pn);
370 if (cf)
371 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
372 (unsigned long long)cf->offset_node.key,
373 (unsigned long long)cf->len);
374 if (ssl) {
375 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
Frédéric Lécaille57e6e9e2021-09-23 18:10:56 +0200376 chunk_appendf(&trace_buf, " rel=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100377 quic_enc_level_char(ssl_to_quic_enc_level(level)));
378 }
379 }
380
381 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
382 const struct quic_frame *frm = a2;
383
384 if (frm)
385 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
386 }
387
388 if (mask & QUIC_EV_CONN_PHPKTS) {
389 const struct quic_enc_level *qel = a2;
390
391 if (qel) {
392 struct quic_pktns *pktns;
393
394 pktns = qc->pktns;
395 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200396 " 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 +0100397 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200398 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille2766e782021-08-30 17:16:07 +0200399 !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100400 (unsigned long long)qc->path->cwnd,
401 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100402 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100403 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
404 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100405 }
406 }
407
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100408 if (mask & QUIC_EV_CONN_ENCPKT) {
409 const struct enc_debug_info *edi = a2;
410
411 if (edi)
412 chunk_appendf(&trace_buf,
413 " payload=@%p payload_len=%llu"
414 " aad=@%p aad_len=%llu pn=%llu",
415 edi->payload, (unsigned long long)edi->payload_len,
416 edi->aad, (unsigned long long)edi->aad_len,
417 (unsigned long long)edi->pn);
418 }
419
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100420 if (mask & QUIC_EV_CONN_RMHP) {
421 const struct quic_rx_packet *pkt = a2;
422
423 if (pkt) {
424 const int *ret = a3;
425
426 chunk_appendf(&trace_buf, " pkt@%p", pkt);
427 if (ret && *ret)
428 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
429 pkt->pnl, (unsigned long long)pkt->pn);
430 }
431 }
432
433 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200434 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100435 const unsigned long *val1 = a3;
436 const unsigned long *val2 = a4;
437
438 if (frm)
439 chunk_tx_frm_appendf(&trace_buf, frm);
440 if (val1)
441 chunk_appendf(&trace_buf, " %lu", *val1);
442 if (val2)
443 chunk_appendf(&trace_buf, "..%lu", *val2);
444 }
445
446 if (mask & QUIC_EV_CONN_RTTUPDT) {
447 const unsigned int *rtt_sample = a2;
448 const unsigned int *ack_delay = a3;
449 const struct quic_loss *ql = a4;
450
451 if (rtt_sample)
452 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
453 if (ack_delay)
454 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
455 if (ql)
456 chunk_appendf(&trace_buf,
457 " srtt=%ums rttvar=%ums min_rtt=%ums",
458 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
459 }
460 if (mask & QUIC_EV_CONN_CC) {
461 const struct quic_cc_event *ev = a2;
462 const struct quic_cc *cc = a3;
463
464 if (a2)
465 quic_cc_event_trace(&trace_buf, ev);
466 if (a3)
467 quic_cc_state_trace(&trace_buf, cc);
468 }
469
470 if (mask & QUIC_EV_CONN_PKTLOSS) {
471 const struct quic_pktns *pktns = a2;
472 const struct list *lost_pkts = a3;
473 struct quic_conn *qc = conn->qc;
474
475 if (pktns) {
476 chunk_appendf(&trace_buf, " pktns=%s",
477 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
478 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
479 if (pktns->tx.loss_time)
480 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100481 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100482 }
483 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
484 struct quic_tx_packet *pkt;
485
486 chunk_appendf(&trace_buf, " lost_pkts:");
487 list_for_each_entry(pkt, lost_pkts, list)
488 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
489 }
490 }
491
492 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
493 struct quic_conn *qc = conn->qc;
494 const struct quic_pktns *pktns = a2;
495 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100496 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100497
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100498 if (ifae_pkts)
499 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
500 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100501 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100502 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100503 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100504 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
505 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100506 if (mask & QUIC_EV_CONN_STIMER) {
507 if (pktns->tx.loss_time)
508 chunk_appendf(&trace_buf, " loss_time=%dms",
509 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
510 }
511 if (mask & QUIC_EV_CONN_SPTO) {
512 if (pktns->tx.time_of_last_eliciting)
513 chunk_appendf(&trace_buf, " tole=%dms",
514 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
515 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100516 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100517 }
518 }
519
520 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
521 chunk_appendf(&trace_buf,
522 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
523 }
524 }
525
526 if (mask & QUIC_EV_CONN_SPPKTS) {
527 const struct quic_tx_packet *pkt = a2;
528
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100529 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
530 (unsigned long long)qc->path->cwnd,
531 (unsigned long long)qc->path->prep_in_flight,
532 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100533 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100534 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100535 (unsigned long)pkt->pn_node.key,
536 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
537 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100538 (unsigned long long)pkt->in_flight_len,
539 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100540 }
541 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100542
543 if (mask & QUIC_EV_CONN_SSLALERT) {
544 const uint8_t *alert = a2;
545 const enum ssl_encryption_level_t *level = a3;
546
547 if (alert)
548 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
549 if (level)
550 chunk_appendf(&trace_buf, " el=%c",
551 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
552 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100553
554 if (mask & QUIC_EV_CONN_BCFRMS) {
555 const size_t *sz1 = a2;
556 const size_t *sz2 = a3;
557 const size_t *sz3 = a4;
558
559 if (sz1)
560 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
561 if (sz2)
562 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
563 if (sz3)
564 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
565 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100566
567 if (mask & QUIC_EV_CONN_PSTRM) {
568 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100569
570 if (a2) {
571 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100572
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100573 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
574 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
575 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
576 (unsigned long long)s->id,
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200577 (unsigned long long)s->offset.key,
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100578 (unsigned long long)s->len);
579 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100580 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100581 }
582 if (mask & QUIC_EV_CONN_LPKT) {
583 const struct quic_rx_packet *pkt = a2;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200584 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100585
586 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200587 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100588 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200589 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
590 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200591 if (len)
592 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100593 }
594
595}
596
597/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200598static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100599{
600 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200601 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100602
603 qc = ctx->conn->qc;
604 if (objt_server(qc->conn->target))
605 return 1;
606
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200607 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
608 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
609 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
610 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200611 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612 return 1;
613
614 return 0;
615}
616
617/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
618 * both loss detection and PTO and schedule the task assiated to this timer if needed.
619 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200620static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100621{
622 struct quic_conn *qc;
623 struct quic_pktns *pktns;
624 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200625 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100626
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100627 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
628 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100629 qc = ctx->conn->qc;
630 pktns = quic_loss_pktns(qc);
631 if (tick_isset(pktns->tx.loss_time)) {
632 qc->timer = pktns->tx.loss_time;
633 goto out;
634 }
635
636 /* XXX TODO: anti-amplification: the timer must be
637 * cancelled for a server which reached the anti-amplification limit.
638 */
639
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100640 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
641 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100642 /* Timer cancellation. */
643 qc->timer = TICK_ETERNITY;
644 goto out;
645 }
646
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200647 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
648 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100649 if (tick_isset(pto))
650 qc->timer = pto;
651 out:
Amaury Denoyelle336f6fd2021-10-05 14:42:25 +0200652 if (qc->timer != TICK_ETERNITY)
653 task_schedule(qc->timer_task, qc->timer);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100654 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
655}
656
657#ifndef OPENSSL_IS_BORINGSSL
658int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
659 const uint8_t *read_secret,
660 const uint8_t *write_secret, size_t secret_len)
661{
662 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
663 struct quic_tls_ctx *tls_ctx =
664 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
665 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
666
667 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
668 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
669 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
670 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
671
672 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
673 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
674 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
675 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
676 read_secret, secret_len)) {
677 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
678 return 0;
679 }
680
681 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
682 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
683 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
684 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
685 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
686 write_secret, secret_len)) {
687 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
688 return 0;
689 }
690
691 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
692 if (objt_server(conn->target) && level == ssl_encryption_application) {
693 const unsigned char *buf;
694 size_t buflen;
695
696 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
697 if (!buflen)
698 return 0;
699
700 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
701 return 0;
702 }
703 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
704
705 return 1;
706}
707#else
708/* ->set_read_secret callback to derive the RX secrets at <level> encryption
709 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500710 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100711 */
712int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
713 const SSL_CIPHER *cipher,
714 const uint8_t *secret, size_t secret_len)
715{
716 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
717 struct quic_tls_ctx *tls_ctx =
718 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
719
720 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
721 tls_ctx->rx.aead = tls_aead(cipher);
722 tls_ctx->rx.md = tls_md(cipher);
723 tls_ctx->rx.hp = tls_hp(cipher);
724
725 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
726 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
727 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
728 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
729 secret, secret_len)) {
730 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
731 goto err;
732 }
733
734 if (objt_server(conn->target) && level == ssl_encryption_application) {
735 const unsigned char *buf;
736 size_t buflen;
737
738 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
739 if (!buflen)
740 goto err;
741
742 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
743 goto err;
744 }
745
746 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
747 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
748
749 return 1;
750
751 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100752 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100753 return 0;
754}
755
756/* ->set_write_secret callback to derive the TX secrets at <level>
757 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500758 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100759 */
760int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
761 const SSL_CIPHER *cipher,
762 const uint8_t *secret, size_t secret_len)
763{
764 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
765 struct quic_tls_ctx *tls_ctx =
766 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
767
768 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
769 tls_ctx->tx.aead = tls_aead(cipher);
770 tls_ctx->tx.md = tls_md(cipher);
771 tls_ctx->tx.hp = tls_hp(cipher);
772
773 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
774 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
775 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
776 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
777 secret, secret_len)) {
778 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
779 goto err;
780 }
781
782 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
783 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
784
785 return 1;
786
787 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100788 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100789 return 0;
790}
791#endif
792
793/* This function copies the CRYPTO data provided by the TLS stack found at <data>
794 * with <len> as size in CRYPTO buffers dedicated to store the information about
795 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
796 * It fails only if it could not managed to allocate enough CRYPTO buffers to
797 * store all the data.
798 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
799 */
800static int quic_crypto_data_cpy(struct quic_enc_level *qel,
801 const unsigned char *data, size_t len)
802{
803 struct quic_crypto_buf **qcb;
804 /* The remaining byte to store in CRYPTO buffers. */
805 size_t cf_offset, cf_len, *nb_buf;
806 unsigned char *pos;
807
808 nb_buf = &qel->tx.crypto.nb_buf;
809 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
810 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
811 cf_len = len;
812
813 while (len) {
814 size_t to_copy, room;
815
816 pos = (*qcb)->data + (*qcb)->sz;
817 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
818 to_copy = len > room ? room : len;
819 if (to_copy) {
820 memcpy(pos, data, to_copy);
821 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
822 qel->tx.crypto.sz += to_copy;
823 (*qcb)->sz += to_copy;
824 pos += to_copy;
825 len -= to_copy;
826 data += to_copy;
827 }
828 else {
829 struct quic_crypto_buf **tmp;
830
831 tmp = realloc(qel->tx.crypto.bufs,
832 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
833 if (tmp) {
834 qel->tx.crypto.bufs = tmp;
835 qcb = &qel->tx.crypto.bufs[*nb_buf];
836 *qcb = pool_alloc(pool_head_quic_crypto_buf);
837 if (!*qcb)
838 return 0;
839
840 (*qcb)->sz = 0;
841 ++*nb_buf;
842 }
843 else {
844 break;
845 }
846 }
847 }
848
849 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
850 * have been buffered.
851 */
852 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200853 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100854
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200855 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100856 if (!frm)
857 return 0;
858
859 frm->type = QUIC_FT_CRYPTO;
860 frm->crypto.offset = cf_offset;
861 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200862 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200863 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100864 }
865
866 return len == 0;
867}
868
869
870/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
871 * wants to provide the QUIC layer with CRYPTO data.
872 * Returns 1 if succeeded, 0 if not.
873 */
874int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
875 const uint8_t *data, size_t len)
876{
877 struct connection *conn;
878 enum quic_tls_enc_level tel;
879 struct quic_enc_level *qel;
880
881 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
882 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
883 tel = ssl_to_quic_enc_level(level);
884 qel = &conn->qc->els[tel];
885
886 if (tel == -1) {
887 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
888 goto err;
889 }
890
891 if (!quic_crypto_data_cpy(qel, data, len)) {
892 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
893 goto err;
894 }
895
896 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
897 conn, &level, &len);
898
899 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
900 return 1;
901
902 err:
903 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
904 return 0;
905}
906
907int ha_quic_flush_flight(SSL *ssl)
908{
909 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
910
911 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
912 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
913
914 return 1;
915}
916
917int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
918{
919 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
920
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100921 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100922 return 1;
923}
924
925/* QUIC TLS methods */
926static SSL_QUIC_METHOD ha_quic_method = {
927#ifdef OPENSSL_IS_BORINGSSL
928 .set_read_secret = ha_set_rsec,
929 .set_write_secret = ha_set_wsec,
930#else
931 .set_encryption_secrets = ha_quic_set_encryption_secrets,
932#endif
933 .add_handshake_data = ha_quic_add_handshake_data,
934 .flush_flight = ha_quic_flush_flight,
935 .send_alert = ha_quic_send_alert,
936};
937
938/* Initialize the TLS context of a listener with <bind_conf> as configuration.
939 * Returns an error count.
940 */
941int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
942{
943 struct proxy *curproxy = bind_conf->frontend;
944 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
945 int cfgerr = 0;
946
947#if 0
948 /* XXX Did not manage to use this. */
949 const char *ciphers =
950 "TLS_AES_128_GCM_SHA256:"
951 "TLS_AES_256_GCM_SHA384:"
952 "TLS_CHACHA20_POLY1305_SHA256:"
953 "TLS_AES_128_CCM_SHA256";
954#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200955 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100956 long options =
957 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
958 SSL_OP_SINGLE_ECDH_USE |
959 SSL_OP_CIPHER_SERVER_PREFERENCE;
960 SSL_CTX *ctx;
961
962 ctx = SSL_CTX_new(TLS_server_method());
963 bind_conf->initial_ctx = ctx;
964
965 SSL_CTX_set_options(ctx, options);
966#if 0
967 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
968 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
969 "for bind '%s' at [%s:%d].\n",
970 curproxy->id, ciphers,
971 bind_conf->arg, bind_conf->file, bind_conf->line);
972 cfgerr++;
973 }
974#endif
975
976 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
977 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
978 "for bind '%s' at [%s:%d].\n",
979 curproxy->id, groups,
980 bind_conf->arg, bind_conf->file, bind_conf->line);
981 cfgerr++;
982 }
983
984 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
985 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
986 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
987 SSL_CTX_set_default_verify_paths(ctx);
988
989#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
990#ifdef OPENSSL_IS_BORINGSSL
991 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
992 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
993#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
994 if (bind_conf->ssl_conf.early_data) {
995 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
996 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
997 }
998 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
999 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
1000#else
1001 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1002#endif
1003 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1004#endif
1005 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1006
1007 return cfgerr;
1008}
1009
1010/* Decode an expected packet number from <truncated_on> its truncated value,
1011 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1012 * the number of bits used to encode this packet number (its length in bytes * 8).
1013 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1014 */
1015static uint64_t decode_packet_number(uint64_t largest_pn,
1016 uint32_t truncated_pn, unsigned int pn_nbits)
1017{
1018 uint64_t expected_pn = largest_pn + 1;
1019 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1020 uint64_t pn_hwin = pn_win / 2;
1021 uint64_t pn_mask = pn_win - 1;
1022 uint64_t candidate_pn;
1023
1024
1025 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1026 /* Note that <pn_win> > <pn_hwin>. */
1027 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1028 candidate_pn + pn_hwin <= expected_pn)
1029 return candidate_pn + pn_win;
1030
1031 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1032 return candidate_pn - pn_win;
1033
1034 return candidate_pn;
1035}
1036
1037/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1038 * cryptographic context.
1039 * <largest_pn> is the largest received packet number and <pn> the address of
1040 * the packet number field for this packet with <byte0> address of its first byte.
1041 * <end> points to one byte past the end of this packet.
1042 * Returns 1 if succeeded, 0 if not.
1043 */
1044static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1045 int64_t largest_pn, unsigned char *pn,
1046 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001047 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001048{
1049 int ret, outlen, i, pnlen;
1050 uint64_t packet_number;
1051 uint32_t truncated_pn = 0;
1052 unsigned char mask[5] = {0};
1053 unsigned char *sample;
1054 EVP_CIPHER_CTX *cctx;
1055 unsigned char *hp_key;
1056
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001057 /* Check there is enough data in this packet. */
1058 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1059 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1060 return 0;
1061 }
1062
1063 cctx = EVP_CIPHER_CTX_new();
1064 if (!cctx) {
1065 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1066 return 0;
1067 }
1068
1069 ret = 0;
1070 sample = pn + QUIC_PACKET_PN_MAXLEN;
1071
1072 hp_key = tls_ctx->rx.hp_key;
1073 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1074 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1075 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1076 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1077 goto out;
1078 }
1079
1080 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1081 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1082 for (i = 0; i < pnlen; i++) {
1083 pn[i] ^= mask[i + 1];
1084 truncated_pn = (truncated_pn << 8) | pn[i];
1085 }
1086
1087 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1088 /* Store remaining information for this unprotected header */
1089 pkt->pn = packet_number;
1090 pkt->pnl = pnlen;
1091
1092 ret = 1;
1093
1094 out:
1095 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001096
1097 return ret;
1098}
1099
1100/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1101 * address, with <payload_len> as payload length, <aad> as address of
1102 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1103 * context.
1104 * Returns 1 if succeeded, 0 if not.
1105 */
1106static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1107 unsigned char *aad, size_t aad_len, uint64_t pn,
1108 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1109{
1110 unsigned char iv[12];
1111 unsigned char *tx_iv = tls_ctx->tx.iv;
1112 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001113 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001114
1115 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1116 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001117 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001118 }
1119
1120 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1121 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1122 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001123 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001124 }
1125
1126 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001127
1128 err:
1129 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1130 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1131 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001132}
1133
1134/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1135 * Returns 1 if succeeded, 0 if not.
1136 */
1137static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1138{
1139 int ret;
1140 unsigned char iv[12];
1141 unsigned char *rx_iv = tls_ctx->rx.iv;
1142 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1143
1144 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1145 return 0;
1146
1147 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1148 pkt->data, pkt->aad_len,
1149 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1150 if (!ret)
1151 return 0;
1152
1153 /* Update the packet length (required to parse the frames). */
1154 pkt->len = pkt->aad_len + ret;
1155
1156 return 1;
1157}
1158
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001159/* Remove from <qcs> stream the acknowledged frames.
1160 * Never fails.
1161 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001162static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001163{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001164 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001165 struct eb64_node *frm_node;
1166
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001167 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001168 frm_node = eb64_first(&qcs->tx.acked_frms);
1169 while (frm_node) {
1170 struct quic_stream *strm;
1171
1172 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1173 if (strm->offset.key != qcs->tx.ack_offset)
1174 break;
1175
1176 b_del(strm->buf, strm->len);
1177 qcs->tx.ack_offset += strm->len;
1178 frm_node = eb64_next(frm_node);
1179 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001180 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001181 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001182
1183 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001184}
1185
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001186/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001187static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001188 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001189{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001190 int stream_acked;
1191 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001192
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001193 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001194 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001195 switch (frm->type) {
1196 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1197 {
1198 struct qcs *qcs = frm->stream.qcs;
1199 struct quic_stream *strm = &frm->stream;
1200
1201 if (qcs->tx.ack_offset == strm->offset.key) {
1202 b_del(strm->buf, strm->len);
1203 qcs->tx.ack_offset += strm->len;
1204 LIST_DELETE(&frm->list);
1205 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001206 qc->qcc->flags &= ~QC_CF_MUX_MFULL;
1207 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001208 }
1209 else {
1210 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1211 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001212 stream_acked |= qcs_try_to_consume(qcs);
Amaury Denoyellecae07912021-10-08 17:57:41 +02001213
1214 if (qcs->flags & QC_SF_DETACH)
1215 tasklet_wakeup(qcs->shut_tl);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001216 }
1217 break;
1218 default:
1219 LIST_DELETE(&frm->list);
1220 pool_free(pool_head_quic_frame, frm);
1221 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001222
1223 if (stream_acked) {
1224 struct qcc *qcc = qc->qcc;
1225
1226 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1227 tasklet_wakeup(qcc->subs->tasklet);
1228 qcc->subs->events &= ~SUB_RETRY_SEND;
1229 if (!qcc->subs->events)
1230 qcc->subs = NULL;
1231 }
1232 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001233}
1234
1235/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1236 * deallocating them, and their TX frames.
1237 * Returns the last node reached to be used for the next range.
1238 * May be NULL if <largest> node could not be found.
1239 */
1240static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1241 struct list *newly_acked_pkts,
1242 struct eb64_node *largest_node,
1243 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001244 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001245{
1246 struct eb64_node *node;
1247 struct quic_tx_packet *pkt;
1248
1249 if (largest_node)
1250 node = largest_node;
1251 else {
1252 node = eb64_lookup(pkts, largest);
1253 while (!node && largest > smallest) {
1254 node = eb64_lookup(pkts, --largest);
1255 }
1256 }
1257
1258 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001259 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001260
1261 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1262 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001263 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001264 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1265 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1266 qc_treat_acked_tx_frm(frm, ctx);
1267 node = eb64_prev(node);
1268 eb64_delete(&pkt->pn_node);
1269 }
1270
1271 return node;
1272}
1273
1274/* Treat <frm> frame whose packet it is attached to has just been detected as non
1275 * acknowledged.
1276 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001277static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001278 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001279 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001280{
1281 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001282 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001283 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001284}
1285
1286
1287/* Free the TX packets of <pkts> list */
1288static inline void free_quic_tx_pkts(struct list *pkts)
1289{
1290 struct quic_tx_packet *pkt, *tmp;
1291
1292 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001293 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001294 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001295 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001296 }
1297}
1298
1299/* Send a packet loss event nofification to the congestion controller
1300 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1301 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1302 * at <now_us> current time.
1303 * Always succeeds.
1304 */
1305static inline void qc_cc_loss_event(struct quic_conn *qc,
1306 unsigned int lost_bytes,
1307 unsigned int newest_time_sent,
1308 unsigned int period,
1309 unsigned int now_us)
1310{
1311 struct quic_cc_event ev = {
1312 .type = QUIC_CC_EVT_LOSS,
1313 .loss.now_ms = now_ms,
1314 .loss.max_ack_delay = qc->max_ack_delay,
1315 .loss.lost_bytes = lost_bytes,
1316 .loss.newest_time_sent = newest_time_sent,
1317 .loss.period = period,
1318 };
1319
1320 quic_cc_event(&qc->path->cc, &ev);
1321}
1322
1323/* Send a packet ack event nofication for each newly acked packet of
1324 * <newly_acked_pkts> list and free them.
1325 * Always succeeds.
1326 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001327static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001328 struct list *newly_acked_pkts)
1329{
1330 struct quic_conn *qc = ctx->conn->qc;
1331 struct quic_tx_packet *pkt, *tmp;
1332 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1333
1334 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1335 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001336 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001337 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001338 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001339 ev.ack.acked = pkt->in_flight_len;
1340 ev.ack.time_sent = pkt->time_sent;
1341 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001342 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001343 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001344 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001345 }
1346
1347}
1348
1349/* Handle <pkts> list of lost packets detected at <now_us> handling
1350 * their TX frames.
1351 * Send a packet loss event to the congestion controller if
1352 * in flight packet have been lost.
1353 * Also frees the packet in <pkts> list.
1354 * Never fails.
1355 */
1356static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001357 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001358 struct list *pkts,
1359 uint64_t now_us)
1360{
1361 struct quic_conn *qc = ctx->conn->qc;
1362 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001363 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001364 uint64_t lost_bytes;
1365
1366 lost_bytes = 0;
1367 oldest_lost = newest_lost = NULL;
1368 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1369 lost_bytes += pkt->in_flight_len;
1370 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001371 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001372 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001373 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001374 /* Treat the frames of this lost packet. */
1375 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1376 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001377 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001378 if (!oldest_lost) {
1379 oldest_lost = newest_lost = pkt;
1380 }
1381 else {
1382 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001383 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001384 newest_lost = pkt;
1385 }
1386 }
1387
1388 if (lost_bytes) {
1389 /* Sent a packet loss event to the congestion controller. */
1390 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1391 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001392 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001393 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001394 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001395 }
1396}
1397
1398/* Look for packet loss from sent packets for <qel> encryption level of a
1399 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1400 * their tree if deemed as lost or set the <loss_time> value the packet number
1401 * space if any not deemed lost.
1402 * Should be called after having received an ACK frame with newly acknowledged
1403 * packets or when the the loss detection timer has expired.
1404 * Always succeeds.
1405 */
1406static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1407 struct quic_conn *qc,
1408 struct list *lost_pkts)
1409{
1410 struct eb_root *pkts;
1411 struct eb64_node *node;
1412 struct quic_loss *ql;
1413 unsigned int loss_delay;
1414
1415 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1416 pkts = &pktns->tx.pkts;
1417 pktns->tx.loss_time = TICK_ETERNITY;
1418 if (eb_is_empty(pkts))
1419 goto out;
1420
1421 ql = &qc->path->loss;
1422 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1423 loss_delay += loss_delay >> 3;
1424 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1425
1426 node = eb64_first(pkts);
1427 while (node) {
1428 struct quic_tx_packet *pkt;
1429 int64_t largest_acked_pn;
1430 unsigned int loss_time_limit, time_sent;
1431
1432 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001433 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001434 node = eb64_next(node);
1435 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1436 break;
1437
1438 time_sent = pkt->time_sent;
1439 loss_time_limit = tick_add(time_sent, loss_delay);
1440 if (tick_is_le(time_sent, now_ms) ||
1441 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1442 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001443 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001444 }
1445 else {
1446 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1447 }
1448 }
1449
1450 out:
1451 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1452}
1453
1454/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1455 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001456 * 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 +01001457 * acked ack-eliciting packet.
1458 * Return 1, if succeeded, 0 if not.
1459 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001460static 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 +01001461 struct quic_enc_level *qel,
1462 unsigned int *rtt_sample,
1463 const unsigned char **pos, const unsigned char *end)
1464{
1465 struct quic_ack *ack = &frm->ack;
1466 uint64_t smallest, largest;
1467 struct eb_root *pkts;
1468 struct eb64_node *largest_node;
1469 unsigned int time_sent, pkt_flags;
1470 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1471 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1472
1473 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1474 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1475 ctx->conn,, &ack->largest_ack);
1476 goto err;
1477 }
1478
1479 if (ack->first_ack_range > ack->largest_ack) {
1480 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1481 ctx->conn,, &ack->first_ack_range);
1482 goto err;
1483 }
1484
1485 largest = ack->largest_ack;
1486 smallest = largest - ack->first_ack_range;
1487 pkts = &qel->pktns->tx.pkts;
1488 pkt_flags = 0;
1489 largest_node = NULL;
1490 time_sent = 0;
1491
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001492 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 +01001493 largest_node = eb64_lookup(pkts, largest);
1494 if (!largest_node) {
1495 TRACE_DEVEL("Largest acked packet not found",
1496 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1497 goto err;
1498 }
1499
1500 time_sent = eb64_entry(&largest_node->node,
1501 struct quic_tx_packet, pn_node)->time_sent;
1502 }
1503
1504 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1505 ctx->conn,, &largest, &smallest);
1506 do {
1507 uint64_t gap, ack_range;
1508
1509 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1510 largest_node, largest, smallest, ctx);
1511 if (!ack->ack_range_num--)
1512 break;
1513
1514 if (!quic_dec_int(&gap, pos, end))
1515 goto err;
1516
1517 if (smallest < gap + 2) {
1518 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1519 ctx->conn,, &gap, &smallest);
1520 goto err;
1521 }
1522
1523 largest = smallest - gap - 2;
1524 if (!quic_dec_int(&ack_range, pos, end))
1525 goto err;
1526
1527 if (largest < ack_range) {
1528 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1529 ctx->conn,, &largest, &ack_range);
1530 goto err;
1531 }
1532
1533 /* Do not use this node anymore. */
1534 largest_node = NULL;
1535 /* Next range */
1536 smallest = largest - ack_range;
1537
1538 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1539 ctx->conn,, &largest, &smallest);
1540 } while (1);
1541
1542 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001543 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001544
1545 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1546 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001547 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001548 }
1549
1550 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1551 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1552 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1553 if (!LIST_ISEMPTY(&lost_pkts))
1554 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1555 }
1556 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1557 if (quic_peer_validated_addr(ctx))
1558 ctx->conn->qc->path->loss.pto_count = 0;
1559 qc_set_timer(ctx);
1560 }
1561
1562
1563 return 1;
1564
1565 err:
1566 free_quic_tx_pkts(&newly_acked_pkts);
1567 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1568 return 0;
1569}
1570
Frédéric Lécaille6f0fadb2021-09-28 09:04:12 +02001571/* This function gives the detail of the SSL error. It is used only
1572 * if the debug mode and the verbose mode are activated. It dump all
1573 * the SSL error until the stack was empty.
1574 */
1575static forceinline void qc_ssl_dump_errors(struct connection *conn)
1576{
1577 if (unlikely(global.mode & MODE_DEBUG)) {
1578 while (1) {
1579 unsigned long ret;
1580
1581 ret = ERR_get_error();
1582 if (!ret)
1583 return;
1584
1585 fprintf(stderr, "conn. @%p OpenSSL error[0x%lx] %s: %s\n", conn, ret,
1586 ERR_func_error_string(ret), ERR_reason_error_string(ret));
1587 }
1588 }
1589}
1590
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001591/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1592 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001593 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001594 * Return 1 if succeeded, 0 if not.
1595 */
1596static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001597 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001598 const unsigned char *data, size_t len,
1599 struct quic_rx_packet *pkt,
1600 struct quic_rx_crypto_frm *cf)
1601{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001602 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001603 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001604
1605 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1606 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001607 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001608 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1609 TRACE_PROTO("SSL_provide_quic_data() error",
1610 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1611 goto err;
1612 }
1613
1614 el->rx.crypto.offset += len;
1615 TRACE_PROTO("in order CRYPTO data",
1616 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1617
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001618 state = HA_ATOMIC_LOAD(&qc->state);
1619 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001620 ssl_err = SSL_do_handshake(ctx->ssl);
1621 if (ssl_err != 1) {
1622 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1623 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1624 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001625 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001626 goto out;
1627 }
1628
1629 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001630 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaille7c881bd2021-09-28 09:05:59 +02001631 qc_ssl_dump_errors(ctx->conn);
1632 BUG_ON(1);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001633 goto err;
1634 }
1635
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001636 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001637 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001638 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001639 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001640 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001641 } else {
1642 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1643 if (ssl_err != 1) {
1644 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1645 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1646 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001647 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001648 goto out;
1649 }
1650
1651 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001652 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001653 goto err;
1654 }
1655
1656 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001657 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001658 }
1659
1660 out:
1661 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1662 return 1;
1663
1664 err:
1665 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1666 return 0;
1667}
1668
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001669/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1670 * <pkt> RX packet.
1671 * Return it if succeeded, NULL if not.
1672 */
1673static inline
1674struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1675 struct quic_rx_packet *pkt)
1676{
1677 struct quic_rx_strm_frm *frm;
1678
1679 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1680 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001681 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001682 frm->len = stream_frm->len;
1683 frm->data = stream_frm->data;
1684 frm->pkt = pkt;
1685 }
1686
1687 return frm;
1688}
1689
1690/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1691 * several streams, depending on the already open onces.
1692 * Return this node if succeeded, NULL if not.
1693 */
1694static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1695{
1696 unsigned int strm_type;
1697 int64_t sub_id;
1698 struct eb64_node *strm_node;
1699
1700 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1701
1702 strm_type = id & QCS_ID_TYPE_MASK;
1703 sub_id = id >> QCS_ID_TYPE_SHIFT;
1704 strm_node = NULL;
1705 if (qc_local_stream_id(qcc, id)) {
1706 /* Local streams: this stream must be already opened. */
1707 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1708 if (!strm_node) {
1709 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1710 goto out;
1711 }
1712 }
1713 else {
1714 /* Remote streams. */
1715 struct eb_root *strms;
1716 uint64_t largest_id;
1717 enum qcs_type qcs_type;
1718
1719 strms = &qcc->streams_by_id;
1720 qcs_type = qcs_id_type(id);
1721 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1722 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1723 goto out;
1724 }
1725
1726 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1727 * correct value.
1728 */
1729 largest_id = qcc->strms[qcs_type].largest_id;
1730 if (sub_id > (int64_t)largest_id) {
1731 /* RFC: "A stream ID that is used out of order results in all streams
1732 * of that type with lower-numbered stream IDs also being opened".
1733 * So, let's "open" these streams.
1734 */
1735 int64_t i;
1736 struct qcs *qcs;
1737
1738 qcs = NULL;
1739 for (i = largest_id + 1; i <= sub_id; i++) {
1740 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1741 if (!qcs) {
1742 TRACE_PROTO("Could not allocate a new stream",
1743 QUIC_EV_CONN_PSTRM, qcc->conn);
1744 goto out;
1745 }
1746
1747 qcc->strms[qcs_type].largest_id = i;
1748 }
1749 if (qcs)
1750 strm_node = &qcs->by_id;
1751 }
1752 else {
1753 strm_node = eb64_lookup(strms, id);
1754 }
1755 }
1756
1757 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1758 return strm_node;
1759
1760 out:
1761 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1762 return NULL;
1763}
1764
1765/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1766 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1767 * to reflect the data which have been consumed.
1768 */
1769static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1770{
1771 size_t ret;
1772
1773 ret = 0;
1774 while (strm_frm->len) {
1775 size_t try;
1776
1777 try = b_contig_space(buf);
1778 if (!try)
1779 break;
1780
1781 if (try > strm_frm->len)
1782 try = strm_frm->len;
1783 memcpy(b_tail(buf), strm_frm->data, try);
1784 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001785 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001786 b_add(buf, try);
1787 ret += try;
1788 }
1789
1790 return ret;
1791}
1792
1793/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1794 * streams may be open. The data are copied to the stream RX buffer if possible.
1795 * If not, the STREAM frame is stored to be treated again later.
1796 * We rely on the flow control so that not to store too much STREAM frames.
1797 * Return 1 if succeeded, 0 if not.
1798 */
1799static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1800 struct quic_stream *strm_frm,
1801 struct quic_conn *qc)
1802{
1803 struct qcs *strm;
1804 struct eb64_node *strm_node, *frm_node;
1805 struct quic_rx_strm_frm *frm;
1806
1807 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1808 if (!strm_node) {
1809 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1810 return 0;
1811 }
1812
1813 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001814 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001815 /* FIXME: handle the case where this frame overlap others */
1816 if (frm_node) {
1817 TRACE_PROTO("Already existing stream data",
1818 QUIC_EV_CONN_PSTRM, qc->conn);
1819 goto out;
1820 }
1821
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001822 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001823 int ret;
1824
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001825 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001826 goto store_frm;
1827
1828 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1829 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1830 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1831 return 0;
1832 }
1833
1834 strm->rx.offset += ret;
1835 }
1836
1837 if (!strm_frm->len)
1838 goto out;
1839
1840 store_frm:
1841 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1842 if (!frm) {
1843 TRACE_PROTO("Could not alloc RX STREAM frame",
1844 QUIC_EV_CONN_PSTRM, qc->conn);
1845 return 0;
1846 }
1847
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001848 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001849 quic_rx_packet_refinc(pkt);
1850
1851 out:
1852 return 1;
1853}
1854
1855/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1856 * streams may be open. The data are copied to the stream RX buffer if possible.
1857 * If not, the STREAM frame is stored to be treated again later.
1858 * We rely on the flow control so that not to store too much STREAM frames.
1859 * Return 1 if succeeded, 0 if not.
1860 */
1861static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1862 struct quic_stream *strm_frm,
1863 struct quic_conn *qc)
1864{
1865 struct qcs *strm;
1866 struct eb64_node *strm_node, *frm_node;
1867 struct quic_rx_strm_frm *frm;
1868 size_t strm_frm_len;
1869
1870 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1871 if (!strm_node) {
1872 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1873 return 0;
1874 }
1875
1876 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001877 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001878 /* FIXME: handle the case where this frame overlap others */
1879 if (frm_node) {
1880 TRACE_PROTO("Already existing stream data",
1881 QUIC_EV_CONN_PSTRM, qc->conn);
1882 goto out;
1883 }
1884
1885 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001886 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001887 int ret;
1888
Amaury Denoyelle1e308ff2021-10-12 18:14:12 +02001889 if (!qc_get_buf(strm, &strm->rx.buf))
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001890 goto store_frm;
1891
1892 /* qc_strm_cpy() will modify the offset, depending on the number
1893 * of bytes copied.
1894 */
1895 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1896 /* Inform the application of the arrival of this new stream */
1897 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1898 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1899 return 0;
1900 }
1901
1902 if (ret)
1903 ruqs_notify_recv(strm);
1904
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001905 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001906 }
1907 /* Take this frame into an account for the stream flow control */
1908 strm->rx.offset += strm_frm_len;
1909 /* It all the data were provided to the application, there is no need to
1910 * store any more inforamtion for it.
1911 */
1912 if (!strm_frm->len)
1913 goto out;
1914
1915 store_frm:
1916 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1917 if (!frm) {
1918 TRACE_PROTO("Could not alloc RX STREAM frame",
1919 QUIC_EV_CONN_PSTRM, qc->conn);
1920 return 0;
1921 }
1922
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001923 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001924 quic_rx_packet_refinc(pkt);
1925
1926 out:
1927 return 1;
1928}
1929
1930static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1931 struct quic_stream *strm_frm,
1932 struct quic_conn *qc)
1933{
1934 if (strm_frm->id & QCS_ID_DIR_BIT)
1935 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1936 else
1937 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1938}
1939
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1941 * as I/O handler context and <qel> as encryption level.
1942 * Returns 1 if succeeded, 0 if failed.
1943 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001944static 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 +01001945 struct quic_enc_level *qel)
1946{
1947 struct quic_frame frm;
1948 const unsigned char *pos, *end;
1949 struct quic_conn *conn = ctx->conn->qc;
1950
1951 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1952 /* Skip the AAD */
1953 pos = pkt->data + pkt->aad_len;
1954 end = pkt->data + pkt->len;
1955
1956 while (pos < end) {
1957 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1958 goto err;
1959
1960 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001961 case QUIC_FT_PADDING:
1962 if (pos != end) {
1963 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1964 goto err;
1965 }
1966 break;
1967 case QUIC_FT_PING:
1968 break;
1969 case QUIC_FT_ACK:
1970 {
1971 unsigned int rtt_sample;
1972
1973 rtt_sample = 0;
1974 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1975 goto err;
1976
1977 if (rtt_sample) {
1978 unsigned int ack_delay;
1979
1980 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1981 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1982 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1983 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001984 break;
1985 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001986 case QUIC_FT_CRYPTO:
1987 if (frm.crypto.offset != qel->rx.crypto.offset) {
1988 struct quic_rx_crypto_frm *cf;
1989
1990 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1991 if (!cf) {
1992 TRACE_DEVEL("CRYPTO frame allocation failed",
1993 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1994 goto err;
1995 }
1996
1997 cf->offset_node.key = frm.crypto.offset;
1998 cf->len = frm.crypto.len;
1999 cf->data = frm.crypto.data;
2000 cf->pkt = pkt;
2001 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
2002 quic_rx_packet_refinc(pkt);
2003 }
2004 else {
2005 /* XXX TO DO: <cf> is used only for the traces. */
2006 struct quic_rx_crypto_frm cf = { };
2007
2008 cf.offset_node.key = frm.crypto.offset;
2009 cf.len = frm.crypto.len;
2010 if (!qc_provide_cdata(qel, ctx,
2011 frm.crypto.data, frm.crypto.len,
2012 pkt, &cf))
2013 goto err;
2014 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002015 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002016 case QUIC_FT_STREAM_8:
2017 case QUIC_FT_STREAM_9:
2018 case QUIC_FT_STREAM_A:
2019 case QUIC_FT_STREAM_B:
2020 case QUIC_FT_STREAM_C:
2021 case QUIC_FT_STREAM_D:
2022 case QUIC_FT_STREAM_E:
2023 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002024 {
2025 struct quic_stream *stream = &frm.stream;
2026
2027 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
2028 if (objt_listener(ctx->conn->target)) {
2029 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2030 goto err;
2031 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2032 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002033
2034 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
2035 goto err;
2036
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002037 break;
2038 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002039 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002040 break;
2041 case QUIC_FT_CONNECTION_CLOSE:
2042 case QUIC_FT_CONNECTION_CLOSE_APP:
Amaury Denoyelle493bb1d2021-10-13 16:34:49 +02002043 conn->qcc->flags |= QC_CF_CC_RECV;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002044 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002045 case QUIC_FT_HANDSHAKE_DONE:
2046 if (objt_listener(ctx->conn->target))
2047 goto err;
2048
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002049 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002050 break;
2051 default:
2052 goto err;
2053 }
2054 }
2055
2056 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2057 * has successfully parse a Handshake packet. The Initial encryption must also
2058 * be discarded.
2059 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002060 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
2061 int state = HA_ATOMIC_LOAD(&conn->state);
2062
2063 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
2064 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2065 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2066 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2067 qc_set_timer(ctx);
2068 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
2069 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
2070 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002071 }
2072
2073 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2074 return 1;
2075
2076 err:
2077 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2078 return 0;
2079}
2080
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002081/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
2082 * buffer. This is the responsability of the caller to check there is enough
2083 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2084 * This function must be called only after having built a correct datagram.
2085 * Always succeeds.
2086 */
2087static inline void qc_set_dg(struct cbuf *cbuf,
2088 uint16_t dglen, struct quic_tx_packet *pkt)
2089{
2090 write_u16(cb_wr(cbuf), dglen);
2091 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2092 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2093}
2094
2095/* Prepare as much as possible handshake packets into <qr> ring buffer for
2096 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2097 * several packets in the same datagram. A header made of two fields is added
2098 * to each datagram: the datagram length followed by the address of the first
2099 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002100 * Returns 1 if succeeded, or 0 if something wrong happened.
2101 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002102static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103{
2104 struct quic_conn *qc;
2105 enum quic_tls_enc_level tel, next_tel;
2106 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002107 struct cbuf *cbuf;
2108 unsigned char *end_buf, *end, *pos, *spos;
2109 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2110 /* length of datagrams */
2111 uint16_t dglen;
2112 size_t total;
2113 /* Each datagram is prepended with its length followed by the
2114 * address of the first packet in the datagram.
2115 */
2116 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002117
2118 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2119 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002120 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002121 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002122 goto err;
2123 }
2124
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002125 start:
2126 total = 0;
2127 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002128 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002129 cbuf = qr->cbuf;
2130 spos = pos = cb_wr(cbuf);
2131 /* Leave at least <dglen> bytes at the end of this buffer
2132 * to ensure there is enough room to mark the end of prepared
2133 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002134 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002135 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2136 first_pkt = prv_pkt = NULL;
2137 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002138 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002139 enum quic_pkt_type pkt_type;
2140
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002141 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002142 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002143 if (!prv_pkt) {
2144 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2145 do {
2146 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2147 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2148 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002149 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002150 /* Do not build any more packet if the TX secrets are not available or
2151 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002152 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002153 * and if there is no more CRYPTO data available or in flight
2154 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002155 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002156 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002157 (!ack && !nb_ptos &&
2158 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2159 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002160 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002161 /* Set the current datagram as prepared into <cbuf> if
2162 * the was already a correct packet which was previously written.
2163 */
2164 if (prv_pkt)
2165 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002166 break;
2167 }
2168
2169 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002170 if (!prv_pkt) {
2171 /* Leave room for the datagram header */
2172 pos += dg_headlen;
2173 end = pos + qc->path->mtu;
2174 }
2175
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002176 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 +02002177 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002178 if (err < 0) {
2179 if (!prv_pkt && nb_ptos)
2180 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002181 if (ack)
2182 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002183 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002184 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002185 case -2:
2186 goto err;
2187 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002188 /* If there was already a correct packet present, set the
2189 * current datagram as prepared into <cbuf>.
2190 */
2191 if (prv_pkt) {
2192 qc_set_dg(cbuf, dglen, first_pkt);
2193 goto stop_build;
2194 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002195 goto out;
2196 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002197 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2198 if (!cur_pkt)
2199 goto err;
2200
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002201 total += cur_pkt->len;
2202 /* keep trace of the first packet in the datagram */
2203 if (!first_pkt)
2204 first_pkt = cur_pkt;
2205 /* Attach the current one to the previous one */
2206 if (prv_pkt)
2207 prv_pkt->next = cur_pkt;
2208 /* Let's say we have to build a new dgram */
2209 prv_pkt = NULL;
2210 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002211 /* Discard the Initial encryption keys as soon as
2212 * a handshake packet could be built.
2213 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002214 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002215 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2216 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002217 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002218 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2219 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002220 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002221 }
2222 /* Special case for Initial packets: when they have all
2223 * been sent, select the next level.
2224 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002225 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002226 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2227 (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 +02002228 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2229 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2230 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002231 tel = next_tel;
2232 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002233 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002234 /* If there is data for the next level, do not
2235 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002236 * which sends only one Initial packet, then wait
2237 * for additional CRYPTO data from the server to enter the
2238 * next level.
2239 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002240 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002241 }
2242 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002243 }
2244
2245 /* If we have to build a new datagram, set the current datagram as
2246 * prepared into <cbuf>.
2247 */
2248 if (!prv_pkt) {
2249 qc_set_dg(cbuf, dglen, first_pkt);
2250 first_pkt = NULL;
2251 dglen = 0;
2252 }
2253 }
2254
2255 stop_build:
2256 /* Reset <wr> writer index if in front of <rd> index */
2257 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2258 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2259
2260 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2261 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2262 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2263 /* Mark the end of contiguous data for the reader */
2264 write_u16(cb_wr(cbuf), 0);
2265 cb_add(cbuf, sizeof(uint16_t));
2266 }
2267 }
2268
2269 if (rd && rd <= cbuf->wr) {
2270 cb_wr_reset(cbuf);
2271 if (pos == spos) {
2272 /* Reuse the same buffer if nothing was built. */
2273 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002274 }
2275 }
2276 }
2277
2278 out:
2279 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002280 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002281
2282 err:
2283 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002284 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002285}
2286
2287/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002288 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002289 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002290int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002291{
2292 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002293 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002294
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002295 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002296 cbuf = qr->cbuf;
2297 while (cb_contig_data(cbuf)) {
2298 unsigned char *pos;
2299 struct buffer tmpbuf = { };
2300 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2301 uint16_t dglen;
2302 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002303 unsigned int time_sent;
2304
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002305 pos = cb_rd(cbuf);
2306 dglen = read_u16(pos);
2307 /* End of prepared datagrams.
2308 * Reset the reader index only if in front of the writer index.
2309 */
2310 if (!dglen) {
2311 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2312
2313 if (wr && wr < cbuf->rd) {
2314 cb_rd_reset(cbuf);
2315 continue;
2316 }
2317 break;
2318 }
2319
2320 pos += sizeof dglen;
2321 first_pkt = read_ptr(pos);
2322 pos += sizeof first_pkt;
2323 tmpbuf.area = (char *)pos;
2324 tmpbuf.size = tmpbuf.data = dglen;
2325
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002326 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002327 for (pkt = first_pkt; pkt; pkt = pkt->next)
2328 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002329 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002330 &tmpbuf, tmpbuf.data, 0) <= 0) {
2331 for (pkt = first_pkt; pkt; pkt = pkt->next)
2332 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002333 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002334 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002335
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002336 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002337 qc->tx.bytes += tmpbuf.data;
2338 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002339
2340 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2341 pkt->time_sent = time_sent;
2342 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2343 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002344 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002345 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002346 qc->path->in_flight += pkt->in_flight_len;
2347 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2348 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002350 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2351 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002352 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002353 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002354 }
2355 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002356
2357 return 1;
2358}
2359
2360/* Build all the frames which must be sent just after the handshake have succeeded.
2361 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2362 * a HANDSHAKE_DONE frame.
2363 * Return 1 if succeeded, 0 if not.
2364 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002365static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002366{
2367 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002368 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002369 struct quic_frame *frm;
2370
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002371 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002372 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002373 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002374 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002375 if (!frm)
2376 return 0;
2377
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002378 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002379 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002380 }
2381
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002382 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002383 struct quic_connection_id *cid;
2384
2385 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002386 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002387 if (!frm || !cid)
2388 goto err;
2389
2390 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002391 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002392 }
2393
2394 return 1;
2395
2396 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002397 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398 return 0;
2399}
2400
2401/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002402void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002403{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002404 struct eb64_node *n;
2405 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002407 n = eb64_first(&arngs->root);
2408 while (n) {
2409 struct eb64_node *next;
2410
2411 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2412 next = eb64_next(n);
2413 eb64_delete(n);
2414 free(ar);
2415 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002416 }
2417}
2418
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002419/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2420 * descending order.
2421 */
2422static inline size_t sack_gap(struct quic_arng_node *p,
2423 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002424{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002425 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002426}
2427
2428
2429/* Remove the last elements of <ack_ranges> list of ack range updating its
2430 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002431 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002432 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002433static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002434{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002435 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002436
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002437 last = eb64_last(&arngs->root);
2438 while (last && arngs->enc_sz > limit) {
2439 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002440
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002441 prev = eb64_prev(last);
2442 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002443 return 0;
2444
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002445 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2446 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2447 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2448 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2449 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2450 --arngs->sz;
2451 eb64_delete(last);
2452 pool_free(pool_head_quic_arng, last);
2453 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002454 }
2455
2456 return 1;
2457}
2458
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002459/* Set the encoded size of <arngs> QUIC ack ranges. */
2460static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2461{
2462 struct eb64_node *node, *next;
2463 struct quic_arng_node *ar, *ar_next;
2464
2465 node = eb64_last(&arngs->root);
2466 if (!node)
2467 return;
2468
2469 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2470 arngs->enc_sz = quic_int_getsize(ar->last) +
2471 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2472
2473 while ((next = eb64_prev(node))) {
2474 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2475 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2476 quic_int_getsize(ar_next->last - ar_next->first.key);
2477 node = next;
2478 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2479 }
2480}
2481
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002482/* Insert <ar> ack range into <argns> tree of ack ranges.
2483 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002484 */
2485static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002486struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2487 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002488{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002489 struct quic_arng_node *new_ar;
2490
2491 new_ar = pool_alloc(pool_head_quic_arng);
2492 if (new_ar) {
2493 new_ar->first.key = ar->first;
2494 new_ar->last = ar->last;
2495 eb64_insert(&arngs->root, &new_ar->first);
2496 arngs->sz++;
2497 }
2498
2499 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002500}
2501
2502/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002503 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002504 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002505 *
2506 * Descending order
2507 * ------------->
2508 * range1 range2
2509 * ..........|--------|..............|--------|
2510 * ^ ^ ^ ^
2511 * | | | |
2512 * last1 first1 last2 first2
2513 * ..........+--------+--------------+--------+......
2514 * diff1 gap12 diff2
2515 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002516 * To encode the previous list of ranges we must encode integers as follows in
2517 * descending order:
2518 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002519 * with diff1 = last1 - first1
2520 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002521 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002522 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002523 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002524int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2525 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002526{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002527 struct eb64_node *le;
2528 struct quic_arng_node *new_node;
2529 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002530
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002531 new = NULL;
2532 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002533 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002534 if (!new_node)
2535 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002536
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002537 goto out;
2538 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002539
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002540 le = eb64_lookup_le(&arngs->root, ar->first);
2541 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002542 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002543 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002544 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002545 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002546 else {
2547 struct quic_arng_node *le_ar =
2548 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002549
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002550 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002551 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002552 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002553
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002554 if (le_ar->last + 1 >= ar->first) {
2555 le_ar->last = ar->last;
2556 new = le;
2557 new_node = le_ar;
2558 }
2559 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002560 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002561 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002563
2564 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002565 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002566 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002567
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002568 /* Verify that the new inserted node does not overlap the nodes
2569 * which follow it.
2570 */
2571 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002572 struct eb64_node *next;
2573 struct quic_arng_node *next_node;
2574
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002575 while ((next = eb64_next(new))) {
2576 next_node =
2577 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002578 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002579 break;
2580
2581 if (next_node->last > new_node->last)
2582 new_node->last = next_node->last;
2583 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002584 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002585 /* Decrement the size of these ranges. */
2586 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002587 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002588 }
2589
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002590 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002591 quic_arngs_set_enc_sz(arngs);
2592
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002593 return 1;
2594}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002595/* Remove the header protection of packets at <el> encryption level.
2596 * Always succeeds.
2597 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002598static 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 +01002599{
2600 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002601 struct quic_rx_packet *pqpkt;
2602 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002603 struct quic_enc_level *app_qel;
2604
2605 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2606 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2607 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002608 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002609 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002610 TRACE_PROTO("hp not removed (handshake not completed)",
2611 QUIC_EV_CONN_ELRMHP, ctx->conn);
2612 goto out;
2613 }
2614 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002615 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002616 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2617 pqpkt->data + pqpkt->pn_offset,
2618 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2619 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2620 /* XXX TO DO XXX */
2621 }
2622 else {
2623 /* The AAD includes the packet number field */
2624 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2625 /* Store the packet into the tree of packets to decrypt. */
2626 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002627 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002628 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2629 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002630 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002631 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2632 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002633 MT_LIST_DELETE_SAFE(pkttmp1);
2634 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002635 }
2636
2637 out:
2638 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2639}
2640
2641/* Process all the CRYPTO frame at <el> encryption level.
2642 * Return 1 if succeeded, 0 if not.
2643 */
2644static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002645 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002646{
2647 struct eb64_node *node;
2648
2649 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002650 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002651 node = eb64_first(&el->rx.crypto.frms);
2652 while (node) {
2653 struct quic_rx_crypto_frm *cf;
2654
2655 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2656 if (cf->offset_node.key != el->rx.crypto.offset)
2657 break;
2658
2659 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2660 goto err;
2661
2662 node = eb64_next(node);
2663 quic_rx_packet_refdec(cf->pkt);
2664 eb64_delete(&cf->offset_node);
2665 pool_free(pool_head_quic_rx_crypto_frm, cf);
2666 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002667 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2669 return 1;
2670
2671 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002672 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2674 return 0;
2675}
2676
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002677/* Process all the packets at <el> and <next_el> encryption level.
2678 * This is the caller responsability to check that <cur_el> is different of <next_el>
2679 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002680 * Return 1 if succeeded, 0 if not.
2681 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002682int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2683 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002684{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002685 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002686 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002687 struct quic_conn *qc = ctx->conn->qc;
2688 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002689
2690 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002691 qel = cur_el;
2692 next_tel:
2693 if (!qel)
2694 goto out;
2695
2696 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2697 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002698 while (node) {
2699 struct quic_rx_packet *pkt;
2700
2701 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002702 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2703 ctx->conn, pkt, NULL, ctx->ssl);
2704 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002705 /* Drop the packet */
2706 TRACE_PROTO("packet decryption failed -> dropped",
2707 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2708 }
2709 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002710 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002711 /* Drop the packet */
2712 TRACE_PROTO("packet parsing failed -> dropped",
2713 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2714 }
2715 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002716 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2717
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002718 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002719 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2720 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002721 if (pkt->pn > largest_pn)
2722 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002723 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002724 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002725 TRACE_DEVEL("Could not update ack range list",
2726 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002727 }
2728 }
2729 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002730 eb64_delete(&pkt->pn_node);
2731 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002732 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002733 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002734
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002735 /* Update the largest packet number. */
2736 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002737 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2738 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002739 goto err;
2740
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002741 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002742 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002743 qel = next_el;
2744 goto next_tel;
2745 }
2746
2747 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2749 return 1;
2750
2751 err:
2752 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2753 return 0;
2754}
2755
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002756/* QUIC connection packet handler task. */
2757struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002758{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002759 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002760 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002761 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002762 enum quic_tls_enc_level tel, next_tel;
2763 struct quic_enc_level *qel, *next_qel;
2764 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002765 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002766 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002768 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002769 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002770 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002771 st = HA_ATOMIC_LOAD(&qc->state);
2772 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002773 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002774 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002775 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002776 goto err;
2777
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002778 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002779 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780
2781 next_level:
2782 tls_ctx = &qel->tls_ctx;
2783
2784 /* If the header protection key for this level has been derived,
2785 * remove the packet header protections.
2786 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002787 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2789 qc_rm_hp_pkts(qel, ctx);
2790
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002791 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002792 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2793 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2794 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002795 goto err;
2796
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002797 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002798 if (st >= QUIC_HS_ST_COMPLETE &&
2799 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002800 /* Discard the Handshake keys. */
2801 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002802 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002803 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2804 qc_set_timer(ctx);
2805 if (!quic_build_post_handshake_frames(qc))
2806 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002807 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002808 }
2809
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002810 if (!qr)
2811 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2812 ret = qc_prep_hdshk_pkts(qr, ctx);
2813 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002814 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002815 else if (ret == 0)
2816 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002818 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002819 goto err;
2820
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002821 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002822 /* Check if there is something to do for the next level.
2823 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002824 if (next_qel && next_qel != qel &&
2825 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002826 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002828 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829 goto next_level;
2830 }
2831
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002832 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002833 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002834 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002835
2836 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002837 if (qr)
2838 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002839 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002840 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002841}
2842
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002843/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002844static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2845{
2846 int i;
2847
2848 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2849 if (qel->tx.crypto.bufs[i]) {
2850 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2851 qel->tx.crypto.bufs[i] = NULL;
2852 }
2853 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002854 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002855}
2856
2857/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002858 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859 * Returns 1 if succeeded, 0 if not.
2860 */
2861static int quic_conn_enc_level_init(struct quic_conn *qc,
2862 enum quic_tls_enc_level level)
2863{
2864 struct quic_enc_level *qel;
2865
2866 qel = &qc->els[level];
2867 qel->level = quic_to_ssl_enc_level(level);
2868 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2869 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2870 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2871 qel->tls_ctx.rx.flags = 0;
2872 qel->tls_ctx.tx.flags = 0;
2873
2874 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002875 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002876 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002877 qel->rx.crypto.offset = 0;
2878 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2879 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002880
2881 /* Allocate only one buffer. */
2882 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2883 if (!qel->tx.crypto.bufs)
2884 goto err;
2885
2886 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2887 if (!qel->tx.crypto.bufs[0])
2888 goto err;
2889
2890 qel->tx.crypto.bufs[0]->sz = 0;
2891 qel->tx.crypto.nb_buf = 1;
2892
2893 qel->tx.crypto.sz = 0;
2894 qel->tx.crypto.offset = 0;
2895
2896 return 1;
2897
2898 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002899 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002900 return 0;
2901}
2902
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002903/* Release all the memory allocated for <conn> QUIC connection. */
2904static void quic_conn_free(struct quic_conn *conn)
2905{
2906 int i;
2907
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002908 if (!conn)
2909 return;
2910
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002911 free_quic_conn_cids(conn);
Amaury Denoyelle2af19852021-09-30 11:03:28 +02002912
2913 /* remove the connection from receiver cids trees */
2914 HA_RWLOCK_WRLOCK(OTHER_LOCK, &conn->li->rx.cids_lock);
2915 ebmb_delete(&conn->odcid_node);
2916 ebmb_delete(&conn->scid_node);
2917 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &conn->li->rx.cids_lock);
2918
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002919 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2920 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002921 if (conn->timer_task)
2922 task_destroy(conn->timer_task);
2923 pool_free(pool_head_quic_conn, conn);
2924}
2925
Amaury Denoyelle414cac52021-09-22 11:14:37 +02002926void quic_close(struct connection *conn, void *xprt_ctx)
2927{
2928 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
2929 struct quic_conn *qc = conn_ctx->conn->qc;
2930 quic_conn_free(qc);
2931}
2932
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002933/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002934static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002935{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002936 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002937 struct quic_conn *qc;
2938 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002939 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002940
2941 conn_ctx = task->context;
2942 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002943 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2944 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002945 task->expire = TICK_ETERNITY;
2946 pktns = quic_loss_pktns(qc);
2947 if (tick_isset(pktns->tx.loss_time)) {
2948 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2949
2950 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2951 if (!LIST_ISEMPTY(&lost_pkts))
2952 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2953 qc_set_timer(conn_ctx);
2954 goto out;
2955 }
2956
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002957 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002958 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002959 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002960 pktns->tx.pto_probe = 1;
2961 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002962 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002963 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2964 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2965
2966 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2967 hel->pktns->tx.pto_probe = 1;
2968 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2969 iel->pktns->tx.pto_probe = 1;
2970 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002971 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002972 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2973 qc->path->loss.pto_count++;
2974
2975 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002976 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002977
2978 return task;
2979}
2980
2981/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2982 * connections used to identify the first Initial packets of client connecting
2983 * to listeners. This parameter must be NULL for QUIC connections attached
2984 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2985 * <scid> is the source connection ID with <scid_len> as length.
2986 * Returns 1 if succeeded, 0 if not.
2987 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002988static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2989 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002990 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002991{
2992 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002993 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002994 /* Initial CID. */
2995 struct quic_connection_id *icid;
2996
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002997 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002998 qc = pool_zalloc(pool_head_quic_conn);
2999 if (!qc) {
3000 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
3001 goto err;
3002 }
3003
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003004 qc->cids = EB_ROOT;
3005 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003006 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003007 struct listener *l = owner;
3008
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003009 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003010 /* Copy the initial DCID. */
3011 qc->odcid.len = dcid_len;
3012 if (qc->odcid.len)
3013 memcpy(qc->odcid.data, dcid, dcid_len);
3014
3015 /* Copy the SCID as our DCID for this connection. */
3016 if (scid_len)
3017 memcpy(qc->dcid.data, scid, scid_len);
3018 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003019 qc->tx.qring_list = &l->rx.tx_qrings;
Amaury Denoyelle2af19852021-09-30 11:03:28 +02003020 qc->li = l;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003021 }
3022 /* QUIC Client (outgoing connection to servers) */
3023 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003024 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025 if (dcid_len)
3026 memcpy(qc->dcid.data, dcid, dcid_len);
3027 qc->dcid.len = dcid_len;
3028 }
3029
3030 /* Initialize the output buffer */
3031 qc->obuf.pos = qc->obuf.data;
3032
3033 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003034 if (!icid) {
3035 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
3036 goto err;
3037 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003038
3039 /* Select our SCID which is the first CID with 0 as sequence number. */
3040 qc->scid = icid->cid;
3041
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003042 /* Packet number spaces initialization. */
3043 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3044 quic_pktns_init(&qc->pktns[i]);
3045 /* QUIC encryption level context initialization. */
3046 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003047 if (!quic_conn_enc_level_init(qc, i)) {
3048 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003049 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003050 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003051 /* Initialize the packet number space. */
3052 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3053 }
3054
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003055 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003056 /* TX part. */
3057 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003058 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3059 qc->tx.wbuf = qc->tx.rbuf = 0;
3060 qc->tx.bytes = 0;
3061 qc->tx.nb_pto_dgrams = 0;
3062 /* RX part. */
3063 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003064 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003065
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003066 /* XXX TO DO: Only one path at this time. */
3067 qc->path = &qc->paths[0];
3068 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3069
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003070 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003071
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003072 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003073
3074 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003075 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003076 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003077 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003078}
3079
3080/* Initialize the timer task of <qc> QUIC connection.
3081 * Returns 1 if succeeded, 0 if not.
3082 */
3083static int quic_conn_init_timer(struct quic_conn *qc)
3084{
Willy Tarreaubeeabf52021-10-01 18:23:30 +02003085 qc->timer_task = task_new_anywhere();
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003086 if (!qc->timer_task)
3087 return 0;
3088
3089 qc->timer = TICK_ETERNITY;
3090 qc->timer_task->process = process_timer;
3091 qc->timer_task->context = qc->conn->xprt_ctx;
3092
3093 return 1;
3094}
3095
3096/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3097 * past one byte of this buffer.
3098 */
3099static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3100 struct quic_rx_packet *pkt)
3101{
3102 unsigned char dcid_len, scid_len;
3103
3104 /* Version */
3105 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3106 return 0;
3107
3108 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
3109
3110 /* Destination Connection ID Length */
3111 dcid_len = *(*buf)++;
3112 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3113 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3114 /* XXX MUST BE DROPPED */
3115 return 0;
3116
3117 if (dcid_len) {
3118 /* Check that the length of this received DCID matches the CID lengths
3119 * of our implementation for non Initials packets only.
3120 */
3121 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3122 return 0;
3123
3124 memcpy(pkt->dcid.data, *buf, dcid_len);
3125 }
3126
3127 pkt->dcid.len = dcid_len;
3128 *buf += dcid_len;
3129
3130 /* Source Connection ID Length */
3131 scid_len = *(*buf)++;
3132 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3133 /* XXX MUST BE DROPPED */
3134 return 0;
3135
3136 if (scid_len)
3137 memcpy(pkt->scid.data, *buf, scid_len);
3138 pkt->scid.len = scid_len;
3139 *buf += scid_len;
3140
3141 return 1;
3142}
3143
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003144/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3145 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3146 * encryption level matching with the packet type. <*qel> may be null if not found.
3147 * Note that <ctx> may be null (for Initial packets).
3148 */
3149static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003150 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003151 struct quic_enc_level **qel)
3152{
3153 enum quic_tls_enc_level tel;
3154
3155 /* Special case without connection context (firt Initial packets) */
3156 if (!ctx) {
3157 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3158 return 1;
3159 }
3160
3161 tel = quic_packet_type_enc_level(pkt->type);
3162 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3163 *qel = NULL;
3164 return 0;
3165 }
3166
3167 *qel = &qc->els[tel];
3168 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3169 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3170 return 0;
3171 }
3172
3173 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003174 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3175 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003176 return 1;
3177
3178 return 0;
3179}
3180
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003181/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3182 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3183 * byte past the end of the buffer containing this packet and <beg> the address of
3184 * the packet first byte.
3185 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3186 * Returns 1 if succeeded, 0 if not.
3187 */
3188static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3189 unsigned char **buf, unsigned char *beg,
3190 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003191 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003192{
3193 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003194 struct quic_enc_level *qel;
3195 /* Only for traces. */
3196 struct quic_rx_packet *qpkt_trace;
3197
3198 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003199 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003200 /* The packet number is here. This is also the start minus
3201 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3202 * protection.
3203 */
3204 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003205 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003206 /* Note that the following function enables us to unprotect the packet
3207 * number and its length subsequently used to decrypt the entire
3208 * packets.
3209 */
3210 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3211 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003212 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003213 goto err;
3214 }
3215
3216 /* The AAD includes the packet number field found at <pn>. */
3217 pkt->aad_len = pn - beg + pkt->pnl;
3218 qpkt_trace = pkt;
3219 /* Store the packet */
3220 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003221 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003222 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3223 quic_rx_packet_refinc(pkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003224 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003225 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003226 else if (qel) {
3227 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003228 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003229 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3230 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003231 }
3232
3233 memcpy(pkt->data, beg, pkt->len);
3234 /* Updtate the offset of <*buf> for the next QUIC packet. */
3235 *buf = beg + pkt->len;
3236
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003237 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003238 return 1;
3239
3240 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003241 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 +01003242 return 0;
3243}
3244
3245/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3246 * Also set <*long_header> to 1 if this form is long, 0 if not.
3247 */
3248static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3249 unsigned char byte0, int *long_header)
3250{
3251 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3252 pkt->type =
3253 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3254 *long_header = 1;
3255 }
3256 else {
3257 pkt->type = QUIC_PACKET_TYPE_SHORT;
3258 *long_header = 0;
3259 }
3260}
3261
3262static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3263 struct quic_rx_packet *pkt,
3264 struct quic_dgram_ctx *dgram_ctx,
3265 struct sockaddr_storage *saddr)
3266{
3267 unsigned char *beg;
3268 uint64_t len;
3269 struct quic_conn *qc;
3270 struct eb_root *cids;
3271 struct ebmb_node *node;
3272 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003273 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003274 int long_header;
3275
3276 qc = NULL;
3277 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3278 if (end <= *buf)
3279 goto err;
3280
3281 /* Fixed bit */
3282 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3283 /* XXX TO BE DISCARDED */
3284 goto err;
3285
3286 srv_conn = dgram_ctx->owner;
3287 beg = *buf;
3288 /* Header form */
3289 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3290 if (long_header) {
3291 size_t cid_lookup_len;
3292
3293 if (!quic_packet_read_long_header(buf, end, pkt))
3294 goto err;
3295
3296 /* For Initial packets, and for servers (QUIC clients connections),
3297 * there is no Initial connection IDs storage.
3298 */
3299 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3300 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3301 cid_lookup_len = pkt->dcid.len;
3302 }
3303 else {
3304 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3305 cid_lookup_len = QUIC_CID_LEN;
3306 }
3307
3308 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3309 if (!node)
3310 goto err;
3311
3312 qc = ebmb_entry(node, struct quic_conn, scid_node);
3313
3314 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3315 qc->dcid.len = pkt->scid.len;
3316 if (pkt->scid.len)
3317 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3318 }
3319
3320 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3321 uint64_t token_len;
3322
3323 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3324 goto err;
3325
3326 /* XXX TO DO XXX 0 value means "the token is not present".
3327 * A server which sends an Initial packet must not set the token.
3328 * So, a client which receives an Initial packet with a token
3329 * MUST discard the packet or generate a connection error with
3330 * PROTOCOL_VIOLATION as type.
3331 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3332 */
3333 pkt->token_len = token_len;
3334 }
3335 }
3336 else {
3337 /* XXX TO DO: Short header XXX */
3338 if (end - *buf < QUIC_CID_LEN)
3339 goto err;
3340
3341 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3342 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3343 if (!node)
3344 goto err;
3345
3346 qc = ebmb_entry(node, struct quic_conn, scid_node);
3347 *buf += QUIC_CID_LEN;
3348 }
3349 /* Store the DCID used for this packet to check the packet which
3350 * come in this UDP datagram match with it.
3351 */
3352 if (!dgram_ctx->dcid_node)
3353 dgram_ctx->dcid_node = node;
3354 /* Only packets packets with long headers and not RETRY or VERSION as type
3355 * have a length field.
3356 */
3357 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3358 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3359 goto err;
3360
3361 pkt->len = len;
3362 }
3363 else if (!long_header) {
3364 /* A short packet is the last one of an UDP datagram. */
3365 pkt->len = end - *buf;
3366 }
3367
3368 conn_ctx = qc->conn->xprt_ctx;
3369
3370 /* Increase the total length of this packet by the header length. */
3371 pkt->len += *buf - beg;
3372 /* Do not check the DCID node before the length. */
3373 if (dgram_ctx->dcid_node != node) {
3374 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3375 goto err;
3376 }
3377
3378 if (pkt->len > sizeof pkt->data) {
3379 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3380 goto err;
3381 }
3382
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003383 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003384 goto err;
3385
3386 /* Wake the tasklet of the QUIC connection packet handler. */
3387 if (conn_ctx)
3388 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3389
3390 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3391
3392 return pkt->len;
3393
3394 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003395 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 return -1;
3397}
3398
3399static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3400 struct quic_rx_packet *pkt,
3401 struct quic_dgram_ctx *dgram_ctx,
3402 struct sockaddr_storage *saddr)
3403{
3404 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003405 struct quic_conn *qc;
3406 struct eb_root *cids;
3407 struct ebmb_node *node;
3408 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003409 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003410 int long_header = 0;
3411
3412 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003413 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003414 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003415 if (end <= *buf)
3416 goto err;
3417
3418 /* Fixed bit */
3419 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3420 /* XXX TO BE DISCARDED */
3421 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3422 goto err;
3423 }
3424
3425 l = dgram_ctx->owner;
3426 beg = *buf;
3427 /* Header form */
3428 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3429 if (long_header) {
3430 unsigned char dcid_len;
3431
3432 if (!quic_packet_read_long_header(buf, end, pkt)) {
3433 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3434 goto err;
3435 }
3436
3437 dcid_len = pkt->dcid.len;
3438 /* For Initial packets, and for servers (QUIC clients connections),
3439 * there is no Initial connection IDs storage.
3440 */
3441 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003442 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003443 /* DCIDs of first packets coming from clients may have the same values.
3444 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3445 */
3446 quic_cid_saddr_cat(&pkt->dcid, saddr);
3447 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003448
3449 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3450 end - *buf < token_len) {
3451 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3452 goto err;
3453 }
3454
3455 /* XXX TO DO XXX 0 value means "the token is not present".
3456 * A server which sends an Initial packet must not set the token.
3457 * So, a client which receives an Initial packet with a token
3458 * MUST discard the packet or generate a connection error with
3459 * PROTOCOL_VIOLATION as type.
3460 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3461 */
3462 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003463 }
3464 else {
3465 if (pkt->dcid.len != QUIC_CID_LEN) {
3466 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3467 goto err;
3468 }
3469
3470 cids = &l->rx.cids;
3471 }
3472
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003473 /* Only packets packets with long headers and not RETRY or VERSION as type
3474 * have a length field.
3475 */
3476 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3477 uint64_t len;
3478
3479 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3480 end - *buf < len) {
3481 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3482 goto err;
3483 }
3484
3485 pkt->len = len;
3486 }
3487
3488
3489 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003490 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3491 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3492 cids == &l->rx.odcids) {
3493 /* Switch to the definitive tree ->cids containing the final CIDs. */
3494 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3495 if (node) {
3496 /* If found, signal this with NULL as special value for <cids>. */
3497 pkt->dcid.len = dcid_len;
3498 cids = NULL;
3499 }
3500 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003501 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003502
3503 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003504 int ipv4;
3505 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003506 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003507 const unsigned char *salt = initial_salt_v1;
3508 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003509
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003510 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3511 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3512 goto err;
3513 }
3514
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003515 pkt->saddr = *saddr;
3516 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3517 * of <saddr>.
3518 */
3519 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003520 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003521 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003522 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003523 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003524 goto err;
3525
3526 odcid = &qc->rx.params.original_destination_connection_id;
3527 /* Copy the transport parameters. */
3528 qc->rx.params = l->bind_conf->quic_params;
3529 /* Copy original_destination_connection_id transport parameter. */
3530 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3531 odcid->len = pkt->odcid_len;
3532 /* Copy the initial source connection ID. */
3533 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3534 qc->enc_params_len =
3535 quic_transport_params_encode(qc->enc_params,
3536 qc->enc_params + sizeof qc->enc_params,
3537 &qc->rx.params, 1);
3538 if (!qc->enc_params_len)
3539 goto err;
3540
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003541 /* NOTE: the socket address has been concatenated to the destination ID
3542 * chosen by the client for Initial packets.
3543 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003544 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3545 salt = initial_salt_draft_29;
3546 salt_len = sizeof initial_salt_draft_29;
3547 }
3548 if (!qc_new_isecs(qc, salt, salt_len,
3549 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003550 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3551 goto err;
3552 }
3553
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003554 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003555 /* This is the DCID node sent in this packet by the client. */
3556 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003557 /* Enqueue this packet. */
3558 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3559 /* Try to accept a new connection. */
3560 listener_accept(l);
3561
3562 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3563 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3564 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3565 /* Insert our SCID, the connection ID for the QUIC client. */
3566 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3567 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3568 if (n != &qc->scid_node) {
3569 quic_conn_free(qc);
3570 qc = ebmb_entry(n, struct quic_conn, scid_node);
3571 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572 }
3573 else {
3574 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3575 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3576 else
3577 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003578 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003579 }
3580 }
3581 else {
3582 if (end - *buf < QUIC_CID_LEN) {
3583 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3584 goto err;
3585 }
3586
3587 cids = &l->rx.cids;
3588 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3589 if (!node) {
3590 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3591 goto err;
3592 }
3593
3594 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003595 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003596 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003597 /* A short packet is the last one of an UDP datagram. */
3598 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003599 }
3600
3601 /* Store the DCID used for this packet to check the packet which
3602 * come in this UDP datagram match with it.
3603 */
3604 if (!dgram_ctx->dcid_node) {
3605 dgram_ctx->dcid_node = node;
3606 dgram_ctx->qc = qc;
3607 }
3608
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003609 /* Increase the total length of this packet by the header length. */
3610 pkt->len += *buf - beg;
3611 /* Do not check the DCID node before the length. */
3612 if (dgram_ctx->dcid_node != node) {
3613 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3614 goto err;
3615 }
3616
3617 if (pkt->len > sizeof pkt->data) {
3618 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3619 goto err;
3620 }
3621
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003622 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3623 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3624 goto err;
3625 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003626
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003627
3628 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003629 /* Wake up the connection packet handler task from here only if all
3630 * the contexts have been initialized, especially the mux context
3631 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3632 * will start it if these contexts for the connection are not already
3633 * initialized.
3634 */
3635 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003636 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003637
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003638 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3639
3640 return pkt->len;
3641
3642 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003643 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003644 qc ? qc->conn : NULL, pkt);
3645 return -1;
3646}
3647
3648/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3649 * in advance. This is the reponsability of the caller to check there is enough room in this
3650 * buffer to build a long header.
3651 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3652 */
3653static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3654 int type, size_t pn_len, struct quic_conn *conn)
3655{
3656 if (type > QUIC_PACKET_TYPE_RETRY)
3657 return 0;
3658
3659 /* #0 byte flags */
3660 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3661 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3662 /* Version */
3663 quic_write_uint32(buf, end, conn->version);
3664 *(*buf)++ = conn->dcid.len;
3665 /* Destination connection ID */
3666 if (conn->dcid.len) {
3667 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3668 *buf += conn->dcid.len;
3669 }
3670 /* Source connection ID */
3671 *(*buf)++ = conn->scid.len;
3672 if (conn->scid.len) {
3673 memcpy(*buf, conn->scid.data, conn->scid.len);
3674 *buf += conn->scid.len;
3675 }
3676
3677 return 1;
3678}
3679
3680/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3681 * in advance. This is the reponsability of the caller to check there is enough room in this
3682 * buffer to build a long header.
3683 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3684 */
3685static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3686 size_t pn_len, struct quic_conn *conn)
3687{
3688 /* #0 byte flags */
3689 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3690 /* Destination connection ID */
3691 if (conn->dcid.len) {
3692 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3693 *buf += conn->dcid.len;
3694 }
3695
3696 return 1;
3697}
3698
3699/* Apply QUIC header protection to the packet with <buf> as first byte address,
3700 * <pn> as address of the Packet number field, <pnlen> being this field length
3701 * with <aead> as AEAD cipher and <key> as secret key.
3702 * Returns 1 if succeeded or 0 if failed.
3703 */
3704static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3705 const EVP_CIPHER *aead, const unsigned char *key)
3706{
3707 int i, ret, outlen;
3708 EVP_CIPHER_CTX *ctx;
3709 /* We need an IV of at least 5 bytes: one byte for bytes #0
3710 * and at most 4 bytes for the packet number
3711 */
3712 unsigned char mask[5] = {0};
3713
3714 ret = 0;
3715 ctx = EVP_CIPHER_CTX_new();
3716 if (!ctx)
3717 return 0;
3718
3719 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3720 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3721 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3722 goto out;
3723
3724 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3725 for (i = 0; i < pnlen; i++)
3726 pn[i] ^= mask[i + 1];
3727
3728 ret = 1;
3729
3730 out:
3731 EVP_CIPHER_CTX_free(ctx);
3732
3733 return ret;
3734}
3735
3736/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3737 * ACK ranges if needed to a value below <limit> in bytes.
3738 * Return 1 if succeeded, 0 if not.
3739 */
3740static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3741{
3742 size_t room, ack_delay_sz;
3743
3744 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3745 /* A frame is made of 1 byte for the frame type. */
3746 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003747 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748 return 0;
3749
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003750 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751}
3752
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003753/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3754 * 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 +01003755 * and <*len> the packet Length field initialized with the number of bytes already present
3756 * 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 +02003757 * <headlen> is the number of bytes already present in this packet before building frames.
3758 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003759 * Update consequently <*len> to reflect the size of these frames built
3760 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003761 * Return 1 if succeeded, 0 if not.
3762 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003763static inline int qc_build_frms(struct quic_tx_packet *pkt,
3764 size_t room, size_t *len, size_t headlen,
3765 struct quic_enc_level *qel,
3766 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003768 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003769 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003770 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003771 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003772
Frédéric Lécailleea604992020-12-24 13:01:37 +01003773 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003774 if (*len > room || headlen > remain)
3775 return 0;
3776
Frédéric Lécailleea604992020-12-24 13:01:37 +01003777 /* If we are not probing we must take into an account the congestion
3778 * control window.
3779 */
3780 if (!conn->tx.nb_pto_dgrams)
3781 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003782 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003783 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003784 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 +01003785 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003786 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787
Frédéric Lécailleea604992020-12-24 13:01:37 +01003788 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789 break;
3790
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003791 switch (cf->type) {
3792 case QUIC_FT_CRYPTO:
3793 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3794 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3795 /* Compute the length of this CRYPTO frame header */
3796 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3797 /* Compute the data length of this CRyPTO frame. */
3798 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3799 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3800 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3801 if (!dlen)
3802 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003803
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003804 pkt->cdata_len += dlen;
3805 /* CRYPTO frame length. */
3806 flen = hlen + quic_int_getsize(dlen) + dlen;
3807 TRACE_PROTO(" CRYPTO frame length (flen)",
3808 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3809 /* Add the CRYPTO data length and its encoded length to the packet
3810 * length and the length of this length.
3811 */
3812 *len += flen;
3813 room -= flen;
3814 if (dlen == cf->crypto.len) {
3815 /* <cf> CRYPTO data have been consumed. */
3816 MT_LIST_DELETE_SAFE(tmp1);
3817 LIST_APPEND(&pkt->frms, &cf->list);
3818 }
3819 else {
3820 struct quic_frame *new_cf;
3821
3822 new_cf = pool_alloc(pool_head_quic_frame);
3823 if (!new_cf) {
3824 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3825 return 0;
3826 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003827
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003828 new_cf->type = QUIC_FT_CRYPTO;
3829 new_cf->crypto.len = dlen;
3830 new_cf->crypto.offset = cf->crypto.offset;
3831 new_cf->crypto.qel = qel;
3832 LIST_APPEND(&pkt->frms, &new_cf->list);
3833 /* Consume <dlen> bytes of the current frame. */
3834 cf->crypto.len -= dlen;
3835 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003836 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003837 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003838
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003839 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003840 /* Note that these frames are accepted in short packets only without
3841 * "Length" packet field. Here, <*len> is used only to compute the
3842 * sum of the lengths of the already built frames for this packet.
3843 */
3844 TRACE_PROTO(" New STREAM frame build (room, len)",
3845 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3846 /* Compute the length of this STREAM frame "header" made a all the field
3847 * excepting the variable ones. Note that +1 is for the type of this frame.
3848 */
3849 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003850 ((cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT) ? quic_int_getsize(cf->stream.offset.key) : 0);
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003851 /* Compute the data length of this STREAM frame. */
3852 avail_room = room - hlen - *len;
3853 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003854 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003855
3856 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3857 dlen = max_available_room(avail_room, &dlen_sz);
3858 if (dlen > cf->stream.len) {
3859 dlen = cf->stream.len;
3860 }
3861 dlen_sz = quic_int_getsize(dlen);
3862 flen = hlen + dlen_sz + dlen;
3863 }
3864 else {
3865 dlen = QUIC_MIN(avail_room, cf->stream.len);
3866 flen = hlen + dlen;
3867 }
3868 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3869 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3870 TRACE_PROTO(" STREAM frame length (flen)",
3871 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3872 /* Add the STREAM data length and its encoded length to the packet
3873 * length and the length of this length.
3874 */
3875 *len += flen;
3876 room -= flen;
3877 if (dlen == cf->stream.len) {
3878 /* <cf> STREAM data have been consumed. */
3879 MT_LIST_DELETE_SAFE(tmp1);
3880 LIST_APPEND(&pkt->frms, &cf->list);
3881 }
3882 else {
3883 struct quic_frame *new_cf;
3884
3885 new_cf = pool_zalloc(pool_head_quic_frame);
3886 if (!new_cf) {
3887 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3888 return 0;
3889 }
3890
3891 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003892 new_cf->stream.qcs = cf->stream.qcs;
3893 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003894 new_cf->stream.id = cf->stream.id;
3895 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3896 new_cf->stream.offset = cf->stream.offset;
3897 new_cf->stream.len = dlen;
3898 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3899 /* FIN bit reset */
3900 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3901 new_cf->stream.data = cf->stream.data;
3902 LIST_APPEND(&pkt->frms, &new_cf->list);
3903 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3904 /* Consume <dlen> bytes of the current frame. */
3905 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003906 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003907 cf->stream.data += dlen;
3908 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003909 break;
3910
3911 default:
3912 flen = qc_frm_len(cf);
3913 BUG_ON(!flen);
3914 if (flen > room)
3915 continue;
3916
3917 *len += flen;
3918 room -= flen;
3919 MT_LIST_DELETE_SAFE(tmp1);
3920 LIST_APPEND(&pkt->frms, &cf->list);
3921 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003922 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003923 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003924 }
3925
Frédéric Lécailleea604992020-12-24 13:01:37 +01003926 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003927}
3928
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003929/* This function evaluates if <pkt> packet may be built into a buffer with
3930 * <room> as available room. A valid packet should at least contain a valid
3931 * header and at least a frame.
3932 * To estimate the minimal space to build a packet, we consider the worst case:
3933 - there is not enough space to build ack-eliciting frames from
3934 qel->pktns->tx.frms. This is safe to consider this because when we build
3935 a packet we first build the ACK frames, then the ack-eliciting frames
3936 from qel->pktns->tx.frms only if there is enough space for these
3937 ack-eliciting frames, finally PING and PADDING frames if needed,
3938 - we have to ensure there is enough space to build an ACK frame if required,
3939 and a PING frame, even if we do not have to probe,
3940 - we also have to verify there is enough space to build a PADDING frame
3941 if needed, especially if there is no need to send an ACK frame.
3942 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3943 * a valid packet).
3944 */
3945static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3946 int ack, int nb_pto_dgrams,
3947 struct quic_enc_level *qel, struct quic_conn *conn)
3948{
3949 size_t minlen, token_fields_len;
3950 /* XXX FIXME XXX : ack delay not supported */
3951 uint64_t ack_delay = 0;
3952 size_t ack_frm_len = 0;
3953
3954 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3955 conn->conn, NULL, NULL, &room);
3956 /* When we do not have to probe nor send acks either, we must take into
3957 * an account the data which have already been prepared and limit
3958 * the size of this packet. We will potentially build an ack-eliciting
3959 * packet.
3960 */
3961 if (!nb_pto_dgrams && !ack) {
3962 size_t path_room;
3963
3964 path_room = quic_path_prep_data(conn->path);
3965 if (room > path_room)
3966 room = path_room;
3967 }
3968
3969 if (ack)
3970 /* A frame is made of 1 byte for the frame type. */
3971 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3972
3973 /* XXX FIXME XXX : token not supported */
3974 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3975 /* Check there is enough room to build the header followed by a token,
3976 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3977 * encryption tag is also taken into an account. Note that we have no
3978 * knowledge of the packet number for this packet. It must be atomically
3979 * incremented each time a packet is built. But before building a packet
3980 * we must estimate if it may be built if we do not want to consume a packet
3981 * number for nothing! Note that we add 1 byte more to
3982 * <minlen> to be able to build an ack-eliciting packet when probing without
3983 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3984 * PING frame.
3985 */
3986 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3987 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3988 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3989 + token_fields_len;
3990 else
3991 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3992
3993 /* Consider any PADDING frame to add */
3994 if (objt_server(conn->conn->target) &&
3995 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3996 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3997 /* Pad too short client Initial packet */
3998 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3999 }
4000 else if (!ack) {
4001 /* Consider we will have to add the longest short PADDING frame to
4002 * protect a 1-byte length packet number.
4003 */
4004 minlen += QUIC_PACKET_PN_MAXLEN - 1;
4005 }
4006
4007 if (room < minlen) {
4008 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
4009 conn->conn, NULL, NULL, &room);
4010 return 0;
4011 }
4012
4013 return 1;
4014}
4015
4016/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004017 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
4018 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
4019 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004020 * 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 +02004021 * 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 +02004022 * having returned from this function.
4023 * 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 +01004024 * number field in this packet. <pn_len> will also have the packet number
4025 * length as value.
4026 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004027 * Always succeeds: this is the responsability of the caller to ensure there is
4028 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004029 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004030static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004031 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004032 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
4033 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004034{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004035 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004036 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004037 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4038 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004039 size_t ack_frm_len;
4040 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004041 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004042
Frédéric Lécailleea604992020-12-24 13:01:37 +01004043 /* Length field value with CRYPTO frames if present. */
4044 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004045 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004046 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004047 * the congestion controller window. So, we do not limit the size of this
4048 * packet if we have an ACK frame to send because an ACK frame is not
4049 * ack-eliciting. This size will be limited if we have ack-eliciting
4050 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004051 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004052 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004053 size_t path_room;
4054
4055 path_room = quic_path_prep_data(conn->path);
4056 if (end - beg > path_room)
4057 end = beg + path_room;
4058 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004059
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004060 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004061 /* packet number length */
4062 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004063 /* Build the header */
4064 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004065 quic_build_packet_short_header(&pos, end, *pn_len, conn);
4066 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004067 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
4068 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4069 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004070 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004071 /* Ensure there is enough room for the TLS encryption tag */
4072 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004073 /* Build an ACK frame if required. */
4074 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004075 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004076 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004077 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004078 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4079 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4080 * that from here, we do not know if we will have to send a PING frame.
4081 * This will be decided after having computed the ack-eliciting frames
4082 * to be added to this packet.
4083 */
4084 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 +01004085 if (!ack_frm_len) {
4086 ssize_t room = end - pos;
4087 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4088 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004089 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004090 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004091 }
4092
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004093 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004094 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004095 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004096 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004097
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004098 /* Initialize the length of the frames built below to <len>.
4099 * If any frame could be successfully built by qc_build_frms(),
4100 * we will have len_frms > len.
4101 */
4102 len_frms = len;
4103 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004104 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4105 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004106 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004107
4108 add_ping_frm = 0;
4109 padding_len = 0;
4110 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004111 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004112 len < QUIC_INITIAL_PACKET_MINLEN) {
4113 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4114 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004115 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004116 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004117 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004118 add_ping_frm = 1;
4119 len += 1;
4120 }
4121 /* If there is no frame at all to follow, add at least a PADDING frame. */
4122 if (!ack_frm_len)
4123 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4124 }
4125
4126 /* Length (of the remaining data). Must not fail because, the buffer size
4127 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004128 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004129 * of this packet.
4130 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004131 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004132 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004133 else
4134 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004135 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004136 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004137
4138 /* Packet number field address. */
4139 *buf_pn = pos;
4140
4141 /* Packet number encoding. */
4142 quic_packet_number_encode(&pos, end, pn, *pn_len);
4143
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004144 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4145 ssize_t room = end - pos;
4146 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4147 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004148 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004149 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004150
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004151 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004152 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004153 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004154
4155 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004156 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004157 ssize_t room = end - pos;
4158 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4159 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004160 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004161 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004162 }
4163 }
4164
4165 /* Build a PING frame if needed. */
4166 if (add_ping_frm) {
4167 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004168 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4169 ssize_t room = end - pos;
4170 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4171 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004172 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004173 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004174 }
4175
4176 /* Build a PADDING frame if needed. */
4177 if (padding_len) {
4178 frm.type = QUIC_FT_PADDING;
4179 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004180 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4181 ssize_t room = end - pos;
4182 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4183 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004184 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004185 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004186 }
4187
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004188 /* Always reset this variable as this function has no idea
4189 * if it was set. It is handle by the loss detection timer.
4190 */
4191 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004192 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004193}
4194
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004195static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004197 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004198 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004199 pkt->cdata_len = 0;
4200 pkt->in_flight_len = 0;
4201 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004202 pkt->next = NULL;
4203 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004204}
4205
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004206/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004207static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4208{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004209 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004210
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004211 if (!pkt)
4212 return;
4213
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004214 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004215 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004216 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004217 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004218 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004219}
4220
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004221/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4222 * type for <qc> QUIC connection from <qel> encryption level.
4223 * Return -2 if the packet could not be allocated or encrypted for any reason,
4224 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004225 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004226static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4227 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004228 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004229 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004230 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004231{
4232 /* The pointer to the packet number field. */
4233 unsigned char *buf_pn;
4234 unsigned char *beg, *end, *payload;
4235 int64_t pn;
4236 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004237 struct quic_tls_ctx *tls_ctx;
4238 struct quic_tx_packet *pkt;
4239
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004240 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004241 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004242 pkt = pool_alloc(pool_head_quic_tx_packet);
4243 if (!pkt) {
4244 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 +02004245 *err = -2;
4246 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004247 }
4248
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004249 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004250 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004251 pn_len = 0;
4252 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004253 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004254 *err = -1;
4255 goto err;
4256 }
4257
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004258 /* Consume a packet number. */
4259 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004260 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 +01004261
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004262 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004263 payload = buf_pn + pn_len;
4264 payload_len = end - payload;
4265 aad_len = payload - beg;
4266
4267 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004268 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4269 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004270 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004271 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004272
4273 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004274 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004275 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4276 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4277 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004278 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004279 goto err;
4280 }
4281
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004282 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4283 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004284 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004285 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004286 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004287 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004288 pkt->in_flight_len = pkt->len;
4289 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004290 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004291 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004292 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4293
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004294 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004295
4296 err:
4297 free_quic_tx_packet(pkt);
4298 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004299 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004300}
4301
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004302/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4303 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004304 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004305static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4306 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004307{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004308 size_t try, done = 0;
4309
4310 if (!conn_ctrl_ready(conn))
4311 return 0;
4312
4313 if (!fd_recv_ready(conn->handle.fd))
4314 return 0;
4315
4316 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317
4318 /* read the largest possible block. For this, we perform only one call
4319 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004320 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004321 */
4322 while (count > 0) {
4323 try = b_contig_space(buf);
4324 if (!try)
4325 break;
4326
4327 if (try > count)
4328 try = count;
4329
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004330 b_add(buf, try);
4331 done += try;
4332 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004333 }
4334
4335 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4336 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4337
4338 leave:
4339 return done;
4340
4341 read0:
4342 conn_sock_read0(conn);
4343 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4344
4345 /* Now a final check for a possible asynchronous low-level error
4346 * report. This can happen when a connection receives a reset
4347 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4348 * we might have come from there by just checking POLL_HUP instead
4349 * of recv()'s return value 0, so we have no way to tell there was
4350 * an error without checking.
4351 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004352 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004353 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4354 goto leave;
4355}
4356
4357
4358/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4359 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4360 * other pending data for example, but this flag is ignored at the moment.
4361 * Only one call to send() is performed, unless the buffer wraps, in which case
4362 * a second call may be performed. The connection's flags are updated with
4363 * whatever special event is detected (error, empty). The caller is responsible
4364 * for taking care of those events and avoiding the call if inappropriate. The
4365 * function does not call the connection's polling update function, so the caller
4366 * is responsible for this. It's up to the caller to update the buffer's contents
4367 * based on the return value.
4368 */
4369static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4370{
4371 ssize_t ret;
4372 size_t try, done;
4373 int send_flag;
4374
4375 if (!conn_ctrl_ready(conn))
4376 return 0;
4377
4378 if (!fd_send_ready(conn->handle.fd))
4379 return 0;
4380
4381 done = 0;
4382 /* send the largest possible block. For this we perform only one call
4383 * to send() unless the buffer wraps and we exactly fill the first hunk,
4384 * in which case we accept to do it once again.
4385 */
4386 while (count) {
4387 try = b_contig_data(buf, done);
4388 if (try > count)
4389 try = count;
4390
4391 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4392 if (try < count || flags & CO_SFL_MSG_MORE)
4393 send_flag |= MSG_MORE;
4394
4395 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4396 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4397 if (ret > 0) {
4398 count -= ret;
4399 done += ret;
4400
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004401 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004402 conn->flags |= CO_FL_WAIT_L4L6;
4403 /* if the system buffer is full, don't insist */
4404 if (ret < try)
4405 break;
4406 }
4407 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4408 /* nothing written, we need to poll for write first */
4409 fd_cant_send(conn->handle.fd);
4410 break;
4411 }
4412 else if (errno != EINTR) {
4413 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4414 break;
4415 }
4416 }
4417 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4418 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4419
4420 if (done > 0) {
4421 /* we count the total bytes sent, and the send rate for 32-byte
4422 * blocks. The reason for the latter is that freq_ctr are
4423 * limited to 4GB and that it's not enough per second.
4424 */
4425 _HA_ATOMIC_ADD(&global.out_bytes, done);
4426 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4427 }
4428 return done;
4429}
4430
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004431/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4432 * event subscriber <es> is not allowed to change from a previous call as long
4433 * as at least one event is still subscribed. The <event_type> must only be a
4434 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4435 */
4436static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4437{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004438 struct qcc *qcc = conn->qc->qcc;
4439
4440 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4441 BUG_ON(qcc->subs && qcc->subs != es);
4442
4443 es->events |= event_type;
4444 qcc->subs = es;
4445
4446 if (event_type & SUB_RETRY_RECV)
4447 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc);
4448
4449 if (event_type & SUB_RETRY_SEND)
4450 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc);
4451
4452 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004453}
4454
4455/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4456 * The <es> pointer is not allowed to differ from the one passed to the
4457 * subscribe() call. It always returns zero.
4458 */
4459static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4460{
4461 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4462}
4463
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004464/* Try to allocate the <*ssl> SSL session object for <qc> QUIC connection
4465 * with <ssl_ctx> as SSL context inherited settings. Also set the transport
4466 * parameters of this session.
4467 * This is the responsibility of the caller to check the validity of all the
4468 * pointers passed as parameter to this function.
4469 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <qc->conn> to
4470 * CO_ER_SSL_NO_MEM.
4471 */
4472static int qc_ssl_sess_init(struct quic_conn *qc, SSL_CTX *ssl_ctx, SSL **ssl,
4473 unsigned char *params, size_t params_len)
4474{
4475 int retry;
4476
4477 retry = 1;
4478 retry:
4479 *ssl = SSL_new(ssl_ctx);
4480 if (!*ssl) {
4481 if (!retry--)
4482 goto err;
4483
4484 pool_gc(NULL);
4485 goto retry;
4486 }
4487
4488 if (!SSL_set_quic_method(*ssl, &ha_quic_method) ||
4489 !SSL_set_ex_data(*ssl, ssl_app_data_index, qc->conn) ||
4490 !SSL_set_quic_transport_params(*ssl, qc->enc_params, qc->enc_params_len)) {
4491 goto err;
4492
4493 SSL_free(*ssl);
4494 *ssl = NULL;
4495 if (!retry--)
4496 goto err;
4497
4498 pool_gc(NULL);
4499 goto retry;
4500 }
4501
4502 return 0;
4503
4504 err:
4505 qc->conn->err_code = CO_ER_SSL_NO_MEM;
4506 return -1;
4507}
4508
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004509/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4510 * connection with <xprt_ctx> as address of the xprt context.
4511 * Returns 1 if succeeded, 0 if not.
4512 */
4513static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4514{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004515 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004516
4517 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4518
4519 if (*xprt_ctx)
4520 goto out;
4521
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004522 ctx = pool_alloc(pool_head_quic_conn_ctx);
4523 if (!ctx) {
4524 conn->err_code = CO_ER_SYS_MEMLIM;
4525 goto err;
4526 }
4527
4528 ctx->wait_event.tasklet = tasklet_new();
4529 if (!ctx->wait_event.tasklet) {
4530 conn->err_code = CO_ER_SYS_MEMLIM;
4531 goto err;
4532 }
4533
4534 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4535 ctx->wait_event.tasklet->context = ctx;
4536 ctx->wait_event.events = 0;
4537 ctx->conn = conn;
4538 ctx->subs = NULL;
4539 ctx->xprt_ctx = NULL;
4540
4541 ctx->xprt = xprt_get(XPRT_QUIC);
4542 if (objt_server(conn->target)) {
4543 /* Server */
4544 struct server *srv = __objt_server(conn->target);
4545 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004546 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004547 int ssl_err, ipv4;
4548
4549 ssl_err = SSL_ERROR_NONE;
4550 if (RAND_bytes(dcid, sizeof dcid) != 1)
4551 goto err;
4552
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004553 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004554 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004555 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004556 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004557 goto err;
4558
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004559 /* Insert our SCID, the connection ID for the QUIC client. */
4560 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4561
4562 conn->qc = qc;
4563 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004564 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4565 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004566 goto err;
4567
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004568 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004569 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004570 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4571 qc->enc_params_len =
4572 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4573 &qc->rx.params, 0);
4574 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004575 goto err;
4576
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004577 if (qc_ssl_sess_init(qc, srv->ssl_ctx.ctx, &ctx->ssl,
4578 qc->enc_params, qc->enc_params_len) == -1)
4579 goto err;
4580
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004581 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004582 SSL_set_connect_state(ctx->ssl);
4583 ssl_err = SSL_do_handshake(ctx->ssl);
4584 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004585 int st;
4586
4587 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004588 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4589 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004590 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004591 }
4592 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004593 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004594 goto err;
4595 }
4596 }
4597 }
4598 else if (objt_listener(conn->target)) {
4599 /* Listener */
4600 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004601 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004602
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004603 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaille75dd2b72021-09-28 09:51:23 +02004604 if (qc_ssl_sess_init(qc, bc->initial_ctx, &ctx->ssl,
4605 qc->enc_params, qc->enc_params_len) == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004606 goto err;
4607
4608 SSL_set_accept_state(ctx->ssl);
4609 }
4610
4611 *xprt_ctx = ctx;
4612
4613 /* Leave init state and start handshake */
4614 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004615
4616 out:
4617 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4618
4619 return 0;
4620
4621 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004622 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004623 tasklet_free(ctx->wait_event.tasklet);
4624 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004625 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004626 return -1;
4627}
4628
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004629/* Start the QUIC transport layer */
4630static int qc_xprt_start(struct connection *conn, void *ctx)
4631{
4632 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004633 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004634
4635 qc = conn->qc;
4636 if (!quic_conn_init_timer(qc)) {
4637 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4638 return 0;
4639 }
4640
4641 tasklet_wakeup(qctx->wait_event.tasklet);
4642 return 1;
4643}
4644
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004645/* transport-layer operations for QUIC connections. */
4646static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004647 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004648 .snd_buf = quic_conn_from_buf,
4649 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004650 .subscribe = quic_conn_subscribe,
4651 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004652 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004653 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004654 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4655 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4656 .name = "QUIC",
4657};
4658
4659__attribute__((constructor))
4660static void __quic_conn_init(void)
4661{
4662 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4663 xprt_register(XPRT_QUIC, &ssl_quic);
4664}
4665
4666__attribute__((destructor))
4667static void __quic_conn_deinit(void)
4668{
4669 BIO_meth_free(ha_quic_meth);
4670}
4671
4672/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4673 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4674 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004675 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004676 */
4677static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4678 struct sockaddr_storage *saddr, qpkt_read_func *func)
4679{
4680 unsigned char *pos;
4681 const unsigned char *end;
4682 struct quic_dgram_ctx dgram_ctx = {
4683 .dcid_node = NULL,
4684 .owner = owner,
4685 };
4686
4687 pos = (unsigned char *)buf;
4688 end = pos + len;
4689
4690 do {
4691 int ret;
4692 struct quic_rx_packet *pkt;
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004693 size_t pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004694
Willy Tarreaue4498932021-03-22 21:13:05 +01004695 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004696 if (!pkt)
4697 goto err;
4698
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004699 quic_rx_packet_refinc(pkt);
4700 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004701 pkt_len = pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02004702 quic_rx_packet_refdec(pkt);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004703 if (ret == -1 && !pkt_len)
4704 /* If the packet length could not be found, we cannot continue. */
4705 break;
4706
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004707 } while (pos < end);
4708
4709 /* Increasing the received bytes counter by the UDP datagram length
4710 * if this datagram could be associated to a connection.
4711 */
4712 if (dgram_ctx.qc)
4713 dgram_ctx.qc->rx.bytes += len;
4714
4715 return pos - (unsigned char *)buf;
4716
4717 err:
4718 return -1;
4719}
4720
4721ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4722 struct sockaddr_storage *saddr)
4723{
4724 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4725}
4726
4727ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4728 struct sockaddr_storage *saddr)
4729{
4730 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4731}
4732
4733/* QUIC I/O handler for connection to local listeners or remove servers
4734 * depending on <listener> boolean value, with <fd> as socket file
4735 * descriptor and <ctx> as context.
4736 */
4737static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4738{
4739 ssize_t ret;
4740 size_t done = 0;
4741 struct buffer *buf = get_trash_chunk();
4742 /* Source address */
4743 struct sockaddr_storage saddr = {0};
4744 socklen_t saddrlen = sizeof saddr;
4745
4746 if (!fd_recv_ready(fd))
4747 return 0;
4748
4749 do {
4750 ret = recvfrom(fd, buf->area, buf->size, 0,
4751 (struct sockaddr *)&saddr, &saddrlen);
4752 if (ret < 0) {
4753 if (errno == EINTR)
4754 continue;
4755 if (errno == EAGAIN)
4756 fd_cant_recv(fd);
4757 goto out;
4758 }
4759 } while (0);
4760
4761 done = buf->data = ret;
4762 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4763
4764 out:
4765 return done;
4766}
4767
4768/* QUIC I/O handler for connections to local listeners with <fd> as socket
4769 * file descriptor.
4770 */
4771void quic_fd_handler(int fd)
4772{
Willy Tarreauf5090652021-04-06 17:23:40 +02004773 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004774 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4775}
4776
4777/* QUIC I/O handler for connections to remote servers with <fd> as socket
4778 * file descriptor.
4779 */
4780void quic_conn_fd_handler(int fd)
4781{
Willy Tarreauf5090652021-04-06 17:23:40 +02004782 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004783 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4784}
4785
4786/*
4787 * Local variables:
4788 * c-indent-level: 8
4789 * c-basic-offset: 8
4790 * End:
4791 */