blob: 80591e116642743074756828619b8acd627d74e3 [file] [log] [blame]
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001/*
2 * QUIC transport layer over SOCK_DGRAM sockets.
3 *
4 * Copyright 2020 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <haproxy/buf-t.h>
26#include <haproxy/compat.h>
27#include <haproxy/api.h>
28#include <haproxy/debug.h>
29#include <haproxy/tools.h>
30#include <haproxy/ticks.h>
31#include <haproxy/time.h>
32
33#include <haproxy/connection.h>
34#include <haproxy/fd.h>
35#include <haproxy/freq_ctr.h>
36#include <haproxy/global.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010037#include <haproxy/h3.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010038#include <haproxy/log.h>
Frédéric Lécailledfbae762021-02-18 09:59:01 +010039#include <haproxy/mux_quic.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010040#include <haproxy/pipe.h>
41#include <haproxy/proxy.h>
42#include <haproxy/quic_cc.h>
43#include <haproxy/quic_frame.h>
44#include <haproxy/quic_loss.h>
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +020045#include <haproxy/cbuf.h>
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010046#include <haproxy/quic_tls.h>
47#include <haproxy/ssl_sock.h>
48#include <haproxy/stream_interface.h>
49#include <haproxy/task.h>
50#include <haproxy/trace.h>
51#include <haproxy/xprt_quic.h>
52
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020053/* This is the values of some QUIC transport parameters when absent.
54 * Should be used to initialize any transport parameters (local or remote)
55 * before updating them with customized values.
56 */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010057struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020058 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
59 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
60 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaille48fc74a2021-09-03 16:42:19 +020061 .active_connection_id_limit = QUIC_ACTIVE_CONNECTION_ID_LIMIT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010062};
63
64/* trace source and events */
65static void quic_trace(enum trace_level level, uint64_t mask, \
66 const struct trace_source *src,
67 const struct ist where, const struct ist func,
68 const void *a1, const void *a2, const void *a3, const void *a4);
69
70static const struct trace_event quic_trace_events[] = {
71 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
72 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
73 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
74 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
75 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
76 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
77 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050078 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010079 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
80 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
81 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
82 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
83 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
84 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
85 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
86 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
87 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
88 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
89 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
90 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
91 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
92 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
93 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
94 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
95 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
96 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
97 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010098 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
99 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
100 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
101 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
102 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
103 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100104 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaille513b4f22021-09-20 15:23:17 +0200105 { .mask = QUIC_EV_CONN_XPRTSEND, .name = "xprt_send", .desc = "sending XRPT subscription" },
106 { .mask = QUIC_EV_CONN_XPRTRECV, .name = "xprt_recv", .desc = "receiving XRPT subscription" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100107 { /* end */ }
108};
109
110static const struct name_desc quic_trace_lockon_args[4] = {
111 /* arg1 */ { /* already used by the connection */ },
112 /* arg2 */ { .name="quic", .desc="QUIC transport" },
113 /* arg3 */ { },
114 /* arg4 */ { }
115};
116
117static const struct name_desc quic_trace_decoding[] = {
118#define QUIC_VERB_CLEAN 1
119 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
120 { /* end */ }
121};
122
123
124struct trace_source trace_quic = {
125 .name = IST("quic"),
126 .desc = "QUIC xprt",
127 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
128 .default_cb = quic_trace,
129 .known_events = quic_trace_events,
130 .lockon_args = quic_trace_lockon_args,
131 .decoding = quic_trace_decoding,
132 .report_events = ~0, /* report everything by default */
133};
134
135#define TRACE_SOURCE &trace_quic
136INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
137
138static BIO_METHOD *ha_quic_meth;
139
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200140DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100141DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200142 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100143DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100144DECLARE_POOL(pool_head_quic_connection_id,
145 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100146DECLARE_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 +0100147DECLARE_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 +0100148DECLARE_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 +0100149DECLARE_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 +0100150DECLARE_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 +0200151DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100152DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100153
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200154static 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 +0200155 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200156 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200157 int ack, int nb_pto_dgrams, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100158
159/* Add traces to <buf> depending on <frm> TX frame type. */
160static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200161 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100162{
163 switch (frm->type) {
164 case QUIC_FT_CRYPTO:
165 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
166 (unsigned long long)frm->crypto.offset,
167 (unsigned long long)frm->crypto.len);
168 break;
169 default:
170 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
171 }
172}
173
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100174/* Only for debug purpose */
175struct enc_debug_info {
176 unsigned char *payload;
177 size_t payload_len;
178 unsigned char *aad;
179 size_t aad_len;
180 uint64_t pn;
181};
182
183/* Initializes a enc_debug_info struct (only for debug purpose) */
184static inline void enc_debug_info_init(struct enc_debug_info *edi,
185 unsigned char *payload, size_t payload_len,
186 unsigned char *aad, size_t aad_len, uint64_t pn)
187{
188 edi->payload = payload;
189 edi->payload_len = payload_len;
190 edi->aad = aad;
191 edi->aad_len = aad_len;
192 edi->pn = pn;
193}
194
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100195/* Trace callback for QUIC.
196 * These traces always expect that arg1, if non-null, is of type connection.
197 */
198static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
199 const struct ist where, const struct ist func,
200 const void *a1, const void *a2, const void *a3, const void *a4)
201{
202 const struct connection *conn = a1;
203
204 if (conn) {
205 struct quic_tls_secrets *secs;
206 struct quic_conn *qc;
207
208 qc = conn->qc;
209 chunk_appendf(&trace_buf, " : conn@%p", conn);
210 if ((mask & QUIC_EV_CONN_INIT) && qc) {
211 chunk_appendf(&trace_buf, "\n odcid");
212 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100213 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100214 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100215 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100216 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100217 }
218
219 if (mask & QUIC_EV_CONN_ADDDATA) {
220 const enum ssl_encryption_level_t *level = a2;
221 const size_t *len = a3;
222
223 if (level) {
224 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
225
226 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
227 }
228 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100229 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100230 }
231 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
232 /* Initial read & write secrets. */
233 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
234 const unsigned char *rx_sec = a2;
235 const unsigned char *tx_sec = a3;
236
237 secs = &qc->els[level].tls_ctx.rx;
238 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
239 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
240 if (rx_sec)
241 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
242 quic_tls_keys_hexdump(&trace_buf, secs);
243 }
244 secs = &qc->els[level].tls_ctx.tx;
245 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
246 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
247 if (tx_sec)
248 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
249 quic_tls_keys_hexdump(&trace_buf, secs);
250 }
251 }
252 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
253 const enum ssl_encryption_level_t *level = a2;
254 const unsigned char *secret = a3;
255 const size_t *secret_len = a4;
256
257 if (level) {
258 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
259
260 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
261 if (secret && secret_len)
262 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
263 secs = &qc->els[lvl].tls_ctx.rx;
264 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
265 quic_tls_keys_hexdump(&trace_buf, secs);
266 }
267 }
268
269 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
270 const enum ssl_encryption_level_t *level = a2;
271 const unsigned char *secret = a3;
272 const size_t *secret_len = a4;
273
274 if (level) {
275 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
276
277 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
278 if (secret && secret_len)
279 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
280 secs = &qc->els[lvl].tls_ctx.tx;
281 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
282 quic_tls_keys_hexdump(&trace_buf, secs);
283 }
284
285 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100286
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100287 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100288 const struct quic_tx_packet *pkt = a2;
289 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100290 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100291
292 if (qel) {
293 struct quic_pktns *pktns;
294
295 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100296 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
297 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100298 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100299 (unsigned long long)qc->path->cwnd,
300 (unsigned long long)qc->path->prep_in_flight,
301 (unsigned long long)qc->path->in_flight,
302 (unsigned long long)pktns->tx.in_flight,
303 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100304 }
305 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200306 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100307 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
308 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
309 list_for_each_entry(frm, &pkt->frms, list)
310 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100311 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
312 }
313
314 if (room) {
315 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
316 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
317 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100318 }
319 }
320
321 if (mask & QUIC_EV_CONN_HDSHK) {
322 const enum quic_handshake_state *state = a2;
323 const int *err = a3;
324
325 if (state)
326 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
327 if (err)
328 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
329 }
330
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100331 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100332 const struct quic_rx_packet *pkt = a2;
333 const unsigned long *pktlen = a3;
334 const SSL *ssl = a4;
335
336 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100337 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100338 pkt, quic_packet_type_enc_level_char(pkt->type));
339 if (pkt->pnl)
340 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
341 (unsigned long long)pkt->pn);
342 if (pkt->token_len)
343 chunk_appendf(&trace_buf, " toklen=%llu",
344 (unsigned long long)pkt->token_len);
345 if (pkt->aad_len)
346 chunk_appendf(&trace_buf, " aadlen=%llu",
347 (unsigned long long)pkt->aad_len);
348 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
349 pkt->flags, (unsigned long long)pkt->len);
350 }
351 if (pktlen)
352 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
353 if (ssl) {
354 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
355 chunk_appendf(&trace_buf, " el=%c",
356 quic_enc_level_char(ssl_to_quic_enc_level(level)));
357 }
358 }
359
360 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
361 const struct quic_rx_packet *pkt = a2;
362 const struct quic_rx_crypto_frm *cf = a3;
363 const SSL *ssl = a4;
364
365 if (pkt)
366 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
367 quic_packet_type_enc_level_char(pkt->type),
368 (unsigned long long)pkt->pn);
369 if (cf)
370 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
371 (unsigned long long)cf->offset_node.key,
372 (unsigned long long)cf->len);
373 if (ssl) {
374 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
375 chunk_appendf(&trace_buf, " el=%c",
376 quic_enc_level_char(ssl_to_quic_enc_level(level)));
377 }
378 }
379
380 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
381 const struct quic_frame *frm = a2;
382
383 if (frm)
384 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
385 }
386
387 if (mask & QUIC_EV_CONN_PHPKTS) {
388 const struct quic_enc_level *qel = a2;
389
390 if (qel) {
391 struct quic_pktns *pktns;
392
393 pktns = qc->pktns;
394 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200395 " 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 +0100396 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200397 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaille2766e782021-08-30 17:16:07 +0200398 !!(HA_ATOMIC_LOAD(&qc->flags) & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100399 (unsigned long long)qc->path->cwnd,
400 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100401 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100402 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
403 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100404 }
405 }
406
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100407 if (mask & QUIC_EV_CONN_ENCPKT) {
408 const struct enc_debug_info *edi = a2;
409
410 if (edi)
411 chunk_appendf(&trace_buf,
412 " payload=@%p payload_len=%llu"
413 " aad=@%p aad_len=%llu pn=%llu",
414 edi->payload, (unsigned long long)edi->payload_len,
415 edi->aad, (unsigned long long)edi->aad_len,
416 (unsigned long long)edi->pn);
417 }
418
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100419 if (mask & QUIC_EV_CONN_RMHP) {
420 const struct quic_rx_packet *pkt = a2;
421
422 if (pkt) {
423 const int *ret = a3;
424
425 chunk_appendf(&trace_buf, " pkt@%p", pkt);
426 if (ret && *ret)
427 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
428 pkt->pnl, (unsigned long long)pkt->pn);
429 }
430 }
431
432 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200433 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100434 const unsigned long *val1 = a3;
435 const unsigned long *val2 = a4;
436
437 if (frm)
438 chunk_tx_frm_appendf(&trace_buf, frm);
439 if (val1)
440 chunk_appendf(&trace_buf, " %lu", *val1);
441 if (val2)
442 chunk_appendf(&trace_buf, "..%lu", *val2);
443 }
444
445 if (mask & QUIC_EV_CONN_RTTUPDT) {
446 const unsigned int *rtt_sample = a2;
447 const unsigned int *ack_delay = a3;
448 const struct quic_loss *ql = a4;
449
450 if (rtt_sample)
451 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
452 if (ack_delay)
453 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
454 if (ql)
455 chunk_appendf(&trace_buf,
456 " srtt=%ums rttvar=%ums min_rtt=%ums",
457 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
458 }
459 if (mask & QUIC_EV_CONN_CC) {
460 const struct quic_cc_event *ev = a2;
461 const struct quic_cc *cc = a3;
462
463 if (a2)
464 quic_cc_event_trace(&trace_buf, ev);
465 if (a3)
466 quic_cc_state_trace(&trace_buf, cc);
467 }
468
469 if (mask & QUIC_EV_CONN_PKTLOSS) {
470 const struct quic_pktns *pktns = a2;
471 const struct list *lost_pkts = a3;
472 struct quic_conn *qc = conn->qc;
473
474 if (pktns) {
475 chunk_appendf(&trace_buf, " pktns=%s",
476 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
477 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
478 if (pktns->tx.loss_time)
479 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100480 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100481 }
482 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
483 struct quic_tx_packet *pkt;
484
485 chunk_appendf(&trace_buf, " lost_pkts:");
486 list_for_each_entry(pkt, lost_pkts, list)
487 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
488 }
489 }
490
491 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
492 struct quic_conn *qc = conn->qc;
493 const struct quic_pktns *pktns = a2;
494 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100495 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100496
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100497 if (ifae_pkts)
498 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
499 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100500 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100501 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100502 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100503 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
504 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100505 if (mask & QUIC_EV_CONN_STIMER) {
506 if (pktns->tx.loss_time)
507 chunk_appendf(&trace_buf, " loss_time=%dms",
508 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
509 }
510 if (mask & QUIC_EV_CONN_SPTO) {
511 if (pktns->tx.time_of_last_eliciting)
512 chunk_appendf(&trace_buf, " tole=%dms",
513 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
514 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100515 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100516 }
517 }
518
519 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
520 chunk_appendf(&trace_buf,
521 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
522 }
523 }
524
525 if (mask & QUIC_EV_CONN_SPPKTS) {
526 const struct quic_tx_packet *pkt = a2;
527
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100528 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
529 (unsigned long long)qc->path->cwnd,
530 (unsigned long long)qc->path->prep_in_flight,
531 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100533 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100534 (unsigned long)pkt->pn_node.key,
535 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
536 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100537 (unsigned long long)pkt->in_flight_len,
538 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100539 }
540 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100541
542 if (mask & QUIC_EV_CONN_SSLALERT) {
543 const uint8_t *alert = a2;
544 const enum ssl_encryption_level_t *level = a3;
545
546 if (alert)
547 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
548 if (level)
549 chunk_appendf(&trace_buf, " el=%c",
550 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
551 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100552
553 if (mask & QUIC_EV_CONN_BCFRMS) {
554 const size_t *sz1 = a2;
555 const size_t *sz2 = a3;
556 const size_t *sz3 = a4;
557
558 if (sz1)
559 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
560 if (sz2)
561 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
562 if (sz3)
563 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
564 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100565
566 if (mask & QUIC_EV_CONN_PSTRM) {
567 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100568
569 if (a2) {
570 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100571
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100572 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
573 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
574 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
575 (unsigned long long)s->id,
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +0200576 (unsigned long long)s->offset.key,
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100577 (unsigned long long)s->len);
578 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100579 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100580 }
581 if (mask & QUIC_EV_CONN_LPKT) {
582 const struct quic_rx_packet *pkt = a2;
583
584 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200585 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100586 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200587 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
588 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100589 }
590
591}
592
593/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200594static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100595{
596 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200597 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598
599 qc = ctx->conn->qc;
600 if (objt_server(qc->conn->target))
601 return 1;
602
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200603 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
604 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
605 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
606 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200607 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100608 return 1;
609
610 return 0;
611}
612
613/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
614 * both loss detection and PTO and schedule the task assiated to this timer if needed.
615 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200616static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100617{
618 struct quic_conn *qc;
619 struct quic_pktns *pktns;
620 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200621 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100622
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100623 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
624 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100625 qc = ctx->conn->qc;
626 pktns = quic_loss_pktns(qc);
627 if (tick_isset(pktns->tx.loss_time)) {
628 qc->timer = pktns->tx.loss_time;
629 goto out;
630 }
631
632 /* XXX TODO: anti-amplification: the timer must be
633 * cancelled for a server which reached the anti-amplification limit.
634 */
635
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100636 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
637 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100638 /* Timer cancellation. */
639 qc->timer = TICK_ETERNITY;
640 goto out;
641 }
642
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200643 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
644 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100645 if (tick_isset(pto))
646 qc->timer = pto;
647 out:
648 task_schedule(qc->timer_task, qc->timer);
649 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
650}
651
652#ifndef OPENSSL_IS_BORINGSSL
653int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
654 const uint8_t *read_secret,
655 const uint8_t *write_secret, size_t secret_len)
656{
657 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
658 struct quic_tls_ctx *tls_ctx =
659 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
660 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
661
662 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
663 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
664 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
665 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
666
667 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
668 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
669 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
670 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
671 read_secret, secret_len)) {
672 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
673 return 0;
674 }
675
676 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
677 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
678 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
679 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
680 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
681 write_secret, secret_len)) {
682 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
683 return 0;
684 }
685
686 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
687 if (objt_server(conn->target) && level == ssl_encryption_application) {
688 const unsigned char *buf;
689 size_t buflen;
690
691 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
692 if (!buflen)
693 return 0;
694
695 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
696 return 0;
697 }
698 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
699
700 return 1;
701}
702#else
703/* ->set_read_secret callback to derive the RX secrets at <level> encryption
704 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500705 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100706 */
707int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
708 const SSL_CIPHER *cipher,
709 const uint8_t *secret, size_t secret_len)
710{
711 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
712 struct quic_tls_ctx *tls_ctx =
713 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
714
715 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
716 tls_ctx->rx.aead = tls_aead(cipher);
717 tls_ctx->rx.md = tls_md(cipher);
718 tls_ctx->rx.hp = tls_hp(cipher);
719
720 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
721 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
722 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
723 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
724 secret, secret_len)) {
725 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
726 goto err;
727 }
728
729 if (objt_server(conn->target) && level == ssl_encryption_application) {
730 const unsigned char *buf;
731 size_t buflen;
732
733 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
734 if (!buflen)
735 goto err;
736
737 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
738 goto err;
739 }
740
741 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
742 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
743
744 return 1;
745
746 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100747 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100748 return 0;
749}
750
751/* ->set_write_secret callback to derive the TX secrets at <level>
752 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500753 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100754 */
755int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
756 const SSL_CIPHER *cipher,
757 const uint8_t *secret, size_t secret_len)
758{
759 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
760 struct quic_tls_ctx *tls_ctx =
761 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
762
763 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
764 tls_ctx->tx.aead = tls_aead(cipher);
765 tls_ctx->tx.md = tls_md(cipher);
766 tls_ctx->tx.hp = tls_hp(cipher);
767
768 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
769 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
770 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
771 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
772 secret, secret_len)) {
773 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
774 goto err;
775 }
776
777 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
778 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
779
780 return 1;
781
782 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100783 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100784 return 0;
785}
786#endif
787
788/* This function copies the CRYPTO data provided by the TLS stack found at <data>
789 * with <len> as size in CRYPTO buffers dedicated to store the information about
790 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
791 * It fails only if it could not managed to allocate enough CRYPTO buffers to
792 * store all the data.
793 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
794 */
795static int quic_crypto_data_cpy(struct quic_enc_level *qel,
796 const unsigned char *data, size_t len)
797{
798 struct quic_crypto_buf **qcb;
799 /* The remaining byte to store in CRYPTO buffers. */
800 size_t cf_offset, cf_len, *nb_buf;
801 unsigned char *pos;
802
803 nb_buf = &qel->tx.crypto.nb_buf;
804 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
805 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
806 cf_len = len;
807
808 while (len) {
809 size_t to_copy, room;
810
811 pos = (*qcb)->data + (*qcb)->sz;
812 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
813 to_copy = len > room ? room : len;
814 if (to_copy) {
815 memcpy(pos, data, to_copy);
816 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
817 qel->tx.crypto.sz += to_copy;
818 (*qcb)->sz += to_copy;
819 pos += to_copy;
820 len -= to_copy;
821 data += to_copy;
822 }
823 else {
824 struct quic_crypto_buf **tmp;
825
826 tmp = realloc(qel->tx.crypto.bufs,
827 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
828 if (tmp) {
829 qel->tx.crypto.bufs = tmp;
830 qcb = &qel->tx.crypto.bufs[*nb_buf];
831 *qcb = pool_alloc(pool_head_quic_crypto_buf);
832 if (!*qcb)
833 return 0;
834
835 (*qcb)->sz = 0;
836 ++*nb_buf;
837 }
838 else {
839 break;
840 }
841 }
842 }
843
844 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
845 * have been buffered.
846 */
847 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200848 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100849
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200850 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100851 if (!frm)
852 return 0;
853
854 frm->type = QUIC_FT_CRYPTO;
855 frm->crypto.offset = cf_offset;
856 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200857 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200858 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100859 }
860
861 return len == 0;
862}
863
864
865/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
866 * wants to provide the QUIC layer with CRYPTO data.
867 * Returns 1 if succeeded, 0 if not.
868 */
869int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
870 const uint8_t *data, size_t len)
871{
872 struct connection *conn;
873 enum quic_tls_enc_level tel;
874 struct quic_enc_level *qel;
875
876 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
877 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
878 tel = ssl_to_quic_enc_level(level);
879 qel = &conn->qc->els[tel];
880
881 if (tel == -1) {
882 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
883 goto err;
884 }
885
886 if (!quic_crypto_data_cpy(qel, data, len)) {
887 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
888 goto err;
889 }
890
891 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
892 conn, &level, &len);
893
894 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
895 return 1;
896
897 err:
898 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
899 return 0;
900}
901
902int ha_quic_flush_flight(SSL *ssl)
903{
904 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
905
906 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
907 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
908
909 return 1;
910}
911
912int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
913{
914 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
915
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100916 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100917 return 1;
918}
919
920/* QUIC TLS methods */
921static SSL_QUIC_METHOD ha_quic_method = {
922#ifdef OPENSSL_IS_BORINGSSL
923 .set_read_secret = ha_set_rsec,
924 .set_write_secret = ha_set_wsec,
925#else
926 .set_encryption_secrets = ha_quic_set_encryption_secrets,
927#endif
928 .add_handshake_data = ha_quic_add_handshake_data,
929 .flush_flight = ha_quic_flush_flight,
930 .send_alert = ha_quic_send_alert,
931};
932
933/* Initialize the TLS context of a listener with <bind_conf> as configuration.
934 * Returns an error count.
935 */
936int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
937{
938 struct proxy *curproxy = bind_conf->frontend;
939 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
940 int cfgerr = 0;
941
942#if 0
943 /* XXX Did not manage to use this. */
944 const char *ciphers =
945 "TLS_AES_128_GCM_SHA256:"
946 "TLS_AES_256_GCM_SHA384:"
947 "TLS_CHACHA20_POLY1305_SHA256:"
948 "TLS_AES_128_CCM_SHA256";
949#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200950 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100951 long options =
952 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
953 SSL_OP_SINGLE_ECDH_USE |
954 SSL_OP_CIPHER_SERVER_PREFERENCE;
955 SSL_CTX *ctx;
956
957 ctx = SSL_CTX_new(TLS_server_method());
958 bind_conf->initial_ctx = ctx;
959
960 SSL_CTX_set_options(ctx, options);
961#if 0
962 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
963 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
964 "for bind '%s' at [%s:%d].\n",
965 curproxy->id, ciphers,
966 bind_conf->arg, bind_conf->file, bind_conf->line);
967 cfgerr++;
968 }
969#endif
970
971 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
972 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
973 "for bind '%s' at [%s:%d].\n",
974 curproxy->id, groups,
975 bind_conf->arg, bind_conf->file, bind_conf->line);
976 cfgerr++;
977 }
978
979 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
980 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
981 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
982 SSL_CTX_set_default_verify_paths(ctx);
983
984#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
985#ifdef OPENSSL_IS_BORINGSSL
986 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
987 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
988#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
989 if (bind_conf->ssl_conf.early_data) {
990 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
991 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
992 }
993 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
994 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
995#else
996 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
997#endif
998 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
999#endif
1000 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1001
1002 return cfgerr;
1003}
1004
1005/* Decode an expected packet number from <truncated_on> its truncated value,
1006 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1007 * the number of bits used to encode this packet number (its length in bytes * 8).
1008 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1009 */
1010static uint64_t decode_packet_number(uint64_t largest_pn,
1011 uint32_t truncated_pn, unsigned int pn_nbits)
1012{
1013 uint64_t expected_pn = largest_pn + 1;
1014 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1015 uint64_t pn_hwin = pn_win / 2;
1016 uint64_t pn_mask = pn_win - 1;
1017 uint64_t candidate_pn;
1018
1019
1020 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1021 /* Note that <pn_win> > <pn_hwin>. */
1022 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1023 candidate_pn + pn_hwin <= expected_pn)
1024 return candidate_pn + pn_win;
1025
1026 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1027 return candidate_pn - pn_win;
1028
1029 return candidate_pn;
1030}
1031
1032/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1033 * cryptographic context.
1034 * <largest_pn> is the largest received packet number and <pn> the address of
1035 * the packet number field for this packet with <byte0> address of its first byte.
1036 * <end> points to one byte past the end of this packet.
1037 * Returns 1 if succeeded, 0 if not.
1038 */
1039static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1040 int64_t largest_pn, unsigned char *pn,
1041 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001042 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001043{
1044 int ret, outlen, i, pnlen;
1045 uint64_t packet_number;
1046 uint32_t truncated_pn = 0;
1047 unsigned char mask[5] = {0};
1048 unsigned char *sample;
1049 EVP_CIPHER_CTX *cctx;
1050 unsigned char *hp_key;
1051
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001052 /* Check there is enough data in this packet. */
1053 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1054 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1055 return 0;
1056 }
1057
1058 cctx = EVP_CIPHER_CTX_new();
1059 if (!cctx) {
1060 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1061 return 0;
1062 }
1063
1064 ret = 0;
1065 sample = pn + QUIC_PACKET_PN_MAXLEN;
1066
1067 hp_key = tls_ctx->rx.hp_key;
1068 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1069 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1070 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1071 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1072 goto out;
1073 }
1074
1075 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1076 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1077 for (i = 0; i < pnlen; i++) {
1078 pn[i] ^= mask[i + 1];
1079 truncated_pn = (truncated_pn << 8) | pn[i];
1080 }
1081
1082 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1083 /* Store remaining information for this unprotected header */
1084 pkt->pn = packet_number;
1085 pkt->pnl = pnlen;
1086
1087 ret = 1;
1088
1089 out:
1090 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001091
1092 return ret;
1093}
1094
1095/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1096 * address, with <payload_len> as payload length, <aad> as address of
1097 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1098 * context.
1099 * Returns 1 if succeeded, 0 if not.
1100 */
1101static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1102 unsigned char *aad, size_t aad_len, uint64_t pn,
1103 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1104{
1105 unsigned char iv[12];
1106 unsigned char *tx_iv = tls_ctx->tx.iv;
1107 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001108 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001109
1110 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1111 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001112 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001113 }
1114
1115 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1116 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1117 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001118 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001119 }
1120
1121 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001122
1123 err:
1124 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1125 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1126 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001127}
1128
1129/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1130 * Returns 1 if succeeded, 0 if not.
1131 */
1132static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1133{
1134 int ret;
1135 unsigned char iv[12];
1136 unsigned char *rx_iv = tls_ctx->rx.iv;
1137 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1138
1139 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1140 return 0;
1141
1142 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1143 pkt->data, pkt->aad_len,
1144 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1145 if (!ret)
1146 return 0;
1147
1148 /* Update the packet length (required to parse the frames). */
1149 pkt->len = pkt->aad_len + ret;
1150
1151 return 1;
1152}
1153
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001154/* Remove from <qcs> stream the acknowledged frames.
1155 * Never fails.
1156 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001157static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001158{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001159 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001160 struct eb64_node *frm_node;
1161
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001162 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001163 frm_node = eb64_first(&qcs->tx.acked_frms);
1164 while (frm_node) {
1165 struct quic_stream *strm;
1166
1167 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1168 if (strm->offset.key != qcs->tx.ack_offset)
1169 break;
1170
1171 b_del(strm->buf, strm->len);
1172 qcs->tx.ack_offset += strm->len;
1173 frm_node = eb64_next(frm_node);
1174 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001175 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001176 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001177
1178 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001179}
1180
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001181/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001182static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001183 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001184{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001185 int stream_acked;
1186 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001187
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001188 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001189 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001190 switch (frm->type) {
1191 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1192 {
1193 struct qcs *qcs = frm->stream.qcs;
1194 struct quic_stream *strm = &frm->stream;
1195
1196 if (qcs->tx.ack_offset == strm->offset.key) {
1197 b_del(strm->buf, strm->len);
1198 qcs->tx.ack_offset += strm->len;
1199 LIST_DELETE(&frm->list);
1200 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001201 qc->qcc->flags &= ~QC_CF_MUX_MFULL;
1202 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001203 }
1204 else {
1205 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1206 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001207 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001208 }
1209 break;
1210 default:
1211 LIST_DELETE(&frm->list);
1212 pool_free(pool_head_quic_frame, frm);
1213 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001214
1215 if (stream_acked) {
1216 struct qcc *qcc = qc->qcc;
1217
1218 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1219 tasklet_wakeup(qcc->subs->tasklet);
1220 qcc->subs->events &= ~SUB_RETRY_SEND;
1221 if (!qcc->subs->events)
1222 qcc->subs = NULL;
1223 }
1224 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001225}
1226
1227/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1228 * deallocating them, and their TX frames.
1229 * Returns the last node reached to be used for the next range.
1230 * May be NULL if <largest> node could not be found.
1231 */
1232static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1233 struct list *newly_acked_pkts,
1234 struct eb64_node *largest_node,
1235 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001236 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001237{
1238 struct eb64_node *node;
1239 struct quic_tx_packet *pkt;
1240
1241 if (largest_node)
1242 node = largest_node;
1243 else {
1244 node = eb64_lookup(pkts, largest);
1245 while (!node && largest > smallest) {
1246 node = eb64_lookup(pkts, --largest);
1247 }
1248 }
1249
1250 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001251 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001252
1253 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1254 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001255 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001256 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1257 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1258 qc_treat_acked_tx_frm(frm, ctx);
1259 node = eb64_prev(node);
1260 eb64_delete(&pkt->pn_node);
1261 }
1262
1263 return node;
1264}
1265
1266/* Treat <frm> frame whose packet it is attached to has just been detected as non
1267 * acknowledged.
1268 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001269static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001270 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001271 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001272{
1273 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001274 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001275 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001276}
1277
1278
1279/* Free the TX packets of <pkts> list */
1280static inline void free_quic_tx_pkts(struct list *pkts)
1281{
1282 struct quic_tx_packet *pkt, *tmp;
1283
1284 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001285 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001286 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001287 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001288 }
1289}
1290
1291/* Send a packet loss event nofification to the congestion controller
1292 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1293 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1294 * at <now_us> current time.
1295 * Always succeeds.
1296 */
1297static inline void qc_cc_loss_event(struct quic_conn *qc,
1298 unsigned int lost_bytes,
1299 unsigned int newest_time_sent,
1300 unsigned int period,
1301 unsigned int now_us)
1302{
1303 struct quic_cc_event ev = {
1304 .type = QUIC_CC_EVT_LOSS,
1305 .loss.now_ms = now_ms,
1306 .loss.max_ack_delay = qc->max_ack_delay,
1307 .loss.lost_bytes = lost_bytes,
1308 .loss.newest_time_sent = newest_time_sent,
1309 .loss.period = period,
1310 };
1311
1312 quic_cc_event(&qc->path->cc, &ev);
1313}
1314
1315/* Send a packet ack event nofication for each newly acked packet of
1316 * <newly_acked_pkts> list and free them.
1317 * Always succeeds.
1318 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001319static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001320 struct list *newly_acked_pkts)
1321{
1322 struct quic_conn *qc = ctx->conn->qc;
1323 struct quic_tx_packet *pkt, *tmp;
1324 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1325
1326 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1327 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001328 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001329 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001330 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001331 ev.ack.acked = pkt->in_flight_len;
1332 ev.ack.time_sent = pkt->time_sent;
1333 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001334 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001335 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001336 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001337 }
1338
1339}
1340
1341/* Handle <pkts> list of lost packets detected at <now_us> handling
1342 * their TX frames.
1343 * Send a packet loss event to the congestion controller if
1344 * in flight packet have been lost.
1345 * Also frees the packet in <pkts> list.
1346 * Never fails.
1347 */
1348static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001349 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001350 struct list *pkts,
1351 uint64_t now_us)
1352{
1353 struct quic_conn *qc = ctx->conn->qc;
1354 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001355 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001356 uint64_t lost_bytes;
1357
1358 lost_bytes = 0;
1359 oldest_lost = newest_lost = NULL;
1360 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1361 lost_bytes += pkt->in_flight_len;
1362 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001363 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001364 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001365 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001366 /* Treat the frames of this lost packet. */
1367 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1368 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001369 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001370 if (!oldest_lost) {
1371 oldest_lost = newest_lost = pkt;
1372 }
1373 else {
1374 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001375 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001376 newest_lost = pkt;
1377 }
1378 }
1379
1380 if (lost_bytes) {
1381 /* Sent a packet loss event to the congestion controller. */
1382 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1383 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001384 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001385 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001386 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001387 }
1388}
1389
1390/* Look for packet loss from sent packets for <qel> encryption level of a
1391 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1392 * their tree if deemed as lost or set the <loss_time> value the packet number
1393 * space if any not deemed lost.
1394 * Should be called after having received an ACK frame with newly acknowledged
1395 * packets or when the the loss detection timer has expired.
1396 * Always succeeds.
1397 */
1398static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1399 struct quic_conn *qc,
1400 struct list *lost_pkts)
1401{
1402 struct eb_root *pkts;
1403 struct eb64_node *node;
1404 struct quic_loss *ql;
1405 unsigned int loss_delay;
1406
1407 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1408 pkts = &pktns->tx.pkts;
1409 pktns->tx.loss_time = TICK_ETERNITY;
1410 if (eb_is_empty(pkts))
1411 goto out;
1412
1413 ql = &qc->path->loss;
1414 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1415 loss_delay += loss_delay >> 3;
1416 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1417
1418 node = eb64_first(pkts);
1419 while (node) {
1420 struct quic_tx_packet *pkt;
1421 int64_t largest_acked_pn;
1422 unsigned int loss_time_limit, time_sent;
1423
1424 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001425 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001426 node = eb64_next(node);
1427 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1428 break;
1429
1430 time_sent = pkt->time_sent;
1431 loss_time_limit = tick_add(time_sent, loss_delay);
1432 if (tick_is_le(time_sent, now_ms) ||
1433 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1434 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001435 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001436 }
1437 else {
1438 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1439 }
1440 }
1441
1442 out:
1443 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1444}
1445
1446/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1447 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001448 * 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 +01001449 * acked ack-eliciting packet.
1450 * Return 1, if succeeded, 0 if not.
1451 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001452static 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 +01001453 struct quic_enc_level *qel,
1454 unsigned int *rtt_sample,
1455 const unsigned char **pos, const unsigned char *end)
1456{
1457 struct quic_ack *ack = &frm->ack;
1458 uint64_t smallest, largest;
1459 struct eb_root *pkts;
1460 struct eb64_node *largest_node;
1461 unsigned int time_sent, pkt_flags;
1462 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1463 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1464
1465 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1466 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1467 ctx->conn,, &ack->largest_ack);
1468 goto err;
1469 }
1470
1471 if (ack->first_ack_range > ack->largest_ack) {
1472 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1473 ctx->conn,, &ack->first_ack_range);
1474 goto err;
1475 }
1476
1477 largest = ack->largest_ack;
1478 smallest = largest - ack->first_ack_range;
1479 pkts = &qel->pktns->tx.pkts;
1480 pkt_flags = 0;
1481 largest_node = NULL;
1482 time_sent = 0;
1483
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001484 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 +01001485 largest_node = eb64_lookup(pkts, largest);
1486 if (!largest_node) {
1487 TRACE_DEVEL("Largest acked packet not found",
1488 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1489 goto err;
1490 }
1491
1492 time_sent = eb64_entry(&largest_node->node,
1493 struct quic_tx_packet, pn_node)->time_sent;
1494 }
1495
1496 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1497 ctx->conn,, &largest, &smallest);
1498 do {
1499 uint64_t gap, ack_range;
1500
1501 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1502 largest_node, largest, smallest, ctx);
1503 if (!ack->ack_range_num--)
1504 break;
1505
1506 if (!quic_dec_int(&gap, pos, end))
1507 goto err;
1508
1509 if (smallest < gap + 2) {
1510 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1511 ctx->conn,, &gap, &smallest);
1512 goto err;
1513 }
1514
1515 largest = smallest - gap - 2;
1516 if (!quic_dec_int(&ack_range, pos, end))
1517 goto err;
1518
1519 if (largest < ack_range) {
1520 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1521 ctx->conn,, &largest, &ack_range);
1522 goto err;
1523 }
1524
1525 /* Do not use this node anymore. */
1526 largest_node = NULL;
1527 /* Next range */
1528 smallest = largest - ack_range;
1529
1530 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1531 ctx->conn,, &largest, &smallest);
1532 } while (1);
1533
1534 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001535 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001536
1537 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1538 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001539 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001540 }
1541
1542 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1543 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1544 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1545 if (!LIST_ISEMPTY(&lost_pkts))
1546 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1547 }
1548 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1549 if (quic_peer_validated_addr(ctx))
1550 ctx->conn->qc->path->loss.pto_count = 0;
1551 qc_set_timer(ctx);
1552 }
1553
1554
1555 return 1;
1556
1557 err:
1558 free_quic_tx_pkts(&newly_acked_pkts);
1559 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1560 return 0;
1561}
1562
1563/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1564 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001565 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001566 * Return 1 if succeeded, 0 if not.
1567 */
1568static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001569 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001570 const unsigned char *data, size_t len,
1571 struct quic_rx_packet *pkt,
1572 struct quic_rx_crypto_frm *cf)
1573{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001574 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001575 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001576
1577 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1578 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001579 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001580 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1581 TRACE_PROTO("SSL_provide_quic_data() error",
1582 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1583 goto err;
1584 }
1585
1586 el->rx.crypto.offset += len;
1587 TRACE_PROTO("in order CRYPTO data",
1588 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1589
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001590 state = HA_ATOMIC_LOAD(&qc->state);
1591 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001592 ssl_err = SSL_do_handshake(ctx->ssl);
1593 if (ssl_err != 1) {
1594 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1595 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1596 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001597 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001598 goto out;
1599 }
1600
1601 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001602 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001603 goto err;
1604 }
1605
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001606 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001607 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001608 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001609 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001610 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001611 } else {
1612 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1613 if (ssl_err != 1) {
1614 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1615 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1616 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001617 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001618 goto out;
1619 }
1620
1621 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001622 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001623 goto err;
1624 }
1625
1626 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001627 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001628 }
1629
1630 out:
1631 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1632 return 1;
1633
1634 err:
1635 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1636 return 0;
1637}
1638
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001639/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1640 * <pkt> RX packet.
1641 * Return it if succeeded, NULL if not.
1642 */
1643static inline
1644struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1645 struct quic_rx_packet *pkt)
1646{
1647 struct quic_rx_strm_frm *frm;
1648
1649 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1650 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001651 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001652 frm->len = stream_frm->len;
1653 frm->data = stream_frm->data;
1654 frm->pkt = pkt;
1655 }
1656
1657 return frm;
1658}
1659
1660/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1661 * several streams, depending on the already open onces.
1662 * Return this node if succeeded, NULL if not.
1663 */
1664static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1665{
1666 unsigned int strm_type;
1667 int64_t sub_id;
1668 struct eb64_node *strm_node;
1669
1670 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1671
1672 strm_type = id & QCS_ID_TYPE_MASK;
1673 sub_id = id >> QCS_ID_TYPE_SHIFT;
1674 strm_node = NULL;
1675 if (qc_local_stream_id(qcc, id)) {
1676 /* Local streams: this stream must be already opened. */
1677 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1678 if (!strm_node) {
1679 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1680 goto out;
1681 }
1682 }
1683 else {
1684 /* Remote streams. */
1685 struct eb_root *strms;
1686 uint64_t largest_id;
1687 enum qcs_type qcs_type;
1688
1689 strms = &qcc->streams_by_id;
1690 qcs_type = qcs_id_type(id);
1691 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1692 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1693 goto out;
1694 }
1695
1696 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1697 * correct value.
1698 */
1699 largest_id = qcc->strms[qcs_type].largest_id;
1700 if (sub_id > (int64_t)largest_id) {
1701 /* RFC: "A stream ID that is used out of order results in all streams
1702 * of that type with lower-numbered stream IDs also being opened".
1703 * So, let's "open" these streams.
1704 */
1705 int64_t i;
1706 struct qcs *qcs;
1707
1708 qcs = NULL;
1709 for (i = largest_id + 1; i <= sub_id; i++) {
1710 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1711 if (!qcs) {
1712 TRACE_PROTO("Could not allocate a new stream",
1713 QUIC_EV_CONN_PSTRM, qcc->conn);
1714 goto out;
1715 }
1716
1717 qcc->strms[qcs_type].largest_id = i;
1718 }
1719 if (qcs)
1720 strm_node = &qcs->by_id;
1721 }
1722 else {
1723 strm_node = eb64_lookup(strms, id);
1724 }
1725 }
1726
1727 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1728 return strm_node;
1729
1730 out:
1731 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1732 return NULL;
1733}
1734
1735/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1736 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1737 * to reflect the data which have been consumed.
1738 */
1739static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1740{
1741 size_t ret;
1742
1743 ret = 0;
1744 while (strm_frm->len) {
1745 size_t try;
1746
1747 try = b_contig_space(buf);
1748 if (!try)
1749 break;
1750
1751 if (try > strm_frm->len)
1752 try = strm_frm->len;
1753 memcpy(b_tail(buf), strm_frm->data, try);
1754 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001755 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001756 b_add(buf, try);
1757 ret += try;
1758 }
1759
1760 return ret;
1761}
1762
1763/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1764 * streams may be open. The data are copied to the stream RX buffer if possible.
1765 * If not, the STREAM frame is stored to be treated again later.
1766 * We rely on the flow control so that not to store too much STREAM frames.
1767 * Return 1 if succeeded, 0 if not.
1768 */
1769static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1770 struct quic_stream *strm_frm,
1771 struct quic_conn *qc)
1772{
1773 struct qcs *strm;
1774 struct eb64_node *strm_node, *frm_node;
1775 struct quic_rx_strm_frm *frm;
1776
1777 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1778 if (!strm_node) {
1779 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1780 return 0;
1781 }
1782
1783 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001784 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001785 /* FIXME: handle the case where this frame overlap others */
1786 if (frm_node) {
1787 TRACE_PROTO("Already existing stream data",
1788 QUIC_EV_CONN_PSTRM, qc->conn);
1789 goto out;
1790 }
1791
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001792 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001793 int ret;
1794
1795 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1796 goto store_frm;
1797
1798 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1799 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1800 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1801 return 0;
1802 }
1803
1804 strm->rx.offset += ret;
1805 }
1806
1807 if (!strm_frm->len)
1808 goto out;
1809
1810 store_frm:
1811 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1812 if (!frm) {
1813 TRACE_PROTO("Could not alloc RX STREAM frame",
1814 QUIC_EV_CONN_PSTRM, qc->conn);
1815 return 0;
1816 }
1817
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001818 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001819 quic_rx_packet_refinc(pkt);
1820
1821 out:
1822 return 1;
1823}
1824
1825/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1826 * streams may be open. The data are copied to the stream RX buffer if possible.
1827 * If not, the STREAM frame is stored to be treated again later.
1828 * We rely on the flow control so that not to store too much STREAM frames.
1829 * Return 1 if succeeded, 0 if not.
1830 */
1831static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1832 struct quic_stream *strm_frm,
1833 struct quic_conn *qc)
1834{
1835 struct qcs *strm;
1836 struct eb64_node *strm_node, *frm_node;
1837 struct quic_rx_strm_frm *frm;
1838 size_t strm_frm_len;
1839
1840 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1841 if (!strm_node) {
1842 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1843 return 0;
1844 }
1845
1846 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001847 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001848 /* FIXME: handle the case where this frame overlap others */
1849 if (frm_node) {
1850 TRACE_PROTO("Already existing stream data",
1851 QUIC_EV_CONN_PSTRM, qc->conn);
1852 goto out;
1853 }
1854
1855 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001856 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001857 int ret;
1858
1859 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1860 goto store_frm;
1861
1862 /* qc_strm_cpy() will modify the offset, depending on the number
1863 * of bytes copied.
1864 */
1865 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1866 /* Inform the application of the arrival of this new stream */
1867 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1868 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1869 return 0;
1870 }
1871
1872 if (ret)
1873 ruqs_notify_recv(strm);
1874
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001875 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001876 }
1877 /* Take this frame into an account for the stream flow control */
1878 strm->rx.offset += strm_frm_len;
1879 /* It all the data were provided to the application, there is no need to
1880 * store any more inforamtion for it.
1881 */
1882 if (!strm_frm->len)
1883 goto out;
1884
1885 store_frm:
1886 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1887 if (!frm) {
1888 TRACE_PROTO("Could not alloc RX STREAM frame",
1889 QUIC_EV_CONN_PSTRM, qc->conn);
1890 return 0;
1891 }
1892
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001893 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001894 quic_rx_packet_refinc(pkt);
1895
1896 out:
1897 return 1;
1898}
1899
1900static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1901 struct quic_stream *strm_frm,
1902 struct quic_conn *qc)
1903{
1904 if (strm_frm->id & QCS_ID_DIR_BIT)
1905 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1906 else
1907 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1908}
1909
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001910/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1911 * as I/O handler context and <qel> as encryption level.
1912 * Returns 1 if succeeded, 0 if failed.
1913 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001914static 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 +01001915 struct quic_enc_level *qel)
1916{
1917 struct quic_frame frm;
1918 const unsigned char *pos, *end;
1919 struct quic_conn *conn = ctx->conn->qc;
1920
1921 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1922 /* Skip the AAD */
1923 pos = pkt->data + pkt->aad_len;
1924 end = pkt->data + pkt->len;
1925
1926 while (pos < end) {
1927 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1928 goto err;
1929
1930 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001931 case QUIC_FT_PADDING:
1932 if (pos != end) {
1933 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1934 goto err;
1935 }
1936 break;
1937 case QUIC_FT_PING:
1938 break;
1939 case QUIC_FT_ACK:
1940 {
1941 unsigned int rtt_sample;
1942
1943 rtt_sample = 0;
1944 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1945 goto err;
1946
1947 if (rtt_sample) {
1948 unsigned int ack_delay;
1949
1950 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1951 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1952 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1953 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001954 break;
1955 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 case QUIC_FT_CRYPTO:
1957 if (frm.crypto.offset != qel->rx.crypto.offset) {
1958 struct quic_rx_crypto_frm *cf;
1959
1960 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1961 if (!cf) {
1962 TRACE_DEVEL("CRYPTO frame allocation failed",
1963 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1964 goto err;
1965 }
1966
1967 cf->offset_node.key = frm.crypto.offset;
1968 cf->len = frm.crypto.len;
1969 cf->data = frm.crypto.data;
1970 cf->pkt = pkt;
1971 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1972 quic_rx_packet_refinc(pkt);
1973 }
1974 else {
1975 /* XXX TO DO: <cf> is used only for the traces. */
1976 struct quic_rx_crypto_frm cf = { };
1977
1978 cf.offset_node.key = frm.crypto.offset;
1979 cf.len = frm.crypto.len;
1980 if (!qc_provide_cdata(qel, ctx,
1981 frm.crypto.data, frm.crypto.len,
1982 pkt, &cf))
1983 goto err;
1984 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001985 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001986 case QUIC_FT_STREAM_8:
1987 case QUIC_FT_STREAM_9:
1988 case QUIC_FT_STREAM_A:
1989 case QUIC_FT_STREAM_B:
1990 case QUIC_FT_STREAM_C:
1991 case QUIC_FT_STREAM_D:
1992 case QUIC_FT_STREAM_E:
1993 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001994 {
1995 struct quic_stream *stream = &frm.stream;
1996
1997 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1998 if (objt_listener(ctx->conn->target)) {
1999 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2000 goto err;
2001 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2002 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002003
2004 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
2005 goto err;
2006
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002007 break;
2008 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002009 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002010 break;
2011 case QUIC_FT_CONNECTION_CLOSE:
2012 case QUIC_FT_CONNECTION_CLOSE_APP:
2013 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002014 case QUIC_FT_HANDSHAKE_DONE:
2015 if (objt_listener(ctx->conn->target))
2016 goto err;
2017
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002018 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002019 break;
2020 default:
2021 goto err;
2022 }
2023 }
2024
2025 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2026 * has successfully parse a Handshake packet. The Initial encryption must also
2027 * be discarded.
2028 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002029 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
2030 int state = HA_ATOMIC_LOAD(&conn->state);
2031
2032 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
2033 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2034 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2035 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2036 qc_set_timer(ctx);
2037 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
2038 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
2039 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002040 }
2041
2042 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2043 return 1;
2044
2045 err:
2046 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2047 return 0;
2048}
2049
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002050/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
2051 * buffer. This is the responsability of the caller to check there is enough
2052 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2053 * This function must be called only after having built a correct datagram.
2054 * Always succeeds.
2055 */
2056static inline void qc_set_dg(struct cbuf *cbuf,
2057 uint16_t dglen, struct quic_tx_packet *pkt)
2058{
2059 write_u16(cb_wr(cbuf), dglen);
2060 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2061 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2062}
2063
2064/* Prepare as much as possible handshake packets into <qr> ring buffer for
2065 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2066 * several packets in the same datagram. A header made of two fields is added
2067 * to each datagram: the datagram length followed by the address of the first
2068 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002069 * Returns 1 if succeeded, or 0 if something wrong happened.
2070 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002071static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002072{
2073 struct quic_conn *qc;
2074 enum quic_tls_enc_level tel, next_tel;
2075 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002076 struct cbuf *cbuf;
2077 unsigned char *end_buf, *end, *pos, *spos;
2078 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2079 /* length of datagrams */
2080 uint16_t dglen;
2081 size_t total;
2082 /* Each datagram is prepended with its length followed by the
2083 * address of the first packet in the datagram.
2084 */
2085 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002086
2087 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2088 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002089 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002090 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002091 goto err;
2092 }
2093
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002094 start:
2095 total = 0;
2096 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002097 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002098 cbuf = qr->cbuf;
2099 spos = pos = cb_wr(cbuf);
2100 /* Leave at least <dglen> bytes at the end of this buffer
2101 * to ensure there is enough room to mark the end of prepared
2102 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002103 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002104 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2105 first_pkt = prv_pkt = NULL;
2106 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002107 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002108 enum quic_pkt_type pkt_type;
2109
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002110 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002111 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002112 if (!prv_pkt) {
2113 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2114 do {
2115 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2116 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2117 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002118 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002119 /* Do not build any more packet if the TX secrets are not available or
2120 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002121 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002122 * and if there is no more CRYPTO data available or in flight
2123 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002124 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002125 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002126 (!ack && !nb_ptos &&
2127 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2128 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002129 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002130 /* Set the current datagram as prepared into <cbuf> if
2131 * the was already a correct packet which was previously written.
2132 */
2133 if (prv_pkt)
2134 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002135 break;
2136 }
2137
2138 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002139 if (!prv_pkt) {
2140 /* Leave room for the datagram header */
2141 pos += dg_headlen;
2142 end = pos + qc->path->mtu;
2143 }
2144
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002145 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 +02002146 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002147 if (err < 0) {
2148 if (!prv_pkt && nb_ptos)
2149 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002150 if (ack)
2151 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002152 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002153 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002154 case -2:
2155 goto err;
2156 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002157 /* If there was already a correct packet present, set the
2158 * current datagram as prepared into <cbuf>.
2159 */
2160 if (prv_pkt) {
2161 qc_set_dg(cbuf, dglen, first_pkt);
2162 goto stop_build;
2163 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002164 goto out;
2165 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002166 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2167 if (!cur_pkt)
2168 goto err;
2169
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002170 total += cur_pkt->len;
2171 /* keep trace of the first packet in the datagram */
2172 if (!first_pkt)
2173 first_pkt = cur_pkt;
2174 /* Attach the current one to the previous one */
2175 if (prv_pkt)
2176 prv_pkt->next = cur_pkt;
2177 /* Let's say we have to build a new dgram */
2178 prv_pkt = NULL;
2179 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002180 /* Discard the Initial encryption keys as soon as
2181 * a handshake packet could be built.
2182 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002183 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002184 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2185 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002186 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002187 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2188 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002189 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002190 }
2191 /* Special case for Initial packets: when they have all
2192 * been sent, select the next level.
2193 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002194 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002195 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2196 (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 +02002197 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2198 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2199 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002200 tel = next_tel;
2201 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002202 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002203 /* If there is data for the next level, do not
2204 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002205 * which sends only one Initial packet, then wait
2206 * for additional CRYPTO data from the server to enter the
2207 * next level.
2208 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002209 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002210 }
2211 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002212 }
2213
2214 /* If we have to build a new datagram, set the current datagram as
2215 * prepared into <cbuf>.
2216 */
2217 if (!prv_pkt) {
2218 qc_set_dg(cbuf, dglen, first_pkt);
2219 first_pkt = NULL;
2220 dglen = 0;
2221 }
2222 }
2223
2224 stop_build:
2225 /* Reset <wr> writer index if in front of <rd> index */
2226 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2227 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2228
2229 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2230 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2231 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2232 /* Mark the end of contiguous data for the reader */
2233 write_u16(cb_wr(cbuf), 0);
2234 cb_add(cbuf, sizeof(uint16_t));
2235 }
2236 }
2237
2238 if (rd && rd <= cbuf->wr) {
2239 cb_wr_reset(cbuf);
2240 if (pos == spos) {
2241 /* Reuse the same buffer if nothing was built. */
2242 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002243 }
2244 }
2245 }
2246
2247 out:
2248 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002249 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002250
2251 err:
2252 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002253 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002254}
2255
2256/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002257 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002258 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002259int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002260{
2261 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002262 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002263
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002264 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002265 cbuf = qr->cbuf;
2266 while (cb_contig_data(cbuf)) {
2267 unsigned char *pos;
2268 struct buffer tmpbuf = { };
2269 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2270 uint16_t dglen;
2271 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002272 unsigned int time_sent;
2273
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002274 pos = cb_rd(cbuf);
2275 dglen = read_u16(pos);
2276 /* End of prepared datagrams.
2277 * Reset the reader index only if in front of the writer index.
2278 */
2279 if (!dglen) {
2280 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2281
2282 if (wr && wr < cbuf->rd) {
2283 cb_rd_reset(cbuf);
2284 continue;
2285 }
2286 break;
2287 }
2288
2289 pos += sizeof dglen;
2290 first_pkt = read_ptr(pos);
2291 pos += sizeof first_pkt;
2292 tmpbuf.area = (char *)pos;
2293 tmpbuf.size = tmpbuf.data = dglen;
2294
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002295 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002296 for (pkt = first_pkt; pkt; pkt = pkt->next)
2297 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002298 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002299 &tmpbuf, tmpbuf.data, 0) <= 0) {
2300 for (pkt = first_pkt; pkt; pkt = pkt->next)
2301 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002302 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002303 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002304
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002305 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002306 qc->tx.bytes += tmpbuf.data;
2307 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002308
2309 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2310 pkt->time_sent = time_sent;
2311 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2312 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002313 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002314 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002315 qc->path->in_flight += pkt->in_flight_len;
2316 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2317 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002319 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2320 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002321 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002322 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002323 }
2324 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002325
2326 return 1;
2327}
2328
2329/* Build all the frames which must be sent just after the handshake have succeeded.
2330 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2331 * a HANDSHAKE_DONE frame.
2332 * Return 1 if succeeded, 0 if not.
2333 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002334static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002335{
2336 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002337 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002338 struct quic_frame *frm;
2339
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002340 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002341 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002342 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002343 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002344 if (!frm)
2345 return 0;
2346
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002347 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002348 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002349 }
2350
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002351 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002352 struct quic_connection_id *cid;
2353
2354 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002355 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002356 if (!frm || !cid)
2357 goto err;
2358
2359 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002360 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002361 }
2362
2363 return 1;
2364
2365 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002366 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002367 return 0;
2368}
2369
2370/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002371void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002372{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002373 struct eb64_node *n;
2374 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002375
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002376 n = eb64_first(&arngs->root);
2377 while (n) {
2378 struct eb64_node *next;
2379
2380 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2381 next = eb64_next(n);
2382 eb64_delete(n);
2383 free(ar);
2384 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002385 }
2386}
2387
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002388/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2389 * descending order.
2390 */
2391static inline size_t sack_gap(struct quic_arng_node *p,
2392 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002393{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002394 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002395}
2396
2397
2398/* Remove the last elements of <ack_ranges> list of ack range updating its
2399 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002400 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002402static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
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 *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002405
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002406 last = eb64_last(&arngs->root);
2407 while (last && arngs->enc_sz > limit) {
2408 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002409
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002410 prev = eb64_prev(last);
2411 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412 return 0;
2413
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002414 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2415 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2416 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2417 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2418 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2419 --arngs->sz;
2420 eb64_delete(last);
2421 pool_free(pool_head_quic_arng, last);
2422 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002423 }
2424
2425 return 1;
2426}
2427
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002428/* Set the encoded size of <arngs> QUIC ack ranges. */
2429static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2430{
2431 struct eb64_node *node, *next;
2432 struct quic_arng_node *ar, *ar_next;
2433
2434 node = eb64_last(&arngs->root);
2435 if (!node)
2436 return;
2437
2438 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2439 arngs->enc_sz = quic_int_getsize(ar->last) +
2440 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2441
2442 while ((next = eb64_prev(node))) {
2443 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2444 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2445 quic_int_getsize(ar_next->last - ar_next->first.key);
2446 node = next;
2447 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2448 }
2449}
2450
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002451/* Insert <ar> ack range into <argns> tree of ack ranges.
2452 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002453 */
2454static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002455struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2456 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002457{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002458 struct quic_arng_node *new_ar;
2459
2460 new_ar = pool_alloc(pool_head_quic_arng);
2461 if (new_ar) {
2462 new_ar->first.key = ar->first;
2463 new_ar->last = ar->last;
2464 eb64_insert(&arngs->root, &new_ar->first);
2465 arngs->sz++;
2466 }
2467
2468 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002469}
2470
2471/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002472 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002473 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002474 *
2475 * Descending order
2476 * ------------->
2477 * range1 range2
2478 * ..........|--------|..............|--------|
2479 * ^ ^ ^ ^
2480 * | | | |
2481 * last1 first1 last2 first2
2482 * ..........+--------+--------------+--------+......
2483 * diff1 gap12 diff2
2484 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002485 * To encode the previous list of ranges we must encode integers as follows in
2486 * descending order:
2487 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002488 * with diff1 = last1 - first1
2489 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002490 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002491 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002492 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002493int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2494 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002495{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002496 struct eb64_node *le;
2497 struct quic_arng_node *new_node;
2498 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002499
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002500 new = NULL;
2501 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002502 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002503 if (!new_node)
2504 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002505
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002506 goto out;
2507 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002508
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002509 le = eb64_lookup_le(&arngs->root, ar->first);
2510 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002511 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002512 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002513 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002514 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002515 else {
2516 struct quic_arng_node *le_ar =
2517 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002518
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002519 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002520 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002521 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002522
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002523 if (le_ar->last + 1 >= ar->first) {
2524 le_ar->last = ar->last;
2525 new = le;
2526 new_node = le_ar;
2527 }
2528 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002529 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002530 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002531 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002532
2533 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002535 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002536
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002537 /* Verify that the new inserted node does not overlap the nodes
2538 * which follow it.
2539 */
2540 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002541 struct eb64_node *next;
2542 struct quic_arng_node *next_node;
2543
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002544 while ((next = eb64_next(new))) {
2545 next_node =
2546 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002547 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002548 break;
2549
2550 if (next_node->last > new_node->last)
2551 new_node->last = next_node->last;
2552 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002553 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002554 /* Decrement the size of these ranges. */
2555 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002556 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002557 }
2558
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002559 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002560 quic_arngs_set_enc_sz(arngs);
2561
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002562 return 1;
2563}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002564/* Remove the header protection of packets at <el> encryption level.
2565 * Always succeeds.
2566 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002567static 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 +01002568{
2569 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002570 struct quic_rx_packet *pqpkt;
2571 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002572 struct quic_enc_level *app_qel;
2573
2574 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2575 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2576 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002577 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002578 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002579 TRACE_PROTO("hp not removed (handshake not completed)",
2580 QUIC_EV_CONN_ELRMHP, ctx->conn);
2581 goto out;
2582 }
2583 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002584 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2586 pqpkt->data + pqpkt->pn_offset,
2587 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2588 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2589 /* XXX TO DO XXX */
2590 }
2591 else {
2592 /* The AAD includes the packet number field */
2593 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2594 /* Store the packet into the tree of packets to decrypt. */
2595 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002596 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002597 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2598 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002599 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002600 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2601 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002602 MT_LIST_DELETE_SAFE(pkttmp1);
2603 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002604 }
2605
2606 out:
2607 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2608}
2609
2610/* Process all the CRYPTO frame at <el> encryption level.
2611 * Return 1 if succeeded, 0 if not.
2612 */
2613static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002614 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002615{
2616 struct eb64_node *node;
2617
2618 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002619 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002620 node = eb64_first(&el->rx.crypto.frms);
2621 while (node) {
2622 struct quic_rx_crypto_frm *cf;
2623
2624 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2625 if (cf->offset_node.key != el->rx.crypto.offset)
2626 break;
2627
2628 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2629 goto err;
2630
2631 node = eb64_next(node);
2632 quic_rx_packet_refdec(cf->pkt);
2633 eb64_delete(&cf->offset_node);
2634 pool_free(pool_head_quic_rx_crypto_frm, cf);
2635 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002636 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002637 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2638 return 1;
2639
2640 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002641 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002642 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2643 return 0;
2644}
2645
2646/* Process all the packets at <el> encryption level.
2647 * Return 1 if succeeded, 0 if not.
2648 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002649int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2650 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002651{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002653 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002654 struct quic_conn *qc = ctx->conn->qc;
2655 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002656
2657 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002658 qel = cur_el;
2659 next_tel:
2660 if (!qel)
2661 goto out;
2662
2663 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2664 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665 while (node) {
2666 struct quic_rx_packet *pkt;
2667
2668 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002669 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2670 ctx->conn, pkt, NULL, ctx->ssl);
2671 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002672 /* Drop the packet */
2673 TRACE_PROTO("packet decryption failed -> dropped",
2674 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2675 }
2676 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002677 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002678 /* Drop the packet */
2679 TRACE_PROTO("packet parsing failed -> dropped",
2680 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2681 }
2682 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002683 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2684
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002685 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002686 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2687 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002688 if (pkt->pn > largest_pn)
2689 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002690 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002691 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002692 TRACE_DEVEL("Could not update ack range list",
2693 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002694 }
2695 }
2696 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002697 eb64_delete(&pkt->pn_node);
2698 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002699 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002700 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002701
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002702 /* Update the largest packet number. */
2703 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002704 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2705 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002706 goto err;
2707
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002708 if (qel == cur_el) {
2709 qel = next_el;
2710 goto next_tel;
2711 }
2712
2713 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002714 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2715 return 1;
2716
2717 err:
2718 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2719 return 0;
2720}
2721
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002722/* QUIC connection packet handler task. */
2723struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002724{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002725 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002726 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002727 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002728 enum quic_tls_enc_level tel, next_tel;
2729 struct quic_enc_level *qel, *next_qel;
2730 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002731 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002732 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002733
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002734 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002735 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002736 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002737 st = HA_ATOMIC_LOAD(&qc->state);
2738 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002739 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002740 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002741 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002742 goto err;
2743
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002744 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002745 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002746
2747 next_level:
2748 tls_ctx = &qel->tls_ctx;
2749
2750 /* If the header protection key for this level has been derived,
2751 * remove the packet header protections.
2752 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002753 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002754 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2755 qc_rm_hp_pkts(qel, ctx);
2756
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002757 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002758 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2759 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2760 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002761 goto err;
2762
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002763 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002764 if (st >= QUIC_HS_ST_COMPLETE &&
2765 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002766 /* Discard the Handshake keys. */
2767 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002768 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002769 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2770 qc_set_timer(ctx);
2771 if (!quic_build_post_handshake_frames(qc))
2772 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002773 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002774 }
2775
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002776 if (!qr)
2777 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2778 ret = qc_prep_hdshk_pkts(qr, ctx);
2779 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002781 else if (ret == 0)
2782 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002784 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002785 goto err;
2786
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002787 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002788 /* Check if there is something to do for the next level.
2789 */
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002790 if (next_qel && (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002791 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002792 qel = next_qel;
2793 goto next_level;
2794 }
2795
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002796 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002797 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002798 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799
2800 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002801 if (qr)
2802 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002803 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002804 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002805}
2806
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002807/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002808static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2809{
2810 int i;
2811
2812 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2813 if (qel->tx.crypto.bufs[i]) {
2814 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2815 qel->tx.crypto.bufs[i] = NULL;
2816 }
2817 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002818 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002819}
2820
2821/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002822 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002823 * Returns 1 if succeeded, 0 if not.
2824 */
2825static int quic_conn_enc_level_init(struct quic_conn *qc,
2826 enum quic_tls_enc_level level)
2827{
2828 struct quic_enc_level *qel;
2829
2830 qel = &qc->els[level];
2831 qel->level = quic_to_ssl_enc_level(level);
2832 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2833 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2834 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2835 qel->tls_ctx.rx.flags = 0;
2836 qel->tls_ctx.tx.flags = 0;
2837
2838 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002839 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002840 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002841 qel->rx.crypto.offset = 0;
2842 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2843 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002844
2845 /* Allocate only one buffer. */
2846 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2847 if (!qel->tx.crypto.bufs)
2848 goto err;
2849
2850 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2851 if (!qel->tx.crypto.bufs[0])
2852 goto err;
2853
2854 qel->tx.crypto.bufs[0]->sz = 0;
2855 qel->tx.crypto.nb_buf = 1;
2856
2857 qel->tx.crypto.sz = 0;
2858 qel->tx.crypto.offset = 0;
2859
2860 return 1;
2861
2862 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002863 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002864 return 0;
2865}
2866
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002867/* Release all the memory allocated for <conn> QUIC connection. */
2868static void quic_conn_free(struct quic_conn *conn)
2869{
2870 int i;
2871
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002872 if (!conn)
2873 return;
2874
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002875 free_quic_conn_cids(conn);
2876 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2877 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002878 if (conn->timer_task)
2879 task_destroy(conn->timer_task);
2880 pool_free(pool_head_quic_conn, conn);
2881}
2882
2883/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002884static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002885{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002886 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002887 struct quic_conn *qc;
2888 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002889 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890
2891 conn_ctx = task->context;
2892 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002893 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2894 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002895 task->expire = TICK_ETERNITY;
2896 pktns = quic_loss_pktns(qc);
2897 if (tick_isset(pktns->tx.loss_time)) {
2898 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2899
2900 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2901 if (!LIST_ISEMPTY(&lost_pkts))
2902 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2903 qc_set_timer(conn_ctx);
2904 goto out;
2905 }
2906
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002907 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002908 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002909 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002910 pktns->tx.pto_probe = 1;
2911 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002912 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002913 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2914 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2915
2916 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2917 hel->pktns->tx.pto_probe = 1;
2918 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2919 iel->pktns->tx.pto_probe = 1;
2920 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002921 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002922 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2923 qc->path->loss.pto_count++;
2924
2925 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002926 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002927
2928 return task;
2929}
2930
2931/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2932 * connections used to identify the first Initial packets of client connecting
2933 * to listeners. This parameter must be NULL for QUIC connections attached
2934 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2935 * <scid> is the source connection ID with <scid_len> as length.
2936 * Returns 1 if succeeded, 0 if not.
2937 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002938static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2939 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002940 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002941{
2942 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002943 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002944 /* Initial CID. */
2945 struct quic_connection_id *icid;
2946
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002947 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002948 qc = pool_zalloc(pool_head_quic_conn);
2949 if (!qc) {
2950 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2951 goto err;
2952 }
2953
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002954 qc->cids = EB_ROOT;
2955 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002956 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002957 struct listener *l = owner;
2958
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002959 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002960 /* Copy the initial DCID. */
2961 qc->odcid.len = dcid_len;
2962 if (qc->odcid.len)
2963 memcpy(qc->odcid.data, dcid, dcid_len);
2964
2965 /* Copy the SCID as our DCID for this connection. */
2966 if (scid_len)
2967 memcpy(qc->dcid.data, scid, scid_len);
2968 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002969 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002970 }
2971 /* QUIC Client (outgoing connection to servers) */
2972 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002973 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002974 if (dcid_len)
2975 memcpy(qc->dcid.data, dcid, dcid_len);
2976 qc->dcid.len = dcid_len;
2977 }
2978
2979 /* Initialize the output buffer */
2980 qc->obuf.pos = qc->obuf.data;
2981
2982 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002983 if (!icid) {
2984 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2985 goto err;
2986 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002987
2988 /* Select our SCID which is the first CID with 0 as sequence number. */
2989 qc->scid = icid->cid;
2990
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002991 /* Packet number spaces initialization. */
2992 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2993 quic_pktns_init(&qc->pktns[i]);
2994 /* QUIC encryption level context initialization. */
2995 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002996 if (!quic_conn_enc_level_init(qc, i)) {
2997 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002999 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003000 /* Initialize the packet number space. */
3001 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3002 }
3003
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003004 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003005 /* TX part. */
3006 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003007 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3008 qc->tx.wbuf = qc->tx.rbuf = 0;
3009 qc->tx.bytes = 0;
3010 qc->tx.nb_pto_dgrams = 0;
3011 /* RX part. */
3012 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003013 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 /* XXX TO DO: Only one path at this time. */
3016 qc->path = &qc->paths[0];
3017 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3018
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003019 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003021 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022
3023 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003024 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003026 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027}
3028
3029/* Initialize the timer task of <qc> QUIC connection.
3030 * Returns 1 if succeeded, 0 if not.
3031 */
3032static int quic_conn_init_timer(struct quic_conn *qc)
3033{
3034 qc->timer_task = task_new(MAX_THREADS_MASK);
3035 if (!qc->timer_task)
3036 return 0;
3037
3038 qc->timer = TICK_ETERNITY;
3039 qc->timer_task->process = process_timer;
3040 qc->timer_task->context = qc->conn->xprt_ctx;
3041
3042 return 1;
3043}
3044
3045/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3046 * past one byte of this buffer.
3047 */
3048static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3049 struct quic_rx_packet *pkt)
3050{
3051 unsigned char dcid_len, scid_len;
3052
3053 /* Version */
3054 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3055 return 0;
3056
3057 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
3058
3059 /* Destination Connection ID Length */
3060 dcid_len = *(*buf)++;
3061 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3062 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3063 /* XXX MUST BE DROPPED */
3064 return 0;
3065
3066 if (dcid_len) {
3067 /* Check that the length of this received DCID matches the CID lengths
3068 * of our implementation for non Initials packets only.
3069 */
3070 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3071 return 0;
3072
3073 memcpy(pkt->dcid.data, *buf, dcid_len);
3074 }
3075
3076 pkt->dcid.len = dcid_len;
3077 *buf += dcid_len;
3078
3079 /* Source Connection ID Length */
3080 scid_len = *(*buf)++;
3081 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3082 /* XXX MUST BE DROPPED */
3083 return 0;
3084
3085 if (scid_len)
3086 memcpy(pkt->scid.data, *buf, scid_len);
3087 pkt->scid.len = scid_len;
3088 *buf += scid_len;
3089
3090 return 1;
3091}
3092
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003093/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3094 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3095 * encryption level matching with the packet type. <*qel> may be null if not found.
3096 * Note that <ctx> may be null (for Initial packets).
3097 */
3098static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003099 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003100 struct quic_enc_level **qel)
3101{
3102 enum quic_tls_enc_level tel;
3103
3104 /* Special case without connection context (firt Initial packets) */
3105 if (!ctx) {
3106 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3107 return 1;
3108 }
3109
3110 tel = quic_packet_type_enc_level(pkt->type);
3111 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3112 *qel = NULL;
3113 return 0;
3114 }
3115
3116 *qel = &qc->els[tel];
3117 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3118 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3119 return 0;
3120 }
3121
3122 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003123 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3124 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003125 return 1;
3126
3127 return 0;
3128}
3129
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003130/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3131 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3132 * byte past the end of the buffer containing this packet and <beg> the address of
3133 * the packet first byte.
3134 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3135 * Returns 1 if succeeded, 0 if not.
3136 */
3137static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3138 unsigned char **buf, unsigned char *beg,
3139 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003140 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003141{
3142 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003143 struct quic_enc_level *qel;
3144 /* Only for traces. */
3145 struct quic_rx_packet *qpkt_trace;
3146
3147 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003148 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003149 /* The packet number is here. This is also the start minus
3150 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3151 * protection.
3152 */
3153 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003154 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003155 /* Note that the following function enables us to unprotect the packet
3156 * number and its length subsequently used to decrypt the entire
3157 * packets.
3158 */
3159 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3160 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003161 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003162 goto err;
3163 }
3164
3165 /* The AAD includes the packet number field found at <pn>. */
3166 pkt->aad_len = pn - beg + pkt->pnl;
3167 qpkt_trace = pkt;
3168 /* Store the packet */
3169 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003170 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003171 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3172 quic_rx_packet_refinc(pkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003173 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003174 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003175 else if (qel) {
3176 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003178 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3179 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003180 }
3181
3182 memcpy(pkt->data, beg, pkt->len);
3183 /* Updtate the offset of <*buf> for the next QUIC packet. */
3184 *buf = beg + pkt->len;
3185
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003186 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003187 return 1;
3188
3189 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003190 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 +01003191 return 0;
3192}
3193
3194/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3195 * Also set <*long_header> to 1 if this form is long, 0 if not.
3196 */
3197static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3198 unsigned char byte0, int *long_header)
3199{
3200 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3201 pkt->type =
3202 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3203 *long_header = 1;
3204 }
3205 else {
3206 pkt->type = QUIC_PACKET_TYPE_SHORT;
3207 *long_header = 0;
3208 }
3209}
3210
3211static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3212 struct quic_rx_packet *pkt,
3213 struct quic_dgram_ctx *dgram_ctx,
3214 struct sockaddr_storage *saddr)
3215{
3216 unsigned char *beg;
3217 uint64_t len;
3218 struct quic_conn *qc;
3219 struct eb_root *cids;
3220 struct ebmb_node *node;
3221 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003222 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003223 int long_header;
3224
3225 qc = NULL;
3226 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3227 if (end <= *buf)
3228 goto err;
3229
3230 /* Fixed bit */
3231 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3232 /* XXX TO BE DISCARDED */
3233 goto err;
3234
3235 srv_conn = dgram_ctx->owner;
3236 beg = *buf;
3237 /* Header form */
3238 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3239 if (long_header) {
3240 size_t cid_lookup_len;
3241
3242 if (!quic_packet_read_long_header(buf, end, pkt))
3243 goto err;
3244
3245 /* For Initial packets, and for servers (QUIC clients connections),
3246 * there is no Initial connection IDs storage.
3247 */
3248 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3249 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3250 cid_lookup_len = pkt->dcid.len;
3251 }
3252 else {
3253 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3254 cid_lookup_len = QUIC_CID_LEN;
3255 }
3256
3257 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3258 if (!node)
3259 goto err;
3260
3261 qc = ebmb_entry(node, struct quic_conn, scid_node);
3262
3263 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3264 qc->dcid.len = pkt->scid.len;
3265 if (pkt->scid.len)
3266 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3267 }
3268
3269 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3270 uint64_t token_len;
3271
3272 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3273 goto err;
3274
3275 /* XXX TO DO XXX 0 value means "the token is not present".
3276 * A server which sends an Initial packet must not set the token.
3277 * So, a client which receives an Initial packet with a token
3278 * MUST discard the packet or generate a connection error with
3279 * PROTOCOL_VIOLATION as type.
3280 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3281 */
3282 pkt->token_len = token_len;
3283 }
3284 }
3285 else {
3286 /* XXX TO DO: Short header XXX */
3287 if (end - *buf < QUIC_CID_LEN)
3288 goto err;
3289
3290 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3291 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3292 if (!node)
3293 goto err;
3294
3295 qc = ebmb_entry(node, struct quic_conn, scid_node);
3296 *buf += QUIC_CID_LEN;
3297 }
3298 /* Store the DCID used for this packet to check the packet which
3299 * come in this UDP datagram match with it.
3300 */
3301 if (!dgram_ctx->dcid_node)
3302 dgram_ctx->dcid_node = node;
3303 /* Only packets packets with long headers and not RETRY or VERSION as type
3304 * have a length field.
3305 */
3306 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3307 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3308 goto err;
3309
3310 pkt->len = len;
3311 }
3312 else if (!long_header) {
3313 /* A short packet is the last one of an UDP datagram. */
3314 pkt->len = end - *buf;
3315 }
3316
3317 conn_ctx = qc->conn->xprt_ctx;
3318
3319 /* Increase the total length of this packet by the header length. */
3320 pkt->len += *buf - beg;
3321 /* Do not check the DCID node before the length. */
3322 if (dgram_ctx->dcid_node != node) {
3323 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3324 goto err;
3325 }
3326
3327 if (pkt->len > sizeof pkt->data) {
3328 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3329 goto err;
3330 }
3331
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003332 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003333 goto err;
3334
3335 /* Wake the tasklet of the QUIC connection packet handler. */
3336 if (conn_ctx)
3337 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3338
3339 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3340
3341 return pkt->len;
3342
3343 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003344 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003345 return -1;
3346}
3347
3348static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3349 struct quic_rx_packet *pkt,
3350 struct quic_dgram_ctx *dgram_ctx,
3351 struct sockaddr_storage *saddr)
3352{
3353 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003354 struct quic_conn *qc;
3355 struct eb_root *cids;
3356 struct ebmb_node *node;
3357 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003358 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003359 int long_header = 0;
3360
3361 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003362 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003363 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003364 if (end <= *buf)
3365 goto err;
3366
3367 /* Fixed bit */
3368 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3369 /* XXX TO BE DISCARDED */
3370 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3371 goto err;
3372 }
3373
3374 l = dgram_ctx->owner;
3375 beg = *buf;
3376 /* Header form */
3377 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3378 if (long_header) {
3379 unsigned char dcid_len;
3380
3381 if (!quic_packet_read_long_header(buf, end, pkt)) {
3382 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3383 goto err;
3384 }
3385
3386 dcid_len = pkt->dcid.len;
3387 /* For Initial packets, and for servers (QUIC clients connections),
3388 * there is no Initial connection IDs storage.
3389 */
3390 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003391 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003392 /* DCIDs of first packets coming from clients may have the same values.
3393 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3394 */
3395 quic_cid_saddr_cat(&pkt->dcid, saddr);
3396 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003397
3398 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3399 end - *buf < token_len) {
3400 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3401 goto err;
3402 }
3403
3404 /* XXX TO DO XXX 0 value means "the token is not present".
3405 * A server which sends an Initial packet must not set the token.
3406 * So, a client which receives an Initial packet with a token
3407 * MUST discard the packet or generate a connection error with
3408 * PROTOCOL_VIOLATION as type.
3409 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3410 */
3411 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003412 }
3413 else {
3414 if (pkt->dcid.len != QUIC_CID_LEN) {
3415 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3416 goto err;
3417 }
3418
3419 cids = &l->rx.cids;
3420 }
3421
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003422 /* Only packets packets with long headers and not RETRY or VERSION as type
3423 * have a length field.
3424 */
3425 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3426 uint64_t len;
3427
3428 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3429 end - *buf < len) {
3430 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3431 goto err;
3432 }
3433
3434 pkt->len = len;
3435 }
3436
3437
3438 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003439 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3440 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3441 cids == &l->rx.odcids) {
3442 /* Switch to the definitive tree ->cids containing the final CIDs. */
3443 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3444 if (node) {
3445 /* If found, signal this with NULL as special value for <cids>. */
3446 pkt->dcid.len = dcid_len;
3447 cids = NULL;
3448 }
3449 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003450 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003451
3452 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003453 int ipv4;
3454 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003455 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003456 const unsigned char *salt = initial_salt_v1;
3457 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003458
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003459 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3460 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3461 goto err;
3462 }
3463
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003464 pkt->saddr = *saddr;
3465 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3466 * of <saddr>.
3467 */
3468 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003469 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003470 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003471 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003472 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003473 goto err;
3474
3475 odcid = &qc->rx.params.original_destination_connection_id;
3476 /* Copy the transport parameters. */
3477 qc->rx.params = l->bind_conf->quic_params;
3478 /* Copy original_destination_connection_id transport parameter. */
3479 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3480 odcid->len = pkt->odcid_len;
3481 /* Copy the initial source connection ID. */
3482 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3483 qc->enc_params_len =
3484 quic_transport_params_encode(qc->enc_params,
3485 qc->enc_params + sizeof qc->enc_params,
3486 &qc->rx.params, 1);
3487 if (!qc->enc_params_len)
3488 goto err;
3489
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003490 /* NOTE: the socket address has been concatenated to the destination ID
3491 * chosen by the client for Initial packets.
3492 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003493 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3494 salt = initial_salt_draft_29;
3495 salt_len = sizeof initial_salt_draft_29;
3496 }
3497 if (!qc_new_isecs(qc, salt, salt_len,
3498 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003499 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3500 goto err;
3501 }
3502
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003503 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003504 /* This is the DCID node sent in this packet by the client. */
3505 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003506 /* Enqueue this packet. */
3507 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3508 /* Try to accept a new connection. */
3509 listener_accept(l);
3510
3511 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3512 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3513 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3514 /* Insert our SCID, the connection ID for the QUIC client. */
3515 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3516 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3517 if (n != &qc->scid_node) {
3518 quic_conn_free(qc);
3519 qc = ebmb_entry(n, struct quic_conn, scid_node);
3520 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003521 }
3522 else {
3523 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3524 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3525 else
3526 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003527 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003528 }
3529 }
3530 else {
3531 if (end - *buf < QUIC_CID_LEN) {
3532 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3533 goto err;
3534 }
3535
3536 cids = &l->rx.cids;
3537 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3538 if (!node) {
3539 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3540 goto err;
3541 }
3542
3543 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003544 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003545 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003546 /* A short packet is the last one of an UDP datagram. */
3547 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003548 }
3549
3550 /* Store the DCID used for this packet to check the packet which
3551 * come in this UDP datagram match with it.
3552 */
3553 if (!dgram_ctx->dcid_node) {
3554 dgram_ctx->dcid_node = node;
3555 dgram_ctx->qc = qc;
3556 }
3557
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003558 /* Increase the total length of this packet by the header length. */
3559 pkt->len += *buf - beg;
3560 /* Do not check the DCID node before the length. */
3561 if (dgram_ctx->dcid_node != node) {
3562 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3563 goto err;
3564 }
3565
3566 if (pkt->len > sizeof pkt->data) {
3567 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3568 goto err;
3569 }
3570
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003571 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3572 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3573 goto err;
3574 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003575
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003576
3577 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003578 /* Wake up the connection packet handler task from here only if all
3579 * the contexts have been initialized, especially the mux context
3580 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3581 * will start it if these contexts for the connection are not already
3582 * initialized.
3583 */
3584 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003585 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003586
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003587 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3588
3589 return pkt->len;
3590
3591 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003592 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003593 qc ? qc->conn : NULL, pkt);
3594 return -1;
3595}
3596
3597/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3598 * in advance. This is the reponsability of the caller to check there is enough room in this
3599 * buffer to build a long header.
3600 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3601 */
3602static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3603 int type, size_t pn_len, struct quic_conn *conn)
3604{
3605 if (type > QUIC_PACKET_TYPE_RETRY)
3606 return 0;
3607
3608 /* #0 byte flags */
3609 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3610 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3611 /* Version */
3612 quic_write_uint32(buf, end, conn->version);
3613 *(*buf)++ = conn->dcid.len;
3614 /* Destination connection ID */
3615 if (conn->dcid.len) {
3616 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3617 *buf += conn->dcid.len;
3618 }
3619 /* Source connection ID */
3620 *(*buf)++ = conn->scid.len;
3621 if (conn->scid.len) {
3622 memcpy(*buf, conn->scid.data, conn->scid.len);
3623 *buf += conn->scid.len;
3624 }
3625
3626 return 1;
3627}
3628
3629/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3630 * in advance. This is the reponsability of the caller to check there is enough room in this
3631 * buffer to build a long header.
3632 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3633 */
3634static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3635 size_t pn_len, struct quic_conn *conn)
3636{
3637 /* #0 byte flags */
3638 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3639 /* Destination connection ID */
3640 if (conn->dcid.len) {
3641 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3642 *buf += conn->dcid.len;
3643 }
3644
3645 return 1;
3646}
3647
3648/* Apply QUIC header protection to the packet with <buf> as first byte address,
3649 * <pn> as address of the Packet number field, <pnlen> being this field length
3650 * with <aead> as AEAD cipher and <key> as secret key.
3651 * Returns 1 if succeeded or 0 if failed.
3652 */
3653static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3654 const EVP_CIPHER *aead, const unsigned char *key)
3655{
3656 int i, ret, outlen;
3657 EVP_CIPHER_CTX *ctx;
3658 /* We need an IV of at least 5 bytes: one byte for bytes #0
3659 * and at most 4 bytes for the packet number
3660 */
3661 unsigned char mask[5] = {0};
3662
3663 ret = 0;
3664 ctx = EVP_CIPHER_CTX_new();
3665 if (!ctx)
3666 return 0;
3667
3668 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3669 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3670 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3671 goto out;
3672
3673 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3674 for (i = 0; i < pnlen; i++)
3675 pn[i] ^= mask[i + 1];
3676
3677 ret = 1;
3678
3679 out:
3680 EVP_CIPHER_CTX_free(ctx);
3681
3682 return ret;
3683}
3684
3685/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3686 * ACK ranges if needed to a value below <limit> in bytes.
3687 * Return 1 if succeeded, 0 if not.
3688 */
3689static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3690{
3691 size_t room, ack_delay_sz;
3692
3693 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3694 /* A frame is made of 1 byte for the frame type. */
3695 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003696 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003697 return 0;
3698
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003699 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003700}
3701
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003702/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3703 * 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 +01003704 * and <*len> the packet Length field initialized with the number of bytes already present
3705 * 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 +02003706 * <headlen> is the number of bytes already present in this packet before building frames.
3707 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003708 * Update consequently <*len> to reflect the size of these frames built
3709 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003710 * Return 1 if succeeded, 0 if not.
3711 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003712static inline int qc_build_frms(struct quic_tx_packet *pkt,
3713 size_t room, size_t *len, size_t headlen,
3714 struct quic_enc_level *qel,
3715 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003716{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003717 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003718 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003719 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003720 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003721
Frédéric Lécailleea604992020-12-24 13:01:37 +01003722 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003723 if (*len > room || headlen > remain)
3724 return 0;
3725
Frédéric Lécailleea604992020-12-24 13:01:37 +01003726 /* If we are not probing we must take into an account the congestion
3727 * control window.
3728 */
3729 if (!conn->tx.nb_pto_dgrams)
3730 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003731 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003732 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003733 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 +01003734 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003735 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003736
Frédéric Lécailleea604992020-12-24 13:01:37 +01003737 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003738 break;
3739
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003740 switch (cf->type) {
3741 case QUIC_FT_CRYPTO:
3742 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3743 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3744 /* Compute the length of this CRYPTO frame header */
3745 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3746 /* Compute the data length of this CRyPTO frame. */
3747 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3748 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3749 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3750 if (!dlen)
3751 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003752
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003753 pkt->cdata_len += dlen;
3754 /* CRYPTO frame length. */
3755 flen = hlen + quic_int_getsize(dlen) + dlen;
3756 TRACE_PROTO(" CRYPTO frame length (flen)",
3757 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3758 /* Add the CRYPTO data length and its encoded length to the packet
3759 * length and the length of this length.
3760 */
3761 *len += flen;
3762 room -= flen;
3763 if (dlen == cf->crypto.len) {
3764 /* <cf> CRYPTO data have been consumed. */
3765 MT_LIST_DELETE_SAFE(tmp1);
3766 LIST_APPEND(&pkt->frms, &cf->list);
3767 }
3768 else {
3769 struct quic_frame *new_cf;
3770
3771 new_cf = pool_alloc(pool_head_quic_frame);
3772 if (!new_cf) {
3773 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3774 return 0;
3775 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003776
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003777 new_cf->type = QUIC_FT_CRYPTO;
3778 new_cf->crypto.len = dlen;
3779 new_cf->crypto.offset = cf->crypto.offset;
3780 new_cf->crypto.qel = qel;
3781 LIST_APPEND(&pkt->frms, &new_cf->list);
3782 /* Consume <dlen> bytes of the current frame. */
3783 cf->crypto.len -= dlen;
3784 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003785 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003786 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003787
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003788 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003789 /* Note that these frames are accepted in short packets only without
3790 * "Length" packet field. Here, <*len> is used only to compute the
3791 * sum of the lengths of the already built frames for this packet.
3792 */
3793 TRACE_PROTO(" New STREAM frame build (room, len)",
3794 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3795 /* Compute the length of this STREAM frame "header" made a all the field
3796 * excepting the variable ones. Note that +1 is for the type of this frame.
3797 */
3798 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003799 ((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 +02003800 /* Compute the data length of this STREAM frame. */
3801 avail_room = room - hlen - *len;
3802 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003803 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003804
3805 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3806 dlen = max_available_room(avail_room, &dlen_sz);
3807 if (dlen > cf->stream.len) {
3808 dlen = cf->stream.len;
3809 }
3810 dlen_sz = quic_int_getsize(dlen);
3811 flen = hlen + dlen_sz + dlen;
3812 }
3813 else {
3814 dlen = QUIC_MIN(avail_room, cf->stream.len);
3815 flen = hlen + dlen;
3816 }
3817 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3818 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3819 TRACE_PROTO(" STREAM frame length (flen)",
3820 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3821 /* Add the STREAM data length and its encoded length to the packet
3822 * length and the length of this length.
3823 */
3824 *len += flen;
3825 room -= flen;
3826 if (dlen == cf->stream.len) {
3827 /* <cf> STREAM data have been consumed. */
3828 MT_LIST_DELETE_SAFE(tmp1);
3829 LIST_APPEND(&pkt->frms, &cf->list);
3830 }
3831 else {
3832 struct quic_frame *new_cf;
3833
3834 new_cf = pool_zalloc(pool_head_quic_frame);
3835 if (!new_cf) {
3836 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3837 return 0;
3838 }
3839
3840 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003841 new_cf->stream.qcs = cf->stream.qcs;
3842 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003843 new_cf->stream.id = cf->stream.id;
3844 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3845 new_cf->stream.offset = cf->stream.offset;
3846 new_cf->stream.len = dlen;
3847 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3848 /* FIN bit reset */
3849 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3850 new_cf->stream.data = cf->stream.data;
3851 LIST_APPEND(&pkt->frms, &new_cf->list);
3852 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3853 /* Consume <dlen> bytes of the current frame. */
3854 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003855 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003856 cf->stream.data += dlen;
3857 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003858 break;
3859
3860 default:
3861 flen = qc_frm_len(cf);
3862 BUG_ON(!flen);
3863 if (flen > room)
3864 continue;
3865
3866 *len += flen;
3867 room -= flen;
3868 MT_LIST_DELETE_SAFE(tmp1);
3869 LIST_APPEND(&pkt->frms, &cf->list);
3870 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003871 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003872 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003873 }
3874
Frédéric Lécailleea604992020-12-24 13:01:37 +01003875 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003876}
3877
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003878/* This function evaluates if <pkt> packet may be built into a buffer with
3879 * <room> as available room. A valid packet should at least contain a valid
3880 * header and at least a frame.
3881 * To estimate the minimal space to build a packet, we consider the worst case:
3882 - there is not enough space to build ack-eliciting frames from
3883 qel->pktns->tx.frms. This is safe to consider this because when we build
3884 a packet we first build the ACK frames, then the ack-eliciting frames
3885 from qel->pktns->tx.frms only if there is enough space for these
3886 ack-eliciting frames, finally PING and PADDING frames if needed,
3887 - we have to ensure there is enough space to build an ACK frame if required,
3888 and a PING frame, even if we do not have to probe,
3889 - we also have to verify there is enough space to build a PADDING frame
3890 if needed, especially if there is no need to send an ACK frame.
3891 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3892 * a valid packet).
3893 */
3894static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3895 int ack, int nb_pto_dgrams,
3896 struct quic_enc_level *qel, struct quic_conn *conn)
3897{
3898 size_t minlen, token_fields_len;
3899 /* XXX FIXME XXX : ack delay not supported */
3900 uint64_t ack_delay = 0;
3901 size_t ack_frm_len = 0;
3902
3903 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3904 conn->conn, NULL, NULL, &room);
3905 /* When we do not have to probe nor send acks either, we must take into
3906 * an account the data which have already been prepared and limit
3907 * the size of this packet. We will potentially build an ack-eliciting
3908 * packet.
3909 */
3910 if (!nb_pto_dgrams && !ack) {
3911 size_t path_room;
3912
3913 path_room = quic_path_prep_data(conn->path);
3914 if (room > path_room)
3915 room = path_room;
3916 }
3917
3918 if (ack)
3919 /* A frame is made of 1 byte for the frame type. */
3920 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3921
3922 /* XXX FIXME XXX : token not supported */
3923 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3924 /* Check there is enough room to build the header followed by a token,
3925 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3926 * encryption tag is also taken into an account. Note that we have no
3927 * knowledge of the packet number for this packet. It must be atomically
3928 * incremented each time a packet is built. But before building a packet
3929 * we must estimate if it may be built if we do not want to consume a packet
3930 * number for nothing! Note that we add 1 byte more to
3931 * <minlen> to be able to build an ack-eliciting packet when probing without
3932 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3933 * PING frame.
3934 */
3935 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3936 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3937 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3938 + token_fields_len;
3939 else
3940 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3941
3942 /* Consider any PADDING frame to add */
3943 if (objt_server(conn->conn->target) &&
3944 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3945 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3946 /* Pad too short client Initial packet */
3947 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3948 }
3949 else if (!ack) {
3950 /* Consider we will have to add the longest short PADDING frame to
3951 * protect a 1-byte length packet number.
3952 */
3953 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3954 }
3955
3956 if (room < minlen) {
3957 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3958 conn->conn, NULL, NULL, &room);
3959 return 0;
3960 }
3961
3962 return 1;
3963}
3964
3965/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003966 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3967 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3968 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003969 * 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 +02003970 * 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 +02003971 * having returned from this function.
3972 * 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 +01003973 * number field in this packet. <pn_len> will also have the packet number
3974 * length as value.
3975 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003976 * Always succeeds: this is the responsability of the caller to ensure there is
3977 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003978 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003979static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003980 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003981 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3982 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003983{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003984 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003985 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3987 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003988 size_t ack_frm_len;
3989 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003990 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003991
Frédéric Lécailleea604992020-12-24 13:01:37 +01003992 /* Length field value with CRYPTO frames if present. */
3993 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003994 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003995 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003996 * the congestion controller window. So, we do not limit the size of this
3997 * packet if we have an ACK frame to send because an ACK frame is not
3998 * ack-eliciting. This size will be limited if we have ack-eliciting
3999 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004000 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004001 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004002 size_t path_room;
4003
4004 path_room = quic_path_prep_data(conn->path);
4005 if (end - beg > path_room)
4006 end = beg + path_room;
4007 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004008
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004009 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004010 /* packet number length */
4011 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004012 /* Build the header */
4013 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004014 quic_build_packet_short_header(&pos, end, *pn_len, conn);
4015 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004016 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
4017 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4018 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004019 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004020 /* Ensure there is enough room for the TLS encryption tag */
4021 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004022 /* Build an ACK frame if required. */
4023 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004024 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004025 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004026 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004027 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4028 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4029 * that from here, we do not know if we will have to send a PING frame.
4030 * This will be decided after having computed the ack-eliciting frames
4031 * to be added to this packet.
4032 */
4033 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 +01004034 if (!ack_frm_len) {
4035 ssize_t room = end - pos;
4036 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4037 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004038 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004039 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004040 }
4041
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004042 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004043 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004044 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004045 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004046
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004047 /* Initialize the length of the frames built below to <len>.
4048 * If any frame could be successfully built by qc_build_frms(),
4049 * we will have len_frms > len.
4050 */
4051 len_frms = len;
4052 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004053 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4054 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004055 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004056
4057 add_ping_frm = 0;
4058 padding_len = 0;
4059 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004060 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004061 len < QUIC_INITIAL_PACKET_MINLEN) {
4062 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4063 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004064 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004065 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004066 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004067 add_ping_frm = 1;
4068 len += 1;
4069 }
4070 /* If there is no frame at all to follow, add at least a PADDING frame. */
4071 if (!ack_frm_len)
4072 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4073 }
4074
4075 /* Length (of the remaining data). Must not fail because, the buffer size
4076 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004077 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004078 * of this packet.
4079 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004080 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004081 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004082 else
4083 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004084 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004085 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004086
4087 /* Packet number field address. */
4088 *buf_pn = pos;
4089
4090 /* Packet number encoding. */
4091 quic_packet_number_encode(&pos, end, pn, *pn_len);
4092
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004093 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4094 ssize_t room = end - pos;
4095 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4096 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004097 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004098 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004099
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004100 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004101 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004102 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004103
4104 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004105 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004106 ssize_t room = end - pos;
4107 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4108 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004109 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004110 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004111 }
4112 }
4113
4114 /* Build a PING frame if needed. */
4115 if (add_ping_frm) {
4116 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004117 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4118 ssize_t room = end - pos;
4119 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4120 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004121 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004122 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004123 }
4124
4125 /* Build a PADDING frame if needed. */
4126 if (padding_len) {
4127 frm.type = QUIC_FT_PADDING;
4128 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004129 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4130 ssize_t room = end - pos;
4131 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4132 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004133 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004134 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004135 }
4136
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004137 /* Always reset this variable as this function has no idea
4138 * if it was set. It is handle by the loss detection timer.
4139 */
4140 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004141 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004142}
4143
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004144static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004145{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004146 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004147 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004148 pkt->cdata_len = 0;
4149 pkt->in_flight_len = 0;
4150 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004151 pkt->next = NULL;
4152 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004153}
4154
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004155/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004156static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4157{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004158 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004159
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004160 if (!pkt)
4161 return;
4162
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004163 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004164 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004165 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004166 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004167 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168}
4169
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004170/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4171 * type for <qc> QUIC connection from <qel> encryption level.
4172 * Return -2 if the packet could not be allocated or encrypted for any reason,
4173 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004174 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004175static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4176 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004177 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004178 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004179 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004180{
4181 /* The pointer to the packet number field. */
4182 unsigned char *buf_pn;
4183 unsigned char *beg, *end, *payload;
4184 int64_t pn;
4185 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004186 struct quic_tls_ctx *tls_ctx;
4187 struct quic_tx_packet *pkt;
4188
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004189 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004190 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004191 pkt = pool_alloc(pool_head_quic_tx_packet);
4192 if (!pkt) {
4193 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 +02004194 *err = -2;
4195 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196 }
4197
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004198 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004199 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004200 pn_len = 0;
4201 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004202 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004203 *err = -1;
4204 goto err;
4205 }
4206
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004207 /* Consume a packet number. */
4208 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004209 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 +01004210
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004211 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004212 payload = buf_pn + pn_len;
4213 payload_len = end - payload;
4214 aad_len = payload - beg;
4215
4216 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004217 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4218 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004219 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004220 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004221
4222 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004223 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004224 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4225 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4226 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004227 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004228 goto err;
4229 }
4230
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004231 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4232 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004233 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004234 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004235 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004236 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004237 pkt->in_flight_len = pkt->len;
4238 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004239 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004241 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4242
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004243 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004244
4245 err:
4246 free_quic_tx_packet(pkt);
4247 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004248 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004249}
4250
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004251/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4252 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004254static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4255 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004256{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004257 size_t try, done = 0;
4258
4259 if (!conn_ctrl_ready(conn))
4260 return 0;
4261
4262 if (!fd_recv_ready(conn->handle.fd))
4263 return 0;
4264
4265 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004266
4267 /* read the largest possible block. For this, we perform only one call
4268 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004269 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004270 */
4271 while (count > 0) {
4272 try = b_contig_space(buf);
4273 if (!try)
4274 break;
4275
4276 if (try > count)
4277 try = count;
4278
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004279 b_add(buf, try);
4280 done += try;
4281 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004282 }
4283
4284 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4285 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4286
4287 leave:
4288 return done;
4289
4290 read0:
4291 conn_sock_read0(conn);
4292 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4293
4294 /* Now a final check for a possible asynchronous low-level error
4295 * report. This can happen when a connection receives a reset
4296 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4297 * we might have come from there by just checking POLL_HUP instead
4298 * of recv()'s return value 0, so we have no way to tell there was
4299 * an error without checking.
4300 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004301 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004302 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4303 goto leave;
4304}
4305
4306
4307/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4308 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4309 * other pending data for example, but this flag is ignored at the moment.
4310 * Only one call to send() is performed, unless the buffer wraps, in which case
4311 * a second call may be performed. The connection's flags are updated with
4312 * whatever special event is detected (error, empty). The caller is responsible
4313 * for taking care of those events and avoiding the call if inappropriate. The
4314 * function does not call the connection's polling update function, so the caller
4315 * is responsible for this. It's up to the caller to update the buffer's contents
4316 * based on the return value.
4317 */
4318static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4319{
4320 ssize_t ret;
4321 size_t try, done;
4322 int send_flag;
4323
4324 if (!conn_ctrl_ready(conn))
4325 return 0;
4326
4327 if (!fd_send_ready(conn->handle.fd))
4328 return 0;
4329
4330 done = 0;
4331 /* send the largest possible block. For this we perform only one call
4332 * to send() unless the buffer wraps and we exactly fill the first hunk,
4333 * in which case we accept to do it once again.
4334 */
4335 while (count) {
4336 try = b_contig_data(buf, done);
4337 if (try > count)
4338 try = count;
4339
4340 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4341 if (try < count || flags & CO_SFL_MSG_MORE)
4342 send_flag |= MSG_MORE;
4343
4344 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4345 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4346 if (ret > 0) {
4347 count -= ret;
4348 done += ret;
4349
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004350 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004351 conn->flags |= CO_FL_WAIT_L4L6;
4352 /* if the system buffer is full, don't insist */
4353 if (ret < try)
4354 break;
4355 }
4356 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4357 /* nothing written, we need to poll for write first */
4358 fd_cant_send(conn->handle.fd);
4359 break;
4360 }
4361 else if (errno != EINTR) {
4362 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4363 break;
4364 }
4365 }
4366 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4367 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4368
4369 if (done > 0) {
4370 /* we count the total bytes sent, and the send rate for 32-byte
4371 * blocks. The reason for the latter is that freq_ctr are
4372 * limited to 4GB and that it's not enough per second.
4373 */
4374 _HA_ATOMIC_ADD(&global.out_bytes, done);
4375 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4376 }
4377 return done;
4378}
4379
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004380/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4381 * event subscriber <es> is not allowed to change from a previous call as long
4382 * as at least one event is still subscribed. The <event_type> must only be a
4383 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4384 */
4385static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4386{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004387 struct qcc *qcc = conn->qc->qcc;
4388
4389 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4390 BUG_ON(qcc->subs && qcc->subs != es);
4391
4392 es->events |= event_type;
4393 qcc->subs = es;
4394
4395 if (event_type & SUB_RETRY_RECV)
4396 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc);
4397
4398 if (event_type & SUB_RETRY_SEND)
4399 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc);
4400
4401 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004402}
4403
4404/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4405 * The <es> pointer is not allowed to differ from the one passed to the
4406 * subscribe() call. It always returns zero.
4407 */
4408static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4409{
4410 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4411}
4412
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004413/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4414 * connection with <xprt_ctx> as address of the xprt context.
4415 * Returns 1 if succeeded, 0 if not.
4416 */
4417static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4418{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004419 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004420
4421 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4422
4423 if (*xprt_ctx)
4424 goto out;
4425
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004426 ctx = pool_alloc(pool_head_quic_conn_ctx);
4427 if (!ctx) {
4428 conn->err_code = CO_ER_SYS_MEMLIM;
4429 goto err;
4430 }
4431
4432 ctx->wait_event.tasklet = tasklet_new();
4433 if (!ctx->wait_event.tasklet) {
4434 conn->err_code = CO_ER_SYS_MEMLIM;
4435 goto err;
4436 }
4437
4438 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4439 ctx->wait_event.tasklet->context = ctx;
4440 ctx->wait_event.events = 0;
4441 ctx->conn = conn;
4442 ctx->subs = NULL;
4443 ctx->xprt_ctx = NULL;
4444
4445 ctx->xprt = xprt_get(XPRT_QUIC);
4446 if (objt_server(conn->target)) {
4447 /* Server */
4448 struct server *srv = __objt_server(conn->target);
4449 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004450 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004451 int ssl_err, ipv4;
4452
4453 ssl_err = SSL_ERROR_NONE;
4454 if (RAND_bytes(dcid, sizeof dcid) != 1)
4455 goto err;
4456
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004458 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004459 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004460 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004461 goto err;
4462
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004463 /* Insert our SCID, the connection ID for the QUIC client. */
4464 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4465
4466 conn->qc = qc;
4467 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004468 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4469 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004470 goto err;
4471
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004472 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4473 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4474 goto err;
4475
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004476 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004477 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004478 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4479 qc->enc_params_len =
4480 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4481 &qc->rx.params, 0);
4482 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004483 goto err;
4484
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004485 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004486 SSL_set_connect_state(ctx->ssl);
4487 ssl_err = SSL_do_handshake(ctx->ssl);
4488 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004489 int st;
4490
4491 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004492 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4493 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004494 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004495 }
4496 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004497 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498 goto err;
4499 }
4500 }
4501 }
4502 else if (objt_listener(conn->target)) {
4503 /* Listener */
4504 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004505 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004506
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004507 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004508 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4509 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4510 goto err;
4511
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004512 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004513 SSL_set_accept_state(ctx->ssl);
4514 }
4515
4516 *xprt_ctx = ctx;
4517
4518 /* Leave init state and start handshake */
4519 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004520
4521 out:
4522 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4523
4524 return 0;
4525
4526 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004527 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004528 tasklet_free(ctx->wait_event.tasklet);
4529 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004530 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004531 return -1;
4532}
4533
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004534/* Start the QUIC transport layer */
4535static int qc_xprt_start(struct connection *conn, void *ctx)
4536{
4537 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004538 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004539
4540 qc = conn->qc;
4541 if (!quic_conn_init_timer(qc)) {
4542 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4543 return 0;
4544 }
4545
4546 tasklet_wakeup(qctx->wait_event.tasklet);
4547 return 1;
4548}
4549
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004550/* transport-layer operations for QUIC connections. */
4551static struct xprt_ops ssl_quic = {
4552 .snd_buf = quic_conn_from_buf,
4553 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004554 .subscribe = quic_conn_subscribe,
4555 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004556 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004557 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004558 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4559 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4560 .name = "QUIC",
4561};
4562
4563__attribute__((constructor))
4564static void __quic_conn_init(void)
4565{
4566 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4567 xprt_register(XPRT_QUIC, &ssl_quic);
4568}
4569
4570__attribute__((destructor))
4571static void __quic_conn_deinit(void)
4572{
4573 BIO_meth_free(ha_quic_meth);
4574}
4575
4576/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4577 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4578 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004579 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004580 */
4581static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4582 struct sockaddr_storage *saddr, qpkt_read_func *func)
4583{
4584 unsigned char *pos;
4585 const unsigned char *end;
4586 struct quic_dgram_ctx dgram_ctx = {
4587 .dcid_node = NULL,
4588 .owner = owner,
4589 };
4590
4591 pos = (unsigned char *)buf;
4592 end = pos + len;
4593
4594 do {
4595 int ret;
4596 struct quic_rx_packet *pkt;
4597
Willy Tarreaue4498932021-03-22 21:13:05 +01004598 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004599 if (!pkt)
4600 goto err;
4601
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004602 quic_rx_packet_refinc(pkt);
4603 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4604 if (ret == -1) {
4605 size_t pkt_len;
4606
4607 pkt_len = pkt->len;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02004608 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004609 /* If the packet length could not be found, we cannot continue. */
4610 if (!pkt_len)
4611 break;
4612 }
4613 } while (pos < end);
4614
4615 /* Increasing the received bytes counter by the UDP datagram length
4616 * if this datagram could be associated to a connection.
4617 */
4618 if (dgram_ctx.qc)
4619 dgram_ctx.qc->rx.bytes += len;
4620
4621 return pos - (unsigned char *)buf;
4622
4623 err:
4624 return -1;
4625}
4626
4627ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4628 struct sockaddr_storage *saddr)
4629{
4630 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4631}
4632
4633ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4634 struct sockaddr_storage *saddr)
4635{
4636 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4637}
4638
4639/* QUIC I/O handler for connection to local listeners or remove servers
4640 * depending on <listener> boolean value, with <fd> as socket file
4641 * descriptor and <ctx> as context.
4642 */
4643static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4644{
4645 ssize_t ret;
4646 size_t done = 0;
4647 struct buffer *buf = get_trash_chunk();
4648 /* Source address */
4649 struct sockaddr_storage saddr = {0};
4650 socklen_t saddrlen = sizeof saddr;
4651
4652 if (!fd_recv_ready(fd))
4653 return 0;
4654
4655 do {
4656 ret = recvfrom(fd, buf->area, buf->size, 0,
4657 (struct sockaddr *)&saddr, &saddrlen);
4658 if (ret < 0) {
4659 if (errno == EINTR)
4660 continue;
4661 if (errno == EAGAIN)
4662 fd_cant_recv(fd);
4663 goto out;
4664 }
4665 } while (0);
4666
4667 done = buf->data = ret;
4668 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4669
4670 out:
4671 return done;
4672}
4673
4674/* QUIC I/O handler for connections to local listeners with <fd> as socket
4675 * file descriptor.
4676 */
4677void quic_fd_handler(int fd)
4678{
Willy Tarreauf5090652021-04-06 17:23:40 +02004679 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004680 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4681}
4682
4683/* QUIC I/O handler for connections to remote servers with <fd> as socket
4684 * file descriptor.
4685 */
4686void quic_conn_fd_handler(int fd)
4687{
Willy Tarreauf5090652021-04-06 17:23:40 +02004688 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004689 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4690}
4691
4692/*
4693 * Local variables:
4694 * c-indent-level: 8
4695 * c-basic-offset: 8
4696 * End:
4697 */