blob: d1bfea8c1a83d9e84f5644c0a76de4a25b0f4575 [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;
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200583 const uint64_t *len = a3;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100584
585 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200586 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100587 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200588 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
589 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaille865b0782021-09-23 07:33:20 +0200590 if (len)
591 chunk_appendf(&trace_buf, " len=%llu", (ull)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100592 }
593
594}
595
596/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200597static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598{
599 struct quic_conn *qc;
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200600 struct quic_pktns *hdshk_pktns, *app_pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100601
602 qc = ctx->conn->qc;
603 if (objt_server(qc->conn->target))
604 return 1;
605
Frédéric Lécaille67f47d02021-08-19 15:19:09 +0200606 hdshk_pktns = qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns;
607 app_pktns = qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns;
608 if ((HA_ATOMIC_LOAD(&hdshk_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
609 (HA_ATOMIC_LOAD(&app_pktns->flags) & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200610 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100611 return 1;
612
613 return 0;
614}
615
616/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
617 * both loss detection and PTO and schedule the task assiated to this timer if needed.
618 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200619static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100620{
621 struct quic_conn *qc;
622 struct quic_pktns *pktns;
623 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200624 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100625
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100626 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
627 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100628 qc = ctx->conn->qc;
629 pktns = quic_loss_pktns(qc);
630 if (tick_isset(pktns->tx.loss_time)) {
631 qc->timer = pktns->tx.loss_time;
632 goto out;
633 }
634
635 /* XXX TODO: anti-amplification: the timer must be
636 * cancelled for a server which reached the anti-amplification limit.
637 */
638
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100639 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
640 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100641 /* Timer cancellation. */
642 qc->timer = TICK_ETERNITY;
643 goto out;
644 }
645
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200646 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
647 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100648 if (tick_isset(pto))
649 qc->timer = pto;
650 out:
651 task_schedule(qc->timer_task, qc->timer);
652 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
653}
654
655#ifndef OPENSSL_IS_BORINGSSL
656int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
657 const uint8_t *read_secret,
658 const uint8_t *write_secret, size_t secret_len)
659{
660 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
661 struct quic_tls_ctx *tls_ctx =
662 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
663 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
664
665 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
666 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
667 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
668 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
669
670 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
671 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
672 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
673 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
674 read_secret, secret_len)) {
675 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
676 return 0;
677 }
678
679 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
680 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
681 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
682 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
683 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
684 write_secret, secret_len)) {
685 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
686 return 0;
687 }
688
689 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
690 if (objt_server(conn->target) && level == ssl_encryption_application) {
691 const unsigned char *buf;
692 size_t buflen;
693
694 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
695 if (!buflen)
696 return 0;
697
698 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
699 return 0;
700 }
701 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
702
703 return 1;
704}
705#else
706/* ->set_read_secret callback to derive the RX secrets at <level> encryption
707 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500708 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100709 */
710int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
711 const SSL_CIPHER *cipher,
712 const uint8_t *secret, size_t secret_len)
713{
714 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
715 struct quic_tls_ctx *tls_ctx =
716 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
717
718 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
719 tls_ctx->rx.aead = tls_aead(cipher);
720 tls_ctx->rx.md = tls_md(cipher);
721 tls_ctx->rx.hp = tls_hp(cipher);
722
723 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
724 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
725 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
726 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
727 secret, secret_len)) {
728 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
729 goto err;
730 }
731
732 if (objt_server(conn->target) && level == ssl_encryption_application) {
733 const unsigned char *buf;
734 size_t buflen;
735
736 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
737 if (!buflen)
738 goto err;
739
740 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
741 goto err;
742 }
743
744 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
745 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
746
747 return 1;
748
749 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100750 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100751 return 0;
752}
753
754/* ->set_write_secret callback to derive the TX secrets at <level>
755 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500756 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100757 */
758int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
759 const SSL_CIPHER *cipher,
760 const uint8_t *secret, size_t secret_len)
761{
762 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
763 struct quic_tls_ctx *tls_ctx =
764 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
765
766 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
767 tls_ctx->tx.aead = tls_aead(cipher);
768 tls_ctx->tx.md = tls_md(cipher);
769 tls_ctx->tx.hp = tls_hp(cipher);
770
771 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
772 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
773 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
774 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
775 secret, secret_len)) {
776 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
777 goto err;
778 }
779
780 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
781 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
782
783 return 1;
784
785 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100786 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100787 return 0;
788}
789#endif
790
791/* This function copies the CRYPTO data provided by the TLS stack found at <data>
792 * with <len> as size in CRYPTO buffers dedicated to store the information about
793 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
794 * It fails only if it could not managed to allocate enough CRYPTO buffers to
795 * store all the data.
796 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
797 */
798static int quic_crypto_data_cpy(struct quic_enc_level *qel,
799 const unsigned char *data, size_t len)
800{
801 struct quic_crypto_buf **qcb;
802 /* The remaining byte to store in CRYPTO buffers. */
803 size_t cf_offset, cf_len, *nb_buf;
804 unsigned char *pos;
805
806 nb_buf = &qel->tx.crypto.nb_buf;
807 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
808 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
809 cf_len = len;
810
811 while (len) {
812 size_t to_copy, room;
813
814 pos = (*qcb)->data + (*qcb)->sz;
815 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
816 to_copy = len > room ? room : len;
817 if (to_copy) {
818 memcpy(pos, data, to_copy);
819 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
820 qel->tx.crypto.sz += to_copy;
821 (*qcb)->sz += to_copy;
822 pos += to_copy;
823 len -= to_copy;
824 data += to_copy;
825 }
826 else {
827 struct quic_crypto_buf **tmp;
828
829 tmp = realloc(qel->tx.crypto.bufs,
830 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
831 if (tmp) {
832 qel->tx.crypto.bufs = tmp;
833 qcb = &qel->tx.crypto.bufs[*nb_buf];
834 *qcb = pool_alloc(pool_head_quic_crypto_buf);
835 if (!*qcb)
836 return 0;
837
838 (*qcb)->sz = 0;
839 ++*nb_buf;
840 }
841 else {
842 break;
843 }
844 }
845 }
846
847 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
848 * have been buffered.
849 */
850 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200851 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100852
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200853 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100854 if (!frm)
855 return 0;
856
857 frm->type = QUIC_FT_CRYPTO;
858 frm->crypto.offset = cf_offset;
859 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200860 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200861 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100862 }
863
864 return len == 0;
865}
866
867
868/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
869 * wants to provide the QUIC layer with CRYPTO data.
870 * Returns 1 if succeeded, 0 if not.
871 */
872int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
873 const uint8_t *data, size_t len)
874{
875 struct connection *conn;
876 enum quic_tls_enc_level tel;
877 struct quic_enc_level *qel;
878
879 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
880 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
881 tel = ssl_to_quic_enc_level(level);
882 qel = &conn->qc->els[tel];
883
884 if (tel == -1) {
885 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
886 goto err;
887 }
888
889 if (!quic_crypto_data_cpy(qel, data, len)) {
890 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
891 goto err;
892 }
893
894 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
895 conn, &level, &len);
896
897 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
898 return 1;
899
900 err:
901 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
902 return 0;
903}
904
905int ha_quic_flush_flight(SSL *ssl)
906{
907 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
908
909 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
910 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
911
912 return 1;
913}
914
915int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
916{
917 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
918
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100919 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100920 return 1;
921}
922
923/* QUIC TLS methods */
924static SSL_QUIC_METHOD ha_quic_method = {
925#ifdef OPENSSL_IS_BORINGSSL
926 .set_read_secret = ha_set_rsec,
927 .set_write_secret = ha_set_wsec,
928#else
929 .set_encryption_secrets = ha_quic_set_encryption_secrets,
930#endif
931 .add_handshake_data = ha_quic_add_handshake_data,
932 .flush_flight = ha_quic_flush_flight,
933 .send_alert = ha_quic_send_alert,
934};
935
936/* Initialize the TLS context of a listener with <bind_conf> as configuration.
937 * Returns an error count.
938 */
939int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
940{
941 struct proxy *curproxy = bind_conf->frontend;
942 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
943 int cfgerr = 0;
944
945#if 0
946 /* XXX Did not manage to use this. */
947 const char *ciphers =
948 "TLS_AES_128_GCM_SHA256:"
949 "TLS_AES_256_GCM_SHA384:"
950 "TLS_CHACHA20_POLY1305_SHA256:"
951 "TLS_AES_128_CCM_SHA256";
952#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200953 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100954 long options =
955 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
956 SSL_OP_SINGLE_ECDH_USE |
957 SSL_OP_CIPHER_SERVER_PREFERENCE;
958 SSL_CTX *ctx;
959
960 ctx = SSL_CTX_new(TLS_server_method());
961 bind_conf->initial_ctx = ctx;
962
963 SSL_CTX_set_options(ctx, options);
964#if 0
965 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
966 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
967 "for bind '%s' at [%s:%d].\n",
968 curproxy->id, ciphers,
969 bind_conf->arg, bind_conf->file, bind_conf->line);
970 cfgerr++;
971 }
972#endif
973
974 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
975 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
976 "for bind '%s' at [%s:%d].\n",
977 curproxy->id, groups,
978 bind_conf->arg, bind_conf->file, bind_conf->line);
979 cfgerr++;
980 }
981
982 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
983 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
984 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
985 SSL_CTX_set_default_verify_paths(ctx);
986
987#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
988#ifdef OPENSSL_IS_BORINGSSL
989 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
990 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
991#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
992 if (bind_conf->ssl_conf.early_data) {
993 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
994 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
995 }
996 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
997 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
998#else
999 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
1000#endif
1001 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
1002#endif
1003 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
1004
1005 return cfgerr;
1006}
1007
1008/* Decode an expected packet number from <truncated_on> its truncated value,
1009 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
1010 * the number of bits used to encode this packet number (its length in bytes * 8).
1011 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
1012 */
1013static uint64_t decode_packet_number(uint64_t largest_pn,
1014 uint32_t truncated_pn, unsigned int pn_nbits)
1015{
1016 uint64_t expected_pn = largest_pn + 1;
1017 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1018 uint64_t pn_hwin = pn_win / 2;
1019 uint64_t pn_mask = pn_win - 1;
1020 uint64_t candidate_pn;
1021
1022
1023 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1024 /* Note that <pn_win> > <pn_hwin>. */
1025 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1026 candidate_pn + pn_hwin <= expected_pn)
1027 return candidate_pn + pn_win;
1028
1029 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1030 return candidate_pn - pn_win;
1031
1032 return candidate_pn;
1033}
1034
1035/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1036 * cryptographic context.
1037 * <largest_pn> is the largest received packet number and <pn> the address of
1038 * the packet number field for this packet with <byte0> address of its first byte.
1039 * <end> points to one byte past the end of this packet.
1040 * Returns 1 if succeeded, 0 if not.
1041 */
1042static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1043 int64_t largest_pn, unsigned char *pn,
1044 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001045 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001046{
1047 int ret, outlen, i, pnlen;
1048 uint64_t packet_number;
1049 uint32_t truncated_pn = 0;
1050 unsigned char mask[5] = {0};
1051 unsigned char *sample;
1052 EVP_CIPHER_CTX *cctx;
1053 unsigned char *hp_key;
1054
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001055 /* Check there is enough data in this packet. */
1056 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1057 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1058 return 0;
1059 }
1060
1061 cctx = EVP_CIPHER_CTX_new();
1062 if (!cctx) {
1063 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1064 return 0;
1065 }
1066
1067 ret = 0;
1068 sample = pn + QUIC_PACKET_PN_MAXLEN;
1069
1070 hp_key = tls_ctx->rx.hp_key;
1071 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1072 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1073 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1074 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1075 goto out;
1076 }
1077
1078 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1079 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1080 for (i = 0; i < pnlen; i++) {
1081 pn[i] ^= mask[i + 1];
1082 truncated_pn = (truncated_pn << 8) | pn[i];
1083 }
1084
1085 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1086 /* Store remaining information for this unprotected header */
1087 pkt->pn = packet_number;
1088 pkt->pnl = pnlen;
1089
1090 ret = 1;
1091
1092 out:
1093 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001094
1095 return ret;
1096}
1097
1098/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1099 * address, with <payload_len> as payload length, <aad> as address of
1100 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1101 * context.
1102 * Returns 1 if succeeded, 0 if not.
1103 */
1104static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1105 unsigned char *aad, size_t aad_len, uint64_t pn,
1106 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1107{
1108 unsigned char iv[12];
1109 unsigned char *tx_iv = tls_ctx->tx.iv;
1110 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001111 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001112
1113 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1114 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001115 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001116 }
1117
1118 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1119 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1120 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001121 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001122 }
1123
1124 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001125
1126 err:
1127 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1128 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1129 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001130}
1131
1132/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1133 * Returns 1 if succeeded, 0 if not.
1134 */
1135static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1136{
1137 int ret;
1138 unsigned char iv[12];
1139 unsigned char *rx_iv = tls_ctx->rx.iv;
1140 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1141
1142 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1143 return 0;
1144
1145 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1146 pkt->data, pkt->aad_len,
1147 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1148 if (!ret)
1149 return 0;
1150
1151 /* Update the packet length (required to parse the frames). */
1152 pkt->len = pkt->aad_len + ret;
1153
1154 return 1;
1155}
1156
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001157/* Remove from <qcs> stream the acknowledged frames.
1158 * Never fails.
1159 */
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001160static int qcs_try_to_consume(struct qcs *qcs)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001161{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001162 int ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001163 struct eb64_node *frm_node;
1164
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001165 ret = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001166 frm_node = eb64_first(&qcs->tx.acked_frms);
1167 while (frm_node) {
1168 struct quic_stream *strm;
1169
1170 strm = eb64_entry(&frm_node->node, struct quic_stream, offset);
1171 if (strm->offset.key != qcs->tx.ack_offset)
1172 break;
1173
1174 b_del(strm->buf, strm->len);
1175 qcs->tx.ack_offset += strm->len;
1176 frm_node = eb64_next(frm_node);
1177 eb64_delete(&strm->offset);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001178 ret = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001179 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001180
1181 return ret;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001182}
1183
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001184/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001185static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001186 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001187{
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001188 int stream_acked;
1189 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001190
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001191 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001192 stream_acked = 0;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001193 switch (frm->type) {
1194 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
1195 {
1196 struct qcs *qcs = frm->stream.qcs;
1197 struct quic_stream *strm = &frm->stream;
1198
1199 if (qcs->tx.ack_offset == strm->offset.key) {
1200 b_del(strm->buf, strm->len);
1201 qcs->tx.ack_offset += strm->len;
1202 LIST_DELETE(&frm->list);
1203 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001204 qc->qcc->flags &= ~QC_CF_MUX_MFULL;
1205 stream_acked = 1;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001206 }
1207 else {
1208 eb64_insert(&qcs->tx.acked_frms, &strm->offset);
1209 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001210 stream_acked |= qcs_try_to_consume(qcs);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001211 }
1212 break;
1213 default:
1214 LIST_DELETE(&frm->list);
1215 pool_free(pool_head_quic_frame, frm);
1216 }
Frédéric Lécaille1c482c62021-09-20 16:59:51 +02001217
1218 if (stream_acked) {
1219 struct qcc *qcc = qc->qcc;
1220
1221 if (qcc->subs && qcc->subs->events & SUB_RETRY_SEND) {
1222 tasklet_wakeup(qcc->subs->tasklet);
1223 qcc->subs->events &= ~SUB_RETRY_SEND;
1224 if (!qcc->subs->events)
1225 qcc->subs = NULL;
1226 }
1227 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001228}
1229
1230/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1231 * deallocating them, and their TX frames.
1232 * Returns the last node reached to be used for the next range.
1233 * May be NULL if <largest> node could not be found.
1234 */
1235static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1236 struct list *newly_acked_pkts,
1237 struct eb64_node *largest_node,
1238 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001239 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001240{
1241 struct eb64_node *node;
1242 struct quic_tx_packet *pkt;
1243
1244 if (largest_node)
1245 node = largest_node;
1246 else {
1247 node = eb64_lookup(pkts, largest);
1248 while (!node && largest > smallest) {
1249 node = eb64_lookup(pkts, --largest);
1250 }
1251 }
1252
1253 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001254 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001255
1256 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1257 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001258 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001259 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1260 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1261 qc_treat_acked_tx_frm(frm, ctx);
1262 node = eb64_prev(node);
1263 eb64_delete(&pkt->pn_node);
1264 }
1265
1266 return node;
1267}
1268
1269/* Treat <frm> frame whose packet it is attached to has just been detected as non
1270 * acknowledged.
1271 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001272static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001273 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001274 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001275{
1276 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001277 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001278 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001279}
1280
1281
1282/* Free the TX packets of <pkts> list */
1283static inline void free_quic_tx_pkts(struct list *pkts)
1284{
1285 struct quic_tx_packet *pkt, *tmp;
1286
1287 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001288 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001289 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001290 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001291 }
1292}
1293
1294/* Send a packet loss event nofification to the congestion controller
1295 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1296 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1297 * at <now_us> current time.
1298 * Always succeeds.
1299 */
1300static inline void qc_cc_loss_event(struct quic_conn *qc,
1301 unsigned int lost_bytes,
1302 unsigned int newest_time_sent,
1303 unsigned int period,
1304 unsigned int now_us)
1305{
1306 struct quic_cc_event ev = {
1307 .type = QUIC_CC_EVT_LOSS,
1308 .loss.now_ms = now_ms,
1309 .loss.max_ack_delay = qc->max_ack_delay,
1310 .loss.lost_bytes = lost_bytes,
1311 .loss.newest_time_sent = newest_time_sent,
1312 .loss.period = period,
1313 };
1314
1315 quic_cc_event(&qc->path->cc, &ev);
1316}
1317
1318/* Send a packet ack event nofication for each newly acked packet of
1319 * <newly_acked_pkts> list and free them.
1320 * Always succeeds.
1321 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001322static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001323 struct list *newly_acked_pkts)
1324{
1325 struct quic_conn *qc = ctx->conn->qc;
1326 struct quic_tx_packet *pkt, *tmp;
1327 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1328
1329 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1330 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001331 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001332 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001333 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001334 ev.ack.acked = pkt->in_flight_len;
1335 ev.ack.time_sent = pkt->time_sent;
1336 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001337 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001338 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001339 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001340 }
1341
1342}
1343
1344/* Handle <pkts> list of lost packets detected at <now_us> handling
1345 * their TX frames.
1346 * Send a packet loss event to the congestion controller if
1347 * in flight packet have been lost.
1348 * Also frees the packet in <pkts> list.
1349 * Never fails.
1350 */
1351static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001352 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001353 struct list *pkts,
1354 uint64_t now_us)
1355{
1356 struct quic_conn *qc = ctx->conn->qc;
1357 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001358 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001359 uint64_t lost_bytes;
1360
1361 lost_bytes = 0;
1362 oldest_lost = newest_lost = NULL;
1363 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1364 lost_bytes += pkt->in_flight_len;
1365 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001366 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001367 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001368 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001369 /* Treat the frames of this lost packet. */
1370 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1371 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001372 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001373 if (!oldest_lost) {
1374 oldest_lost = newest_lost = pkt;
1375 }
1376 else {
1377 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001378 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001379 newest_lost = pkt;
1380 }
1381 }
1382
1383 if (lost_bytes) {
1384 /* Sent a packet loss event to the congestion controller. */
1385 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1386 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001387 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001388 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001389 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001390 }
1391}
1392
1393/* Look for packet loss from sent packets for <qel> encryption level of a
1394 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1395 * their tree if deemed as lost or set the <loss_time> value the packet number
1396 * space if any not deemed lost.
1397 * Should be called after having received an ACK frame with newly acknowledged
1398 * packets or when the the loss detection timer has expired.
1399 * Always succeeds.
1400 */
1401static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1402 struct quic_conn *qc,
1403 struct list *lost_pkts)
1404{
1405 struct eb_root *pkts;
1406 struct eb64_node *node;
1407 struct quic_loss *ql;
1408 unsigned int loss_delay;
1409
1410 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1411 pkts = &pktns->tx.pkts;
1412 pktns->tx.loss_time = TICK_ETERNITY;
1413 if (eb_is_empty(pkts))
1414 goto out;
1415
1416 ql = &qc->path->loss;
1417 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1418 loss_delay += loss_delay >> 3;
1419 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1420
1421 node = eb64_first(pkts);
1422 while (node) {
1423 struct quic_tx_packet *pkt;
1424 int64_t largest_acked_pn;
1425 unsigned int loss_time_limit, time_sent;
1426
1427 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001428 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001429 node = eb64_next(node);
1430 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1431 break;
1432
1433 time_sent = pkt->time_sent;
1434 loss_time_limit = tick_add(time_sent, loss_delay);
1435 if (tick_is_le(time_sent, now_ms) ||
1436 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1437 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001438 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001439 }
1440 else {
1441 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1442 }
1443 }
1444
1445 out:
1446 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1447}
1448
1449/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1450 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001451 * 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 +01001452 * acked ack-eliciting packet.
1453 * Return 1, if succeeded, 0 if not.
1454 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001455static 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 +01001456 struct quic_enc_level *qel,
1457 unsigned int *rtt_sample,
1458 const unsigned char **pos, const unsigned char *end)
1459{
1460 struct quic_ack *ack = &frm->ack;
1461 uint64_t smallest, largest;
1462 struct eb_root *pkts;
1463 struct eb64_node *largest_node;
1464 unsigned int time_sent, pkt_flags;
1465 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1466 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1467
1468 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1469 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1470 ctx->conn,, &ack->largest_ack);
1471 goto err;
1472 }
1473
1474 if (ack->first_ack_range > ack->largest_ack) {
1475 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1476 ctx->conn,, &ack->first_ack_range);
1477 goto err;
1478 }
1479
1480 largest = ack->largest_ack;
1481 smallest = largest - ack->first_ack_range;
1482 pkts = &qel->pktns->tx.pkts;
1483 pkt_flags = 0;
1484 largest_node = NULL;
1485 time_sent = 0;
1486
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001487 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 +01001488 largest_node = eb64_lookup(pkts, largest);
1489 if (!largest_node) {
1490 TRACE_DEVEL("Largest acked packet not found",
1491 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1492 goto err;
1493 }
1494
1495 time_sent = eb64_entry(&largest_node->node,
1496 struct quic_tx_packet, pn_node)->time_sent;
1497 }
1498
1499 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1500 ctx->conn,, &largest, &smallest);
1501 do {
1502 uint64_t gap, ack_range;
1503
1504 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1505 largest_node, largest, smallest, ctx);
1506 if (!ack->ack_range_num--)
1507 break;
1508
1509 if (!quic_dec_int(&gap, pos, end))
1510 goto err;
1511
1512 if (smallest < gap + 2) {
1513 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1514 ctx->conn,, &gap, &smallest);
1515 goto err;
1516 }
1517
1518 largest = smallest - gap - 2;
1519 if (!quic_dec_int(&ack_range, pos, end))
1520 goto err;
1521
1522 if (largest < ack_range) {
1523 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1524 ctx->conn,, &largest, &ack_range);
1525 goto err;
1526 }
1527
1528 /* Do not use this node anymore. */
1529 largest_node = NULL;
1530 /* Next range */
1531 smallest = largest - ack_range;
1532
1533 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1534 ctx->conn,, &largest, &smallest);
1535 } while (1);
1536
1537 /* Flag this packet number space as having received an ACK. */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02001538 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_RECEIVED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001539
1540 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1541 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001542 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001543 }
1544
1545 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1546 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1547 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1548 if (!LIST_ISEMPTY(&lost_pkts))
1549 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1550 }
1551 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1552 if (quic_peer_validated_addr(ctx))
1553 ctx->conn->qc->path->loss.pto_count = 0;
1554 qc_set_timer(ctx);
1555 }
1556
1557
1558 return 1;
1559
1560 err:
1561 free_quic_tx_pkts(&newly_acked_pkts);
1562 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1563 return 0;
1564}
1565
1566/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1567 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001568 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001569 * Return 1 if succeeded, 0 if not.
1570 */
1571static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001572 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001573 const unsigned char *data, size_t len,
1574 struct quic_rx_packet *pkt,
1575 struct quic_rx_crypto_frm *cf)
1576{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001577 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001578 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001579
1580 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1581 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001582 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001583 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1584 TRACE_PROTO("SSL_provide_quic_data() error",
1585 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1586 goto err;
1587 }
1588
1589 el->rx.crypto.offset += len;
1590 TRACE_PROTO("in order CRYPTO data",
1591 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1592
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001593 state = HA_ATOMIC_LOAD(&qc->state);
1594 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001595 ssl_err = SSL_do_handshake(ctx->ssl);
1596 if (ssl_err != 1) {
1597 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1598 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1599 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001600 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001601 goto out;
1602 }
1603
1604 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001605 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001606 goto err;
1607 }
1608
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001609 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001610 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001611 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001612 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001613 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001614 } else {
1615 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1616 if (ssl_err != 1) {
1617 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1618 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1619 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001620 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001621 goto out;
1622 }
1623
1624 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001625 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001626 goto err;
1627 }
1628
1629 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001630 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001631 }
1632
1633 out:
1634 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1635 return 1;
1636
1637 err:
1638 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1639 return 0;
1640}
1641
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001642/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1643 * <pkt> RX packet.
1644 * Return it if succeeded, NULL if not.
1645 */
1646static inline
1647struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1648 struct quic_rx_packet *pkt)
1649{
1650 struct quic_rx_strm_frm *frm;
1651
1652 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1653 if (frm) {
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001654 frm->offset_node.key = stream_frm->offset.key;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001655 frm->len = stream_frm->len;
1656 frm->data = stream_frm->data;
1657 frm->pkt = pkt;
1658 }
1659
1660 return frm;
1661}
1662
1663/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1664 * several streams, depending on the already open onces.
1665 * Return this node if succeeded, NULL if not.
1666 */
1667static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1668{
1669 unsigned int strm_type;
1670 int64_t sub_id;
1671 struct eb64_node *strm_node;
1672
1673 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1674
1675 strm_type = id & QCS_ID_TYPE_MASK;
1676 sub_id = id >> QCS_ID_TYPE_SHIFT;
1677 strm_node = NULL;
1678 if (qc_local_stream_id(qcc, id)) {
1679 /* Local streams: this stream must be already opened. */
1680 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1681 if (!strm_node) {
1682 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1683 goto out;
1684 }
1685 }
1686 else {
1687 /* Remote streams. */
1688 struct eb_root *strms;
1689 uint64_t largest_id;
1690 enum qcs_type qcs_type;
1691
1692 strms = &qcc->streams_by_id;
1693 qcs_type = qcs_id_type(id);
1694 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1695 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1696 goto out;
1697 }
1698
1699 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1700 * correct value.
1701 */
1702 largest_id = qcc->strms[qcs_type].largest_id;
1703 if (sub_id > (int64_t)largest_id) {
1704 /* RFC: "A stream ID that is used out of order results in all streams
1705 * of that type with lower-numbered stream IDs also being opened".
1706 * So, let's "open" these streams.
1707 */
1708 int64_t i;
1709 struct qcs *qcs;
1710
1711 qcs = NULL;
1712 for (i = largest_id + 1; i <= sub_id; i++) {
1713 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1714 if (!qcs) {
1715 TRACE_PROTO("Could not allocate a new stream",
1716 QUIC_EV_CONN_PSTRM, qcc->conn);
1717 goto out;
1718 }
1719
1720 qcc->strms[qcs_type].largest_id = i;
1721 }
1722 if (qcs)
1723 strm_node = &qcs->by_id;
1724 }
1725 else {
1726 strm_node = eb64_lookup(strms, id);
1727 }
1728 }
1729
1730 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1731 return strm_node;
1732
1733 out:
1734 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1735 return NULL;
1736}
1737
1738/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1739 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1740 * to reflect the data which have been consumed.
1741 */
1742static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1743{
1744 size_t ret;
1745
1746 ret = 0;
1747 while (strm_frm->len) {
1748 size_t try;
1749
1750 try = b_contig_space(buf);
1751 if (!try)
1752 break;
1753
1754 if (try > strm_frm->len)
1755 try = strm_frm->len;
1756 memcpy(b_tail(buf), strm_frm->data, try);
1757 strm_frm->len -= try;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001758 strm_frm->offset.key += try;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001759 b_add(buf, try);
1760 ret += try;
1761 }
1762
1763 return ret;
1764}
1765
1766/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1767 * streams may be open. The data are copied to the stream RX buffer if possible.
1768 * If not, the STREAM frame is stored to be treated again later.
1769 * We rely on the flow control so that not to store too much STREAM frames.
1770 * Return 1 if succeeded, 0 if not.
1771 */
1772static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1773 struct quic_stream *strm_frm,
1774 struct quic_conn *qc)
1775{
1776 struct qcs *strm;
1777 struct eb64_node *strm_node, *frm_node;
1778 struct quic_rx_strm_frm *frm;
1779
1780 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1781 if (!strm_node) {
1782 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1783 return 0;
1784 }
1785
1786 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001787 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001788 /* FIXME: handle the case where this frame overlap others */
1789 if (frm_node) {
1790 TRACE_PROTO("Already existing stream data",
1791 QUIC_EV_CONN_PSTRM, qc->conn);
1792 goto out;
1793 }
1794
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001795 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001796 int ret;
1797
1798 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1799 goto store_frm;
1800
1801 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1802 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1803 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1804 return 0;
1805 }
1806
1807 strm->rx.offset += ret;
1808 }
1809
1810 if (!strm_frm->len)
1811 goto out;
1812
1813 store_frm:
1814 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1815 if (!frm) {
1816 TRACE_PROTO("Could not alloc RX STREAM frame",
1817 QUIC_EV_CONN_PSTRM, qc->conn);
1818 return 0;
1819 }
1820
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001821 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001822 quic_rx_packet_refinc(pkt);
1823
1824 out:
1825 return 1;
1826}
1827
1828/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1829 * streams may be open. The data are copied to the stream RX buffer if possible.
1830 * If not, the STREAM frame is stored to be treated again later.
1831 * We rely on the flow control so that not to store too much STREAM frames.
1832 * Return 1 if succeeded, 0 if not.
1833 */
1834static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1835 struct quic_stream *strm_frm,
1836 struct quic_conn *qc)
1837{
1838 struct qcs *strm;
1839 struct eb64_node *strm_node, *frm_node;
1840 struct quic_rx_strm_frm *frm;
1841 size_t strm_frm_len;
1842
1843 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1844 if (!strm_node) {
1845 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1846 return 0;
1847 }
1848
1849 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001850 frm_node = eb64_lookup(&strm->rx.frms, strm_frm->offset.key);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001851 /* FIXME: handle the case where this frame overlap others */
1852 if (frm_node) {
1853 TRACE_PROTO("Already existing stream data",
1854 QUIC_EV_CONN_PSTRM, qc->conn);
1855 goto out;
1856 }
1857
1858 strm_frm_len = strm_frm->len;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001859 if (strm_frm->offset.key == strm->rx.offset) {
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001860 int ret;
1861
1862 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1863 goto store_frm;
1864
1865 /* qc_strm_cpy() will modify the offset, depending on the number
1866 * of bytes copied.
1867 */
1868 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1869 /* Inform the application of the arrival of this new stream */
1870 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1871 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1872 return 0;
1873 }
1874
1875 if (ret)
1876 ruqs_notify_recv(strm);
1877
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001878 strm_frm->offset.key += ret;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001879 }
1880 /* Take this frame into an account for the stream flow control */
1881 strm->rx.offset += strm_frm_len;
1882 /* It all the data were provided to the application, there is no need to
1883 * store any more inforamtion for it.
1884 */
1885 if (!strm_frm->len)
1886 goto out;
1887
1888 store_frm:
1889 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1890 if (!frm) {
1891 TRACE_PROTO("Could not alloc RX STREAM frame",
1892 QUIC_EV_CONN_PSTRM, qc->conn);
1893 return 0;
1894 }
1895
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02001896 eb64_insert(&strm->rx.frms, &frm->offset_node);
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001897 quic_rx_packet_refinc(pkt);
1898
1899 out:
1900 return 1;
1901}
1902
1903static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1904 struct quic_stream *strm_frm,
1905 struct quic_conn *qc)
1906{
1907 if (strm_frm->id & QCS_ID_DIR_BIT)
1908 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1909 else
1910 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1911}
1912
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001913/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1914 * as I/O handler context and <qel> as encryption level.
1915 * Returns 1 if succeeded, 0 if failed.
1916 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001917static 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 +01001918 struct quic_enc_level *qel)
1919{
1920 struct quic_frame frm;
1921 const unsigned char *pos, *end;
1922 struct quic_conn *conn = ctx->conn->qc;
1923
1924 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1925 /* Skip the AAD */
1926 pos = pkt->data + pkt->aad_len;
1927 end = pkt->data + pkt->len;
1928
1929 while (pos < end) {
1930 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1931 goto err;
1932
1933 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001934 case QUIC_FT_PADDING:
1935 if (pos != end) {
1936 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1937 goto err;
1938 }
1939 break;
1940 case QUIC_FT_PING:
1941 break;
1942 case QUIC_FT_ACK:
1943 {
1944 unsigned int rtt_sample;
1945
1946 rtt_sample = 0;
1947 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1948 goto err;
1949
1950 if (rtt_sample) {
1951 unsigned int ack_delay;
1952
1953 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1954 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1955 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1956 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001957 break;
1958 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001959 case QUIC_FT_CRYPTO:
1960 if (frm.crypto.offset != qel->rx.crypto.offset) {
1961 struct quic_rx_crypto_frm *cf;
1962
1963 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1964 if (!cf) {
1965 TRACE_DEVEL("CRYPTO frame allocation failed",
1966 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1967 goto err;
1968 }
1969
1970 cf->offset_node.key = frm.crypto.offset;
1971 cf->len = frm.crypto.len;
1972 cf->data = frm.crypto.data;
1973 cf->pkt = pkt;
1974 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1975 quic_rx_packet_refinc(pkt);
1976 }
1977 else {
1978 /* XXX TO DO: <cf> is used only for the traces. */
1979 struct quic_rx_crypto_frm cf = { };
1980
1981 cf.offset_node.key = frm.crypto.offset;
1982 cf.len = frm.crypto.len;
1983 if (!qc_provide_cdata(qel, ctx,
1984 frm.crypto.data, frm.crypto.len,
1985 pkt, &cf))
1986 goto err;
1987 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001988 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001989 case QUIC_FT_STREAM_8:
1990 case QUIC_FT_STREAM_9:
1991 case QUIC_FT_STREAM_A:
1992 case QUIC_FT_STREAM_B:
1993 case QUIC_FT_STREAM_C:
1994 case QUIC_FT_STREAM_D:
1995 case QUIC_FT_STREAM_E:
1996 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001997 {
1998 struct quic_stream *stream = &frm.stream;
1999
2000 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
2001 if (objt_listener(ctx->conn->target)) {
2002 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
2003 goto err;
2004 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
2005 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01002006
2007 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
2008 goto err;
2009
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01002010 break;
2011 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01002012 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002013 break;
2014 case QUIC_FT_CONNECTION_CLOSE:
2015 case QUIC_FT_CONNECTION_CLOSE_APP:
2016 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002017 case QUIC_FT_HANDSHAKE_DONE:
2018 if (objt_listener(ctx->conn->target))
2019 goto err;
2020
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002021 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002022 break;
2023 default:
2024 goto err;
2025 }
2026 }
2027
2028 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
2029 * has successfully parse a Handshake packet. The Initial encryption must also
2030 * be discarded.
2031 */
Frédéric Lécaille8c27de72021-09-20 11:00:46 +02002032 if (pkt->type == QUIC_PACKET_TYPE_HANDSHAKE && objt_listener(ctx->conn->target)) {
2033 int state = HA_ATOMIC_LOAD(&conn->state);
2034
2035 if (state >= QUIC_HS_ST_SERVER_INITIAL) {
2036 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2037 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2038 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
2039 qc_set_timer(ctx);
2040 if (state < QUIC_HS_ST_SERVER_HANDSHAKE)
2041 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
2042 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002043 }
2044
2045 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
2046 return 1;
2047
2048 err:
2049 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
2050 return 0;
2051}
2052
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002053/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
2054 * buffer. This is the responsability of the caller to check there is enough
2055 * room in <cbuf>. Also increase the <cbuf> write index consequently.
2056 * This function must be called only after having built a correct datagram.
2057 * Always succeeds.
2058 */
2059static inline void qc_set_dg(struct cbuf *cbuf,
2060 uint16_t dglen, struct quic_tx_packet *pkt)
2061{
2062 write_u16(cb_wr(cbuf), dglen);
2063 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
2064 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
2065}
2066
2067/* Prepare as much as possible handshake packets into <qr> ring buffer for
2068 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
2069 * several packets in the same datagram. A header made of two fields is added
2070 * to each datagram: the datagram length followed by the address of the first
2071 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002072 * Returns 1 if succeeded, or 0 if something wrong happened.
2073 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002074static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002075{
2076 struct quic_conn *qc;
2077 enum quic_tls_enc_level tel, next_tel;
2078 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002079 struct cbuf *cbuf;
2080 unsigned char *end_buf, *end, *pos, *spos;
2081 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2082 /* length of datagrams */
2083 uint16_t dglen;
2084 size_t total;
2085 /* Each datagram is prepended with its length followed by the
2086 * address of the first packet in the datagram.
2087 */
2088 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002089
2090 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2091 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002092 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002093 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002094 goto err;
2095 }
2096
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002097 start:
2098 total = 0;
2099 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002100 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002101 cbuf = qr->cbuf;
2102 spos = pos = cb_wr(cbuf);
2103 /* Leave at least <dglen> bytes at the end of this buffer
2104 * to ensure there is enough room to mark the end of prepared
2105 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002106 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002107 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2108 first_pkt = prv_pkt = NULL;
2109 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002110 int err, nb_ptos, ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002111 enum quic_pkt_type pkt_type;
2112
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002113 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002114 nb_ptos = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002115 if (!prv_pkt) {
2116 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2117 do {
2118 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2119 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2120 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002121 ack = HA_ATOMIC_BTR(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002122 /* Do not build any more packet if the TX secrets are not available or
2123 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002124 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002125 * and if there is no more CRYPTO data available or in flight
2126 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002127 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002128 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002129 (!ack && !nb_ptos &&
2130 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2131 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002132 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002133 /* Set the current datagram as prepared into <cbuf> if
2134 * the was already a correct packet which was previously written.
2135 */
2136 if (prv_pkt)
2137 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002138 break;
2139 }
2140
2141 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002142 if (!prv_pkt) {
2143 /* Leave room for the datagram header */
2144 pos += dg_headlen;
2145 end = pos + qc->path->mtu;
2146 }
2147
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002148 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 +02002149 /* Restore the PTO dgrams counter if a packet could not be built */
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002150 if (err < 0) {
2151 if (!prv_pkt && nb_ptos)
2152 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002153 if (ack)
2154 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002155 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002156 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002157 case -2:
2158 goto err;
2159 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002160 /* If there was already a correct packet present, set the
2161 * current datagram as prepared into <cbuf>.
2162 */
2163 if (prv_pkt) {
2164 qc_set_dg(cbuf, dglen, first_pkt);
2165 goto stop_build;
2166 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002167 goto out;
2168 default:
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02002169 /* This is to please to GCC. We cannot have (err >= 0 && !cur_pkt) */
2170 if (!cur_pkt)
2171 goto err;
2172
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002173 total += cur_pkt->len;
2174 /* keep trace of the first packet in the datagram */
2175 if (!first_pkt)
2176 first_pkt = cur_pkt;
2177 /* Attach the current one to the previous one */
2178 if (prv_pkt)
2179 prv_pkt->next = cur_pkt;
2180 /* Let's say we have to build a new dgram */
2181 prv_pkt = NULL;
2182 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002183 /* Discard the Initial encryption keys as soon as
2184 * a handshake packet could be built.
2185 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002186 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002187 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2188 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002189 TRACE_PROTO("discarding Initial pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002190 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2191 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002192 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002193 }
2194 /* Special case for Initial packets: when they have all
2195 * been sent, select the next level.
2196 */
Frédéric Lécailled0670882021-08-19 07:53:27 +02002197 if ((tel == QUIC_TLS_ENC_LEVEL_INITIAL || tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE) &&
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002198 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
2199 (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 +02002200 /* If QUIC_TLS_ENC_LEVEL_HANDSHAKE was already reached let's try QUIC_TLS_ENC_LEVEL_APP */
2201 if (tel == QUIC_TLS_ENC_LEVEL_HANDSHAKE && next_tel == tel)
2202 next_tel = QUIC_TLS_ENC_LEVEL_APP;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002203 tel = next_tel;
2204 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002205 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002206 /* If there is data for the next level, do not
2207 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002208 * which sends only one Initial packet, then wait
2209 * for additional CRYPTO data from the server to enter the
2210 * next level.
2211 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002212 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002213 }
2214 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002215 }
2216
2217 /* If we have to build a new datagram, set the current datagram as
2218 * prepared into <cbuf>.
2219 */
2220 if (!prv_pkt) {
2221 qc_set_dg(cbuf, dglen, first_pkt);
2222 first_pkt = NULL;
2223 dglen = 0;
2224 }
2225 }
2226
2227 stop_build:
2228 /* Reset <wr> writer index if in front of <rd> index */
2229 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2230 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2231
2232 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2233 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2234 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2235 /* Mark the end of contiguous data for the reader */
2236 write_u16(cb_wr(cbuf), 0);
2237 cb_add(cbuf, sizeof(uint16_t));
2238 }
2239 }
2240
2241 if (rd && rd <= cbuf->wr) {
2242 cb_wr_reset(cbuf);
2243 if (pos == spos) {
2244 /* Reuse the same buffer if nothing was built. */
2245 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002246 }
2247 }
2248 }
2249
2250 out:
2251 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002252 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253
2254 err:
2255 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002256 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002257}
2258
2259/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002260 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002261 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002262int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002263{
2264 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002265 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002266
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002267 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002268 cbuf = qr->cbuf;
2269 while (cb_contig_data(cbuf)) {
2270 unsigned char *pos;
2271 struct buffer tmpbuf = { };
2272 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2273 uint16_t dglen;
2274 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002275 unsigned int time_sent;
2276
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002277 pos = cb_rd(cbuf);
2278 dglen = read_u16(pos);
2279 /* End of prepared datagrams.
2280 * Reset the reader index only if in front of the writer index.
2281 */
2282 if (!dglen) {
2283 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2284
2285 if (wr && wr < cbuf->rd) {
2286 cb_rd_reset(cbuf);
2287 continue;
2288 }
2289 break;
2290 }
2291
2292 pos += sizeof dglen;
2293 first_pkt = read_ptr(pos);
2294 pos += sizeof first_pkt;
2295 tmpbuf.area = (char *)pos;
2296 tmpbuf.size = tmpbuf.data = dglen;
2297
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002298 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002299 for (pkt = first_pkt; pkt; pkt = pkt->next)
2300 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002301 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002302 &tmpbuf, tmpbuf.data, 0) <= 0) {
2303 for (pkt = first_pkt; pkt; pkt = pkt->next)
2304 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002305 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002306 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002307
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002308 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002309 qc->tx.bytes += tmpbuf.data;
2310 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002311
2312 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2313 pkt->time_sent = time_sent;
2314 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2315 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002316 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002317 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002318 qc->path->in_flight += pkt->in_flight_len;
2319 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2320 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002321 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002322 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2323 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002324 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002325 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002326 }
2327 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002328
2329 return 1;
2330}
2331
2332/* Build all the frames which must be sent just after the handshake have succeeded.
2333 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2334 * a HANDSHAKE_DONE frame.
2335 * Return 1 if succeeded, 0 if not.
2336 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002337static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002338{
2339 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002340 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002341 struct quic_frame *frm;
2342
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002343 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002344 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002345 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002346 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002347 if (!frm)
2348 return 0;
2349
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002350 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002351 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002352 }
2353
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002354 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002355 struct quic_connection_id *cid;
2356
2357 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002358 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002359 if (!frm || !cid)
2360 goto err;
2361
2362 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002363 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002364 }
2365
2366 return 1;
2367
2368 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002369 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002370 return 0;
2371}
2372
2373/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002374void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002375{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002376 struct eb64_node *n;
2377 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002378
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002379 n = eb64_first(&arngs->root);
2380 while (n) {
2381 struct eb64_node *next;
2382
2383 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2384 next = eb64_next(n);
2385 eb64_delete(n);
2386 free(ar);
2387 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002388 }
2389}
2390
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002391/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2392 * descending order.
2393 */
2394static inline size_t sack_gap(struct quic_arng_node *p,
2395 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002396{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002397 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398}
2399
2400
2401/* Remove the last elements of <ack_ranges> list of ack range updating its
2402 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002403 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002404 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002405static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002406{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002407 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002408
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002409 last = eb64_last(&arngs->root);
2410 while (last && arngs->enc_sz > limit) {
2411 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002412
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002413 prev = eb64_prev(last);
2414 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002415 return 0;
2416
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002417 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2418 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2419 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2420 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2421 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2422 --arngs->sz;
2423 eb64_delete(last);
2424 pool_free(pool_head_quic_arng, last);
2425 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002426 }
2427
2428 return 1;
2429}
2430
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002431/* Set the encoded size of <arngs> QUIC ack ranges. */
2432static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2433{
2434 struct eb64_node *node, *next;
2435 struct quic_arng_node *ar, *ar_next;
2436
2437 node = eb64_last(&arngs->root);
2438 if (!node)
2439 return;
2440
2441 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2442 arngs->enc_sz = quic_int_getsize(ar->last) +
2443 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2444
2445 while ((next = eb64_prev(node))) {
2446 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2447 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2448 quic_int_getsize(ar_next->last - ar_next->first.key);
2449 node = next;
2450 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2451 }
2452}
2453
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002454/* Insert <ar> ack range into <argns> tree of ack ranges.
2455 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002456 */
2457static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002458struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2459 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002460{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002461 struct quic_arng_node *new_ar;
2462
2463 new_ar = pool_alloc(pool_head_quic_arng);
2464 if (new_ar) {
2465 new_ar->first.key = ar->first;
2466 new_ar->last = ar->last;
2467 eb64_insert(&arngs->root, &new_ar->first);
2468 arngs->sz++;
2469 }
2470
2471 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002472}
2473
2474/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002475 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002476 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002477 *
2478 * Descending order
2479 * ------------->
2480 * range1 range2
2481 * ..........|--------|..............|--------|
2482 * ^ ^ ^ ^
2483 * | | | |
2484 * last1 first1 last2 first2
2485 * ..........+--------+--------------+--------+......
2486 * diff1 gap12 diff2
2487 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002488 * To encode the previous list of ranges we must encode integers as follows in
2489 * descending order:
2490 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002491 * with diff1 = last1 - first1
2492 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002493 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002494 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002495 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002496int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2497 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002498{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002499 struct eb64_node *le;
2500 struct quic_arng_node *new_node;
2501 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002502
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002503 new = NULL;
2504 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002505 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002506 if (!new_node)
2507 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002508
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002509 goto out;
2510 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002511
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002512 le = eb64_lookup_le(&arngs->root, ar->first);
2513 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002514 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002515 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002516 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002517 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002518 else {
2519 struct quic_arng_node *le_ar =
2520 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002521
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002522 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002523 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002524 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002526 if (le_ar->last + 1 >= ar->first) {
2527 le_ar->last = ar->last;
2528 new = le;
2529 new_node = le_ar;
2530 }
2531 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002532 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002533 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002534 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002535
2536 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002537 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002538 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002539
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002540 /* Verify that the new inserted node does not overlap the nodes
2541 * which follow it.
2542 */
2543 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002544 struct eb64_node *next;
2545 struct quic_arng_node *next_node;
2546
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002547 while ((next = eb64_next(new))) {
2548 next_node =
2549 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002550 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002551 break;
2552
2553 if (next_node->last > new_node->last)
2554 new_node->last = next_node->last;
2555 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002556 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002557 /* Decrement the size of these ranges. */
2558 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002559 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002560 }
2561
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002562 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002563 quic_arngs_set_enc_sz(arngs);
2564
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002565 return 1;
2566}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002567/* Remove the header protection of packets at <el> encryption level.
2568 * Always succeeds.
2569 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002570static 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 +01002571{
2572 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002573 struct quic_rx_packet *pqpkt;
2574 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 struct quic_enc_level *app_qel;
2576
2577 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2578 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2579 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002580 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002581 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002582 TRACE_PROTO("hp not removed (handshake not completed)",
2583 QUIC_EV_CONN_ELRMHP, ctx->conn);
2584 goto out;
2585 }
2586 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002587 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002588 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2589 pqpkt->data + pqpkt->pn_offset,
2590 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2591 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2592 /* XXX TO DO XXX */
2593 }
2594 else {
2595 /* The AAD includes the packet number field */
2596 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2597 /* Store the packet into the tree of packets to decrypt. */
2598 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002599 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002600 eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
2601 quic_rx_packet_refinc(pqpkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002602 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002603 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2604 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002605 MT_LIST_DELETE_SAFE(pkttmp1);
2606 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002607 }
2608
2609 out:
2610 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2611}
2612
2613/* Process all the CRYPTO frame at <el> encryption level.
2614 * Return 1 if succeeded, 0 if not.
2615 */
2616static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002617 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002618{
2619 struct eb64_node *node;
2620
2621 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002622 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002623 node = eb64_first(&el->rx.crypto.frms);
2624 while (node) {
2625 struct quic_rx_crypto_frm *cf;
2626
2627 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2628 if (cf->offset_node.key != el->rx.crypto.offset)
2629 break;
2630
2631 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2632 goto err;
2633
2634 node = eb64_next(node);
2635 quic_rx_packet_refdec(cf->pkt);
2636 eb64_delete(&cf->offset_node);
2637 pool_free(pool_head_quic_rx_crypto_frm, cf);
2638 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002639 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002640 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2641 return 1;
2642
2643 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002644 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002645 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2646 return 0;
2647}
2648
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002649/* Process all the packets at <el> and <next_el> encryption level.
2650 * This is the caller responsability to check that <cur_el> is different of <next_el>
2651 * as pointer value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002652 * Return 1 if succeeded, 0 if not.
2653 */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002654int qc_treat_rx_pkts(struct quic_enc_level *cur_el, struct quic_enc_level *next_el,
2655 struct ssl_sock_ctx *ctx, int force_ack)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002656{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002657 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002658 int64_t largest_pn = -1;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002659 struct quic_conn *qc = ctx->conn->qc;
2660 struct quic_enc_level *qel = cur_el;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002661
2662 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002663 qel = cur_el;
2664 next_tel:
2665 if (!qel)
2666 goto out;
2667
2668 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
2669 node = eb64_first(&qel->rx.pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002670 while (node) {
2671 struct quic_rx_packet *pkt;
2672
2673 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002674 TRACE_PROTO("new packet", QUIC_EV_CONN_ELRXPKTS,
2675 ctx->conn, pkt, NULL, ctx->ssl);
2676 if (!qc_pkt_decrypt(pkt, &qel->tls_ctx)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002677 /* Drop the packet */
2678 TRACE_PROTO("packet decryption failed -> dropped",
2679 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2680 }
2681 else {
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002682 if (!qc_parse_pkt_frms(pkt, ctx, qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002683 /* Drop the packet */
2684 TRACE_PROTO("packet parsing failed -> dropped",
2685 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2686 }
2687 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002688 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2689
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002690 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002691 (!(HA_ATOMIC_ADD_FETCH(&qc->rx.nb_ack_eliciting, 1) & 1) || force_ack))
2692 HA_ATOMIC_BTS(&qc->flags, QUIC_FL_PKTNS_ACK_REQUIRED_BIT);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002693 if (pkt->pn > largest_pn)
2694 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002695 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002696 if (!quic_update_ack_ranges_list(&qel->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002697 TRACE_DEVEL("Could not update ack range list",
2698 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002699 }
2700 }
2701 node = eb64_next(node);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02002702 eb64_delete(&pkt->pn_node);
2703 quic_rx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002704 }
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002705 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002706
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002707 /* Update the largest packet number. */
2708 if (largest_pn != -1)
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002709 HA_ATOMIC_UPDATE_MAX(&qel->pktns->rx.largest_pn, largest_pn);
2710 if (!qc_treat_rx_crypto_frms(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002711 goto err;
2712
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002713 if (qel == cur_el) {
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002714 BUG_ON(qel == next_el);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002715 qel = next_el;
2716 goto next_tel;
2717 }
2718
2719 out:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002720 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2721 return 1;
2722
2723 err:
2724 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2725 return 0;
2726}
2727
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002728/* QUIC connection packet handler task. */
2729struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002730{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002731 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002732 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002733 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002734 enum quic_tls_enc_level tel, next_tel;
2735 struct quic_enc_level *qel, *next_qel;
2736 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002737 struct qring *qr; // Tx ring
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002738 int prev_st, st, force_ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002739
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002740 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002741 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002742 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002743 st = HA_ATOMIC_LOAD(&qc->state);
2744 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002745 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002746 start:
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002747 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002748 goto err;
2749
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002750 qel = &qc->els[tel];
Frédéric Lécaillef7980962021-08-19 17:35:21 +02002751 next_qel = next_tel == QUIC_TLS_ENC_LEVEL_NONE ? NULL : &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752
2753 next_level:
2754 tls_ctx = &qel->tls_ctx;
2755
2756 /* If the header protection key for this level has been derived,
2757 * remove the packet header protections.
2758 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002759 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2761 qc_rm_hp_pkts(qel, ctx);
2762
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002763 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002764 force_ack = qel == &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL] ||
2765 qel == &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2766 if (!qc_treat_rx_pkts(qel, next_qel, ctx, force_ack))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002767 goto err;
2768
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002769 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille754f99e2021-08-19 15:35:59 +02002770 if (st >= QUIC_HS_ST_COMPLETE &&
2771 (prev_st == QUIC_HS_ST_SERVER_INITIAL || prev_st == QUIC_HS_ST_SERVER_HANDSHAKE)) {
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002772 /* Discard the Handshake keys. */
2773 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
Frédéric Lécailleacd43a52021-09-20 11:18:24 +02002774 TRACE_PROTO("discarding Handshake pktns", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002775 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2776 qc_set_timer(ctx);
2777 if (!quic_build_post_handshake_frames(qc))
2778 goto err;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02002779 goto start;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002780 }
2781
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002782 if (!qr)
2783 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2784 ret = qc_prep_hdshk_pkts(qr, ctx);
2785 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002786 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002787 else if (ret == 0)
2788 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002789
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002790 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002791 goto err;
2792
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002793 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002794 /* Check if there is something to do for the next level.
2795 */
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002796 if (next_qel && next_qel != qel &&
2797 (next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002798 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002799 qel = next_qel;
Frédéric Lécaille3230bcf2021-09-22 15:15:46 +02002800 next_qel = NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002801 goto next_level;
2802 }
2803
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002804 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002805 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002806 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002807
2808 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002809 if (qr)
2810 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002811 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002812 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002813}
2814
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002815/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002816static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2817{
2818 int i;
2819
2820 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2821 if (qel->tx.crypto.bufs[i]) {
2822 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2823 qel->tx.crypto.bufs[i] = NULL;
2824 }
2825 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002826 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002827}
2828
2829/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002830 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002831 * Returns 1 if succeeded, 0 if not.
2832 */
2833static int quic_conn_enc_level_init(struct quic_conn *qc,
2834 enum quic_tls_enc_level level)
2835{
2836 struct quic_enc_level *qel;
2837
2838 qel = &qc->els[level];
2839 qel->level = quic_to_ssl_enc_level(level);
2840 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2841 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2842 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2843 qel->tls_ctx.rx.flags = 0;
2844 qel->tls_ctx.tx.flags = 0;
2845
2846 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002847 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002848 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002849 qel->rx.crypto.offset = 0;
2850 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2851 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002852
2853 /* Allocate only one buffer. */
2854 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2855 if (!qel->tx.crypto.bufs)
2856 goto err;
2857
2858 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2859 if (!qel->tx.crypto.bufs[0])
2860 goto err;
2861
2862 qel->tx.crypto.bufs[0]->sz = 0;
2863 qel->tx.crypto.nb_buf = 1;
2864
2865 qel->tx.crypto.sz = 0;
2866 qel->tx.crypto.offset = 0;
2867
2868 return 1;
2869
2870 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002871 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002872 return 0;
2873}
2874
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002875/* Release all the memory allocated for <conn> QUIC connection. */
2876static void quic_conn_free(struct quic_conn *conn)
2877{
2878 int i;
2879
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002880 if (!conn)
2881 return;
2882
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883 free_quic_conn_cids(conn);
2884 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2885 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002886 if (conn->timer_task)
2887 task_destroy(conn->timer_task);
2888 pool_free(pool_head_quic_conn, conn);
2889}
2890
Amaury Denoyelle414cac52021-09-22 11:14:37 +02002891void quic_close(struct connection *conn, void *xprt_ctx)
2892{
2893 struct ssl_sock_ctx *conn_ctx = xprt_ctx;
2894 struct quic_conn *qc = conn_ctx->conn->qc;
2895 quic_conn_free(qc);
2896}
2897
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002898/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002899static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002900{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002901 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002902 struct quic_conn *qc;
2903 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002904 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002905
2906 conn_ctx = task->context;
2907 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002908 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2909 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002910 task->expire = TICK_ETERNITY;
2911 pktns = quic_loss_pktns(qc);
2912 if (tick_isset(pktns->tx.loss_time)) {
2913 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2914
2915 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2916 if (!LIST_ISEMPTY(&lost_pkts))
2917 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2918 qc_set_timer(conn_ctx);
2919 goto out;
2920 }
2921
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002922 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002923 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002924 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002925 pktns->tx.pto_probe = 1;
2926 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002927 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002928 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2929 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2930
2931 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2932 hel->pktns->tx.pto_probe = 1;
2933 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2934 iel->pktns->tx.pto_probe = 1;
2935 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002936 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002937 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2938 qc->path->loss.pto_count++;
2939
2940 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002941 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002942
2943 return task;
2944}
2945
2946/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2947 * connections used to identify the first Initial packets of client connecting
2948 * to listeners. This parameter must be NULL for QUIC connections attached
2949 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2950 * <scid> is the source connection ID with <scid_len> as length.
2951 * Returns 1 if succeeded, 0 if not.
2952 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002953static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2954 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002955 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002956{
2957 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002958 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002959 /* Initial CID. */
2960 struct quic_connection_id *icid;
2961
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002962 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002963 qc = pool_zalloc(pool_head_quic_conn);
2964 if (!qc) {
2965 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2966 goto err;
2967 }
2968
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002969 qc->cids = EB_ROOT;
2970 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002971 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002972 struct listener *l = owner;
2973
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002974 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002975 /* Copy the initial DCID. */
2976 qc->odcid.len = dcid_len;
2977 if (qc->odcid.len)
2978 memcpy(qc->odcid.data, dcid, dcid_len);
2979
2980 /* Copy the SCID as our DCID for this connection. */
2981 if (scid_len)
2982 memcpy(qc->dcid.data, scid, scid_len);
2983 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002984 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002985 }
2986 /* QUIC Client (outgoing connection to servers) */
2987 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002988 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002989 if (dcid_len)
2990 memcpy(qc->dcid.data, dcid, dcid_len);
2991 qc->dcid.len = dcid_len;
2992 }
2993
2994 /* Initialize the output buffer */
2995 qc->obuf.pos = qc->obuf.data;
2996
2997 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002998 if (!icid) {
2999 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
3000 goto err;
3001 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003002
3003 /* Select our SCID which is the first CID with 0 as sequence number. */
3004 qc->scid = icid->cid;
3005
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003006 /* Packet number spaces initialization. */
3007 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
3008 quic_pktns_init(&qc->pktns[i]);
3009 /* QUIC encryption level context initialization. */
3010 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003011 if (!quic_conn_enc_level_init(qc, i)) {
3012 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003013 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003014 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003015 /* Initialize the packet number space. */
3016 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
3017 }
3018
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02003019 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003020 /* TX part. */
3021 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003022 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
3023 qc->tx.wbuf = qc->tx.rbuf = 0;
3024 qc->tx.bytes = 0;
3025 qc->tx.nb_pto_dgrams = 0;
3026 /* RX part. */
3027 qc->rx.bytes = 0;
Frédéric Lécaille2766e782021-08-30 17:16:07 +02003028 qc->rx.nb_ack_eliciting = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003029
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003030 /* XXX TO DO: Only one path at this time. */
3031 qc->path = &qc->paths[0];
3032 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
3033
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003034 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003035
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003036 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003037
3038 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003039 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003040 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003041 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003042}
3043
3044/* Initialize the timer task of <qc> QUIC connection.
3045 * Returns 1 if succeeded, 0 if not.
3046 */
3047static int quic_conn_init_timer(struct quic_conn *qc)
3048{
3049 qc->timer_task = task_new(MAX_THREADS_MASK);
3050 if (!qc->timer_task)
3051 return 0;
3052
3053 qc->timer = TICK_ETERNITY;
3054 qc->timer_task->process = process_timer;
3055 qc->timer_task->context = qc->conn->xprt_ctx;
3056
3057 return 1;
3058}
3059
3060/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
3061 * past one byte of this buffer.
3062 */
3063static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
3064 struct quic_rx_packet *pkt)
3065{
3066 unsigned char dcid_len, scid_len;
3067
3068 /* Version */
3069 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
3070 return 0;
3071
3072 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
3073
3074 /* Destination Connection ID Length */
3075 dcid_len = *(*buf)++;
3076 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
3077 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
3078 /* XXX MUST BE DROPPED */
3079 return 0;
3080
3081 if (dcid_len) {
3082 /* Check that the length of this received DCID matches the CID lengths
3083 * of our implementation for non Initials packets only.
3084 */
3085 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
3086 return 0;
3087
3088 memcpy(pkt->dcid.data, *buf, dcid_len);
3089 }
3090
3091 pkt->dcid.len = dcid_len;
3092 *buf += dcid_len;
3093
3094 /* Source Connection ID Length */
3095 scid_len = *(*buf)++;
3096 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
3097 /* XXX MUST BE DROPPED */
3098 return 0;
3099
3100 if (scid_len)
3101 memcpy(pkt->scid.data, *buf, scid_len);
3102 pkt->scid.len = scid_len;
3103 *buf += scid_len;
3104
3105 return 1;
3106}
3107
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003108/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
3109 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
3110 * encryption level matching with the packet type. <*qel> may be null if not found.
3111 * Note that <ctx> may be null (for Initial packets).
3112 */
3113static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003114 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003115 struct quic_enc_level **qel)
3116{
3117 enum quic_tls_enc_level tel;
3118
3119 /* Special case without connection context (firt Initial packets) */
3120 if (!ctx) {
3121 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
3122 return 1;
3123 }
3124
3125 tel = quic_packet_type_enc_level(pkt->type);
3126 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
3127 *qel = NULL;
3128 return 0;
3129 }
3130
3131 *qel = &qc->els[tel];
3132 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3133 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3134 return 0;
3135 }
3136
3137 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003138 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3139 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003140 return 1;
3141
3142 return 0;
3143}
3144
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003145/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3146 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3147 * byte past the end of the buffer containing this packet and <beg> the address of
3148 * the packet first byte.
3149 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3150 * Returns 1 if succeeded, 0 if not.
3151 */
3152static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3153 unsigned char **buf, unsigned char *beg,
3154 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003155 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003156{
3157 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003158 struct quic_enc_level *qel;
3159 /* Only for traces. */
3160 struct quic_rx_packet *qpkt_trace;
3161
3162 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003163 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003164 /* The packet number is here. This is also the start minus
3165 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3166 * protection.
3167 */
3168 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003169 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003170 /* Note that the following function enables us to unprotect the packet
3171 * number and its length subsequently used to decrypt the entire
3172 * packets.
3173 */
3174 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3175 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003176 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003177 goto err;
3178 }
3179
3180 /* The AAD includes the packet number field found at <pn>. */
3181 pkt->aad_len = pn - beg + pkt->pnl;
3182 qpkt_trace = pkt;
3183 /* Store the packet */
3184 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003185 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003186 eb64_insert(&qel->rx.pkts, &pkt->pn_node);
3187 quic_rx_packet_refinc(pkt);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003188 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003189 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003190 else if (qel) {
3191 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003192 pkt->pn_offset = pn - beg;
Frédéric Lécailleebc3fc12021-09-22 08:34:21 +02003193 MT_LIST_APPEND(&qel->rx.pqpkts, &pkt->list);
3194 quic_rx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003195 }
3196
3197 memcpy(pkt->data, beg, pkt->len);
3198 /* Updtate the offset of <*buf> for the next QUIC packet. */
3199 *buf = beg + pkt->len;
3200
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003201 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003202 return 1;
3203
3204 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003205 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 +01003206 return 0;
3207}
3208
3209/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3210 * Also set <*long_header> to 1 if this form is long, 0 if not.
3211 */
3212static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3213 unsigned char byte0, int *long_header)
3214{
3215 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3216 pkt->type =
3217 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3218 *long_header = 1;
3219 }
3220 else {
3221 pkt->type = QUIC_PACKET_TYPE_SHORT;
3222 *long_header = 0;
3223 }
3224}
3225
3226static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3227 struct quic_rx_packet *pkt,
3228 struct quic_dgram_ctx *dgram_ctx,
3229 struct sockaddr_storage *saddr)
3230{
3231 unsigned char *beg;
3232 uint64_t len;
3233 struct quic_conn *qc;
3234 struct eb_root *cids;
3235 struct ebmb_node *node;
3236 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003237 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003238 int long_header;
3239
3240 qc = NULL;
3241 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3242 if (end <= *buf)
3243 goto err;
3244
3245 /* Fixed bit */
3246 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3247 /* XXX TO BE DISCARDED */
3248 goto err;
3249
3250 srv_conn = dgram_ctx->owner;
3251 beg = *buf;
3252 /* Header form */
3253 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3254 if (long_header) {
3255 size_t cid_lookup_len;
3256
3257 if (!quic_packet_read_long_header(buf, end, pkt))
3258 goto err;
3259
3260 /* For Initial packets, and for servers (QUIC clients connections),
3261 * there is no Initial connection IDs storage.
3262 */
3263 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3264 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3265 cid_lookup_len = pkt->dcid.len;
3266 }
3267 else {
3268 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3269 cid_lookup_len = QUIC_CID_LEN;
3270 }
3271
3272 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3273 if (!node)
3274 goto err;
3275
3276 qc = ebmb_entry(node, struct quic_conn, scid_node);
3277
3278 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3279 qc->dcid.len = pkt->scid.len;
3280 if (pkt->scid.len)
3281 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3282 }
3283
3284 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3285 uint64_t token_len;
3286
3287 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3288 goto err;
3289
3290 /* XXX TO DO XXX 0 value means "the token is not present".
3291 * A server which sends an Initial packet must not set the token.
3292 * So, a client which receives an Initial packet with a token
3293 * MUST discard the packet or generate a connection error with
3294 * PROTOCOL_VIOLATION as type.
3295 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3296 */
3297 pkt->token_len = token_len;
3298 }
3299 }
3300 else {
3301 /* XXX TO DO: Short header XXX */
3302 if (end - *buf < QUIC_CID_LEN)
3303 goto err;
3304
3305 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3306 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3307 if (!node)
3308 goto err;
3309
3310 qc = ebmb_entry(node, struct quic_conn, scid_node);
3311 *buf += QUIC_CID_LEN;
3312 }
3313 /* Store the DCID used for this packet to check the packet which
3314 * come in this UDP datagram match with it.
3315 */
3316 if (!dgram_ctx->dcid_node)
3317 dgram_ctx->dcid_node = node;
3318 /* Only packets packets with long headers and not RETRY or VERSION as type
3319 * have a length field.
3320 */
3321 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3322 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3323 goto err;
3324
3325 pkt->len = len;
3326 }
3327 else if (!long_header) {
3328 /* A short packet is the last one of an UDP datagram. */
3329 pkt->len = end - *buf;
3330 }
3331
3332 conn_ctx = qc->conn->xprt_ctx;
3333
3334 /* Increase the total length of this packet by the header length. */
3335 pkt->len += *buf - beg;
3336 /* Do not check the DCID node before the length. */
3337 if (dgram_ctx->dcid_node != node) {
3338 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3339 goto err;
3340 }
3341
3342 if (pkt->len > sizeof pkt->data) {
3343 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3344 goto err;
3345 }
3346
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003347 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003348 goto err;
3349
3350 /* Wake the tasklet of the QUIC connection packet handler. */
3351 if (conn_ctx)
3352 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3353
3354 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3355
3356 return pkt->len;
3357
3358 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003359 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003360 return -1;
3361}
3362
3363static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3364 struct quic_rx_packet *pkt,
3365 struct quic_dgram_ctx *dgram_ctx,
3366 struct sockaddr_storage *saddr)
3367{
3368 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003369 struct quic_conn *qc;
3370 struct eb_root *cids;
3371 struct ebmb_node *node;
3372 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003373 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003374 int long_header = 0;
3375
3376 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003377 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003378 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003379 if (end <= *buf)
3380 goto err;
3381
3382 /* Fixed bit */
3383 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3384 /* XXX TO BE DISCARDED */
3385 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3386 goto err;
3387 }
3388
3389 l = dgram_ctx->owner;
3390 beg = *buf;
3391 /* Header form */
3392 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3393 if (long_header) {
3394 unsigned char dcid_len;
3395
3396 if (!quic_packet_read_long_header(buf, end, pkt)) {
3397 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3398 goto err;
3399 }
3400
3401 dcid_len = pkt->dcid.len;
3402 /* For Initial packets, and for servers (QUIC clients connections),
3403 * there is no Initial connection IDs storage.
3404 */
3405 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003406 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003407 /* DCIDs of first packets coming from clients may have the same values.
3408 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3409 */
3410 quic_cid_saddr_cat(&pkt->dcid, saddr);
3411 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003412
3413 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3414 end - *buf < token_len) {
3415 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3416 goto err;
3417 }
3418
3419 /* XXX TO DO XXX 0 value means "the token is not present".
3420 * A server which sends an Initial packet must not set the token.
3421 * So, a client which receives an Initial packet with a token
3422 * MUST discard the packet or generate a connection error with
3423 * PROTOCOL_VIOLATION as type.
3424 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3425 */
3426 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003427 }
3428 else {
3429 if (pkt->dcid.len != QUIC_CID_LEN) {
3430 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3431 goto err;
3432 }
3433
3434 cids = &l->rx.cids;
3435 }
3436
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003437 /* Only packets packets with long headers and not RETRY or VERSION as type
3438 * have a length field.
3439 */
3440 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3441 uint64_t len;
3442
3443 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3444 end - *buf < len) {
3445 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3446 goto err;
3447 }
3448
3449 pkt->len = len;
3450 }
3451
3452
3453 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003454 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3455 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3456 cids == &l->rx.odcids) {
3457 /* Switch to the definitive tree ->cids containing the final CIDs. */
3458 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3459 if (node) {
3460 /* If found, signal this with NULL as special value for <cids>. */
3461 pkt->dcid.len = dcid_len;
3462 cids = NULL;
3463 }
3464 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003465 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003466
3467 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003468 int ipv4;
3469 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003470 struct ebmb_node *n = NULL;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003471 const unsigned char *salt = initial_salt_v1;
3472 size_t salt_len = sizeof initial_salt_v1;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003473
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003474 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3475 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3476 goto err;
3477 }
3478
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003479 pkt->saddr = *saddr;
3480 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3481 * of <saddr>.
3482 */
3483 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003484 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003485 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003486 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003487 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003488 goto err;
3489
3490 odcid = &qc->rx.params.original_destination_connection_id;
3491 /* Copy the transport parameters. */
3492 qc->rx.params = l->bind_conf->quic_params;
3493 /* Copy original_destination_connection_id transport parameter. */
3494 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3495 odcid->len = pkt->odcid_len;
3496 /* Copy the initial source connection ID. */
3497 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3498 qc->enc_params_len =
3499 quic_transport_params_encode(qc->enc_params,
3500 qc->enc_params + sizeof qc->enc_params,
3501 &qc->rx.params, 1);
3502 if (!qc->enc_params_len)
3503 goto err;
3504
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003505 /* NOTE: the socket address has been concatenated to the destination ID
3506 * chosen by the client for Initial packets.
3507 */
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02003508 if (pkt->version == QUIC_PROTOCOL_VERSION_DRAFT_29) {
3509 salt = initial_salt_draft_29;
3510 salt_len = sizeof initial_salt_draft_29;
3511 }
3512 if (!qc_new_isecs(qc, salt, salt_len,
3513 pkt->dcid.data, pkt->odcid_len, 1)) {
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003514 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3515 goto err;
3516 }
3517
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003518 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003519 /* This is the DCID node sent in this packet by the client. */
3520 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003521 /* Enqueue this packet. */
3522 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3523 /* Try to accept a new connection. */
3524 listener_accept(l);
3525
3526 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3527 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3528 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3529 /* Insert our SCID, the connection ID for the QUIC client. */
3530 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3531 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3532 if (n != &qc->scid_node) {
3533 quic_conn_free(qc);
3534 qc = ebmb_entry(n, struct quic_conn, scid_node);
3535 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003536 }
3537 else {
3538 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3539 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3540 else
3541 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003542 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003543 }
3544 }
3545 else {
3546 if (end - *buf < QUIC_CID_LEN) {
3547 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3548 goto err;
3549 }
3550
3551 cids = &l->rx.cids;
3552 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3553 if (!node) {
3554 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3555 goto err;
3556 }
3557
3558 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003559 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003560 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003561 /* A short packet is the last one of an UDP datagram. */
3562 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003563 }
3564
3565 /* Store the DCID used for this packet to check the packet which
3566 * come in this UDP datagram match with it.
3567 */
3568 if (!dgram_ctx->dcid_node) {
3569 dgram_ctx->dcid_node = node;
3570 dgram_ctx->qc = qc;
3571 }
3572
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003573 /* Increase the total length of this packet by the header length. */
3574 pkt->len += *buf - beg;
3575 /* Do not check the DCID node before the length. */
3576 if (dgram_ctx->dcid_node != node) {
3577 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3578 goto err;
3579 }
3580
3581 if (pkt->len > sizeof pkt->data) {
3582 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3583 goto err;
3584 }
3585
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003586 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3587 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3588 goto err;
3589 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003590
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003591
3592 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003593 /* Wake up the connection packet handler task from here only if all
3594 * the contexts have been initialized, especially the mux context
3595 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3596 * will start it if these contexts for the connection are not already
3597 * initialized.
3598 */
3599 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003600 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003601
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003602 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3603
3604 return pkt->len;
3605
3606 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003607 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003608 qc ? qc->conn : NULL, pkt);
3609 return -1;
3610}
3611
3612/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3613 * in advance. This is the reponsability of the caller to check there is enough room in this
3614 * buffer to build a long header.
3615 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3616 */
3617static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3618 int type, size_t pn_len, struct quic_conn *conn)
3619{
3620 if (type > QUIC_PACKET_TYPE_RETRY)
3621 return 0;
3622
3623 /* #0 byte flags */
3624 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3625 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3626 /* Version */
3627 quic_write_uint32(buf, end, conn->version);
3628 *(*buf)++ = conn->dcid.len;
3629 /* Destination connection ID */
3630 if (conn->dcid.len) {
3631 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3632 *buf += conn->dcid.len;
3633 }
3634 /* Source connection ID */
3635 *(*buf)++ = conn->scid.len;
3636 if (conn->scid.len) {
3637 memcpy(*buf, conn->scid.data, conn->scid.len);
3638 *buf += conn->scid.len;
3639 }
3640
3641 return 1;
3642}
3643
3644/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3645 * in advance. This is the reponsability of the caller to check there is enough room in this
3646 * buffer to build a long header.
3647 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3648 */
3649static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3650 size_t pn_len, struct quic_conn *conn)
3651{
3652 /* #0 byte flags */
3653 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3654 /* Destination connection ID */
3655 if (conn->dcid.len) {
3656 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3657 *buf += conn->dcid.len;
3658 }
3659
3660 return 1;
3661}
3662
3663/* Apply QUIC header protection to the packet with <buf> as first byte address,
3664 * <pn> as address of the Packet number field, <pnlen> being this field length
3665 * with <aead> as AEAD cipher and <key> as secret key.
3666 * Returns 1 if succeeded or 0 if failed.
3667 */
3668static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3669 const EVP_CIPHER *aead, const unsigned char *key)
3670{
3671 int i, ret, outlen;
3672 EVP_CIPHER_CTX *ctx;
3673 /* We need an IV of at least 5 bytes: one byte for bytes #0
3674 * and at most 4 bytes for the packet number
3675 */
3676 unsigned char mask[5] = {0};
3677
3678 ret = 0;
3679 ctx = EVP_CIPHER_CTX_new();
3680 if (!ctx)
3681 return 0;
3682
3683 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3684 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3685 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3686 goto out;
3687
3688 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3689 for (i = 0; i < pnlen; i++)
3690 pn[i] ^= mask[i + 1];
3691
3692 ret = 1;
3693
3694 out:
3695 EVP_CIPHER_CTX_free(ctx);
3696
3697 return ret;
3698}
3699
3700/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3701 * ACK ranges if needed to a value below <limit> in bytes.
3702 * Return 1 if succeeded, 0 if not.
3703 */
3704static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3705{
3706 size_t room, ack_delay_sz;
3707
3708 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3709 /* A frame is made of 1 byte for the frame type. */
3710 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003711 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003712 return 0;
3713
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003714 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003715}
3716
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003717/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3718 * 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 +01003719 * and <*len> the packet Length field initialized with the number of bytes already present
3720 * 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 +02003721 * <headlen> is the number of bytes already present in this packet before building frames.
3722 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003723 * Update consequently <*len> to reflect the size of these frames built
3724 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003725 * Return 1 if succeeded, 0 if not.
3726 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003727static inline int qc_build_frms(struct quic_tx_packet *pkt,
3728 size_t room, size_t *len, size_t headlen,
3729 struct quic_enc_level *qel,
3730 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003731{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003732 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003733 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003734 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003735 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003736
Frédéric Lécailleea604992020-12-24 13:01:37 +01003737 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003738 if (*len > room || headlen > remain)
3739 return 0;
3740
Frédéric Lécailleea604992020-12-24 13:01:37 +01003741 /* If we are not probing we must take into an account the congestion
3742 * control window.
3743 */
3744 if (!conn->tx.nb_pto_dgrams)
3745 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003746 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003747 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003748 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 +01003749 /* header length, data length, frame length. */
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003750 size_t hlen, dlen, dlen_sz, avail_room, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003751
Frédéric Lécailleea604992020-12-24 13:01:37 +01003752 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003753 break;
3754
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003755 switch (cf->type) {
3756 case QUIC_FT_CRYPTO:
3757 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3758 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3759 /* Compute the length of this CRYPTO frame header */
3760 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3761 /* Compute the data length of this CRyPTO frame. */
3762 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3763 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3764 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3765 if (!dlen)
3766 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003767
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003768 pkt->cdata_len += dlen;
3769 /* CRYPTO frame length. */
3770 flen = hlen + quic_int_getsize(dlen) + dlen;
3771 TRACE_PROTO(" CRYPTO frame length (flen)",
3772 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3773 /* Add the CRYPTO data length and its encoded length to the packet
3774 * length and the length of this length.
3775 */
3776 *len += flen;
3777 room -= flen;
3778 if (dlen == cf->crypto.len) {
3779 /* <cf> CRYPTO data have been consumed. */
3780 MT_LIST_DELETE_SAFE(tmp1);
3781 LIST_APPEND(&pkt->frms, &cf->list);
3782 }
3783 else {
3784 struct quic_frame *new_cf;
3785
3786 new_cf = pool_alloc(pool_head_quic_frame);
3787 if (!new_cf) {
3788 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3789 return 0;
3790 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003791
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003792 new_cf->type = QUIC_FT_CRYPTO;
3793 new_cf->crypto.len = dlen;
3794 new_cf->crypto.offset = cf->crypto.offset;
3795 new_cf->crypto.qel = qel;
3796 LIST_APPEND(&pkt->frms, &new_cf->list);
3797 /* Consume <dlen> bytes of the current frame. */
3798 cf->crypto.len -= dlen;
3799 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003800 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003801 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003802
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003803 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003804 /* Note that these frames are accepted in short packets only without
3805 * "Length" packet field. Here, <*len> is used only to compute the
3806 * sum of the lengths of the already built frames for this packet.
3807 */
3808 TRACE_PROTO(" New STREAM frame build (room, len)",
3809 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3810 /* Compute the length of this STREAM frame "header" made a all the field
3811 * excepting the variable ones. Note that +1 is for the type of this frame.
3812 */
3813 hlen = 1 + quic_int_getsize(cf->stream.id) +
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003814 ((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 +02003815 /* Compute the data length of this STREAM frame. */
3816 avail_room = room - hlen - *len;
3817 if ((ssize_t)avail_room <= 0)
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003818 break;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003819
3820 if (cf->type & QUIC_STREAM_FRAME_TYPE_LEN_BIT) {
3821 dlen = max_available_room(avail_room, &dlen_sz);
3822 if (dlen > cf->stream.len) {
3823 dlen = cf->stream.len;
3824 }
3825 dlen_sz = quic_int_getsize(dlen);
3826 flen = hlen + dlen_sz + dlen;
3827 }
3828 else {
3829 dlen = QUIC_MIN(avail_room, cf->stream.len);
3830 flen = hlen + dlen;
3831 }
3832 TRACE_PROTO(" STREAM data length (hlen, stream.len, dlen)",
3833 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->stream.len, &dlen);
3834 TRACE_PROTO(" STREAM frame length (flen)",
3835 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3836 /* Add the STREAM data length and its encoded length to the packet
3837 * length and the length of this length.
3838 */
3839 *len += flen;
3840 room -= flen;
3841 if (dlen == cf->stream.len) {
3842 /* <cf> STREAM data have been consumed. */
3843 MT_LIST_DELETE_SAFE(tmp1);
3844 LIST_APPEND(&pkt->frms, &cf->list);
3845 }
3846 else {
3847 struct quic_frame *new_cf;
3848
3849 new_cf = pool_zalloc(pool_head_quic_frame);
3850 if (!new_cf) {
3851 TRACE_PROTO("No memory for new STREAM frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3852 return 0;
3853 }
3854
3855 new_cf->type = cf->type;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003856 new_cf->stream.qcs = cf->stream.qcs;
3857 new_cf->stream.buf = cf->stream.buf;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003858 new_cf->stream.id = cf->stream.id;
3859 if (cf->type & QUIC_STREAM_FRAME_TYPE_OFF_BIT)
3860 new_cf->stream.offset = cf->stream.offset;
3861 new_cf->stream.len = dlen;
3862 new_cf->type |= QUIC_STREAM_FRAME_TYPE_LEN_BIT;
3863 /* FIN bit reset */
3864 new_cf->type &= ~QUIC_STREAM_FRAME_TYPE_FIN_BIT;
3865 new_cf->stream.data = cf->stream.data;
3866 LIST_APPEND(&pkt->frms, &new_cf->list);
3867 cf->type |= QUIC_STREAM_FRAME_TYPE_OFF_BIT;
3868 /* Consume <dlen> bytes of the current frame. */
3869 cf->stream.len -= dlen;
Frédéric Lécaille785d3bd2021-09-10 09:13:39 +02003870 cf->stream.offset.key += dlen;
Frédéric Lécaillea5b1b892021-08-25 17:56:22 +02003871 cf->stream.data += dlen;
3872 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003873 break;
3874
3875 default:
3876 flen = qc_frm_len(cf);
3877 BUG_ON(!flen);
3878 if (flen > room)
3879 continue;
3880
3881 *len += flen;
3882 room -= flen;
3883 MT_LIST_DELETE_SAFE(tmp1);
3884 LIST_APPEND(&pkt->frms, &cf->list);
3885 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003886 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003887 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003888 }
3889
Frédéric Lécailleea604992020-12-24 13:01:37 +01003890 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003891}
3892
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003893/* This function evaluates if <pkt> packet may be built into a buffer with
3894 * <room> as available room. A valid packet should at least contain a valid
3895 * header and at least a frame.
3896 * To estimate the minimal space to build a packet, we consider the worst case:
3897 - there is not enough space to build ack-eliciting frames from
3898 qel->pktns->tx.frms. This is safe to consider this because when we build
3899 a packet we first build the ACK frames, then the ack-eliciting frames
3900 from qel->pktns->tx.frms only if there is enough space for these
3901 ack-eliciting frames, finally PING and PADDING frames if needed,
3902 - we have to ensure there is enough space to build an ACK frame if required,
3903 and a PING frame, even if we do not have to probe,
3904 - we also have to verify there is enough space to build a PADDING frame
3905 if needed, especially if there is no need to send an ACK frame.
3906 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3907 * a valid packet).
3908 */
3909static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3910 int ack, int nb_pto_dgrams,
3911 struct quic_enc_level *qel, struct quic_conn *conn)
3912{
3913 size_t minlen, token_fields_len;
3914 /* XXX FIXME XXX : ack delay not supported */
3915 uint64_t ack_delay = 0;
3916 size_t ack_frm_len = 0;
3917
3918 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3919 conn->conn, NULL, NULL, &room);
3920 /* When we do not have to probe nor send acks either, we must take into
3921 * an account the data which have already been prepared and limit
3922 * the size of this packet. We will potentially build an ack-eliciting
3923 * packet.
3924 */
3925 if (!nb_pto_dgrams && !ack) {
3926 size_t path_room;
3927
3928 path_room = quic_path_prep_data(conn->path);
3929 if (room > path_room)
3930 room = path_room;
3931 }
3932
3933 if (ack)
3934 /* A frame is made of 1 byte for the frame type. */
3935 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3936
3937 /* XXX FIXME XXX : token not supported */
3938 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3939 /* Check there is enough room to build the header followed by a token,
3940 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3941 * encryption tag is also taken into an account. Note that we have no
3942 * knowledge of the packet number for this packet. It must be atomically
3943 * incremented each time a packet is built. But before building a packet
3944 * we must estimate if it may be built if we do not want to consume a packet
3945 * number for nothing! Note that we add 1 byte more to
3946 * <minlen> to be able to build an ack-eliciting packet when probing without
3947 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3948 * PING frame.
3949 */
3950 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3951 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3952 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3953 + token_fields_len;
3954 else
3955 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3956
3957 /* Consider any PADDING frame to add */
3958 if (objt_server(conn->conn->target) &&
3959 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3960 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3961 /* Pad too short client Initial packet */
3962 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3963 }
3964 else if (!ack) {
3965 /* Consider we will have to add the longest short PADDING frame to
3966 * protect a 1-byte length packet number.
3967 */
3968 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3969 }
3970
3971 if (room < minlen) {
3972 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3973 conn->conn, NULL, NULL, &room);
3974 return 0;
3975 }
3976
3977 return 1;
3978}
3979
3980/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003981 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3982 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3983 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003984 * 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 +02003985 * 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 +02003986 * having returned from this function.
3987 * 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 +01003988 * number field in this packet. <pn_len> will also have the packet number
3989 * length as value.
3990 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003991 * Always succeeds: this is the responsability of the caller to ensure there is
3992 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003993 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003994static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003995 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003996 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3997 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003998{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003999 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004000 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004001 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
4002 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004003 size_t ack_frm_len;
4004 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004005 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004006
Frédéric Lécailleea604992020-12-24 13:01:37 +01004007 /* Length field value with CRYPTO frames if present. */
4008 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004009 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004010 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004011 * the congestion controller window. So, we do not limit the size of this
4012 * packet if we have an ACK frame to send because an ACK frame is not
4013 * ack-eliciting. This size will be limited if we have ack-eliciting
4014 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004015 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004016 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004017 size_t path_room;
4018
4019 path_room = quic_path_prep_data(conn->path);
4020 if (end - beg > path_room)
4021 end = beg + path_room;
4022 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004023
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004024 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004025 /* packet number length */
4026 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004027 /* Build the header */
4028 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004029 quic_build_packet_short_header(&pos, end, *pn_len, conn);
4030 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004031 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
4032 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
4033 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004034 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004035 /* Ensure there is enough room for the TLS encryption tag */
4036 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004037 /* Build an ACK frame if required. */
4038 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004039 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004040 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01004041 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004042 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
4043 * smallest frame (PING) and <*pn_len> more for the packet number. Note
4044 * that from here, we do not know if we will have to send a PING frame.
4045 * This will be decided after having computed the ack-eliciting frames
4046 * to be added to this packet.
4047 */
4048 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 +01004049 if (!ack_frm_len) {
4050 ssize_t room = end - pos;
4051 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4052 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004053 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004054 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004055 }
4056
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004057 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004058 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02004059 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004060 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004061
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004062 /* Initialize the length of the frames built below to <len>.
4063 * If any frame could be successfully built by qc_build_frms(),
4064 * we will have len_frms > len.
4065 */
4066 len_frms = len;
4067 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01004068 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4069 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004070 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004071
4072 add_ping_frm = 0;
4073 padding_len = 0;
4074 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004075 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004076 len < QUIC_INITIAL_PACKET_MINLEN) {
4077 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
4078 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004079 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004080 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004081 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004082 add_ping_frm = 1;
4083 len += 1;
4084 }
4085 /* If there is no frame at all to follow, add at least a PADDING frame. */
4086 if (!ack_frm_len)
4087 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
4088 }
4089
4090 /* Length (of the remaining data). Must not fail because, the buffer size
4091 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004092 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004093 * of this packet.
4094 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01004095 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004096 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01004097 else
4098 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004099 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02004100 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004101
4102 /* Packet number field address. */
4103 *buf_pn = pos;
4104
4105 /* Packet number encoding. */
4106 quic_packet_number_encode(&pos, end, pn, *pn_len);
4107
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004108 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
4109 ssize_t room = end - pos;
4110 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4111 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004112 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004113 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004114
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004115 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004116 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004117 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004118
4119 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02004120 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004121 ssize_t room = end - pos;
4122 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4123 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004124 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004125 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004126 }
4127 }
4128
4129 /* Build a PING frame if needed. */
4130 if (add_ping_frm) {
4131 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004132 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4133 ssize_t room = end - pos;
4134 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4135 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004136 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004137 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004138 }
4139
4140 /* Build a PADDING frame if needed. */
4141 if (padding_len) {
4142 frm.type = QUIC_FT_PADDING;
4143 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004144 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
4145 ssize_t room = end - pos;
4146 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
4147 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004148 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004149 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004150 }
4151
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004152 /* Always reset this variable as this function has no idea
4153 * if it was set. It is handle by the loss detection timer.
4154 */
4155 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004156 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004157}
4158
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004159static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004160{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004161 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004162 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004163 pkt->cdata_len = 0;
4164 pkt->in_flight_len = 0;
4165 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004166 pkt->next = NULL;
4167 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004168}
4169
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004170/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004171static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
4172{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004173 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004174
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004175 if (!pkt)
4176 return;
4177
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004178 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004179 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02004180 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004181 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004182 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004183}
4184
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004185/* Build a packet into <buf> packet buffer with <pkt_type> as packet
4186 * type for <qc> QUIC connection from <qel> encryption level.
4187 * Return -2 if the packet could not be allocated or encrypted for any reason,
4188 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004189 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004190static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
4191 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004192 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02004193 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille67f47d02021-08-19 15:19:09 +02004194 int ack, int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004195{
4196 /* The pointer to the packet number field. */
4197 unsigned char *buf_pn;
4198 unsigned char *beg, *end, *payload;
4199 int64_t pn;
4200 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004201 struct quic_tls_ctx *tls_ctx;
4202 struct quic_tx_packet *pkt;
4203
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01004204 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004205 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004206 pkt = pool_alloc(pool_head_quic_tx_packet);
4207 if (!pkt) {
4208 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 +02004209 *err = -2;
4210 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004211 }
4212
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004213 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004214 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004215 pn_len = 0;
4216 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004217 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004218 *err = -1;
4219 goto err;
4220 }
4221
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004222 /* Consume a packet number. */
4223 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004224 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 +01004225
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004226 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004227 payload = buf_pn + pn_len;
4228 payload_len = end - payload;
4229 aad_len = payload - beg;
4230
4231 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004232 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4233 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004234 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004235 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004236
4237 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004238 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004239 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4240 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4241 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004242 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004243 goto err;
4244 }
4245
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004246 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4247 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004248 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004249 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004250 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004251 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004252 pkt->in_flight_len = pkt->len;
4253 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004254 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004255 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004256 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4257
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004258 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004259
4260 err:
4261 free_quic_tx_packet(pkt);
4262 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004263 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004264}
4265
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004266/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4267 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004268 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004269static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4270 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004271{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004272 size_t try, done = 0;
4273
4274 if (!conn_ctrl_ready(conn))
4275 return 0;
4276
4277 if (!fd_recv_ready(conn->handle.fd))
4278 return 0;
4279
4280 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004281
4282 /* read the largest possible block. For this, we perform only one call
4283 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004284 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004285 */
4286 while (count > 0) {
4287 try = b_contig_space(buf);
4288 if (!try)
4289 break;
4290
4291 if (try > count)
4292 try = count;
4293
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004294 b_add(buf, try);
4295 done += try;
4296 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004297 }
4298
4299 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4300 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4301
4302 leave:
4303 return done;
4304
4305 read0:
4306 conn_sock_read0(conn);
4307 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4308
4309 /* Now a final check for a possible asynchronous low-level error
4310 * report. This can happen when a connection receives a reset
4311 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4312 * we might have come from there by just checking POLL_HUP instead
4313 * of recv()'s return value 0, so we have no way to tell there was
4314 * an error without checking.
4315 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004316 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004317 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4318 goto leave;
4319}
4320
4321
4322/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4323 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4324 * other pending data for example, but this flag is ignored at the moment.
4325 * Only one call to send() is performed, unless the buffer wraps, in which case
4326 * a second call may be performed. The connection's flags are updated with
4327 * whatever special event is detected (error, empty). The caller is responsible
4328 * for taking care of those events and avoiding the call if inappropriate. The
4329 * function does not call the connection's polling update function, so the caller
4330 * is responsible for this. It's up to the caller to update the buffer's contents
4331 * based on the return value.
4332 */
4333static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4334{
4335 ssize_t ret;
4336 size_t try, done;
4337 int send_flag;
4338
4339 if (!conn_ctrl_ready(conn))
4340 return 0;
4341
4342 if (!fd_send_ready(conn->handle.fd))
4343 return 0;
4344
4345 done = 0;
4346 /* send the largest possible block. For this we perform only one call
4347 * to send() unless the buffer wraps and we exactly fill the first hunk,
4348 * in which case we accept to do it once again.
4349 */
4350 while (count) {
4351 try = b_contig_data(buf, done);
4352 if (try > count)
4353 try = count;
4354
4355 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4356 if (try < count || flags & CO_SFL_MSG_MORE)
4357 send_flag |= MSG_MORE;
4358
4359 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4360 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4361 if (ret > 0) {
4362 count -= ret;
4363 done += ret;
4364
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004365 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004366 conn->flags |= CO_FL_WAIT_L4L6;
4367 /* if the system buffer is full, don't insist */
4368 if (ret < try)
4369 break;
4370 }
4371 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4372 /* nothing written, we need to poll for write first */
4373 fd_cant_send(conn->handle.fd);
4374 break;
4375 }
4376 else if (errno != EINTR) {
4377 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4378 break;
4379 }
4380 }
4381 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4382 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4383
4384 if (done > 0) {
4385 /* we count the total bytes sent, and the send rate for 32-byte
4386 * blocks. The reason for the latter is that freq_ctr are
4387 * limited to 4GB and that it's not enough per second.
4388 */
4389 _HA_ATOMIC_ADD(&global.out_bytes, done);
4390 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4391 }
4392 return done;
4393}
4394
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004395/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4396 * event subscriber <es> is not allowed to change from a previous call as long
4397 * as at least one event is still subscribed. The <event_type> must only be a
4398 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4399 */
4400static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4401{
Frédéric Lécaille513b4f22021-09-20 15:23:17 +02004402 struct qcc *qcc = conn->qc->qcc;
4403
4404 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
4405 BUG_ON(qcc->subs && qcc->subs != es);
4406
4407 es->events |= event_type;
4408 qcc->subs = es;
4409
4410 if (event_type & SUB_RETRY_RECV)
4411 TRACE_DEVEL("subscribe(recv)", QUIC_EV_CONN_XPRTRECV, conn, qcc);
4412
4413 if (event_type & SUB_RETRY_SEND)
4414 TRACE_DEVEL("subscribe(send)", QUIC_EV_CONN_XPRTSEND, conn, qcc);
4415
4416 return 0;
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004417}
4418
4419/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4420 * The <es> pointer is not allowed to differ from the one passed to the
4421 * subscribe() call. It always returns zero.
4422 */
4423static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4424{
4425 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4426}
4427
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004428/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4429 * connection with <xprt_ctx> as address of the xprt context.
4430 * Returns 1 if succeeded, 0 if not.
4431 */
4432static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4433{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004434 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004435
4436 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4437
4438 if (*xprt_ctx)
4439 goto out;
4440
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004441 ctx = pool_alloc(pool_head_quic_conn_ctx);
4442 if (!ctx) {
4443 conn->err_code = CO_ER_SYS_MEMLIM;
4444 goto err;
4445 }
4446
4447 ctx->wait_event.tasklet = tasklet_new();
4448 if (!ctx->wait_event.tasklet) {
4449 conn->err_code = CO_ER_SYS_MEMLIM;
4450 goto err;
4451 }
4452
4453 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4454 ctx->wait_event.tasklet->context = ctx;
4455 ctx->wait_event.events = 0;
4456 ctx->conn = conn;
4457 ctx->subs = NULL;
4458 ctx->xprt_ctx = NULL;
4459
4460 ctx->xprt = xprt_get(XPRT_QUIC);
4461 if (objt_server(conn->target)) {
4462 /* Server */
4463 struct server *srv = __objt_server(conn->target);
4464 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004465 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004466 int ssl_err, ipv4;
4467
4468 ssl_err = SSL_ERROR_NONE;
4469 if (RAND_bytes(dcid, sizeof dcid) != 1)
4470 goto err;
4471
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004472 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004473 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004474 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004475 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004476 goto err;
4477
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004478 /* Insert our SCID, the connection ID for the QUIC client. */
4479 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4480
4481 conn->qc = qc;
4482 qc->conn = conn;
Frédéric Lécaille2fc76cf2021-08-31 19:10:40 +02004483 if (!qc_new_isecs(qc, initial_salt_v1, sizeof initial_salt_v1,
4484 dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004485 goto err;
4486
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004487 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4488 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4489 goto err;
4490
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004491 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004492 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004493 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4494 qc->enc_params_len =
4495 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4496 &qc->rx.params, 0);
4497 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004498 goto err;
4499
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004500 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004501 SSL_set_connect_state(ctx->ssl);
4502 ssl_err = SSL_do_handshake(ctx->ssl);
4503 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004504 int st;
4505
4506 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004507 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4508 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004509 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004510 }
4511 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004512 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004513 goto err;
4514 }
4515 }
4516 }
4517 else if (objt_listener(conn->target)) {
4518 /* Listener */
4519 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004520 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004521
Frédéric Lécailled4d6aa72021-09-03 15:56:18 +02004522 ctx->wait_event.tasklet->tid = quic_get_cid_tid(&qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004523 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4524 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4525 goto err;
4526
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004527 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004528 SSL_set_accept_state(ctx->ssl);
4529 }
4530
4531 *xprt_ctx = ctx;
4532
4533 /* Leave init state and start handshake */
4534 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004535
4536 out:
4537 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4538
4539 return 0;
4540
4541 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004542 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004543 tasklet_free(ctx->wait_event.tasklet);
4544 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004545 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004546 return -1;
4547}
4548
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004549/* Start the QUIC transport layer */
4550static int qc_xprt_start(struct connection *conn, void *ctx)
4551{
4552 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004553 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004554
4555 qc = conn->qc;
4556 if (!quic_conn_init_timer(qc)) {
4557 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4558 return 0;
4559 }
4560
4561 tasklet_wakeup(qctx->wait_event.tasklet);
4562 return 1;
4563}
4564
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004565/* transport-layer operations for QUIC connections. */
4566static struct xprt_ops ssl_quic = {
Amaury Denoyelle414cac52021-09-22 11:14:37 +02004567 .close = quic_close,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004568 .snd_buf = quic_conn_from_buf,
4569 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004570 .subscribe = quic_conn_subscribe,
4571 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004572 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004573 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004574 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4575 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4576 .name = "QUIC",
4577};
4578
4579__attribute__((constructor))
4580static void __quic_conn_init(void)
4581{
4582 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4583 xprt_register(XPRT_QUIC, &ssl_quic);
4584}
4585
4586__attribute__((destructor))
4587static void __quic_conn_deinit(void)
4588{
4589 BIO_meth_free(ha_quic_meth);
4590}
4591
4592/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4593 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4594 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004595 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004596 */
4597static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4598 struct sockaddr_storage *saddr, qpkt_read_func *func)
4599{
4600 unsigned char *pos;
4601 const unsigned char *end;
4602 struct quic_dgram_ctx dgram_ctx = {
4603 .dcid_node = NULL,
4604 .owner = owner,
4605 };
4606
4607 pos = (unsigned char *)buf;
4608 end = pos + len;
4609
4610 do {
4611 int ret;
4612 struct quic_rx_packet *pkt;
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004613 size_t pkt_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004614
Willy Tarreaue4498932021-03-22 21:13:05 +01004615 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004616 if (!pkt)
4617 goto err;
4618
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004619 quic_rx_packet_refinc(pkt);
4620 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004621 pkt_len = pkt->len;
Frédéric Lécaille310d1bd2021-09-22 15:10:49 +02004622 quic_rx_packet_refdec(pkt);
Frédéric Lécaille865b0782021-09-23 07:33:20 +02004623 if (ret == -1 && !pkt_len)
4624 /* If the packet length could not be found, we cannot continue. */
4625 break;
4626
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004627 } while (pos < end);
4628
4629 /* Increasing the received bytes counter by the UDP datagram length
4630 * if this datagram could be associated to a connection.
4631 */
4632 if (dgram_ctx.qc)
4633 dgram_ctx.qc->rx.bytes += len;
4634
4635 return pos - (unsigned char *)buf;
4636
4637 err:
4638 return -1;
4639}
4640
4641ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4642 struct sockaddr_storage *saddr)
4643{
4644 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4645}
4646
4647ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4648 struct sockaddr_storage *saddr)
4649{
4650 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4651}
4652
4653/* QUIC I/O handler for connection to local listeners or remove servers
4654 * depending on <listener> boolean value, with <fd> as socket file
4655 * descriptor and <ctx> as context.
4656 */
4657static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4658{
4659 ssize_t ret;
4660 size_t done = 0;
4661 struct buffer *buf = get_trash_chunk();
4662 /* Source address */
4663 struct sockaddr_storage saddr = {0};
4664 socklen_t saddrlen = sizeof saddr;
4665
4666 if (!fd_recv_ready(fd))
4667 return 0;
4668
4669 do {
4670 ret = recvfrom(fd, buf->area, buf->size, 0,
4671 (struct sockaddr *)&saddr, &saddrlen);
4672 if (ret < 0) {
4673 if (errno == EINTR)
4674 continue;
4675 if (errno == EAGAIN)
4676 fd_cant_recv(fd);
4677 goto out;
4678 }
4679 } while (0);
4680
4681 done = buf->data = ret;
4682 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4683
4684 out:
4685 return done;
4686}
4687
4688/* QUIC I/O handler for connections to local listeners with <fd> as socket
4689 * file descriptor.
4690 */
4691void quic_fd_handler(int fd)
4692{
Willy Tarreauf5090652021-04-06 17:23:40 +02004693 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004694 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4695}
4696
4697/* QUIC I/O handler for connections to remote servers with <fd> as socket
4698 * file descriptor.
4699 */
4700void quic_conn_fd_handler(int fd)
4701{
Willy Tarreauf5090652021-04-06 17:23:40 +02004702 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004703 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4704}
4705
4706/*
4707 * Local variables:
4708 * c-indent-level: 8
4709 * c-basic-offset: 8
4710 * End:
4711 */