blob: d073fb1f36b7d30c9fbf4cd27465d52e5db9a82f [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écaillea7e7ce92020-11-23 14:14:04 +01002597 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002598 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002599 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2600 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002601 MT_LIST_DELETE_SAFE(pkttmp1);
2602 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002603 }
2604
2605 out:
2606 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2607}
2608
2609/* Process all the CRYPTO frame at <el> encryption level.
2610 * Return 1 if succeeded, 0 if not.
2611 */
2612static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002613 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002614{
2615 struct eb64_node *node;
2616
2617 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002618 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002619 node = eb64_first(&el->rx.crypto.frms);
2620 while (node) {
2621 struct quic_rx_crypto_frm *cf;
2622
2623 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2624 if (cf->offset_node.key != el->rx.crypto.offset)
2625 break;
2626
2627 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2628 goto err;
2629
2630 node = eb64_next(node);
2631 quic_rx_packet_refdec(cf->pkt);
2632 eb64_delete(&cf->offset_node);
2633 pool_free(pool_head_quic_rx_crypto_frm, cf);
2634 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002635 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002636 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2637 return 1;
2638
2639 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002640 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002641 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2642 return 0;
2643}
2644
2645/* Process all the packets at <el> encryption level.
2646 * Return 1 if succeeded, 0 if not.
2647 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002648int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2649 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002650{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002651 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002652 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002653 struct quic_conn *qc = ctx->conn->qc;
2654 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002655
2656 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002657 qel = cur_el;
2658 next_tel:
2659 if (!qel)
2660 goto out;
2661
2662 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2663 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002664 while (node) {
2665 struct quic_rx_packet *pkt;
2666
2667 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002668 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2669 ctx->conn, pkt, NULL, ctx->ssl);
2670 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002671 /* Drop the packet */
2672 TRACE_PROTO("packet decryption failed -> dropped",
2673 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2674 }
2675 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002676 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002677 /* Drop the packet */
2678 TRACE_PROTO("packet parsing failed -> dropped",
2679 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2680 }
2681 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002682 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2683
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002684 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002685 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2686 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002687 if (pkt->pn > largest_pn)
2688 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002689 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002690 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002691 TRACE_DEVEL("Could not update ack range list",
2692 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002693 }
2694 }
2695 node = eb64_next(node);
2696 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002698 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002699
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002700 /* Update the largest packet number. */
2701 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002702 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2703 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002704 goto err;
2705
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002706 if (qel == cur_el) {
2707 qel = next_el;
2708 goto next_tel;
2709 }
2710
2711 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002712 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2713 return 1;
2714
2715 err:
2716 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2717 return 0;
2718}
2719
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002720/* QUIC connection packet handler task. */
2721struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002722{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002723 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002724 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002725 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002726 enum quic_tls_enc_level tel, next_tel;
2727 struct quic_enc_level *qel, *next_qel;
2728 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002729 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002730 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002731
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002732 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002733 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002734 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002735 st = HA_ATOMIC_LOAD(&qc->state);
2736 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002737 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002738 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002739 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002740 goto err;
2741
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002742 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002743 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002744
2745 next_level:
2746 tls_ctx = &qel->tls_ctx;
2747
2748 /* If the header protection key for this level has been derived,
2749 * remove the packet header protections.
2750 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002751 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2753 qc_rm_hp_pkts(qel, ctx);
2754
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002755 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002756 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2757 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2758 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002759 goto err;
2760
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002761 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002762 if (st >= QUIC_HS_ST_COMPLETE &&
2763 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002764 /* Discard the Handshake keys. */
2765 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002766 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002767 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2768 qc_set_timer(ctx);
2769 if (!quic_build_post_handshake_frames(qc))
2770 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002771 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002772 }
2773
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002774 if (!qr)
2775 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2776 ret = qc_prep_hdshk_pkts(qr, ctx);
2777 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002778 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002779 else if (ret == 0)
2780 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002781
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002782 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002783 goto err;
2784
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002785 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002786 /* Check if there is something to do for the next level.
2787 */
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002788 if (next_qel && (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002789 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002790 qel = next_qel;
2791 goto next_level;
2792 }
2793
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002794 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002795 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002796 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002797
2798 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002799 if (qr)
2800 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002801 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002802 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002803}
2804
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002805/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002806static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2807{
2808 int i;
2809
2810 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2811 if (qel->tx.crypto.bufs[i]) {
2812 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2813 qel->tx.crypto.bufs[i] = NULL;
2814 }
2815 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002816 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002817}
2818
2819/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002820 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002821 * Returns 1 if succeeded, 0 if not.
2822 */
2823static int quic_conn_enc_level_init(struct quic_conn *qc,
2824 enum quic_tls_enc_level level)
2825{
2826 struct quic_enc_level *qel;
2827
2828 qel = &qc->els[level];
2829 qel->level = quic_to_ssl_enc_level(level);
2830 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2831 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2832 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2833 qel->tls_ctx.rx.flags = 0;
2834 qel->tls_ctx.tx.flags = 0;
2835
2836 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002837 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002838 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002839 qel->rx.crypto.offset = 0;
2840 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2841 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002842
2843 /* Allocate only one buffer. */
2844 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2845 if (!qel->tx.crypto.bufs)
2846 goto err;
2847
2848 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2849 if (!qel->tx.crypto.bufs[0])
2850 goto err;
2851
2852 qel->tx.crypto.bufs[0]->sz = 0;
2853 qel->tx.crypto.nb_buf = 1;
2854
2855 qel->tx.crypto.sz = 0;
2856 qel->tx.crypto.offset = 0;
2857
2858 return 1;
2859
2860 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002861 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002862 return 0;
2863}
2864
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002865/* Release all the memory allocated for <conn> QUIC connection. */
2866static void quic_conn_free(struct quic_conn *conn)
2867{
2868 int i;
2869
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002870 if (!conn)
2871 return;
2872
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002873 free_quic_conn_cids(conn);
2874 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2875 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876 if (conn->timer_task)
2877 task_destroy(conn->timer_task);
2878 pool_free(pool_head_quic_conn, conn);
2879}
2880
2881/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002882static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002884 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002885 struct quic_conn *qc;
2886 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002887 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002888
2889 conn_ctx = task->context;
2890 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002891 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2892 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002893 task->expire = TICK_ETERNITY;
2894 pktns = quic_loss_pktns(qc);
2895 if (tick_isset(pktns->tx.loss_time)) {
2896 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2897
2898 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2899 if (!LIST_ISEMPTY(&lost_pkts))
2900 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2901 qc_set_timer(conn_ctx);
2902 goto out;
2903 }
2904
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002905 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002906 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002907 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002908 pktns->tx.pto_probe = 1;
2909 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002910 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002911 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2912 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2913
2914 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2915 hel->pktns->tx.pto_probe = 1;
2916 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2917 iel->pktns->tx.pto_probe = 1;
2918 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002919 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002920 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2921 qc->path->loss.pto_count++;
2922
2923 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002924 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002925
2926 return task;
2927}
2928
2929/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2930 * connections used to identify the first Initial packets of client connecting
2931 * to listeners. This parameter must be NULL for QUIC connections attached
2932 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2933 * <scid> is the source connection ID with <scid_len> as length.
2934 * Returns 1 if succeeded, 0 if not.
2935 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002936static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2937 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002938 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002939{
2940 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002941 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002942 /* Initial CID. */
2943 struct quic_connection_id *icid;
2944
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002945 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002946 qc = pool_zalloc(pool_head_quic_conn);
2947 if (!qc) {
2948 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2949 goto err;
2950 }
2951
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002952 qc->cids = EB_ROOT;
2953 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002954 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002955 struct listener *l = owner;
2956
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002957 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002958 /* Copy the initial DCID. */
2959 qc->odcid.len = dcid_len;
2960 if (qc->odcid.len)
2961 memcpy(qc->odcid.data, dcid, dcid_len);
2962
2963 /* Copy the SCID as our DCID for this connection. */
2964 if (scid_len)
2965 memcpy(qc->dcid.data, scid, scid_len);
2966 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002967 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002968 }
2969 /* QUIC Client (outgoing connection to servers) */
2970 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002971 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002972 if (dcid_len)
2973 memcpy(qc->dcid.data, dcid, dcid_len);
2974 qc->dcid.len = dcid_len;
2975 }
2976
2977 /* Initialize the output buffer */
2978 qc->obuf.pos = qc->obuf.data;
2979
2980 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002981 if (!icid) {
2982 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2983 goto err;
2984 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002985
2986 /* Select our SCID which is the first CID with 0 as sequence number. */
2987 qc->scid = icid->cid;
2988
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989 /* Packet number spaces initialization. */
2990 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2991 quic_pktns_init(&qc->pktns[i]);
2992 /* QUIC encryption level context initialization. */
2993 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002994 if (!quic_conn_enc_level_init(qc, i)) {
2995 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002996 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002997 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002998 /* Initialize the packet number space. */
2999 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3000 }
3001
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003002 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003003 /* TX part. */
3004 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003005 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3006 qc->tx.wbuf = qc->tx.rbuf = 0;
3007 qc->tx.bytes = 0;
3008 qc->tx.nb_pto_dgrams = 0;
3009 /* RX part. */
3010 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003011 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003012
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013 /* XXX TO DO: Only one path at this time. */
3014 qc->path = &qc->paths[0];
3015 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3016
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003017 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003018
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003019 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020
3021 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003022 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003023 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003024 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025}
3026
3027/* Initialize the timer task of <qc> QUIC connection.
3028 * Returns 1 if succeeded, 0 if not.
3029 */
3030static int quic_conn_init_timer(struct quic_conn *qc)
3031{
3032 qc->timer_task = task_new(MAX_THREADS_MASK);
3033 if (!qc->timer_task)
3034 return 0;
3035
3036 qc->timer = TICK_ETERNITY;
3037 qc->timer_task->process = process_timer;
3038 qc->timer_task->context = qc->conn->xprt_ctx;
3039
3040 return 1;
3041}
3042
3043/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3044 * past one byte of this buffer.
3045 */
3046static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3047 struct quic_rx_packet *pkt)
3048{
3049 unsigned char dcid_len, scid_len;
3050
3051 /* Version */
3052 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3053 return 0;
3054
3055 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
3056
3057 /* Destination Connection ID Length */
3058 dcid_len = *(*buf)++;
3059 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3060 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3061 /* XXX MUST BE DROPPED */
3062 return 0;
3063
3064 if (dcid_len) {
3065 /* Check that the length of this received DCID matches the CID lengths
3066 * of our implementation for non Initials packets only.
3067 */
3068 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3069 return 0;
3070
3071 memcpy(pkt->dcid.data, *buf, dcid_len);
3072 }
3073
3074 pkt->dcid.len = dcid_len;
3075 *buf += dcid_len;
3076
3077 /* Source Connection ID Length */
3078 scid_len = *(*buf)++;
3079 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3080 /* XXX MUST BE DROPPED */
3081 return 0;
3082
3083 if (scid_len)
3084 memcpy(pkt->scid.data, *buf, scid_len);
3085 pkt->scid.len = scid_len;
3086 *buf += scid_len;
3087
3088 return 1;
3089}
3090
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003091/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3092 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3093 * encryption level matching with the packet type. <*qel> may be null if not found.
3094 * Note that <ctx> may be null (for Initial packets).
3095 */
3096static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003097 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003098 struct quic_enc_level **qel)
3099{
3100 enum quic_tls_enc_level tel;
3101
3102 /* Special case without connection context (firt Initial packets) */
3103 if (!ctx) {
3104 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3105 return 1;
3106 }
3107
3108 tel = quic_packet_type_enc_level(pkt->type);
3109 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3110 *qel = NULL;
3111 return 0;
3112 }
3113
3114 *qel = &qc->els[tel];
3115 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3116 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3117 return 0;
3118 }
3119
3120 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003121 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3122 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003123 return 1;
3124
3125 return 0;
3126}
3127
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003128/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3129 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3130 * byte past the end of the buffer containing this packet and <beg> the address of
3131 * the packet first byte.
3132 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3133 * Returns 1 if succeeded, 0 if not.
3134 */
3135static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3136 unsigned char **buf, unsigned char *beg,
3137 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003138 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003139{
3140 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003141 struct quic_enc_level *qel;
3142 /* Only for traces. */
3143 struct quic_rx_packet *qpkt_trace;
3144
3145 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003146 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003147 /* The packet number is here. This is also the start minus
3148 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3149 * protection.
3150 */
3151 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003152 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003153 /* Note that the following function enables us to unprotect the packet
3154 * number and its length subsequently used to decrypt the entire
3155 * packets.
3156 */
3157 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3158 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003159 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003160 goto err;
3161 }
3162
3163 /* The AAD includes the packet number field found at <pn>. */
3164 pkt->aad_len = pn - beg + pkt->pnl;
3165 qpkt_trace = pkt;
3166 /* Store the packet */
3167 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003168 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003169 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003170 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003171 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003172 else if (qel) {
3173 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003174 pkt->pn_offset = pn - beg;
3175 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3176 }
3177
3178 memcpy(pkt->data, beg, pkt->len);
3179 /* Updtate the offset of <*buf> for the next QUIC packet. */
3180 *buf = beg + pkt->len;
3181
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003182 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003183 return 1;
3184
3185 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003186 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 +01003187 return 0;
3188}
3189
3190/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3191 * Also set <*long_header> to 1 if this form is long, 0 if not.
3192 */
3193static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3194 unsigned char byte0, int *long_header)
3195{
3196 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3197 pkt->type =
3198 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3199 *long_header = 1;
3200 }
3201 else {
3202 pkt->type = QUIC_PACKET_TYPE_SHORT;
3203 *long_header = 0;
3204 }
3205}
3206
3207static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3208 struct quic_rx_packet *pkt,
3209 struct quic_dgram_ctx *dgram_ctx,
3210 struct sockaddr_storage *saddr)
3211{
3212 unsigned char *beg;
3213 uint64_t len;
3214 struct quic_conn *qc;
3215 struct eb_root *cids;
3216 struct ebmb_node *node;
3217 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003218 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003219 int long_header;
3220
3221 qc = NULL;
3222 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3223 if (end <= *buf)
3224 goto err;
3225
3226 /* Fixed bit */
3227 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3228 /* XXX TO BE DISCARDED */
3229 goto err;
3230
3231 srv_conn = dgram_ctx->owner;
3232 beg = *buf;
3233 /* Header form */
3234 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3235 if (long_header) {
3236 size_t cid_lookup_len;
3237
3238 if (!quic_packet_read_long_header(buf, end, pkt))
3239 goto err;
3240
3241 /* For Initial packets, and for servers (QUIC clients connections),
3242 * there is no Initial connection IDs storage.
3243 */
3244 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3245 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3246 cid_lookup_len = pkt->dcid.len;
3247 }
3248 else {
3249 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3250 cid_lookup_len = QUIC_CID_LEN;
3251 }
3252
3253 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3254 if (!node)
3255 goto err;
3256
3257 qc = ebmb_entry(node, struct quic_conn, scid_node);
3258
3259 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3260 qc->dcid.len = pkt->scid.len;
3261 if (pkt->scid.len)
3262 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3263 }
3264
3265 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3266 uint64_t token_len;
3267
3268 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3269 goto err;
3270
3271 /* XXX TO DO XXX 0 value means "the token is not present".
3272 * A server which sends an Initial packet must not set the token.
3273 * So, a client which receives an Initial packet with a token
3274 * MUST discard the packet or generate a connection error with
3275 * PROTOCOL_VIOLATION as type.
3276 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3277 */
3278 pkt->token_len = token_len;
3279 }
3280 }
3281 else {
3282 /* XXX TO DO: Short header XXX */
3283 if (end - *buf < QUIC_CID_LEN)
3284 goto err;
3285
3286 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3287 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3288 if (!node)
3289 goto err;
3290
3291 qc = ebmb_entry(node, struct quic_conn, scid_node);
3292 *buf += QUIC_CID_LEN;
3293 }
3294 /* Store the DCID used for this packet to check the packet which
3295 * come in this UDP datagram match with it.
3296 */
3297 if (!dgram_ctx->dcid_node)
3298 dgram_ctx->dcid_node = node;
3299 /* Only packets packets with long headers and not RETRY or VERSION as type
3300 * have a length field.
3301 */
3302 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3303 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3304 goto err;
3305
3306 pkt->len = len;
3307 }
3308 else if (!long_header) {
3309 /* A short packet is the last one of an UDP datagram. */
3310 pkt->len = end - *buf;
3311 }
3312
3313 conn_ctx = qc->conn->xprt_ctx;
3314
3315 /* Increase the total length of this packet by the header length. */
3316 pkt->len += *buf - beg;
3317 /* Do not check the DCID node before the length. */
3318 if (dgram_ctx->dcid_node != node) {
3319 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3320 goto err;
3321 }
3322
3323 if (pkt->len > sizeof pkt->data) {
3324 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3325 goto err;
3326 }
3327
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003328 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003329 goto err;
3330
3331 /* Wake the tasklet of the QUIC connection packet handler. */
3332 if (conn_ctx)
3333 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3334
3335 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3336
3337 return pkt->len;
3338
3339 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003340 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003341 return -1;
3342}
3343
3344static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3345 struct quic_rx_packet *pkt,
3346 struct quic_dgram_ctx *dgram_ctx,
3347 struct sockaddr_storage *saddr)
3348{
3349 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003350 struct quic_conn *qc;
3351 struct eb_root *cids;
3352 struct ebmb_node *node;
3353 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003354 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003355 int long_header = 0;
3356
3357 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003358 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003359 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003360 if (end <= *buf)
3361 goto err;
3362
3363 /* Fixed bit */
3364 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3365 /* XXX TO BE DISCARDED */
3366 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3367 goto err;
3368 }
3369
3370 l = dgram_ctx->owner;
3371 beg = *buf;
3372 /* Header form */
3373 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3374 if (long_header) {
3375 unsigned char dcid_len;
3376
3377 if (!quic_packet_read_long_header(buf, end, pkt)) {
3378 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3379 goto err;
3380 }
3381
3382 dcid_len = pkt->dcid.len;
3383 /* For Initial packets, and for servers (QUIC clients connections),
3384 * there is no Initial connection IDs storage.
3385 */
3386 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003387 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003388 /* DCIDs of first packets coming from clients may have the same values.
3389 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3390 */
3391 quic_cid_saddr_cat(&pkt->dcid, saddr);
3392 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003393
3394 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3395 end - *buf < token_len) {
3396 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3397 goto err;
3398 }
3399
3400 /* XXX TO DO XXX 0 value means "the token is not present".
3401 * A server which sends an Initial packet must not set the token.
3402 * So, a client which receives an Initial packet with a token
3403 * MUST discard the packet or generate a connection error with
3404 * PROTOCOL_VIOLATION as type.
3405 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3406 */
3407 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003408 }
3409 else {
3410 if (pkt->dcid.len != QUIC_CID_LEN) {
3411 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3412 goto err;
3413 }
3414
3415 cids = &l->rx.cids;
3416 }
3417
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003418 /* Only packets packets with long headers and not RETRY or VERSION as type
3419 * have a length field.
3420 */
3421 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3422 uint64_t len;
3423
3424 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3425 end - *buf < len) {
3426 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3427 goto err;
3428 }
3429
3430 pkt->len = len;
3431 }
3432
3433
3434 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003435 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3436 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3437 cids == &l->rx.odcids) {
3438 /* Switch to the definitive tree ->cids containing the final CIDs. */
3439 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3440 if (node) {
3441 /* If found, signal this with NULL as special value for <cids>. */
3442 pkt->dcid.len = dcid_len;
3443 cids = NULL;
3444 }
3445 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003446 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003447
3448 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003449 int ipv4;
3450 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003451 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003452 const unsigned char *salt = initial_salt_v1;
3453 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003454
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003455 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3456 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3457 goto err;
3458 }
3459
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003460 pkt->saddr = *saddr;
3461 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3462 * of <saddr>.
3463 */
3464 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003465 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003466 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003467 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003468 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003469 goto err;
3470
3471 odcid = &qc->rx.params.original_destination_connection_id;
3472 /* Copy the transport parameters. */
3473 qc->rx.params = l->bind_conf->quic_params;
3474 /* Copy original_destination_connection_id transport parameter. */
3475 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3476 odcid->len = pkt->odcid_len;
3477 /* Copy the initial source connection ID. */
3478 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3479 qc->enc_params_len =
3480 quic_transport_params_encode(qc->enc_params,
3481 qc->enc_params + sizeof qc->enc_params,
3482 &qc->rx.params, 1);
3483 if (!qc->enc_params_len)
3484 goto err;
3485
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003486 /* NOTE: the socket address has been concatenated to the destination ID
3487 * chosen by the client for Initial packets.
3488 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003489 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3490 salt = initial_salt_draft_29;
3491 salt_len = sizeof initial_salt_draft_29;
3492 }
3493 if (!qc_new_isecs(qc, salt, salt_len,
3494 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003495 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3496 goto err;
3497 }
3498
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003499 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003500 /* This is the DCID node sent in this packet by the client. */
3501 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003502 /* Enqueue this packet. */
3503 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3504 /* Try to accept a new connection. */
3505 listener_accept(l);
3506
3507 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3508 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3509 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3510 /* Insert our SCID, the connection ID for the QUIC client. */
3511 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3512 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3513 if (n != &qc->scid_node) {
3514 quic_conn_free(qc);
3515 qc = ebmb_entry(n, struct quic_conn, scid_node);
3516 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003517 }
3518 else {
3519 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3520 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3521 else
3522 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003523 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003524 }
3525 }
3526 else {
3527 if (end - *buf < QUIC_CID_LEN) {
3528 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3529 goto err;
3530 }
3531
3532 cids = &l->rx.cids;
3533 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3534 if (!node) {
3535 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3536 goto err;
3537 }
3538
3539 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003540 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003541 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003542 /* A short packet is the last one of an UDP datagram. */
3543 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003544 }
3545
3546 /* Store the DCID used for this packet to check the packet which
3547 * come in this UDP datagram match with it.
3548 */
3549 if (!dgram_ctx->dcid_node) {
3550 dgram_ctx->dcid_node = node;
3551 dgram_ctx->qc = qc;
3552 }
3553
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003554 /* Increase the total length of this packet by the header length. */
3555 pkt->len += *buf - beg;
3556 /* Do not check the DCID node before the length. */
3557 if (dgram_ctx->dcid_node != node) {
3558 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3559 goto err;
3560 }
3561
3562 if (pkt->len > sizeof pkt->data) {
3563 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3564 goto err;
3565 }
3566
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003567 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3568 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3569 goto err;
3570 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003571
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003572
3573 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003574 /* Wake up the connection packet handler task from here only if all
3575 * the contexts have been initialized, especially the mux context
3576 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3577 * will start it if these contexts for the connection are not already
3578 * initialized.
3579 */
3580 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003581 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003582
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003583 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3584
3585 return pkt->len;
3586
3587 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003588 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003589 qc ? qc->conn : NULL, pkt);
3590 return -1;
3591}
3592
3593/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3594 * in advance. This is the reponsability of the caller to check there is enough room in this
3595 * buffer to build a long header.
3596 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3597 */
3598static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3599 int type, size_t pn_len, struct quic_conn *conn)
3600{
3601 if (type > QUIC_PACKET_TYPE_RETRY)
3602 return 0;
3603
3604 /* #0 byte flags */
3605 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3606 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3607 /* Version */
3608 quic_write_uint32(buf, end, conn->version);
3609 *(*buf)++ = conn->dcid.len;
3610 /* Destination connection ID */
3611 if (conn->dcid.len) {
3612 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3613 *buf += conn->dcid.len;
3614 }
3615 /* Source connection ID */
3616 *(*buf)++ = conn->scid.len;
3617 if (conn->scid.len) {
3618 memcpy(*buf, conn->scid.data, conn->scid.len);
3619 *buf += conn->scid.len;
3620 }
3621
3622 return 1;
3623}
3624
3625/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3626 * in advance. This is the reponsability of the caller to check there is enough room in this
3627 * buffer to build a long header.
3628 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3629 */
3630static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3631 size_t pn_len, struct quic_conn *conn)
3632{
3633 /* #0 byte flags */
3634 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3635 /* Destination connection ID */
3636 if (conn->dcid.len) {
3637 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3638 *buf += conn->dcid.len;
3639 }
3640
3641 return 1;
3642}
3643
3644/* Apply QUIC header protection to the packet with <buf> as first byte address,
3645 * <pn> as address of the Packet number field, <pnlen> being this field length
3646 * with <aead> as AEAD cipher and <key> as secret key.
3647 * Returns 1 if succeeded or 0 if failed.
3648 */
3649static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3650 const EVP_CIPHER *aead, const unsigned char *key)
3651{
3652 int i, ret, outlen;
3653 EVP_CIPHER_CTX *ctx;
3654 /* We need an IV of at least 5 bytes: one byte for bytes #0
3655 * and at most 4 bytes for the packet number
3656 */
3657 unsigned char mask[5] = {0};
3658
3659 ret = 0;
3660 ctx = EVP_CIPHER_CTX_new();
3661 if (!ctx)
3662 return 0;
3663
3664 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3665 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3666 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3667 goto out;
3668
3669 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3670 for (i = 0; i < pnlen; i++)
3671 pn[i] ^= mask[i + 1];
3672
3673 ret = 1;
3674
3675 out:
3676 EVP_CIPHER_CTX_free(ctx);
3677
3678 return ret;
3679}
3680
3681/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3682 * ACK ranges if needed to a value below <limit> in bytes.
3683 * Return 1 if succeeded, 0 if not.
3684 */
3685static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3686{
3687 size_t room, ack_delay_sz;
3688
3689 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3690 /* A frame is made of 1 byte for the frame type. */
3691 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003692 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003693 return 0;
3694
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003695 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003696}
3697
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003698/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3699 * 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 +01003700 * and <*len> the packet Length field initialized with the number of bytes already present
3701 * 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 +02003702 * <headlen> is the number of bytes already present in this packet before building frames.
3703 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003704 * Update consequently <*len> to reflect the size of these frames built
3705 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003706 * Return 1 if succeeded, 0 if not.
3707 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003708static inline int qc_build_frms(struct quic_tx_packet *pkt,
3709 size_t room, size_t *len, size_t headlen,
3710 struct quic_enc_level *qel,
3711 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003713 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003714 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003715 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003716 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003717
Frédéric Lécailleea604992020-12-24 13:01:37 +01003718 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003719 if (*len > room || headlen > remain)
3720 return 0;
3721
Frédéric Lécailleea604992020-12-24 13:01:37 +01003722 /* If we are not probing we must take into an account the congestion
3723 * control window.
3724 */
3725 if (!conn->tx.nb_pto_dgrams)
3726 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003727 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003728 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003729 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 +01003730 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003731 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003732
Frédéric Lécailleea604992020-12-24 13:01:37 +01003733 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003734 break;
3735
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003736 switch (cf->type) {
3737 case QUIC_FT_CRYPTO:
3738 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3739 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3740 /* Compute the length of this CRYPTO frame header */
3741 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3742 /* Compute the data length of this CRyPTO frame. */
3743 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3744 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3745 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3746 if (!dlen)
3747 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003748
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003749 pkt->cdata_len += dlen;
3750 /* CRYPTO frame length. */
3751 flen = hlen + quic_int_getsize(dlen) + dlen;
3752 TRACE_PROTO(" CRYPTO frame length (flen)",
3753 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3754 /* Add the CRYPTO data length and its encoded length to the packet
3755 * length and the length of this length.
3756 */
3757 *len += flen;
3758 room -= flen;
3759 if (dlen == cf->crypto.len) {
3760 /* <cf> CRYPTO data have been consumed. */
3761 MT_LIST_DELETE_SAFE(tmp1);
3762 LIST_APPEND(&pkt->frms, &cf->list);
3763 }
3764 else {
3765 struct quic_frame *new_cf;
3766
3767 new_cf = pool_alloc(pool_head_quic_frame);
3768 if (!new_cf) {
3769 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3770 return 0;
3771 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003772
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003773 new_cf->type = QUIC_FT_CRYPTO;
3774 new_cf->crypto.len = dlen;
3775 new_cf->crypto.offset = cf->crypto.offset;
3776 new_cf->crypto.qel = qel;
3777 LIST_APPEND(&pkt->frms, &new_cf->list);
3778 /* Consume <dlen> bytes of the current frame. */
3779 cf->crypto.len -= dlen;
3780 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003781 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003782 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003783
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003784 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003785 /* Note that these frames are accepted in short packets only without
3786 * "Length" packet field. Here, <*len> is used only to compute the
3787 * sum of the lengths of the already built frames for this packet.
3788 */
3789 TRACE_PROTO(" New STREAM frame build (room, len)",
3790 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3791 /* Compute the length of this STREAM frame "header" made a all the field
3792 * excepting the variable ones. Note that +1 is for the type of this frame.
3793 */
3794 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003795 ((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 +02003796 /* Compute the data length of this STREAM frame. */
3797 avail_room = room - hlen - *len;
3798 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003799 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003800
3801 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3802 dlen = max_available_room(avail_room, &dlen_sz);
3803 if (dlen > cf->stream.len) {
3804 dlen = cf->stream.len;
3805 }
3806 dlen_sz = quic_int_getsize(dlen);
3807 flen = hlen + dlen_sz + dlen;
3808 }
3809 else {
3810 dlen = QUIC_MIN(avail_room, cf->stream.len);
3811 flen = hlen + dlen;
3812 }
3813 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3814 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3815 TRACE_PROTO(" STREAM frame length (flen)",
3816 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3817 /* Add the STREAM data length and its encoded length to the packet
3818 * length and the length of this length.
3819 */
3820 *len += flen;
3821 room -= flen;
3822 if (dlen == cf->stream.len) {
3823 /* <cf> STREAM data have been consumed. */
3824 MT_LIST_DELETE_SAFE(tmp1);
3825 LIST_APPEND(&pkt->frms, &cf->list);
3826 }
3827 else {
3828 struct quic_frame *new_cf;
3829
3830 new_cf = pool_zalloc(pool_head_quic_frame);
3831 if (!new_cf) {
3832 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3833 return 0;
3834 }
3835
3836 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003837 new_cf->stream.qcs = cf->stream.qcs;
3838 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003839 new_cf->stream.id = cf->stream.id;
3840 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3841 new_cf->stream.offset = cf->stream.offset;
3842 new_cf->stream.len = dlen;
3843 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3844 /* FIN bit reset */
3845 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3846 new_cf->stream.data = cf->stream.data;
3847 LIST_APPEND(&pkt->frms, &new_cf->list);
3848 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3849 /* Consume <dlen> bytes of the current frame. */
3850 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003851 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003852 cf->stream.data += dlen;
3853 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003854 break;
3855
3856 default:
3857 flen = qc_frm_len(cf);
3858 BUG_ON(!flen);
3859 if (flen > room)
3860 continue;
3861
3862 *len += flen;
3863 room -= flen;
3864 MT_LIST_DELETE_SAFE(tmp1);
3865 LIST_APPEND(&pkt->frms, &cf->list);
3866 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003867 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003868 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003869 }
3870
Frédéric Lécailleea604992020-12-24 13:01:37 +01003871 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003872}
3873
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003874/* This function evaluates if <pkt> packet may be built into a buffer with
3875 * <room> as available room. A valid packet should at least contain a valid
3876 * header and at least a frame.
3877 * To estimate the minimal space to build a packet, we consider the worst case:
3878 - there is not enough space to build ack-eliciting frames from
3879 qel->pktns->tx.frms. This is safe to consider this because when we build
3880 a packet we first build the ACK frames, then the ack-eliciting frames
3881 from qel->pktns->tx.frms only if there is enough space for these
3882 ack-eliciting frames, finally PING and PADDING frames if needed,
3883 - we have to ensure there is enough space to build an ACK frame if required,
3884 and a PING frame, even if we do not have to probe,
3885 - we also have to verify there is enough space to build a PADDING frame
3886 if needed, especially if there is no need to send an ACK frame.
3887 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3888 * a valid packet).
3889 */
3890static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3891 int ack, int nb_pto_dgrams,
3892 struct quic_enc_level *qel, struct quic_conn *conn)
3893{
3894 size_t minlen, token_fields_len;
3895 /* XXX FIXME XXX : ack delay not supported */
3896 uint64_t ack_delay = 0;
3897 size_t ack_frm_len = 0;
3898
3899 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3900 conn->conn, NULL, NULL, &room);
3901 /* When we do not have to probe nor send acks either, we must take into
3902 * an account the data which have already been prepared and limit
3903 * the size of this packet. We will potentially build an ack-eliciting
3904 * packet.
3905 */
3906 if (!nb_pto_dgrams && !ack) {
3907 size_t path_room;
3908
3909 path_room = quic_path_prep_data(conn->path);
3910 if (room > path_room)
3911 room = path_room;
3912 }
3913
3914 if (ack)
3915 /* A frame is made of 1 byte for the frame type. */
3916 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3917
3918 /* XXX FIXME XXX : token not supported */
3919 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3920 /* Check there is enough room to build the header followed by a token,
3921 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3922 * encryption tag is also taken into an account. Note that we have no
3923 * knowledge of the packet number for this packet. It must be atomically
3924 * incremented each time a packet is built. But before building a packet
3925 * we must estimate if it may be built if we do not want to consume a packet
3926 * number for nothing! Note that we add 1 byte more to
3927 * <minlen> to be able to build an ack-eliciting packet when probing without
3928 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3929 * PING frame.
3930 */
3931 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3932 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3933 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3934 + token_fields_len;
3935 else
3936 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3937
3938 /* Consider any PADDING frame to add */
3939 if (objt_server(conn->conn->target) &&
3940 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3941 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3942 /* Pad too short client Initial packet */
3943 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3944 }
3945 else if (!ack) {
3946 /* Consider we will have to add the longest short PADDING frame to
3947 * protect a 1-byte length packet number.
3948 */
3949 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3950 }
3951
3952 if (room < minlen) {
3953 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3954 conn->conn, NULL, NULL, &room);
3955 return 0;
3956 }
3957
3958 return 1;
3959}
3960
3961/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003962 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3963 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3964 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003965 * 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 +02003966 * 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 +02003967 * having returned from this function.
3968 * 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 +01003969 * number field in this packet. <pn_len> will also have the packet number
3970 * length as value.
3971 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003972 * Always succeeds: this is the responsability of the caller to ensure there is
3973 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003974 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003975static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003976 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003977 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3978 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003979{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003980 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003981 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003982 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3983 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003984 size_t ack_frm_len;
3985 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003986 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003987
Frédéric Lécailleea604992020-12-24 13:01:37 +01003988 /* Length field value with CRYPTO frames if present. */
3989 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003990 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003991 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003992 * the congestion controller window. So, we do not limit the size of this
3993 * packet if we have an ACK frame to send because an ACK frame is not
3994 * ack-eliciting. This size will be limited if we have ack-eliciting
3995 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003996 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003997 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003998 size_t path_room;
3999
4000 path_room = quic_path_prep_data(conn->path);
4001 if (end - beg > path_room)
4002 end = beg + path_room;
4003 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004004
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004005 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004006 /* packet number length */
4007 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004008 /* Build the header */
4009 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004010 quic_build_packet_short_header(&pos, end, *pn_len, conn);
4011 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004012 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
4013 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4014 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004015 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004016 /* Ensure there is enough room for the TLS encryption tag */
4017 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004018 /* Build an ACK frame if required. */
4019 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004020 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004021 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004022 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004023 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4024 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4025 * that from here, we do not know if we will have to send a PING frame.
4026 * This will be decided after having computed the ack-eliciting frames
4027 * to be added to this packet.
4028 */
4029 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 +01004030 if (!ack_frm_len) {
4031 ssize_t room = end - pos;
4032 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4033 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004034 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004035 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004036 }
4037
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004038 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004039 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004040 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004041 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004042
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004043 /* Initialize the length of the frames built below to <len>.
4044 * If any frame could be successfully built by qc_build_frms(),
4045 * we will have len_frms > len.
4046 */
4047 len_frms = len;
4048 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004049 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4050 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004051 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004052
4053 add_ping_frm = 0;
4054 padding_len = 0;
4055 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004056 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004057 len < QUIC_INITIAL_PACKET_MINLEN) {
4058 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4059 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004060 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004061 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004062 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004063 add_ping_frm = 1;
4064 len += 1;
4065 }
4066 /* If there is no frame at all to follow, add at least a PADDING frame. */
4067 if (!ack_frm_len)
4068 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4069 }
4070
4071 /* Length (of the remaining data). Must not fail because, the buffer size
4072 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004073 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004074 * of this packet.
4075 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004076 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004077 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004078 else
4079 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004080 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004081 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082
4083 /* Packet number field address. */
4084 *buf_pn = pos;
4085
4086 /* Packet number encoding. */
4087 quic_packet_number_encode(&pos, end, pn, *pn_len);
4088
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004089 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4090 ssize_t room = end - pos;
4091 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4092 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004093 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004094 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004095
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004096 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004097 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004098 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004099
4100 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004101 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004102 ssize_t room = end - pos;
4103 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4104 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004105 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004106 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004107 }
4108 }
4109
4110 /* Build a PING frame if needed. */
4111 if (add_ping_frm) {
4112 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004113 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4114 ssize_t room = end - pos;
4115 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4116 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004117 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004118 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004119 }
4120
4121 /* Build a PADDING frame if needed. */
4122 if (padding_len) {
4123 frm.type = QUIC_FT_PADDING;
4124 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004125 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4126 ssize_t room = end - pos;
4127 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4128 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004129 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004130 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004131 }
4132
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004133 /* Always reset this variable as this function has no idea
4134 * if it was set. It is handle by the loss detection timer.
4135 */
4136 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004137 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004138}
4139
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004140static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004141{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004142 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004143 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004144 pkt->cdata_len = 0;
4145 pkt->in_flight_len = 0;
4146 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004147 pkt->next = NULL;
4148 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004149}
4150
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004151/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004152static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4153{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004154 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004155
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004156 if (!pkt)
4157 return;
4158
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004159 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004160 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004161 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004162 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004163 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004164}
4165
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004166/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4167 * type for <qc> QUIC connection from <qel> encryption level.
4168 * Return -2 if the packet could not be allocated or encrypted for any reason,
4169 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004170 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004171static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4172 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004173 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004174 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004175 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004176{
4177 /* The pointer to the packet number field. */
4178 unsigned char *buf_pn;
4179 unsigned char *beg, *end, *payload;
4180 int64_t pn;
4181 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004182 struct quic_tls_ctx *tls_ctx;
4183 struct quic_tx_packet *pkt;
4184
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004185 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004186 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004187 pkt = pool_alloc(pool_head_quic_tx_packet);
4188 if (!pkt) {
4189 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 +02004190 *err = -2;
4191 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004192 }
4193
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004194 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004195 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004196 pn_len = 0;
4197 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004198 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004199 *err = -1;
4200 goto err;
4201 }
4202
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004203 /* Consume a packet number. */
4204 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004205 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 +01004206
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004207 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004208 payload = buf_pn + pn_len;
4209 payload_len = end - payload;
4210 aad_len = payload - beg;
4211
4212 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004213 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4214 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004216 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004217
4218 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004219 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004220 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4221 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4222 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004223 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004224 goto err;
4225 }
4226
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004227 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4228 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004229 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004230 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004231 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004232 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004233 pkt->in_flight_len = pkt->len;
4234 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004235 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004237 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4238
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004239 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004240
4241 err:
4242 free_quic_tx_packet(pkt);
4243 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004244 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004245}
4246
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004247/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4248 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004249 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004250static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4251 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004252{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253 size_t try, done = 0;
4254
4255 if (!conn_ctrl_ready(conn))
4256 return 0;
4257
4258 if (!fd_recv_ready(conn->handle.fd))
4259 return 0;
4260
4261 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004262
4263 /* read the largest possible block. For this, we perform only one call
4264 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004265 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004266 */
4267 while (count > 0) {
4268 try = b_contig_space(buf);
4269 if (!try)
4270 break;
4271
4272 if (try > count)
4273 try = count;
4274
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004275 b_add(buf, try);
4276 done += try;
4277 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004278 }
4279
4280 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4281 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4282
4283 leave:
4284 return done;
4285
4286 read0:
4287 conn_sock_read0(conn);
4288 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4289
4290 /* Now a final check for a possible asynchronous low-level error
4291 * report. This can happen when a connection receives a reset
4292 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4293 * we might have come from there by just checking POLL_HUP instead
4294 * of recv()'s return value 0, so we have no way to tell there was
4295 * an error without checking.
4296 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004297 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004298 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4299 goto leave;
4300}
4301
4302
4303/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4304 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4305 * other pending data for example, but this flag is ignored at the moment.
4306 * Only one call to send() is performed, unless the buffer wraps, in which case
4307 * a second call may be performed. The connection's flags are updated with
4308 * whatever special event is detected (error, empty). The caller is responsible
4309 * for taking care of those events and avoiding the call if inappropriate. The
4310 * function does not call the connection's polling update function, so the caller
4311 * is responsible for this. It's up to the caller to update the buffer's contents
4312 * based on the return value.
4313 */
4314static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4315{
4316 ssize_t ret;
4317 size_t try, done;
4318 int send_flag;
4319
4320 if (!conn_ctrl_ready(conn))
4321 return 0;
4322
4323 if (!fd_send_ready(conn->handle.fd))
4324 return 0;
4325
4326 done = 0;
4327 /* send the largest possible block. For this we perform only one call
4328 * to send() unless the buffer wraps and we exactly fill the first hunk,
4329 * in which case we accept to do it once again.
4330 */
4331 while (count) {
4332 try = b_contig_data(buf, done);
4333 if (try > count)
4334 try = count;
4335
4336 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4337 if (try < count || flags & CO_SFL_MSG_MORE)
4338 send_flag |= MSG_MORE;
4339
4340 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4341 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4342 if (ret > 0) {
4343 count -= ret;
4344 done += ret;
4345
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004346 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004347 conn->flags |= CO_FL_WAIT_L4L6;
4348 /* if the system buffer is full, don't insist */
4349 if (ret < try)
4350 break;
4351 }
4352 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4353 /* nothing written, we need to poll for write first */
4354 fd_cant_send(conn->handle.fd);
4355 break;
4356 }
4357 else if (errno != EINTR) {
4358 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4359 break;
4360 }
4361 }
4362 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4363 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4364
4365 if (done > 0) {
4366 /* we count the total bytes sent, and the send rate for 32-byte
4367 * blocks. The reason for the latter is that freq_ctr are
4368 * limited to 4GB and that it's not enough per second.
4369 */
4370 _HA_ATOMIC_ADD(&global.out_bytes, done);
4371 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4372 }
4373 return done;
4374}
4375
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004376/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4377 * event subscriber <es> is not allowed to change from a previous call as long
4378 * as at least one event is still subscribed. The <event_type> must only be a
4379 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4380 */
4381static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4382{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004383 struct qcc *qcc = conn->qc->qcc;
4384
4385 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4386 BUG_ON(qcc->subs && qcc->subs != es);
4387
4388 es->events |= event_type;
4389 qcc->subs = es;
4390
4391 if (event_type & SUB_RETRY_RECV)
4392 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc);
4393
4394 if (event_type & SUB_RETRY_SEND)
4395 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc);
4396
4397 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004398}
4399
4400/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4401 * The <es> pointer is not allowed to differ from the one passed to the
4402 * subscribe() call. It always returns zero.
4403 */
4404static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4405{
4406 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4407}
4408
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004409/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4410 * connection with <xprt_ctx> as address of the xprt context.
4411 * Returns 1 if succeeded, 0 if not.
4412 */
4413static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4414{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004415 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004416
4417 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4418
4419 if (*xprt_ctx)
4420 goto out;
4421
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004422 ctx = pool_alloc(pool_head_quic_conn_ctx);
4423 if (!ctx) {
4424 conn->err_code = CO_ER_SYS_MEMLIM;
4425 goto err;
4426 }
4427
4428 ctx->wait_event.tasklet = tasklet_new();
4429 if (!ctx->wait_event.tasklet) {
4430 conn->err_code = CO_ER_SYS_MEMLIM;
4431 goto err;
4432 }
4433
4434 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4435 ctx->wait_event.tasklet->context = ctx;
4436 ctx->wait_event.events = 0;
4437 ctx->conn = conn;
4438 ctx->subs = NULL;
4439 ctx->xprt_ctx = NULL;
4440
4441 ctx->xprt = xprt_get(XPRT_QUIC);
4442 if (objt_server(conn->target)) {
4443 /* Server */
4444 struct server *srv = __objt_server(conn->target);
4445 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004446 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004447 int ssl_err, ipv4;
4448
4449 ssl_err = SSL_ERROR_NONE;
4450 if (RAND_bytes(dcid, sizeof dcid) != 1)
4451 goto err;
4452
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004453 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004454 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004455 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004456 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004457 goto err;
4458
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004459 /* Insert our SCID, the connection ID for the QUIC client. */
4460 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4461
4462 conn->qc = qc;
4463 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004464 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4465 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004466 goto err;
4467
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004468 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4469 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4470 goto err;
4471
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004472 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004473 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004474 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4475 qc->enc_params_len =
4476 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4477 &qc->rx.params, 0);
4478 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004479 goto err;
4480
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004481 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004482 SSL_set_connect_state(ctx->ssl);
4483 ssl_err = SSL_do_handshake(ctx->ssl);
4484 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004485 int st;
4486
4487 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004488 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4489 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004490 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004491 }
4492 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004493 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004494 goto err;
4495 }
4496 }
4497 }
4498 else if (objt_listener(conn->target)) {
4499 /* Listener */
4500 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004501 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004502
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004503 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004504 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4505 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4506 goto err;
4507
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004508 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004509 SSL_set_accept_state(ctx->ssl);
4510 }
4511
4512 *xprt_ctx = ctx;
4513
4514 /* Leave init state and start handshake */
4515 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004516
4517 out:
4518 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4519
4520 return 0;
4521
4522 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004523 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004524 tasklet_free(ctx->wait_event.tasklet);
4525 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004526 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004527 return -1;
4528}
4529
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004530/* Start the QUIC transport layer */
4531static int qc_xprt_start(struct connection *conn, void *ctx)
4532{
4533 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004534 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004535
4536 qc = conn->qc;
4537 if (!quic_conn_init_timer(qc)) {
4538 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4539 return 0;
4540 }
4541
4542 tasklet_wakeup(qctx->wait_event.tasklet);
4543 return 1;
4544}
4545
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004546/* transport-layer operations for QUIC connections. */
4547static struct xprt_ops ssl_quic = {
4548 .snd_buf = quic_conn_from_buf,
4549 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004550 .subscribe = quic_conn_subscribe,
4551 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004552 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004553 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004554 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4555 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4556 .name = "QUIC",
4557};
4558
4559__attribute__((constructor))
4560static void __quic_conn_init(void)
4561{
4562 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4563 xprt_register(XPRT_QUIC, &ssl_quic);
4564}
4565
4566__attribute__((destructor))
4567static void __quic_conn_deinit(void)
4568{
4569 BIO_meth_free(ha_quic_meth);
4570}
4571
4572/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4573 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4574 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004575 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004576 */
4577static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4578 struct sockaddr_storage *saddr, qpkt_read_func *func)
4579{
4580 unsigned char *pos;
4581 const unsigned char *end;
4582 struct quic_dgram_ctx dgram_ctx = {
4583 .dcid_node = NULL,
4584 .owner = owner,
4585 };
4586
4587 pos = (unsigned char *)buf;
4588 end = pos + len;
4589
4590 do {
4591 int ret;
4592 struct quic_rx_packet *pkt;
4593
Willy Tarreaue4498932021-03-22 21:13:05 +01004594 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004595 if (!pkt)
4596 goto err;
4597
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004598 quic_rx_packet_refinc(pkt);
4599 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4600 if (ret == -1) {
4601 size_t pkt_len;
4602
4603 pkt_len = pkt->len;
4604 free_quic_rx_packet(pkt);
4605 /* If the packet length could not be found, we cannot continue. */
4606 if (!pkt_len)
4607 break;
4608 }
4609 } while (pos < end);
4610
4611 /* Increasing the received bytes counter by the UDP datagram length
4612 * if this datagram could be associated to a connection.
4613 */
4614 if (dgram_ctx.qc)
4615 dgram_ctx.qc->rx.bytes += len;
4616
4617 return pos - (unsigned char *)buf;
4618
4619 err:
4620 return -1;
4621}
4622
4623ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4624 struct sockaddr_storage *saddr)
4625{
4626 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4627}
4628
4629ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4630 struct sockaddr_storage *saddr)
4631{
4632 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4633}
4634
4635/* QUIC I/O handler for connection to local listeners or remove servers
4636 * depending on <listener> boolean value, with <fd> as socket file
4637 * descriptor and <ctx> as context.
4638 */
4639static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4640{
4641 ssize_t ret;
4642 size_t done = 0;
4643 struct buffer *buf = get_trash_chunk();
4644 /* Source address */
4645 struct sockaddr_storage saddr = {0};
4646 socklen_t saddrlen = sizeof saddr;
4647
4648 if (!fd_recv_ready(fd))
4649 return 0;
4650
4651 do {
4652 ret = recvfrom(fd, buf->area, buf->size, 0,
4653 (struct sockaddr *)&saddr, &saddrlen);
4654 if (ret < 0) {
4655 if (errno == EINTR)
4656 continue;
4657 if (errno == EAGAIN)
4658 fd_cant_recv(fd);
4659 goto out;
4660 }
4661 } while (0);
4662
4663 done = buf->data = ret;
4664 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4665
4666 out:
4667 return done;
4668}
4669
4670/* QUIC I/O handler for connections to local listeners with <fd> as socket
4671 * file descriptor.
4672 */
4673void quic_fd_handler(int fd)
4674{
Willy Tarreauf5090652021-04-06 17:23:40 +02004675 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004676 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4677}
4678
4679/* QUIC I/O handler for connections to remote servers with <fd> as socket
4680 * file descriptor.
4681 */
4682void quic_conn_fd_handler(int fd)
4683{
Willy Tarreauf5090652021-04-06 17:23:40 +02004684 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004685 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4686}
4687
4688/*
4689 * Local variables:
4690 * c-indent-level: 8
4691 * c-basic-offset: 8
4692 * End:
4693 */