blob: 96b6a47694e0cc27e3a4f017e8bfb1cf8b28b694 [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
53struct quic_transport_params quic_dflt_transport_params = {
Frédéric Lécaille785c9c92021-05-17 16:42:21 +020054 .max_udp_payload_size = QUIC_DFLT_MAX_UDP_PAYLOAD_SIZE,
55 .ack_delay_exponent = QUIC_DFLT_ACK_DELAY_COMPONENT,
56 .max_ack_delay = QUIC_DFLT_MAX_ACK_DELAY,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010057};
58
59/* trace source and events */
60static void quic_trace(enum trace_level level, uint64_t mask, \
61 const struct trace_source *src,
62 const struct ist where, const struct ist func,
63 const void *a1, const void *a2, const void *a3, const void *a4);
64
65static const struct trace_event quic_trace_events[] = {
66 { .mask = QUIC_EV_CONN_NEW, .name = "new_conn", .desc = "new QUIC connection" },
67 { .mask = QUIC_EV_CONN_INIT, .name = "new_conn_init", .desc = "new QUIC connection initialization" },
68 { .mask = QUIC_EV_CONN_ISEC, .name = "init_secs", .desc = "initial secrets derivation" },
69 { .mask = QUIC_EV_CONN_RSEC, .name = "read_secs", .desc = "read secrets derivation" },
70 { .mask = QUIC_EV_CONN_WSEC, .name = "write_secs", .desc = "write secrets derivation" },
71 { .mask = QUIC_EV_CONN_LPKT, .name = "lstnr_packet", .desc = "new listener received packet" },
72 { .mask = QUIC_EV_CONN_SPKT, .name = "srv_packet", .desc = "new server received packet" },
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +050073 { .mask = QUIC_EV_CONN_ENCPKT, .name = "enc_hdshk_pkt", .desc = "handhshake packet encryption" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010074 { .mask = QUIC_EV_CONN_HPKT, .name = "hdshk_pkt", .desc = "handhshake packet building" },
75 { .mask = QUIC_EV_CONN_PAPKT, .name = "phdshk_apkt", .desc = "post handhshake application packet preparation" },
76 { .mask = QUIC_EV_CONN_PAPKTS, .name = "phdshk_apkts", .desc = "post handhshake application packets preparation" },
77 { .mask = QUIC_EV_CONN_HDSHK, .name = "hdshk", .desc = "SSL handhshake processing" },
78 { .mask = QUIC_EV_CONN_RMHP, .name = "rm_hp", .desc = "Remove header protection" },
79 { .mask = QUIC_EV_CONN_PRSHPKT, .name = "parse_hpkt", .desc = "parse handshake packet" },
80 { .mask = QUIC_EV_CONN_PRSAPKT, .name = "parse_apkt", .desc = "parse application packet" },
81 { .mask = QUIC_EV_CONN_PRSFRM, .name = "parse_frm", .desc = "parse frame" },
82 { .mask = QUIC_EV_CONN_PRSAFRM, .name = "parse_ack_frm", .desc = "parse ACK frame" },
83 { .mask = QUIC_EV_CONN_BFRM, .name = "build_frm", .desc = "build frame" },
84 { .mask = QUIC_EV_CONN_PHPKTS, .name = "phdshk_pkts", .desc = "handhshake packets preparation" },
85 { .mask = QUIC_EV_CONN_TRMHP, .name = "rm_hp_try", .desc = "header protection removing try" },
86 { .mask = QUIC_EV_CONN_ELRMHP, .name = "el_rm_hp", .desc = "handshake enc. level header protection removing" },
87 { .mask = QUIC_EV_CONN_ELRXPKTS, .name = "el_treat_rx_pkts", .desc = "handshake enc. level rx packets treatment" },
88 { .mask = QUIC_EV_CONN_SSLDATA, .name = "ssl_provide_data", .desc = "CRYPTO data provision to TLS stack" },
89 { .mask = QUIC_EV_CONN_RXCDATA, .name = "el_treat_rx_cfrms",.desc = "enc. level RX CRYPTO frames processing"},
90 { .mask = QUIC_EV_CONN_ADDDATA, .name = "add_hdshk_data", .desc = "TLS stack ->add_handshake_data() call"},
91 { .mask = QUIC_EV_CONN_FFLIGHT, .name = "flush_flight", .desc = "TLS stack ->flush_flight() call"},
92 { .mask = QUIC_EV_CONN_SSLALERT, .name = "send_alert", .desc = "TLS stack ->send_alert() call"},
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +010093 { .mask = QUIC_EV_CONN_RTTUPDT, .name = "rtt_updt", .desc = "RTT sampling" },
94 { .mask = QUIC_EV_CONN_SPPKTS, .name = "sppkts", .desc = "send prepared packets" },
95 { .mask = QUIC_EV_CONN_PKTLOSS, .name = "pktloss", .desc = "detect packet loss" },
96 { .mask = QUIC_EV_CONN_STIMER, .name = "stimer", .desc = "set timer" },
97 { .mask = QUIC_EV_CONN_PTIMER, .name = "ptimer", .desc = "process timer" },
98 { .mask = QUIC_EV_CONN_SPTO, .name = "spto", .desc = "set PTO" },
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +010099 { .mask = QUIC_EV_CONN_BCFRMS, .name = "bcfrms", .desc = "build CRYPTO data frames" },
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100100 { /* end */ }
101};
102
103static const struct name_desc quic_trace_lockon_args[4] = {
104 /* arg1 */ { /* already used by the connection */ },
105 /* arg2 */ { .name="quic", .desc="QUIC transport" },
106 /* arg3 */ { },
107 /* arg4 */ { }
108};
109
110static const struct name_desc quic_trace_decoding[] = {
111#define QUIC_VERB_CLEAN 1
112 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
113 { /* end */ }
114};
115
116
117struct trace_source trace_quic = {
118 .name = IST("quic"),
119 .desc = "QUIC xprt",
120 .arg_def = TRC_ARG1_CONN, /* TRACE()'s first argument is always a connection */
121 .default_cb = quic_trace,
122 .known_events = quic_trace_events,
123 .lockon_args = quic_trace_lockon_args,
124 .decoding = quic_trace_decoding,
125 .report_events = ~0, /* report everything by default */
126};
127
128#define TRACE_SOURCE &trace_quic
129INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
130
131static BIO_METHOD *ha_quic_meth;
132
Frédéric Lécailledbe25af2021-08-04 15:27:37 +0200133DECLARE_POOL(pool_head_quic_tx_ring, "quic_tx_ring_pool", QUIC_TX_RING_BUFSZ);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100134DECLARE_STATIC_POOL(pool_head_quic_conn_ctx,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200135 "quic_conn_ctx_pool", sizeof(struct ssl_sock_ctx));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100136DECLARE_STATIC_POOL(pool_head_quic_conn, "quic_conn", sizeof(struct quic_conn));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100137DECLARE_POOL(pool_head_quic_connection_id,
138 "quic_connnection_id_pool", sizeof(struct quic_connection_id));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100139DECLARE_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 +0100140DECLARE_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 +0100141DECLARE_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 +0100142DECLARE_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 +0100143DECLARE_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 +0200144DECLARE_POOL(pool_head_quic_frame, "quic_frame_pool", sizeof(struct quic_frame));
Frédéric Lécaille8090b512020-11-30 16:19:22 +0100145DECLARE_STATIC_POOL(pool_head_quic_arng, "quic_arng_pool", sizeof(struct quic_arng_node));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100146
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200147static 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 +0200148 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +0200149 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +0200150 int nb_pto_dgrams, int *err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100151
152/* Add traces to <buf> depending on <frm> TX frame type. */
153static inline void chunk_tx_frm_appendf(struct buffer *buf,
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200154 const struct quic_frame *frm)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100155{
156 switch (frm->type) {
157 case QUIC_FT_CRYPTO:
158 chunk_appendf(buf, " cfoff=%llu cflen=%llu",
159 (unsigned long long)frm->crypto.offset,
160 (unsigned long long)frm->crypto.len);
161 break;
162 default:
163 chunk_appendf(buf, " %s", quic_frame_type_string(frm->type));
164 }
165}
166
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100167/* Only for debug purpose */
168struct enc_debug_info {
169 unsigned char *payload;
170 size_t payload_len;
171 unsigned char *aad;
172 size_t aad_len;
173 uint64_t pn;
174};
175
176/* Initializes a enc_debug_info struct (only for debug purpose) */
177static inline void enc_debug_info_init(struct enc_debug_info *edi,
178 unsigned char *payload, size_t payload_len,
179 unsigned char *aad, size_t aad_len, uint64_t pn)
180{
181 edi->payload = payload;
182 edi->payload_len = payload_len;
183 edi->aad = aad;
184 edi->aad_len = aad_len;
185 edi->pn = pn;
186}
187
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100188/* Trace callback for QUIC.
189 * These traces always expect that arg1, if non-null, is of type connection.
190 */
191static void quic_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
192 const struct ist where, const struct ist func,
193 const void *a1, const void *a2, const void *a3, const void *a4)
194{
195 const struct connection *conn = a1;
196
197 if (conn) {
198 struct quic_tls_secrets *secs;
199 struct quic_conn *qc;
200
201 qc = conn->qc;
202 chunk_appendf(&trace_buf, " : conn@%p", conn);
203 if ((mask & QUIC_EV_CONN_INIT) && qc) {
204 chunk_appendf(&trace_buf, "\n odcid");
205 quic_cid_dump(&trace_buf, &qc->odcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100206 chunk_appendf(&trace_buf, "\n dcid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100207 quic_cid_dump(&trace_buf, &qc->dcid);
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100208 chunk_appendf(&trace_buf, "\n scid");
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100209 quic_cid_dump(&trace_buf, &qc->scid);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100210 }
211
212 if (mask & QUIC_EV_CONN_ADDDATA) {
213 const enum ssl_encryption_level_t *level = a2;
214 const size_t *len = a3;
215
216 if (level) {
217 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
218
219 chunk_appendf(&trace_buf, " el=%c(%d)", quic_enc_level_char(lvl), lvl);
220 }
221 if (len)
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100222 chunk_appendf(&trace_buf, " len=%llu", (unsigned long long)*len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100223 }
224 if ((mask & QUIC_EV_CONN_ISEC) && qc) {
225 /* Initial read & write secrets. */
226 enum quic_tls_enc_level level = QUIC_TLS_ENC_LEVEL_INITIAL;
227 const unsigned char *rx_sec = a2;
228 const unsigned char *tx_sec = a3;
229
230 secs = &qc->els[level].tls_ctx.rx;
231 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
232 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(level));
233 if (rx_sec)
234 quic_tls_secret_hexdump(&trace_buf, rx_sec, 32);
235 quic_tls_keys_hexdump(&trace_buf, secs);
236 }
237 secs = &qc->els[level].tls_ctx.tx;
238 if (secs->flags & QUIC_FL_TLS_SECRETS_SET) {
239 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(level));
240 if (tx_sec)
241 quic_tls_secret_hexdump(&trace_buf, tx_sec, 32);
242 quic_tls_keys_hexdump(&trace_buf, secs);
243 }
244 }
245 if (mask & (QUIC_EV_CONN_RSEC|QUIC_EV_CONN_RWSEC)) {
246 const enum ssl_encryption_level_t *level = a2;
247 const unsigned char *secret = a3;
248 const size_t *secret_len = a4;
249
250 if (level) {
251 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
252
253 chunk_appendf(&trace_buf, "\n RX el=%c", quic_enc_level_char(lvl));
254 if (secret && secret_len)
255 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
256 secs = &qc->els[lvl].tls_ctx.rx;
257 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
258 quic_tls_keys_hexdump(&trace_buf, secs);
259 }
260 }
261
262 if (mask & (QUIC_EV_CONN_WSEC|QUIC_EV_CONN_RWSEC)) {
263 const enum ssl_encryption_level_t *level = a2;
264 const unsigned char *secret = a3;
265 const size_t *secret_len = a4;
266
267 if (level) {
268 enum quic_tls_enc_level lvl = ssl_to_quic_enc_level(*level);
269
270 chunk_appendf(&trace_buf, "\n TX el=%c", quic_enc_level_char(lvl));
271 if (secret && secret_len)
272 quic_tls_secret_hexdump(&trace_buf, secret, *secret_len);
273 secs = &qc->els[lvl].tls_ctx.tx;
274 if (secs->flags & QUIC_FL_TLS_SECRETS_SET)
275 quic_tls_keys_hexdump(&trace_buf, secs);
276 }
277
278 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100279
Frédéric Lécaille133e8a72020-12-18 09:33:27 +0100280 if (mask & (QUIC_EV_CONN_HPKT|QUIC_EV_CONN_PAPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100281 const struct quic_tx_packet *pkt = a2;
282 const struct quic_enc_level *qel = a3;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100283 const ssize_t *room = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100284
285 if (qel) {
286 struct quic_pktns *pktns;
287
288 pktns = qc->pktns;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100289 chunk_appendf(&trace_buf, " qel=%c cwnd=%llu ppif=%lld pif=%llu "
290 "if=%llu pp=%u pdg=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100291 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100292 (unsigned long long)qc->path->cwnd,
293 (unsigned long long)qc->path->prep_in_flight,
294 (unsigned long long)qc->path->in_flight,
295 (unsigned long long)pktns->tx.in_flight,
296 pktns->tx.pto_probe, qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100297 }
298 if (pkt) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200299 const struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100300 chunk_appendf(&trace_buf, " pn=%llu cdlen=%u",
301 (unsigned long long)pkt->pn_node.key, pkt->cdata_len);
302 list_for_each_entry(frm, &pkt->frms, list)
303 chunk_tx_frm_appendf(&trace_buf, frm);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100304 chunk_appendf(&trace_buf, " tx.bytes=%llu", (unsigned long long)qc->tx.bytes);
305 }
306
307 if (room) {
308 chunk_appendf(&trace_buf, " room=%lld", (long long)*room);
309 chunk_appendf(&trace_buf, " dcid.len=%llu scid.len=%llu",
310 (unsigned long long)qc->dcid.len, (unsigned long long)qc->scid.len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100311 }
312 }
313
314 if (mask & QUIC_EV_CONN_HDSHK) {
315 const enum quic_handshake_state *state = a2;
316 const int *err = a3;
317
318 if (state)
319 chunk_appendf(&trace_buf, " state=%s", quic_hdshk_state_str(*state));
320 if (err)
321 chunk_appendf(&trace_buf, " err=%s", ssl_error_str(*err));
322 }
323
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100324 if (mask & (QUIC_EV_CONN_TRMHP|QUIC_EV_CONN_ELRMHP|QUIC_EV_CONN_SPKT)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100325 const struct quic_rx_packet *pkt = a2;
326 const unsigned long *pktlen = a3;
327 const SSL *ssl = a4;
328
329 if (pkt) {
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100330 chunk_appendf(&trace_buf, " pkt@%p el=%c",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100331 pkt, quic_packet_type_enc_level_char(pkt->type));
332 if (pkt->pnl)
333 chunk_appendf(&trace_buf, " pnl=%u pn=%llu", pkt->pnl,
334 (unsigned long long)pkt->pn);
335 if (pkt->token_len)
336 chunk_appendf(&trace_buf, " toklen=%llu",
337 (unsigned long long)pkt->token_len);
338 if (pkt->aad_len)
339 chunk_appendf(&trace_buf, " aadlen=%llu",
340 (unsigned long long)pkt->aad_len);
341 chunk_appendf(&trace_buf, " flags=0x%x len=%llu",
342 pkt->flags, (unsigned long long)pkt->len);
343 }
344 if (pktlen)
345 chunk_appendf(&trace_buf, " (%ld)", *pktlen);
346 if (ssl) {
347 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
348 chunk_appendf(&trace_buf, " el=%c",
349 quic_enc_level_char(ssl_to_quic_enc_level(level)));
350 }
351 }
352
353 if (mask & (QUIC_EV_CONN_ELRXPKTS|QUIC_EV_CONN_PRSHPKT|QUIC_EV_CONN_SSLDATA)) {
354 const struct quic_rx_packet *pkt = a2;
355 const struct quic_rx_crypto_frm *cf = a3;
356 const SSL *ssl = a4;
357
358 if (pkt)
359 chunk_appendf(&trace_buf, " pkt@%p el=%c pn=%llu", pkt,
360 quic_packet_type_enc_level_char(pkt->type),
361 (unsigned long long)pkt->pn);
362 if (cf)
363 chunk_appendf(&trace_buf, " cfoff=%llu cflen=%llu",
364 (unsigned long long)cf->offset_node.key,
365 (unsigned long long)cf->len);
366 if (ssl) {
367 enum ssl_encryption_level_t level = SSL_quic_read_level(ssl);
368 chunk_appendf(&trace_buf, " el=%c",
369 quic_enc_level_char(ssl_to_quic_enc_level(level)));
370 }
371 }
372
373 if (mask & (QUIC_EV_CONN_PRSFRM|QUIC_EV_CONN_BFRM)) {
374 const struct quic_frame *frm = a2;
375
376 if (frm)
377 chunk_appendf(&trace_buf, " %s", quic_frame_type_string(frm->type));
378 }
379
380 if (mask & QUIC_EV_CONN_PHPKTS) {
381 const struct quic_enc_level *qel = a2;
382
383 if (qel) {
384 struct quic_pktns *pktns;
385
386 pktns = qc->pktns;
387 chunk_appendf(&trace_buf,
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200388 " 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 +0100389 quic_enc_level_char_from_qel(qel, qc),
Frédéric Lécaille546186b2021-08-03 14:25:36 +0200390 quic_hdshk_state_str(HA_ATOMIC_LOAD(&qc->state)),
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100391 !!(pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED),
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100392 (unsigned long long)qc->path->cwnd,
393 (unsigned long long)qc->path->prep_in_flight,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100394 (unsigned long long)qc->path->in_flight,
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100395 (unsigned long long)pktns->tx.in_flight, pktns->tx.pto_probe,
396 (unsigned long long)qc->tx.nb_pto_dgrams);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100397 }
398 }
399
Frédéric Lécaillef63921f2020-12-18 09:48:20 +0100400 if (mask & QUIC_EV_CONN_ENCPKT) {
401 const struct enc_debug_info *edi = a2;
402
403 if (edi)
404 chunk_appendf(&trace_buf,
405 " payload=@%p payload_len=%llu"
406 " aad=@%p aad_len=%llu pn=%llu",
407 edi->payload, (unsigned long long)edi->payload_len,
408 edi->aad, (unsigned long long)edi->aad_len,
409 (unsigned long long)edi->pn);
410 }
411
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100412 if (mask & QUIC_EV_CONN_RMHP) {
413 const struct quic_rx_packet *pkt = a2;
414
415 if (pkt) {
416 const int *ret = a3;
417
418 chunk_appendf(&trace_buf, " pkt@%p", pkt);
419 if (ret && *ret)
420 chunk_appendf(&trace_buf, " pnl=%u pn=%llu",
421 pkt->pnl, (unsigned long long)pkt->pn);
422 }
423 }
424
425 if (mask & QUIC_EV_CONN_PRSAFRM) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200426 const struct quic_frame *frm = a2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100427 const unsigned long *val1 = a3;
428 const unsigned long *val2 = a4;
429
430 if (frm)
431 chunk_tx_frm_appendf(&trace_buf, frm);
432 if (val1)
433 chunk_appendf(&trace_buf, " %lu", *val1);
434 if (val2)
435 chunk_appendf(&trace_buf, "..%lu", *val2);
436 }
437
438 if (mask & QUIC_EV_CONN_RTTUPDT) {
439 const unsigned int *rtt_sample = a2;
440 const unsigned int *ack_delay = a3;
441 const struct quic_loss *ql = a4;
442
443 if (rtt_sample)
444 chunk_appendf(&trace_buf, " rtt_sample=%ums", *rtt_sample);
445 if (ack_delay)
446 chunk_appendf(&trace_buf, " ack_delay=%ums", *ack_delay);
447 if (ql)
448 chunk_appendf(&trace_buf,
449 " srtt=%ums rttvar=%ums min_rtt=%ums",
450 ql->srtt >> 3, ql->rtt_var >> 2, ql->rtt_min);
451 }
452 if (mask & QUIC_EV_CONN_CC) {
453 const struct quic_cc_event *ev = a2;
454 const struct quic_cc *cc = a3;
455
456 if (a2)
457 quic_cc_event_trace(&trace_buf, ev);
458 if (a3)
459 quic_cc_state_trace(&trace_buf, cc);
460 }
461
462 if (mask & QUIC_EV_CONN_PKTLOSS) {
463 const struct quic_pktns *pktns = a2;
464 const struct list *lost_pkts = a3;
465 struct quic_conn *qc = conn->qc;
466
467 if (pktns) {
468 chunk_appendf(&trace_buf, " pktns=%s",
469 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
470 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H");
471 if (pktns->tx.loss_time)
472 chunk_appendf(&trace_buf, " loss_time=%dms",
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100473 TICKS_TO_MS(tick_remain(now_ms, pktns->tx.loss_time)));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100474 }
475 if (lost_pkts && !LIST_ISEMPTY(lost_pkts)) {
476 struct quic_tx_packet *pkt;
477
478 chunk_appendf(&trace_buf, " lost_pkts:");
479 list_for_each_entry(pkt, lost_pkts, list)
480 chunk_appendf(&trace_buf, " %lu", (unsigned long)pkt->pn_node.key);
481 }
482 }
483
484 if (mask & (QUIC_EV_CONN_STIMER|QUIC_EV_CONN_PTIMER|QUIC_EV_CONN_SPTO)) {
485 struct quic_conn *qc = conn->qc;
486 const struct quic_pktns *pktns = a2;
487 const int *duration = a3;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100488 const uint64_t *ifae_pkts = a4;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100489
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100490 if (ifae_pkts)
491 chunk_appendf(&trace_buf, " ifae_pkts=%llu",
492 (unsigned long long)*ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100493 if (pktns) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100494 chunk_appendf(&trace_buf, " pktns=%s pp=%d",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100495 pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100496 pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
497 pktns->tx.pto_probe);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100498 if (mask & QUIC_EV_CONN_STIMER) {
499 if (pktns->tx.loss_time)
500 chunk_appendf(&trace_buf, " loss_time=%dms",
501 TICKS_TO_MS(pktns->tx.loss_time - now_ms));
502 }
503 if (mask & QUIC_EV_CONN_SPTO) {
504 if (pktns->tx.time_of_last_eliciting)
505 chunk_appendf(&trace_buf, " tole=%dms",
506 TICKS_TO_MS(pktns->tx.time_of_last_eliciting - now_ms));
507 if (duration)
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +0100508 chunk_appendf(&trace_buf, " dur=%dms", TICKS_TO_MS(*duration));
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100509 }
510 }
511
512 if (!(mask & QUIC_EV_CONN_SPTO) && qc->timer_task) {
513 chunk_appendf(&trace_buf,
514 " expire=%dms", TICKS_TO_MS(qc->timer - now_ms));
515 }
516 }
517
518 if (mask & QUIC_EV_CONN_SPPKTS) {
519 const struct quic_tx_packet *pkt = a2;
520
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100521 chunk_appendf(&trace_buf, " cwnd=%llu ppif=%llu pif=%llu",
522 (unsigned long long)qc->path->cwnd,
523 (unsigned long long)qc->path->prep_in_flight,
524 (unsigned long long)qc->path->in_flight);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100525 if (pkt) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100526 chunk_appendf(&trace_buf, " pn=%lu(%s) iflen=%llu cdlen=%llu",
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100527 (unsigned long)pkt->pn_node.key,
528 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_INITIAL] ? "I" :
529 pkt->pktns == &qc->pktns[QUIC_TLS_PKTNS_01RTT] ? "01RTT": "H",
Frédéric Lécaille04ffb662020-12-08 15:58:39 +0100530 (unsigned long long)pkt->in_flight_len,
531 (unsigned long long)pkt->cdata_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100532 }
533 }
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100534
535 if (mask & QUIC_EV_CONN_SSLALERT) {
536 const uint8_t *alert = a2;
537 const enum ssl_encryption_level_t *level = a3;
538
539 if (alert)
540 chunk_appendf(&trace_buf, " alert=0x%02x", *alert);
541 if (level)
542 chunk_appendf(&trace_buf, " el=%c",
543 quic_enc_level_char(ssl_to_quic_enc_level(*level)));
544 }
Frédéric Lécailleea604992020-12-24 13:01:37 +0100545
546 if (mask & QUIC_EV_CONN_BCFRMS) {
547 const size_t *sz1 = a2;
548 const size_t *sz2 = a3;
549 const size_t *sz3 = a4;
550
551 if (sz1)
552 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz1);
553 if (sz2)
554 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz2);
555 if (sz3)
556 chunk_appendf(&trace_buf, " %llu", (unsigned long long)*sz3);
557 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100558
559 if (mask & QUIC_EV_CONN_PSTRM) {
560 const struct quic_frame *frm = a2;
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100561
562 if (a2) {
563 const struct quic_stream *s = &frm->stream;
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100564
Frédéric Lécaille577fe482021-01-11 15:10:06 +0100565 chunk_appendf(&trace_buf, " uni=%d fin=%d id=%llu off=%llu len=%llu",
566 !!(s->id & QUIC_STREAM_FRAME_ID_DIR_BIT),
567 !!(frm->type & QUIC_STREAM_FRAME_TYPE_FIN_BIT),
568 (unsigned long long)s->id,
569 (unsigned long long)s->offset,
570 (unsigned long long)s->len);
571 }
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +0100572 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100573 }
574 if (mask & QUIC_EV_CONN_LPKT) {
575 const struct quic_rx_packet *pkt = a2;
576
577 if (conn)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200578 chunk_appendf(&trace_buf, " xprt_ctx@%p qc@%p", conn->xprt_ctx, conn->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100579 if (pkt)
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +0200580 chunk_appendf(&trace_buf, " pkt@%p type=0x%02x %s pkt->qc@%p",
581 pkt, pkt->type, qc_pkt_long(pkt) ? "long" : "short", pkt->qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100582 }
583
584}
585
586/* Returns 1 if the peer has validated <qc> QUIC connection address, 0 if not. */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200587static inline int quic_peer_validated_addr(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100588{
589 struct quic_conn *qc;
590
591 qc = ctx->conn->qc;
592 if (objt_server(qc->conn->target))
593 return 1;
594
595 if ((qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
596 (qc->els[QUIC_TLS_ENC_LEVEL_APP].pktns->flags & QUIC_FL_PKTNS_ACK_RECEIVED) ||
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200597 HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100598 return 1;
599
600 return 0;
601}
602
603/* Set the timer attached to the QUIC connection with <ctx> as I/O handler and used for
604 * both loss detection and PTO and schedule the task assiated to this timer if needed.
605 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +0200606static inline void qc_set_timer(struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100607{
608 struct quic_conn *qc;
609 struct quic_pktns *pktns;
610 unsigned int pto;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200611 int handshake_complete;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100612
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100613 TRACE_ENTER(QUIC_EV_CONN_STIMER, ctx->conn,
614 NULL, NULL, &ctx->conn->qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100615 qc = ctx->conn->qc;
616 pktns = quic_loss_pktns(qc);
617 if (tick_isset(pktns->tx.loss_time)) {
618 qc->timer = pktns->tx.loss_time;
619 goto out;
620 }
621
622 /* XXX TODO: anti-amplification: the timer must be
623 * cancelled for a server which reached the anti-amplification limit.
624 */
625
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +0100626 if (!qc->path->ifae_pkts && quic_peer_validated_addr(ctx)) {
627 TRACE_PROTO("timer cancellation", QUIC_EV_CONN_STIMER, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100628 /* Timer cancellation. */
629 qc->timer = TICK_ETERNITY;
630 goto out;
631 }
632
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +0200633 handshake_complete = HA_ATOMIC_LOAD(&qc->state) >= QUIC_HS_ST_COMPLETE;
634 pktns = quic_pto_pktns(qc, handshake_complete, &pto);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100635 if (tick_isset(pto))
636 qc->timer = pto;
637 out:
638 task_schedule(qc->timer_task, qc->timer);
639 TRACE_LEAVE(QUIC_EV_CONN_STIMER, ctx->conn, pktns);
640}
641
642#ifndef OPENSSL_IS_BORINGSSL
643int ha_quic_set_encryption_secrets(SSL *ssl, enum ssl_encryption_level_t level,
644 const uint8_t *read_secret,
645 const uint8_t *write_secret, size_t secret_len)
646{
647 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
648 struct quic_tls_ctx *tls_ctx =
649 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
650 const SSL_CIPHER *cipher = SSL_get_current_cipher(ssl);
651
652 TRACE_ENTER(QUIC_EV_CONN_RWSEC, conn);
653 tls_ctx->rx.aead = tls_ctx->tx.aead = tls_aead(cipher);
654 tls_ctx->rx.md = tls_ctx->tx.md = tls_md(cipher);
655 tls_ctx->rx.hp = tls_ctx->tx.hp = tls_hp(cipher);
656
657 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
658 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
659 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
660 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
661 read_secret, secret_len)) {
662 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
663 return 0;
664 }
665
666 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
667 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
668 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
669 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
670 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
671 write_secret, secret_len)) {
672 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_RWSEC, conn);
673 return 0;
674 }
675
676 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
677 if (objt_server(conn->target) && level == ssl_encryption_application) {
678 const unsigned char *buf;
679 size_t buflen;
680
681 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
682 if (!buflen)
683 return 0;
684
685 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
686 return 0;
687 }
688 TRACE_LEAVE(QUIC_EV_CONN_RWSEC, conn, &level);
689
690 return 1;
691}
692#else
693/* ->set_read_secret callback to derive the RX secrets at <level> encryption
694 * level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500695 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100696 */
697int ha_set_rsec(SSL *ssl, enum ssl_encryption_level_t level,
698 const SSL_CIPHER *cipher,
699 const uint8_t *secret, size_t secret_len)
700{
701 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
702 struct quic_tls_ctx *tls_ctx =
703 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
704
705 TRACE_ENTER(QUIC_EV_CONN_RSEC, conn);
706 tls_ctx->rx.aead = tls_aead(cipher);
707 tls_ctx->rx.md = tls_md(cipher);
708 tls_ctx->rx.hp = tls_hp(cipher);
709
710 if (!quic_tls_derive_keys(tls_ctx->rx.aead, tls_ctx->rx.hp, tls_ctx->rx.md,
711 tls_ctx->rx.key, sizeof tls_ctx->rx.key,
712 tls_ctx->rx.iv, sizeof tls_ctx->rx.iv,
713 tls_ctx->rx.hp_key, sizeof tls_ctx->rx.hp_key,
714 secret, secret_len)) {
715 TRACE_DEVEL("RX key derivation failed", QUIC_EV_CONN_RSEC, conn);
716 goto err;
717 }
718
719 if (objt_server(conn->target) && level == ssl_encryption_application) {
720 const unsigned char *buf;
721 size_t buflen;
722
723 SSL_get_peer_quic_transport_params(ssl, &buf, &buflen);
724 if (!buflen)
725 goto err;
726
727 if (!quic_transport_params_store(conn->qc, 1, buf, buf + buflen))
728 goto err;
729 }
730
731 tls_ctx->rx.flags |= QUIC_FL_TLS_SECRETS_SET;
732 TRACE_LEAVE(QUIC_EV_CONN_RSEC, conn, &level, secret, &secret_len);
733
734 return 1;
735
736 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100737 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100738 return 0;
739}
740
741/* ->set_write_secret callback to derive the TX secrets at <level>
742 * encryption level.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +0500743 * Returns 1 if succeeded, 0 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100744 */
745int ha_set_wsec(SSL *ssl, enum ssl_encryption_level_t level,
746 const SSL_CIPHER *cipher,
747 const uint8_t *secret, size_t secret_len)
748{
749 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
750 struct quic_tls_ctx *tls_ctx =
751 &conn->qc->els[ssl_to_quic_enc_level(level)].tls_ctx;
752
753 TRACE_ENTER(QUIC_EV_CONN_WSEC, conn);
754 tls_ctx->tx.aead = tls_aead(cipher);
755 tls_ctx->tx.md = tls_md(cipher);
756 tls_ctx->tx.hp = tls_hp(cipher);
757
758 if (!quic_tls_derive_keys(tls_ctx->tx.aead, tls_ctx->tx.hp, tls_ctx->tx.md,
759 tls_ctx->tx.key, sizeof tls_ctx->tx.key,
760 tls_ctx->tx.iv, sizeof tls_ctx->tx.iv,
761 tls_ctx->tx.hp_key, sizeof tls_ctx->tx.hp_key,
762 secret, secret_len)) {
763 TRACE_DEVEL("TX key derivation failed", QUIC_EV_CONN_WSEC, conn);
764 goto err;
765 }
766
767 tls_ctx->tx.flags |= QUIC_FL_TLS_SECRETS_SET;
768 TRACE_LEAVE(QUIC_EV_CONN_WSEC, conn, &level, secret, &secret_len);
769
770 return 1;
771
772 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +0100773 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_WSEC, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100774 return 0;
775}
776#endif
777
778/* This function copies the CRYPTO data provided by the TLS stack found at <data>
779 * with <len> as size in CRYPTO buffers dedicated to store the information about
780 * outgoing CRYPTO frames so that to be able to replay the CRYPTO data streams.
781 * It fails only if it could not managed to allocate enough CRYPTO buffers to
782 * store all the data.
783 * Note that CRYPTO data may exist at any encryption level except at 0-RTT.
784 */
785static int quic_crypto_data_cpy(struct quic_enc_level *qel,
786 const unsigned char *data, size_t len)
787{
788 struct quic_crypto_buf **qcb;
789 /* The remaining byte to store in CRYPTO buffers. */
790 size_t cf_offset, cf_len, *nb_buf;
791 unsigned char *pos;
792
793 nb_buf = &qel->tx.crypto.nb_buf;
794 qcb = &qel->tx.crypto.bufs[*nb_buf - 1];
795 cf_offset = (*nb_buf - 1) * QUIC_CRYPTO_BUF_SZ + (*qcb)->sz;
796 cf_len = len;
797
798 while (len) {
799 size_t to_copy, room;
800
801 pos = (*qcb)->data + (*qcb)->sz;
802 room = QUIC_CRYPTO_BUF_SZ - (*qcb)->sz;
803 to_copy = len > room ? room : len;
804 if (to_copy) {
805 memcpy(pos, data, to_copy);
806 /* Increment the total size of this CRYPTO buffers by <to_copy>. */
807 qel->tx.crypto.sz += to_copy;
808 (*qcb)->sz += to_copy;
809 pos += to_copy;
810 len -= to_copy;
811 data += to_copy;
812 }
813 else {
814 struct quic_crypto_buf **tmp;
815
816 tmp = realloc(qel->tx.crypto.bufs,
817 (*nb_buf + 1) * sizeof *qel->tx.crypto.bufs);
818 if (tmp) {
819 qel->tx.crypto.bufs = tmp;
820 qcb = &qel->tx.crypto.bufs[*nb_buf];
821 *qcb = pool_alloc(pool_head_quic_crypto_buf);
822 if (!*qcb)
823 return 0;
824
825 (*qcb)->sz = 0;
826 ++*nb_buf;
827 }
828 else {
829 break;
830 }
831 }
832 }
833
834 /* Allocate a TX CRYPTO frame only if all the CRYPTO data
835 * have been buffered.
836 */
837 if (!len) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200838 struct quic_frame *frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100839
Frédéric Lécaille0ad04582021-07-27 14:51:54 +0200840 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100841 if (!frm)
842 return 0;
843
844 frm->type = QUIC_FT_CRYPTO;
845 frm->crypto.offset = cf_offset;
846 frm->crypto.len = cf_len;
Frédéric Lécaillef252adb2021-08-03 17:01:25 +0200847 frm->crypto.qel = qel;
Frédéric Lécaillec88df072021-07-27 11:43:11 +0200848 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100849 }
850
851 return len == 0;
852}
853
854
855/* ->add_handshake_data QUIC TLS callback used by the QUIC TLS stack when it
856 * wants to provide the QUIC layer with CRYPTO data.
857 * Returns 1 if succeeded, 0 if not.
858 */
859int ha_quic_add_handshake_data(SSL *ssl, enum ssl_encryption_level_t level,
860 const uint8_t *data, size_t len)
861{
862 struct connection *conn;
863 enum quic_tls_enc_level tel;
864 struct quic_enc_level *qel;
865
866 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
867 TRACE_ENTER(QUIC_EV_CONN_ADDDATA, conn);
868 tel = ssl_to_quic_enc_level(level);
869 qel = &conn->qc->els[tel];
870
871 if (tel == -1) {
872 TRACE_PROTO("Wrong encryption level", QUIC_EV_CONN_ADDDATA, conn);
873 goto err;
874 }
875
876 if (!quic_crypto_data_cpy(qel, data, len)) {
877 TRACE_PROTO("Could not bufferize", QUIC_EV_CONN_ADDDATA, conn);
878 goto err;
879 }
880
881 TRACE_PROTO("CRYPTO data buffered", QUIC_EV_CONN_ADDDATA,
882 conn, &level, &len);
883
884 TRACE_LEAVE(QUIC_EV_CONN_ADDDATA, conn);
885 return 1;
886
887 err:
888 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ADDDATA, conn);
889 return 0;
890}
891
892int ha_quic_flush_flight(SSL *ssl)
893{
894 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
895
896 TRACE_ENTER(QUIC_EV_CONN_FFLIGHT, conn);
897 TRACE_LEAVE(QUIC_EV_CONN_FFLIGHT, conn);
898
899 return 1;
900}
901
902int ha_quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert)
903{
904 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
905
Frédéric Lécaille47c433f2020-12-10 17:03:11 +0100906 TRACE_DEVEL("SSL alert", QUIC_EV_CONN_SSLALERT, conn, &alert, &level);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100907 return 1;
908}
909
910/* QUIC TLS methods */
911static SSL_QUIC_METHOD ha_quic_method = {
912#ifdef OPENSSL_IS_BORINGSSL
913 .set_read_secret = ha_set_rsec,
914 .set_write_secret = ha_set_wsec,
915#else
916 .set_encryption_secrets = ha_quic_set_encryption_secrets,
917#endif
918 .add_handshake_data = ha_quic_add_handshake_data,
919 .flush_flight = ha_quic_flush_flight,
920 .send_alert = ha_quic_send_alert,
921};
922
923/* Initialize the TLS context of a listener with <bind_conf> as configuration.
924 * Returns an error count.
925 */
926int ssl_quic_initial_ctx(struct bind_conf *bind_conf)
927{
928 struct proxy *curproxy = bind_conf->frontend;
929 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
930 int cfgerr = 0;
931
932#if 0
933 /* XXX Did not manage to use this. */
934 const char *ciphers =
935 "TLS_AES_128_GCM_SHA256:"
936 "TLS_AES_256_GCM_SHA384:"
937 "TLS_CHACHA20_POLY1305_SHA256:"
938 "TLS_AES_128_CCM_SHA256";
939#endif
Frédéric Lécaille4b1fddc2021-07-01 17:09:05 +0200940 const char *groups = "X25519:P-256:P-384:P-521";
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +0100941 long options =
942 (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
943 SSL_OP_SINGLE_ECDH_USE |
944 SSL_OP_CIPHER_SERVER_PREFERENCE;
945 SSL_CTX *ctx;
946
947 ctx = SSL_CTX_new(TLS_server_method());
948 bind_conf->initial_ctx = ctx;
949
950 SSL_CTX_set_options(ctx, options);
951#if 0
952 if (SSL_CTX_set_cipher_list(ctx, ciphers) != 1) {
953 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher list to '%s' "
954 "for bind '%s' at [%s:%d].\n",
955 curproxy->id, ciphers,
956 bind_conf->arg, bind_conf->file, bind_conf->line);
957 cfgerr++;
958 }
959#endif
960
961 if (SSL_CTX_set1_curves_list(ctx, groups) != 1) {
962 ha_alert("Proxy '%s': unable to set TLS 1.3 curves list to '%s' "
963 "for bind '%s' at [%s:%d].\n",
964 curproxy->id, groups,
965 bind_conf->arg, bind_conf->file, bind_conf->line);
966 cfgerr++;
967 }
968
969 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
970 SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
971 SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION);
972 SSL_CTX_set_default_verify_paths(ctx);
973
974#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
975#ifdef OPENSSL_IS_BORINGSSL
976 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
977 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
978#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
979 if (bind_conf->ssl_conf.early_data) {
980 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
981 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
982 }
983 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
984 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
985#else
986 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
987#endif
988 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
989#endif
990 SSL_CTX_set_quic_method(ctx, &ha_quic_method);
991
992 return cfgerr;
993}
994
995/* Decode an expected packet number from <truncated_on> its truncated value,
996 * depending on <largest_pn> the largest received packet number, and <pn_nbits>
997 * the number of bits used to encode this packet number (its length in bytes * 8).
998 * See https://quicwg.org/base-drafts/draft-ietf-quic-transport.html#packet-encoding
999 */
1000static uint64_t decode_packet_number(uint64_t largest_pn,
1001 uint32_t truncated_pn, unsigned int pn_nbits)
1002{
1003 uint64_t expected_pn = largest_pn + 1;
1004 uint64_t pn_win = (uint64_t)1 << pn_nbits;
1005 uint64_t pn_hwin = pn_win / 2;
1006 uint64_t pn_mask = pn_win - 1;
1007 uint64_t candidate_pn;
1008
1009
1010 candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
1011 /* Note that <pn_win> > <pn_hwin>. */
1012 if (candidate_pn < QUIC_MAX_PACKET_NUM - pn_win &&
1013 candidate_pn + pn_hwin <= expected_pn)
1014 return candidate_pn + pn_win;
1015
1016 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
1017 return candidate_pn - pn_win;
1018
1019 return candidate_pn;
1020}
1021
1022/* Remove the header protection of <pkt> QUIC packet using <tls_ctx> as QUIC TLS
1023 * cryptographic context.
1024 * <largest_pn> is the largest received packet number and <pn> the address of
1025 * the packet number field for this packet with <byte0> address of its first byte.
1026 * <end> points to one byte past the end of this packet.
1027 * Returns 1 if succeeded, 0 if not.
1028 */
1029static int qc_do_rm_hp(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx,
1030 int64_t largest_pn, unsigned char *pn,
1031 unsigned char *byte0, const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001032 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001033{
1034 int ret, outlen, i, pnlen;
1035 uint64_t packet_number;
1036 uint32_t truncated_pn = 0;
1037 unsigned char mask[5] = {0};
1038 unsigned char *sample;
1039 EVP_CIPHER_CTX *cctx;
1040 unsigned char *hp_key;
1041
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001042 /* Check there is enough data in this packet. */
1043 if (end - pn < QUIC_PACKET_PN_MAXLEN + sizeof mask) {
1044 TRACE_DEVEL("too short packet", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1045 return 0;
1046 }
1047
1048 cctx = EVP_CIPHER_CTX_new();
1049 if (!cctx) {
1050 TRACE_DEVEL("memory allocation failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1051 return 0;
1052 }
1053
1054 ret = 0;
1055 sample = pn + QUIC_PACKET_PN_MAXLEN;
1056
1057 hp_key = tls_ctx->rx.hp_key;
1058 if (!EVP_DecryptInit_ex(cctx, tls_ctx->rx.hp, NULL, hp_key, sample) ||
1059 !EVP_DecryptUpdate(cctx, mask, &outlen, mask, sizeof mask) ||
1060 !EVP_DecryptFinal_ex(cctx, mask, &outlen)) {
1061 TRACE_DEVEL("decryption failed", QUIC_EV_CONN_RMHP, ctx->conn, pkt);
1062 goto out;
1063 }
1064
1065 *byte0 ^= mask[0] & (*byte0 & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
1066 pnlen = (*byte0 & QUIC_PACKET_PNL_BITMASK) + 1;
1067 for (i = 0; i < pnlen; i++) {
1068 pn[i] ^= mask[i + 1];
1069 truncated_pn = (truncated_pn << 8) | pn[i];
1070 }
1071
1072 packet_number = decode_packet_number(largest_pn, truncated_pn, pnlen * 8);
1073 /* Store remaining information for this unprotected header */
1074 pkt->pn = packet_number;
1075 pkt->pnl = pnlen;
1076
1077 ret = 1;
1078
1079 out:
1080 EVP_CIPHER_CTX_free(cctx);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001081
1082 return ret;
1083}
1084
1085/* Encrypt the payload of a QUIC packet with <pn> as number found at <payload>
1086 * address, with <payload_len> as payload length, <aad> as address of
1087 * the ADD and <aad_len> as AAD length depending on the <tls_ctx> QUIC TLS
1088 * context.
1089 * Returns 1 if succeeded, 0 if not.
1090 */
1091static int quic_packet_encrypt(unsigned char *payload, size_t payload_len,
1092 unsigned char *aad, size_t aad_len, uint64_t pn,
1093 struct quic_tls_ctx *tls_ctx, struct connection *conn)
1094{
1095 unsigned char iv[12];
1096 unsigned char *tx_iv = tls_ctx->tx.iv;
1097 size_t tx_iv_sz = sizeof tls_ctx->tx.iv;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001098 struct enc_debug_info edi;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001099
1100 if (!quic_aead_iv_build(iv, sizeof iv, tx_iv, tx_iv_sz, pn)) {
1101 TRACE_DEVEL("AEAD IV building for encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001102 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001103 }
1104
1105 if (!quic_tls_encrypt(payload, payload_len, aad, aad_len,
1106 tls_ctx->tx.aead, tls_ctx->tx.key, iv)) {
1107 TRACE_DEVEL("QUIC packet encryption failed", QUIC_EV_CONN_HPKT, conn);
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001108 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001109 }
1110
1111 return 1;
Frédéric Lécaillef63921f2020-12-18 09:48:20 +01001112
1113 err:
1114 enc_debug_info_init(&edi, payload, payload_len, aad, aad_len, pn);
1115 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ENCPKT, conn, &edi);
1116 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001117}
1118
1119/* Decrypt <pkt> QUIC packet with <tls_ctx> as QUIC TLS cryptographic context.
1120 * Returns 1 if succeeded, 0 if not.
1121 */
1122static int qc_pkt_decrypt(struct quic_rx_packet *pkt, struct quic_tls_ctx *tls_ctx)
1123{
1124 int ret;
1125 unsigned char iv[12];
1126 unsigned char *rx_iv = tls_ctx->rx.iv;
1127 size_t rx_iv_sz = sizeof tls_ctx->rx.iv;
1128
1129 if (!quic_aead_iv_build(iv, sizeof iv, rx_iv, rx_iv_sz, pkt->pn))
1130 return 0;
1131
1132 ret = quic_tls_decrypt(pkt->data + pkt->aad_len, pkt->len - pkt->aad_len,
1133 pkt->data, pkt->aad_len,
1134 tls_ctx->rx.aead, tls_ctx->rx.key, iv);
1135 if (!ret)
1136 return 0;
1137
1138 /* Update the packet length (required to parse the frames). */
1139 pkt->len = pkt->aad_len + ret;
1140
1141 return 1;
1142}
1143
1144/* Treat <frm> frame whose packet it is attached to has just been acknowledged. */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001145static inline void qc_treat_acked_tx_frm(struct quic_frame *frm,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001146 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001147{
1148 TRACE_PROTO("Removing frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001149 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001150 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001151}
1152
1153/* Remove <largest> down to <smallest> node entries from <pkts> tree of TX packet,
1154 * deallocating them, and their TX frames.
1155 * Returns the last node reached to be used for the next range.
1156 * May be NULL if <largest> node could not be found.
1157 */
1158static inline struct eb64_node *qc_ackrng_pkts(struct eb_root *pkts, unsigned int *pkt_flags,
1159 struct list *newly_acked_pkts,
1160 struct eb64_node *largest_node,
1161 uint64_t largest, uint64_t smallest,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001162 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001163{
1164 struct eb64_node *node;
1165 struct quic_tx_packet *pkt;
1166
1167 if (largest_node)
1168 node = largest_node;
1169 else {
1170 node = eb64_lookup(pkts, largest);
1171 while (!node && largest > smallest) {
1172 node = eb64_lookup(pkts, --largest);
1173 }
1174 }
1175
1176 while (node && node->key >= smallest) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001177 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001178
1179 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
1180 *pkt_flags |= pkt->flags;
Willy Tarreau2b718102021-04-21 07:32:39 +02001181 LIST_INSERT(newly_acked_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001182 TRACE_PROTO("Removing packet #", QUIC_EV_CONN_PRSAFRM, ctx->conn,, &pkt->pn_node.key);
1183 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1184 qc_treat_acked_tx_frm(frm, ctx);
1185 node = eb64_prev(node);
1186 eb64_delete(&pkt->pn_node);
1187 }
1188
1189 return node;
1190}
1191
1192/* Treat <frm> frame whose packet it is attached to has just been detected as non
1193 * acknowledged.
1194 */
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001195static inline void qc_treat_nacked_tx_frm(struct quic_frame *frm,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001196 struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001197 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001198{
1199 TRACE_PROTO("to resend frame", QUIC_EV_CONN_PRSAFRM, ctx->conn, frm);
Willy Tarreau2b718102021-04-21 07:32:39 +02001200 LIST_DELETE(&frm->list);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02001201 MT_LIST_INSERT(&pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001202}
1203
1204
1205/* Free the TX packets of <pkts> list */
1206static inline void free_quic_tx_pkts(struct list *pkts)
1207{
1208 struct quic_tx_packet *pkt, *tmp;
1209
1210 list_for_each_entry_safe(pkt, tmp, pkts, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001211 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001212 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001213 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001214 }
1215}
1216
1217/* Send a packet loss event nofification to the congestion controller
1218 * attached to <qc> connection with <lost_bytes> the number of lost bytes,
1219 * <oldest_lost>, <newest_lost> the oldest lost packet and newest lost packet
1220 * at <now_us> current time.
1221 * Always succeeds.
1222 */
1223static inline void qc_cc_loss_event(struct quic_conn *qc,
1224 unsigned int lost_bytes,
1225 unsigned int newest_time_sent,
1226 unsigned int period,
1227 unsigned int now_us)
1228{
1229 struct quic_cc_event ev = {
1230 .type = QUIC_CC_EVT_LOSS,
1231 .loss.now_ms = now_ms,
1232 .loss.max_ack_delay = qc->max_ack_delay,
1233 .loss.lost_bytes = lost_bytes,
1234 .loss.newest_time_sent = newest_time_sent,
1235 .loss.period = period,
1236 };
1237
1238 quic_cc_event(&qc->path->cc, &ev);
1239}
1240
1241/* Send a packet ack event nofication for each newly acked packet of
1242 * <newly_acked_pkts> list and free them.
1243 * Always succeeds.
1244 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001245static inline void qc_treat_newly_acked_pkts(struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001246 struct list *newly_acked_pkts)
1247{
1248 struct quic_conn *qc = ctx->conn->qc;
1249 struct quic_tx_packet *pkt, *tmp;
1250 struct quic_cc_event ev = { .type = QUIC_CC_EVT_ACK, };
1251
1252 list_for_each_entry_safe(pkt, tmp, newly_acked_pkts, list) {
1253 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001254 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001255 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001256 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001257 ev.ack.acked = pkt->in_flight_len;
1258 ev.ack.time_sent = pkt->time_sent;
1259 quic_cc_event(&qc->path->cc, &ev);
Willy Tarreau2b718102021-04-21 07:32:39 +02001260 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001261 eb64_delete(&pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001262 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001263 }
1264
1265}
1266
1267/* Handle <pkts> list of lost packets detected at <now_us> handling
1268 * their TX frames.
1269 * Send a packet loss event to the congestion controller if
1270 * in flight packet have been lost.
1271 * Also frees the packet in <pkts> list.
1272 * Never fails.
1273 */
1274static inline void qc_release_lost_pkts(struct quic_pktns *pktns,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001275 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001276 struct list *pkts,
1277 uint64_t now_us)
1278{
1279 struct quic_conn *qc = ctx->conn->qc;
1280 struct quic_tx_packet *pkt, *tmp, *oldest_lost, *newest_lost;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02001281 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001282 uint64_t lost_bytes;
1283
1284 lost_bytes = 0;
1285 oldest_lost = newest_lost = NULL;
1286 list_for_each_entry_safe(pkt, tmp, pkts, list) {
1287 lost_bytes += pkt->in_flight_len;
1288 pkt->pktns->tx.in_flight -= pkt->in_flight_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01001289 qc->path->prep_in_flight -= pkt->in_flight_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001290 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING)
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01001291 qc->path->ifae_pkts--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001292 /* Treat the frames of this lost packet. */
1293 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list)
1294 qc_treat_nacked_tx_frm(frm, pktns, ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02001295 LIST_DELETE(&pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001296 if (!oldest_lost) {
1297 oldest_lost = newest_lost = pkt;
1298 }
1299 else {
1300 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001301 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001302 newest_lost = pkt;
1303 }
1304 }
1305
1306 if (lost_bytes) {
1307 /* Sent a packet loss event to the congestion controller. */
1308 qc_cc_loss_event(ctx->conn->qc, lost_bytes, newest_lost->time_sent,
1309 newest_lost->time_sent - oldest_lost->time_sent, now_us);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001310 quic_tx_packet_refdec(oldest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001311 if (newest_lost != oldest_lost)
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001312 quic_tx_packet_refdec(newest_lost);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001313 }
1314}
1315
1316/* Look for packet loss from sent packets for <qel> encryption level of a
1317 * connection with <ctx> as I/O handler context. If remove is true, remove them from
1318 * their tree if deemed as lost or set the <loss_time> value the packet number
1319 * space if any not deemed lost.
1320 * Should be called after having received an ACK frame with newly acknowledged
1321 * packets or when the the loss detection timer has expired.
1322 * Always succeeds.
1323 */
1324static void qc_packet_loss_lookup(struct quic_pktns *pktns,
1325 struct quic_conn *qc,
1326 struct list *lost_pkts)
1327{
1328 struct eb_root *pkts;
1329 struct eb64_node *node;
1330 struct quic_loss *ql;
1331 unsigned int loss_delay;
1332
1333 TRACE_ENTER(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns);
1334 pkts = &pktns->tx.pkts;
1335 pktns->tx.loss_time = TICK_ETERNITY;
1336 if (eb_is_empty(pkts))
1337 goto out;
1338
1339 ql = &qc->path->loss;
1340 loss_delay = QUIC_MAX(ql->latest_rtt, ql->srtt >> 3);
1341 loss_delay += loss_delay >> 3;
1342 loss_delay = QUIC_MAX(loss_delay, MS_TO_TICKS(QUIC_TIMER_GRANULARITY));
1343
1344 node = eb64_first(pkts);
1345 while (node) {
1346 struct quic_tx_packet *pkt;
1347 int64_t largest_acked_pn;
1348 unsigned int loss_time_limit, time_sent;
1349
1350 pkt = eb64_entry(&node->node, struct quic_tx_packet, pn_node);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001351 largest_acked_pn = HA_ATOMIC_LOAD(&pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001352 node = eb64_next(node);
1353 if ((int64_t)pkt->pn_node.key > largest_acked_pn)
1354 break;
1355
1356 time_sent = pkt->time_sent;
1357 loss_time_limit = tick_add(time_sent, loss_delay);
1358 if (tick_is_le(time_sent, now_ms) ||
1359 (int64_t)largest_acked_pn >= pkt->pn_node.key + QUIC_LOSS_PACKET_THRESHOLD) {
1360 eb64_delete(&pkt->pn_node);
Willy Tarreau2b718102021-04-21 07:32:39 +02001361 LIST_APPEND(lost_pkts, &pkt->list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001362 }
1363 else {
1364 pktns->tx.loss_time = tick_first(pktns->tx.loss_time, loss_time_limit);
1365 }
1366 }
1367
1368 out:
1369 TRACE_LEAVE(QUIC_EV_CONN_PKTLOSS, qc->conn, pktns, lost_pkts);
1370}
1371
1372/* Parse ACK frame into <frm> from a buffer at <buf> address with <end> being at
1373 * one byte past the end of this buffer. Also update <rtt_sample> if needed, i.e.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001374 * 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 +01001375 * acked ack-eliciting packet.
1376 * Return 1, if succeeded, 0 if not.
1377 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001378static 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 +01001379 struct quic_enc_level *qel,
1380 unsigned int *rtt_sample,
1381 const unsigned char **pos, const unsigned char *end)
1382{
1383 struct quic_ack *ack = &frm->ack;
1384 uint64_t smallest, largest;
1385 struct eb_root *pkts;
1386 struct eb64_node *largest_node;
1387 unsigned int time_sent, pkt_flags;
1388 struct list newly_acked_pkts = LIST_HEAD_INIT(newly_acked_pkts);
1389 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
1390
1391 if (ack->largest_ack > qel->pktns->tx.next_pn) {
1392 TRACE_DEVEL("ACK for not sent packet", QUIC_EV_CONN_PRSAFRM,
1393 ctx->conn,, &ack->largest_ack);
1394 goto err;
1395 }
1396
1397 if (ack->first_ack_range > ack->largest_ack) {
1398 TRACE_DEVEL("too big first ACK range", QUIC_EV_CONN_PRSAFRM,
1399 ctx->conn,, &ack->first_ack_range);
1400 goto err;
1401 }
1402
1403 largest = ack->largest_ack;
1404 smallest = largest - ack->first_ack_range;
1405 pkts = &qel->pktns->tx.pkts;
1406 pkt_flags = 0;
1407 largest_node = NULL;
1408 time_sent = 0;
1409
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001410 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 +01001411 largest_node = eb64_lookup(pkts, largest);
1412 if (!largest_node) {
1413 TRACE_DEVEL("Largest acked packet not found",
1414 QUIC_EV_CONN_PRSAFRM, ctx->conn);
1415 goto err;
1416 }
1417
1418 time_sent = eb64_entry(&largest_node->node,
1419 struct quic_tx_packet, pn_node)->time_sent;
1420 }
1421
1422 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1423 ctx->conn,, &largest, &smallest);
1424 do {
1425 uint64_t gap, ack_range;
1426
1427 qc_ackrng_pkts(pkts, &pkt_flags, &newly_acked_pkts,
1428 largest_node, largest, smallest, ctx);
1429 if (!ack->ack_range_num--)
1430 break;
1431
1432 if (!quic_dec_int(&gap, pos, end))
1433 goto err;
1434
1435 if (smallest < gap + 2) {
1436 TRACE_DEVEL("wrong gap value", QUIC_EV_CONN_PRSAFRM,
1437 ctx->conn,, &gap, &smallest);
1438 goto err;
1439 }
1440
1441 largest = smallest - gap - 2;
1442 if (!quic_dec_int(&ack_range, pos, end))
1443 goto err;
1444
1445 if (largest < ack_range) {
1446 TRACE_DEVEL("wrong ack range value", QUIC_EV_CONN_PRSAFRM,
1447 ctx->conn,, &largest, &ack_range);
1448 goto err;
1449 }
1450
1451 /* Do not use this node anymore. */
1452 largest_node = NULL;
1453 /* Next range */
1454 smallest = largest - ack_range;
1455
1456 TRACE_PROTO("ack range", QUIC_EV_CONN_PRSAFRM,
1457 ctx->conn,, &largest, &smallest);
1458 } while (1);
1459
1460 /* Flag this packet number space as having received an ACK. */
1461 qel->pktns->flags |= QUIC_FL_PKTNS_ACK_RECEIVED;
1462
1463 if (time_sent && (pkt_flags & QUIC_FL_TX_PACKET_ACK_ELICITING)) {
1464 *rtt_sample = tick_remain(time_sent, now_ms);
Frédéric Lécaille59b07c72021-08-03 16:06:01 +02001465 HA_ATOMIC_STORE(&qel->pktns->tx.largest_acked_pn, ack->largest_ack);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001466 }
1467
1468 if (!LIST_ISEMPTY(&newly_acked_pkts)) {
1469 if (!eb_is_empty(&qel->pktns->tx.pkts)) {
1470 qc_packet_loss_lookup(qel->pktns, ctx->conn->qc, &lost_pkts);
1471 if (!LIST_ISEMPTY(&lost_pkts))
1472 qc_release_lost_pkts(qel->pktns, ctx, &lost_pkts, now_ms);
1473 }
1474 qc_treat_newly_acked_pkts(ctx, &newly_acked_pkts);
1475 if (quic_peer_validated_addr(ctx))
1476 ctx->conn->qc->path->loss.pto_count = 0;
1477 qc_set_timer(ctx);
1478 }
1479
1480
1481 return 1;
1482
1483 err:
1484 free_quic_tx_pkts(&newly_acked_pkts);
1485 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSAFRM, ctx->conn);
1486 return 0;
1487}
1488
1489/* Provide CRYPTO data to the TLS stack found at <data> with <len> as length
1490 * from <qel> encryption level with <ctx> as QUIC connection context.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05001491 * Remaining parameter are there for debugging purposes.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001492 * Return 1 if succeeded, 0 if not.
1493 */
1494static inline int qc_provide_cdata(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001495 struct ssl_sock_ctx *ctx,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001496 const unsigned char *data, size_t len,
1497 struct quic_rx_packet *pkt,
1498 struct quic_rx_crypto_frm *cf)
1499{
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001500 int ssl_err, state;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001501 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001502
1503 TRACE_ENTER(QUIC_EV_CONN_SSLDATA, ctx->conn);
1504 ssl_err = SSL_ERROR_NONE;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02001505 qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001506 if (SSL_provide_quic_data(ctx->ssl, el->level, data, len) != 1) {
1507 TRACE_PROTO("SSL_provide_quic_data() error",
1508 QUIC_EV_CONN_SSLDATA, ctx->conn, pkt, cf, ctx->ssl);
1509 goto err;
1510 }
1511
1512 el->rx.crypto.offset += len;
1513 TRACE_PROTO("in order CRYPTO data",
1514 QUIC_EV_CONN_SSLDATA, ctx->conn,, cf, ctx->ssl);
1515
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001516 state = HA_ATOMIC_LOAD(&qc->state);
1517 if (state < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001518 ssl_err = SSL_do_handshake(ctx->ssl);
1519 if (ssl_err != 1) {
1520 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1521 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1522 TRACE_PROTO("SSL handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001523 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001524 goto out;
1525 }
1526
1527 TRACE_DEVEL("SSL handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001528 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001529 goto err;
1530 }
1531
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001532 TRACE_PROTO("SSL handshake OK", QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001533 if (objt_listener(ctx->conn->target))
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001534 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001535 else
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001536 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_COMPLETE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001537 } else {
1538 ssl_err = SSL_process_quic_post_handshake(ctx->ssl);
1539 if (ssl_err != 1) {
1540 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
1541 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
1542 TRACE_DEVEL("SSL post handshake",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001543 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001544 goto out;
1545 }
1546
1547 TRACE_DEVEL("SSL post handshake error",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001548 QUIC_EV_CONN_HDSHK, ctx->conn, &state, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001549 goto err;
1550 }
1551
1552 TRACE_PROTO("SSL post handshake succeeded",
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001553 QUIC_EV_CONN_HDSHK, ctx->conn, &state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001554 }
1555
1556 out:
1557 TRACE_LEAVE(QUIC_EV_CONN_SSLDATA, ctx->conn);
1558 return 1;
1559
1560 err:
1561 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_SSLDATA, ctx->conn);
1562 return 0;
1563}
1564
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001565/* Allocate a new STREAM RX frame from <stream_fm> STREAM frame attached to
1566 * <pkt> RX packet.
1567 * Return it if succeeded, NULL if not.
1568 */
1569static inline
1570struct quic_rx_strm_frm *new_quic_rx_strm_frm(struct quic_stream *stream_frm,
1571 struct quic_rx_packet *pkt)
1572{
1573 struct quic_rx_strm_frm *frm;
1574
1575 frm = pool_alloc(pool_head_quic_rx_strm_frm);
1576 if (frm) {
1577 frm->offset_node.key = stream_frm->offset;
1578 frm->len = stream_frm->len;
1579 frm->data = stream_frm->data;
1580 frm->pkt = pkt;
1581 }
1582
1583 return frm;
1584}
1585
1586/* Retrieve as an ebtree node the stream with <id> as ID, possibly allocates
1587 * several streams, depending on the already open onces.
1588 * Return this node if succeeded, NULL if not.
1589 */
1590static struct eb64_node *qcc_get_qcs(struct qcc *qcc, uint64_t id)
1591{
1592 unsigned int strm_type;
1593 int64_t sub_id;
1594 struct eb64_node *strm_node;
1595
1596 TRACE_ENTER(QUIC_EV_CONN_PSTRM, qcc->conn);
1597
1598 strm_type = id & QCS_ID_TYPE_MASK;
1599 sub_id = id >> QCS_ID_TYPE_SHIFT;
1600 strm_node = NULL;
1601 if (qc_local_stream_id(qcc, id)) {
1602 /* Local streams: this stream must be already opened. */
1603 strm_node = eb64_lookup(&qcc->streams_by_id, id);
1604 if (!strm_node) {
1605 TRACE_PROTO("Unknown stream ID", QUIC_EV_CONN_PSTRM, qcc->conn);
1606 goto out;
1607 }
1608 }
1609 else {
1610 /* Remote streams. */
1611 struct eb_root *strms;
1612 uint64_t largest_id;
1613 enum qcs_type qcs_type;
1614
1615 strms = &qcc->streams_by_id;
1616 qcs_type = qcs_id_type(id);
1617 if (sub_id + 1 > qcc->strms[qcs_type].max_streams) {
1618 TRACE_PROTO("Streams limit reached", QUIC_EV_CONN_PSTRM, qcc->conn);
1619 goto out;
1620 }
1621
1622 /* Note: ->largest_id was initialized with (uint64_t)-1 as value, 0 being a
1623 * correct value.
1624 */
1625 largest_id = qcc->strms[qcs_type].largest_id;
1626 if (sub_id > (int64_t)largest_id) {
1627 /* RFC: "A stream ID that is used out of order results in all streams
1628 * of that type with lower-numbered stream IDs also being opened".
1629 * So, let's "open" these streams.
1630 */
1631 int64_t i;
1632 struct qcs *qcs;
1633
1634 qcs = NULL;
1635 for (i = largest_id + 1; i <= sub_id; i++) {
1636 qcs = qcs_new(qcc, (i << QCS_ID_TYPE_SHIFT) | strm_type);
1637 if (!qcs) {
1638 TRACE_PROTO("Could not allocate a new stream",
1639 QUIC_EV_CONN_PSTRM, qcc->conn);
1640 goto out;
1641 }
1642
1643 qcc->strms[qcs_type].largest_id = i;
1644 }
1645 if (qcs)
1646 strm_node = &qcs->by_id;
1647 }
1648 else {
1649 strm_node = eb64_lookup(strms, id);
1650 }
1651 }
1652
1653 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1654 return strm_node;
1655
1656 out:
1657 TRACE_LEAVE(QUIC_EV_CONN_PSTRM, qcc->conn);
1658 return NULL;
1659}
1660
1661/* Copy as most as possible STREAM data from <strm_frm> into <strm> stream.
1662 * Returns the number of bytes copied or -1 if failed. Also update <strm_frm> frame
1663 * to reflect the data which have been consumed.
1664 */
1665static size_t qc_strm_cpy(struct buffer *buf, struct quic_stream *strm_frm)
1666{
1667 size_t ret;
1668
1669 ret = 0;
1670 while (strm_frm->len) {
1671 size_t try;
1672
1673 try = b_contig_space(buf);
1674 if (!try)
1675 break;
1676
1677 if (try > strm_frm->len)
1678 try = strm_frm->len;
1679 memcpy(b_tail(buf), strm_frm->data, try);
1680 strm_frm->len -= try;
1681 strm_frm->offset += try;
1682 b_add(buf, try);
1683 ret += try;
1684 }
1685
1686 return ret;
1687}
1688
1689/* Handle <strm_frm> bidirectional STREAM frame. Depending on its ID, several
1690 * streams may be open. The data are copied to the stream RX buffer if possible.
1691 * If not, the STREAM frame is stored to be treated again later.
1692 * We rely on the flow control so that not to store too much STREAM frames.
1693 * Return 1 if succeeded, 0 if not.
1694 */
1695static int qc_handle_bidi_strm_frm(struct quic_rx_packet *pkt,
1696 struct quic_stream *strm_frm,
1697 struct quic_conn *qc)
1698{
1699 struct qcs *strm;
1700 struct eb64_node *strm_node, *frm_node;
1701 struct quic_rx_strm_frm *frm;
1702
1703 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1704 if (!strm_node) {
1705 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1706 return 0;
1707 }
1708
1709 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1710 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1711 /* FIXME: handle the case where this frame overlap others */
1712 if (frm_node) {
1713 TRACE_PROTO("Already existing stream data",
1714 QUIC_EV_CONN_PSTRM, qc->conn);
1715 goto out;
1716 }
1717
1718 if (strm_frm->offset == strm->rx.offset) {
1719 int ret;
1720
1721 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1722 goto store_frm;
1723
1724 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1725 if (ret && qc->qcc->app_ops->decode_qcs(strm, qc->qcc->ctx) == -1) {
1726 TRACE_PROTO("Decoding error", QUIC_EV_CONN_PSTRM);
1727 return 0;
1728 }
1729
1730 strm->rx.offset += ret;
1731 }
1732
1733 if (!strm_frm->len)
1734 goto out;
1735
1736 store_frm:
1737 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1738 if (!frm) {
1739 TRACE_PROTO("Could not alloc RX STREAM frame",
1740 QUIC_EV_CONN_PSTRM, qc->conn);
1741 return 0;
1742 }
1743
1744 eb64_insert(&strm->frms, &frm->offset_node);
1745 quic_rx_packet_refinc(pkt);
1746
1747 out:
1748 return 1;
1749}
1750
1751/* Handle <strm_frm> unidirectional STREAM frame. Depending on its ID, several
1752 * streams may be open. The data are copied to the stream RX buffer if possible.
1753 * If not, the STREAM frame is stored to be treated again later.
1754 * We rely on the flow control so that not to store too much STREAM frames.
1755 * Return 1 if succeeded, 0 if not.
1756 */
1757static int qc_handle_uni_strm_frm(struct quic_rx_packet *pkt,
1758 struct quic_stream *strm_frm,
1759 struct quic_conn *qc)
1760{
1761 struct qcs *strm;
1762 struct eb64_node *strm_node, *frm_node;
1763 struct quic_rx_strm_frm *frm;
1764 size_t strm_frm_len;
1765
1766 strm_node = qcc_get_qcs(qc->qcc, strm_frm->id);
1767 if (!strm_node) {
1768 TRACE_PROTO("Stream not found", QUIC_EV_CONN_PSTRM, qc->conn);
1769 return 0;
1770 }
1771
1772 strm = eb64_entry(&strm_node->node, struct qcs, by_id);
1773 frm_node = eb64_lookup(&strm->frms, strm_frm->offset);
1774 /* FIXME: handle the case where this frame overlap others */
1775 if (frm_node) {
1776 TRACE_PROTO("Already existing stream data",
1777 QUIC_EV_CONN_PSTRM, qc->conn);
1778 goto out;
1779 }
1780
1781 strm_frm_len = strm_frm->len;
1782 if (strm_frm->offset == strm->rx.offset) {
1783 int ret;
1784
1785 if (!qc_get_buf(qc->qcc, &strm->rx.buf))
1786 goto store_frm;
1787
1788 /* qc_strm_cpy() will modify the offset, depending on the number
1789 * of bytes copied.
1790 */
1791 ret = qc_strm_cpy(&strm->rx.buf, strm_frm);
1792 /* Inform the application of the arrival of this new stream */
1793 if (!strm->rx.offset && !qc->qcc->app_ops->attach_ruqs(strm, qc->qcc->ctx)) {
1794 TRACE_PROTO("Could not set an uni-stream", QUIC_EV_CONN_PSTRM, qc->conn);
1795 return 0;
1796 }
1797
1798 if (ret)
1799 ruqs_notify_recv(strm);
1800
1801 strm_frm->offset += ret;
1802 }
1803 /* Take this frame into an account for the stream flow control */
1804 strm->rx.offset += strm_frm_len;
1805 /* It all the data were provided to the application, there is no need to
1806 * store any more inforamtion for it.
1807 */
1808 if (!strm_frm->len)
1809 goto out;
1810
1811 store_frm:
1812 frm = new_quic_rx_strm_frm(strm_frm, pkt);
1813 if (!frm) {
1814 TRACE_PROTO("Could not alloc RX STREAM frame",
1815 QUIC_EV_CONN_PSTRM, qc->conn);
1816 return 0;
1817 }
1818
1819 eb64_insert(&strm->frms, &frm->offset_node);
1820 quic_rx_packet_refinc(pkt);
1821
1822 out:
1823 return 1;
1824}
1825
1826static inline int qc_handle_strm_frm(struct quic_rx_packet *pkt,
1827 struct quic_stream *strm_frm,
1828 struct quic_conn *qc)
1829{
1830 if (strm_frm->id & QCS_ID_DIR_BIT)
1831 return qc_handle_uni_strm_frm(pkt, strm_frm, qc);
1832 else
1833 return qc_handle_bidi_strm_frm(pkt, strm_frm, qc);
1834}
1835
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001836/* Parse all the frames of <pkt> QUIC packet for QUIC connection with <ctx>
1837 * as I/O handler context and <qel> as encryption level.
1838 * Returns 1 if succeeded, 0 if failed.
1839 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02001840static 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 +01001841 struct quic_enc_level *qel)
1842{
1843 struct quic_frame frm;
1844 const unsigned char *pos, *end;
1845 struct quic_conn *conn = ctx->conn->qc;
1846
1847 TRACE_ENTER(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1848 /* Skip the AAD */
1849 pos = pkt->data + pkt->aad_len;
1850 end = pkt->data + pkt->len;
1851
1852 while (pos < end) {
1853 if (!qc_parse_frm(&frm, pkt, &pos, end, conn))
1854 goto err;
1855
1856 switch (frm.type) {
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001857 case QUIC_FT_PADDING:
1858 if (pos != end) {
1859 TRACE_DEVEL("wrong frame", QUIC_EV_CONN_PRSHPKT, ctx->conn, pkt);
1860 goto err;
1861 }
1862 break;
1863 case QUIC_FT_PING:
1864 break;
1865 case QUIC_FT_ACK:
1866 {
1867 unsigned int rtt_sample;
1868
1869 rtt_sample = 0;
1870 if (!qc_parse_ack_frm(&frm, ctx, qel, &rtt_sample, &pos, end))
1871 goto err;
1872
1873 if (rtt_sample) {
1874 unsigned int ack_delay;
1875
1876 ack_delay = !quic_application_pktns(qel->pktns, conn) ? 0 :
1877 MS_TO_TICKS(QUIC_MIN(quic_ack_delay_ms(&frm.ack, conn), conn->max_ack_delay));
1878 quic_loss_srtt_update(&conn->path->loss, rtt_sample, ack_delay, conn);
1879 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001880 break;
1881 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001882 case QUIC_FT_CRYPTO:
1883 if (frm.crypto.offset != qel->rx.crypto.offset) {
1884 struct quic_rx_crypto_frm *cf;
1885
1886 cf = pool_alloc(pool_head_quic_rx_crypto_frm);
1887 if (!cf) {
1888 TRACE_DEVEL("CRYPTO frame allocation failed",
1889 QUIC_EV_CONN_PRSHPKT, ctx->conn);
1890 goto err;
1891 }
1892
1893 cf->offset_node.key = frm.crypto.offset;
1894 cf->len = frm.crypto.len;
1895 cf->data = frm.crypto.data;
1896 cf->pkt = pkt;
1897 eb64_insert(&qel->rx.crypto.frms, &cf->offset_node);
1898 quic_rx_packet_refinc(pkt);
1899 }
1900 else {
1901 /* XXX TO DO: <cf> is used only for the traces. */
1902 struct quic_rx_crypto_frm cf = { };
1903
1904 cf.offset_node.key = frm.crypto.offset;
1905 cf.len = frm.crypto.len;
1906 if (!qc_provide_cdata(qel, ctx,
1907 frm.crypto.data, frm.crypto.len,
1908 pkt, &cf))
1909 goto err;
1910 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001911 break;
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001912 case QUIC_FT_STREAM_8:
1913 case QUIC_FT_STREAM_9:
1914 case QUIC_FT_STREAM_A:
1915 case QUIC_FT_STREAM_B:
1916 case QUIC_FT_STREAM_C:
1917 case QUIC_FT_STREAM_D:
1918 case QUIC_FT_STREAM_E:
1919 case QUIC_FT_STREAM_F:
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001920 {
1921 struct quic_stream *stream = &frm.stream;
1922
1923 TRACE_PROTO("STREAM frame", QUIC_EV_CONN_PSTRM, ctx->conn, &frm);
1924 if (objt_listener(ctx->conn->target)) {
1925 if (stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT)
1926 goto err;
1927 } else if (!(stream->id & QUIC_STREAM_FRAME_ID_INITIATOR_BIT))
1928 goto err;
Frédéric Lécailledfbae762021-02-18 09:59:01 +01001929
1930 if (!qc_handle_strm_frm(pkt, stream, ctx->conn->qc))
1931 goto err;
1932
Frédéric Lécaille242fb1b2020-12-31 12:45:38 +01001933 break;
1934 }
Frédéric Lécaille0c140202020-12-09 15:56:48 +01001935 case QUIC_FT_NEW_CONNECTION_ID:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001936 break;
1937 case QUIC_FT_CONNECTION_CLOSE:
1938 case QUIC_FT_CONNECTION_CLOSE_APP:
1939 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001940 case QUIC_FT_HANDSHAKE_DONE:
1941 if (objt_listener(ctx->conn->target))
1942 goto err;
1943
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001944 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_CONFIRMED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001945 break;
1946 default:
1947 goto err;
1948 }
1949 }
1950
1951 /* The server must switch from INITIAL to HANDSHAKE handshake state when it
1952 * has successfully parse a Handshake packet. The Initial encryption must also
1953 * be discarded.
1954 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001955 if (HA_ATOMIC_LOAD(&conn->state) == QUIC_HS_ST_SERVER_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001956 pkt->type == QUIC_PACKET_TYPE_HANDSHAKE) {
1957 quic_tls_discard_keys(&conn->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
1958 quic_pktns_discard(conn->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, conn);
1959 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02001960 HA_ATOMIC_STORE(&conn->state, QUIC_HS_ST_SERVER_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001961 }
1962
1963 TRACE_LEAVE(QUIC_EV_CONN_PRSHPKT, ctx->conn);
1964 return 1;
1965
1966 err:
1967 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PRSHPKT, ctx->conn);
1968 return 0;
1969}
1970
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001971/* Write <dglen> datagram length and <pkt> first packet address into <cbuf> ring
1972 * buffer. This is the responsability of the caller to check there is enough
1973 * room in <cbuf>. Also increase the <cbuf> write index consequently.
1974 * This function must be called only after having built a correct datagram.
1975 * Always succeeds.
1976 */
1977static inline void qc_set_dg(struct cbuf *cbuf,
1978 uint16_t dglen, struct quic_tx_packet *pkt)
1979{
1980 write_u16(cb_wr(cbuf), dglen);
1981 write_ptr(cb_wr(cbuf) + sizeof dglen, pkt);
1982 cb_add(cbuf, dglen + sizeof dglen + sizeof pkt);
1983}
1984
1985/* Prepare as much as possible handshake packets into <qr> ring buffer for
1986 * the QUIC connection with <ctx> as I/O handler context, possibly concatenating
1987 * several packets in the same datagram. A header made of two fields is added
1988 * to each datagram: the datagram length followed by the address of the first
1989 * packet in this datagram.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001990 * Returns 1 if succeeded, or 0 if something wrong happened.
1991 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001992static int qc_prep_hdshk_pkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01001993{
1994 struct quic_conn *qc;
1995 enum quic_tls_enc_level tel, next_tel;
1996 struct quic_enc_level *qel;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02001997 struct cbuf *cbuf;
1998 unsigned char *end_buf, *end, *pos, *spos;
1999 struct quic_tx_packet *first_pkt, *cur_pkt, *prv_pkt;
2000 /* length of datagrams */
2001 uint16_t dglen;
2002 size_t total;
2003 /* Each datagram is prepended with its length followed by the
2004 * address of the first packet in the datagram.
2005 */
2006 size_t dg_headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002007
2008 TRACE_ENTER(QUIC_EV_CONN_PHPKTS, ctx->conn);
2009 qc = ctx->conn->qc;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002010 if (!quic_get_tls_enc_levels(&tel, &next_tel, HA_ATOMIC_LOAD(&qc->state))) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002011 TRACE_DEVEL("unknown enc. levels", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002012 goto err;
2013 }
2014
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002015 start:
2016 total = 0;
2017 dglen = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002018 qel = &qc->els[tel];
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002019 cbuf = qr->cbuf;
2020 spos = pos = cb_wr(cbuf);
2021 /* Leave at least <dglen> bytes at the end of this buffer
2022 * to ensure there is enough room to mark the end of prepared
2023 * contiguous data with a zero length.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002024 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002025 end_buf = pos + cb_contig_space(cbuf) - sizeof dglen;
2026 first_pkt = prv_pkt = NULL;
2027 while (end_buf - pos >= (int)qc->path->mtu + dg_headlen || prv_pkt) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002028 int err, nb_ptos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002029 enum quic_pkt_type pkt_type;
2030
Frédéric Lécaillec5e72b92020-12-02 16:11:40 +01002031 TRACE_POINT(QUIC_EV_CONN_PHPKTS, ctx->conn, qel);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002032 if (!prv_pkt) {
2033 /* Consume a PTO dgram only if building a new dgrams (!prv_pkt) */
2034 do {
2035 nb_ptos = HA_ATOMIC_LOAD(&qc->tx.nb_pto_dgrams);
2036 } while (nb_ptos && !HA_ATOMIC_CAS(&qc->tx.nb_pto_dgrams, &nb_ptos, nb_ptos - 1));
2037 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002038 /* Do not build any more packet if the TX secrets are not available or
2039 * if there is nothing to send, i.e. if no ACK are required
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002040 * and if there is no more packets to send upon PTO expiration
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002041 * and if there is no more CRYPTO data available or in flight
2042 * congestion control limit is reached for prepared data
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002043 */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002044 if (!(qel->tls_ctx.tx.flags & QUIC_FL_TLS_SECRETS_SET) ||
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002045 (!(qel->pktns->flags & QUIC_FL_PKTNS_ACK_REQUIRED) && !nb_ptos &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002046 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) ||
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002047 qc->path->prep_in_flight >= qc->path->cwnd))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002048 TRACE_DEVEL("nothing more to do", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002049 /* Set the current datagram as prepared into <cbuf> if
2050 * the was already a correct packet which was previously written.
2051 */
2052 if (prv_pkt)
2053 qc_set_dg(cbuf, dglen, first_pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002054 break;
2055 }
2056
2057 pkt_type = quic_tls_level_pkt_type(tel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002058 if (!prv_pkt) {
2059 /* Leave room for the datagram header */
2060 pos += dg_headlen;
2061 end = pos + qc->path->mtu;
2062 }
2063
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002064 cur_pkt = qc_build_pkt(&pos, end, qel, qc, pkt_type, nb_ptos, &err);
2065 /* Restore the PTO dgrams counter if a packet could not be built */
2066 if (err < 0 && !prv_pkt && nb_ptos)
2067 HA_ATOMIC_ADD(&qc->tx.nb_pto_dgrams, 1);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002068 switch (err) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002069 case -2:
2070 goto err;
2071 case -1:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002072 /* If there was already a correct packet present, set the
2073 * current datagram as prepared into <cbuf>.
2074 */
2075 if (prv_pkt) {
2076 qc_set_dg(cbuf, dglen, first_pkt);
2077 goto stop_build;
2078 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002079 goto out;
2080 default:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002081 total += cur_pkt->len;
2082 /* keep trace of the first packet in the datagram */
2083 if (!first_pkt)
2084 first_pkt = cur_pkt;
2085 /* Attach the current one to the previous one */
2086 if (prv_pkt)
2087 prv_pkt->next = cur_pkt;
2088 /* Let's say we have to build a new dgram */
2089 prv_pkt = NULL;
2090 dglen += cur_pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002091 /* Discard the Initial encryption keys as soon as
2092 * a handshake packet could be built.
2093 */
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002094 if (HA_ATOMIC_LOAD(&qc->state) == QUIC_HS_ST_CLIENT_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002095 pkt_type == QUIC_PACKET_TYPE_HANDSHAKE) {
2096 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_INITIAL]);
2097 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_INITIAL].pktns, qc);
2098 qc_set_timer(ctx);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002099 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_HANDSHAKE);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002100 }
2101 /* Special case for Initial packets: when they have all
2102 * been sent, select the next level.
2103 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002104 if (tel == QUIC_TLS_ENC_LEVEL_INITIAL &&
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002105 (MT_LIST_ISEMPTY(&qel->pktns->tx.frms) || qc->els[next_tel].pktns->tx.in_flight)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002106 tel = next_tel;
2107 qel = &qc->els[tel];
Frédéric Lécaillec88df072021-07-27 11:43:11 +02002108 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002109 /* If there is data for the next level, do not
2110 * consume a datagram. This is the case for a client
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002111 * which sends only one Initial packet, then wait
2112 * for additional CRYPTO data from the server to enter the
2113 * next level.
2114 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002115 prv_pkt = cur_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002116 }
2117 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002118 }
2119
2120 /* If we have to build a new datagram, set the current datagram as
2121 * prepared into <cbuf>.
2122 */
2123 if (!prv_pkt) {
2124 qc_set_dg(cbuf, dglen, first_pkt);
2125 first_pkt = NULL;
2126 dglen = 0;
2127 }
2128 }
2129
2130 stop_build:
2131 /* Reset <wr> writer index if in front of <rd> index */
2132 if (end_buf - pos < (int)qc->path->mtu + dg_headlen) {
2133 int rd = HA_ATOMIC_LOAD(&cbuf->rd);
2134
2135 TRACE_DEVEL("buffer full", QUIC_EV_CONN_PHPKTS, ctx->conn);
2136 if (cb_contig_space(cbuf) >= sizeof(uint16_t)) {
2137 if ((pos != spos && cbuf->wr > rd) || (pos == spos && rd <= cbuf->wr)) {
2138 /* Mark the end of contiguous data for the reader */
2139 write_u16(cb_wr(cbuf), 0);
2140 cb_add(cbuf, sizeof(uint16_t));
2141 }
2142 }
2143
2144 if (rd && rd <= cbuf->wr) {
2145 cb_wr_reset(cbuf);
2146 if (pos == spos) {
2147 /* Reuse the same buffer if nothing was built. */
2148 goto start;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002149 }
2150 }
2151 }
2152
2153 out:
2154 TRACE_LEAVE(QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002155 return total;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002156
2157 err:
2158 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_PHPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002159 return -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002160}
2161
2162/* Send the QUIC packets which have been prepared for QUIC connections
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002163 * from <qr> ring buffer with <ctx> as I/O handler context.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002164 */
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002165int qc_send_ppkts(struct qring *qr, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002166{
2167 struct quic_conn *qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002168 struct cbuf *cbuf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002169
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002170 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002171 cbuf = qr->cbuf;
2172 while (cb_contig_data(cbuf)) {
2173 unsigned char *pos;
2174 struct buffer tmpbuf = { };
2175 struct quic_tx_packet *first_pkt, *pkt, *next_pkt;
2176 uint16_t dglen;
2177 size_t headlen = sizeof dglen + sizeof first_pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002178 unsigned int time_sent;
2179
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002180 pos = cb_rd(cbuf);
2181 dglen = read_u16(pos);
2182 /* End of prepared datagrams.
2183 * Reset the reader index only if in front of the writer index.
2184 */
2185 if (!dglen) {
2186 int wr = HA_ATOMIC_LOAD(&cbuf->wr);
2187
2188 if (wr && wr < cbuf->rd) {
2189 cb_rd_reset(cbuf);
2190 continue;
2191 }
2192 break;
2193 }
2194
2195 pos += sizeof dglen;
2196 first_pkt = read_ptr(pos);
2197 pos += sizeof first_pkt;
2198 tmpbuf.area = (char *)pos;
2199 tmpbuf.size = tmpbuf.data = dglen;
2200
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002201 TRACE_PROTO("to send", QUIC_EV_CONN_SPPKTS, ctx->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002202 for (pkt = first_pkt; pkt; pkt = pkt->next)
2203 quic_tx_packet_refinc(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002204 if (ctx->xprt->snd_buf(qc->conn, qc->conn->xprt_ctx,
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002205 &tmpbuf, tmpbuf.data, 0) <= 0) {
2206 for (pkt = first_pkt; pkt; pkt = pkt->next)
2207 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002208 break;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002209 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002210
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002211 cb_del(cbuf, dglen + headlen);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002212 qc->tx.bytes += tmpbuf.data;
2213 time_sent = now_ms;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002214
2215 for (pkt = first_pkt; pkt; pkt = next_pkt) {
2216 pkt->time_sent = time_sent;
2217 if (pkt->flags & QUIC_FL_TX_PACKET_ACK_ELICITING) {
2218 pkt->pktns->tx.time_of_last_eliciting = time_sent;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002219 qc->path->ifae_pkts++;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002220 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002221 qc->path->in_flight += pkt->in_flight_len;
2222 pkt->pktns->tx.in_flight += pkt->in_flight_len;
2223 if (pkt->in_flight_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002224 qc_set_timer(ctx);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002225 TRACE_PROTO("sent pkt", QUIC_EV_CONN_SPPKTS, ctx->conn, pkt);
2226 next_pkt = pkt->next;
Frédéric Lécaille0eb60c52021-07-19 14:48:36 +02002227 eb64_insert(&pkt->pktns->tx.pkts, &pkt->pn_node);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002228 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002229 }
2230 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002231
2232 return 1;
2233}
2234
2235/* Build all the frames which must be sent just after the handshake have succeeded.
2236 * This is essentially NEW_CONNECTION_ID frames. A QUIC server must also send
2237 * a HANDSHAKE_DONE frame.
2238 * Return 1 if succeeded, 0 if not.
2239 */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002240static int quic_build_post_handshake_frames(struct quic_conn *qc)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002241{
2242 int i;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002243 struct quic_enc_level *qel;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002244 struct quic_frame *frm;
2245
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002246 qel = &qc->els[QUIC_TLS_ENC_LEVEL_APP];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002247 /* Only servers must send a HANDSHAKE_DONE frame. */
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002248 if (!objt_server(qc->conn->target)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002249 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille153d4a82021-01-06 12:12:39 +01002250 if (!frm)
2251 return 0;
2252
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002253 frm->type = QUIC_FT_HANDSHAKE_DONE;
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002254 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002255 }
2256
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002257 for (i = 1; i < qc->tx.params.active_connection_id_limit; i++) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002258 struct quic_connection_id *cid;
2259
2260 frm = pool_alloc(pool_head_quic_frame);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002261 cid = new_quic_cid(&qc->cids, i);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002262 if (!frm || !cid)
2263 goto err;
2264
2265 quic_connection_id_to_frm_cpy(frm, cid);
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002266 MT_LIST_APPEND(&qel->pktns->tx.frms, &frm->mt_list);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002267 }
2268
2269 return 1;
2270
2271 err:
Frédéric Lécaille522c65c2021-08-03 14:29:03 +02002272 free_quic_conn_cids(qc);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002273 return 0;
2274}
2275
2276/* Deallocate <l> list of ACK ranges. */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002277void free_quic_arngs(struct quic_arngs *arngs)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002278{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002279 struct eb64_node *n;
2280 struct quic_arng_node *ar;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002281
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002282 n = eb64_first(&arngs->root);
2283 while (n) {
2284 struct eb64_node *next;
2285
2286 ar = eb64_entry(&n->node, struct quic_arng_node, first);
2287 next = eb64_next(n);
2288 eb64_delete(n);
2289 free(ar);
2290 n = next;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002291 }
2292}
2293
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002294/* Return the gap value between <p> and <q> ACK ranges where <q> follows <p> in
2295 * descending order.
2296 */
2297static inline size_t sack_gap(struct quic_arng_node *p,
2298 struct quic_arng_node *q)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002299{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002300 return p->first.key - q->last - 2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002301}
2302
2303
2304/* Remove the last elements of <ack_ranges> list of ack range updating its
2305 * encoded size until it goes below <limit>.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002306 * Returns 1 if succeeded, 0 if not (no more element to remove).
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002307 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002308static int quic_rm_last_ack_ranges(struct quic_arngs *arngs, size_t limit)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002309{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002310 struct eb64_node *last, *prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002311
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002312 last = eb64_last(&arngs->root);
2313 while (last && arngs->enc_sz > limit) {
2314 struct quic_arng_node *last_node, *prev_node;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002315
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002316 prev = eb64_prev(last);
2317 if (!prev)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002318 return 0;
2319
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002320 last_node = eb64_entry(&last->node, struct quic_arng_node, first);
2321 prev_node = eb64_entry(&prev->node, struct quic_arng_node, first);
2322 arngs->enc_sz -= quic_int_getsize(last_node->last - last_node->first.key);
2323 arngs->enc_sz -= quic_int_getsize(sack_gap(prev_node, last_node));
2324 arngs->enc_sz -= quic_decint_size_diff(arngs->sz);
2325 --arngs->sz;
2326 eb64_delete(last);
2327 pool_free(pool_head_quic_arng, last);
2328 last = prev;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002329 }
2330
2331 return 1;
2332}
2333
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002334/* Set the encoded size of <arngs> QUIC ack ranges. */
2335static void quic_arngs_set_enc_sz(struct quic_arngs *arngs)
2336{
2337 struct eb64_node *node, *next;
2338 struct quic_arng_node *ar, *ar_next;
2339
2340 node = eb64_last(&arngs->root);
2341 if (!node)
2342 return;
2343
2344 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2345 arngs->enc_sz = quic_int_getsize(ar->last) +
2346 quic_int_getsize(ar->last - ar->first.key) + quic_int_getsize(arngs->sz - 1);
2347
2348 while ((next = eb64_prev(node))) {
2349 ar_next = eb64_entry(&next->node, struct quic_arng_node, first);
2350 arngs->enc_sz += quic_int_getsize(sack_gap(ar, ar_next)) +
2351 quic_int_getsize(ar_next->last - ar_next->first.key);
2352 node = next;
2353 ar = eb64_entry(&node->node, struct quic_arng_node, first);
2354 }
2355}
2356
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002357/* Insert <ar> ack range into <argns> tree of ack ranges.
2358 * Returns the ack range node which has been inserted if succeeded, NULL if not.
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002359 */
2360static inline
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002361struct quic_arng_node *quic_insert_new_range(struct quic_arngs *arngs,
2362 struct quic_arng *ar)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002363{
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002364 struct quic_arng_node *new_ar;
2365
2366 new_ar = pool_alloc(pool_head_quic_arng);
2367 if (new_ar) {
2368 new_ar->first.key = ar->first;
2369 new_ar->last = ar->last;
2370 eb64_insert(&arngs->root, &new_ar->first);
2371 arngs->sz++;
2372 }
2373
2374 return new_ar;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002375}
2376
2377/* Update <arngs> tree of ACK ranges with <ar> as new ACK range value.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002378 * Note that this function computes the number of bytes required to encode
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002379 * this tree of ACK ranges in descending order.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002380 *
2381 * Descending order
2382 * ------------->
2383 * range1 range2
2384 * ..........|--------|..............|--------|
2385 * ^ ^ ^ ^
2386 * | | | |
2387 * last1 first1 last2 first2
2388 * ..........+--------+--------------+--------+......
2389 * diff1 gap12 diff2
2390 *
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002391 * To encode the previous list of ranges we must encode integers as follows in
2392 * descending order:
2393 * enc(last2),enc(diff2),enc(gap12),enc(diff1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002394 * with diff1 = last1 - first1
2395 * diff2 = last2 - first2
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002396 * gap12 = first1 - last2 - 2 (>= 0)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002397 *
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002398 */
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002399int quic_update_ack_ranges_list(struct quic_arngs *arngs,
2400 struct quic_arng *ar)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002401{
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002402 struct eb64_node *le;
2403 struct quic_arng_node *new_node;
2404 struct eb64_node *new;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002405
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002406 new = NULL;
2407 if (eb_is_empty(&arngs->root)) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002408 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002409 if (!new_node)
2410 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002411
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002412 goto out;
2413 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002414
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002415 le = eb64_lookup_le(&arngs->root, ar->first);
2416 if (!le) {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002417 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002418 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002419 return 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002420 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002421 else {
2422 struct quic_arng_node *le_ar =
2423 eb64_entry(&le->node, struct quic_arng_node, first);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002424
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002425 /* Already existing range */
Frédéric Lécailled3f4dd82021-06-02 15:36:12 +02002426 if (le_ar->last >= ar->last)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002427 return 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002428
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002429 if (le_ar->last + 1 >= ar->first) {
2430 le_ar->last = ar->last;
2431 new = le;
2432 new_node = le_ar;
2433 }
2434 else {
Frédéric Lécaille9ef64cd2021-06-02 15:27:34 +02002435 new_node = quic_insert_new_range(arngs, ar);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002436 if (!new_node)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002437 return 0;
Frédéric Lécaille8ba42762021-06-02 17:40:09 +02002438
2439 new = &new_node->first;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002440 }
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002441 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002442
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002443 /* Verify that the new inserted node does not overlap the nodes
2444 * which follow it.
2445 */
2446 if (new) {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002447 struct eb64_node *next;
2448 struct quic_arng_node *next_node;
2449
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002450 while ((next = eb64_next(new))) {
2451 next_node =
2452 eb64_entry(&next->node, struct quic_arng_node, first);
Frédéric Lécaillec825eba2021-06-02 17:38:13 +02002453 if (new_node->last + 1 < next_node->first.key)
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002454 break;
2455
2456 if (next_node->last > new_node->last)
2457 new_node->last = next_node->last;
2458 eb64_delete(next);
Frédéric Lécaillebaea2842021-06-02 15:04:03 +02002459 pool_free(pool_head_quic_arng, next_node);
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002460 /* Decrement the size of these ranges. */
2461 arngs->sz--;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002462 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002463 }
2464
Frédéric Lécaille82b86522021-08-10 09:54:03 +02002465 out:
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002466 quic_arngs_set_enc_sz(arngs);
2467
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002468 return 1;
2469}
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002470/* Remove the header protection of packets at <el> encryption level.
2471 * Always succeeds.
2472 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002473static 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 +01002474{
2475 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002476 struct quic_rx_packet *pqpkt;
2477 struct mt_list *pkttmp1, pkttmp2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002478 struct quic_enc_level *app_qel;
2479
2480 TRACE_ENTER(QUIC_EV_CONN_ELRMHP, ctx->conn);
2481 app_qel = &ctx->conn->qc->els[QUIC_TLS_ENC_LEVEL_APP];
2482 /* A server must not process incoming 1-RTT packets before the handshake is complete. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002483 if (el == app_qel && objt_listener(ctx->conn->target) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002484 HA_ATOMIC_LOAD(&ctx->conn->qc->state) < QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002485 TRACE_PROTO("hp not removed (handshake not completed)",
2486 QUIC_EV_CONN_ELRMHP, ctx->conn);
2487 goto out;
2488 }
2489 tls_ctx = &el->tls_ctx;
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002490 mt_list_for_each_entry_safe(pqpkt, &el->rx.pqpkts, list, pkttmp1, pkttmp2) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002491 if (!qc_do_rm_hp(pqpkt, tls_ctx, el->pktns->rx.largest_pn,
2492 pqpkt->data + pqpkt->pn_offset,
2493 pqpkt->data, pqpkt->data + pqpkt->len, ctx)) {
2494 TRACE_PROTO("hp removing error", QUIC_EV_CONN_ELRMHP, ctx->conn);
2495 /* XXX TO DO XXX */
2496 }
2497 else {
2498 /* The AAD includes the packet number field */
2499 pqpkt->aad_len = pqpkt->pn_offset + pqpkt->pnl;
2500 /* Store the packet into the tree of packets to decrypt. */
2501 pqpkt->pn_node.key = pqpkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002502 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002503 quic_rx_packet_eb64_insert(&el->rx.pkts, &pqpkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002504 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002505 TRACE_PROTO("hp removed", QUIC_EV_CONN_ELRMHP, ctx->conn, pqpkt);
2506 }
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002507 MT_LIST_DELETE_SAFE(pkttmp1);
2508 quic_rx_packet_refdec(pqpkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002509 }
2510
2511 out:
2512 TRACE_LEAVE(QUIC_EV_CONN_ELRMHP, ctx->conn);
2513}
2514
2515/* Process all the CRYPTO frame at <el> encryption level.
2516 * Return 1 if succeeded, 0 if not.
2517 */
2518static inline int qc_treat_rx_crypto_frms(struct quic_enc_level *el,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002519 struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002520{
2521 struct eb64_node *node;
2522
2523 TRACE_ENTER(QUIC_EV_CONN_RXCDATA, ctx->conn);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002524 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002525 node = eb64_first(&el->rx.crypto.frms);
2526 while (node) {
2527 struct quic_rx_crypto_frm *cf;
2528
2529 cf = eb64_entry(&node->node, struct quic_rx_crypto_frm, offset_node);
2530 if (cf->offset_node.key != el->rx.crypto.offset)
2531 break;
2532
2533 if (!qc_provide_cdata(el, ctx, cf->data, cf->len, cf->pkt, cf))
2534 goto err;
2535
2536 node = eb64_next(node);
2537 quic_rx_packet_refdec(cf->pkt);
2538 eb64_delete(&cf->offset_node);
2539 pool_free(pool_head_quic_rx_crypto_frm, cf);
2540 }
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002541 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002542 TRACE_LEAVE(QUIC_EV_CONN_RXCDATA, ctx->conn);
2543 return 1;
2544
2545 err:
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002546 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002547 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_RXCDATA, ctx->conn);
2548 return 0;
2549}
2550
2551/* Process all the packets at <el> encryption level.
2552 * Return 1 if succeeded, 0 if not.
2553 */
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002554int qc_treat_rx_pkts(struct quic_enc_level *el, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002555{
2556 struct quic_tls_ctx *tls_ctx;
2557 struct eb64_node *node;
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002558 int64_t largest_pn = -1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002559
2560 TRACE_ENTER(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2561 tls_ctx = &el->tls_ctx;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002562 HA_RWLOCK_WRLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002563 node = eb64_first(&el->rx.pkts);
2564 while (node) {
2565 struct quic_rx_packet *pkt;
2566
2567 pkt = eb64_entry(&node->node, struct quic_rx_packet, pn_node);
2568 if (!qc_pkt_decrypt(pkt, tls_ctx)) {
2569 /* Drop the packet */
2570 TRACE_PROTO("packet decryption failed -> dropped",
2571 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2572 }
2573 else {
Frédéric Lécaillec4b93ea2021-06-04 10:12:43 +02002574 if (!qc_parse_pkt_frms(pkt, ctx, el)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002575 /* Drop the packet */
2576 TRACE_PROTO("packet parsing failed -> dropped",
2577 QUIC_EV_CONN_ELRXPKTS, ctx->conn, pkt);
2578 }
2579 else {
Frédéric Lécaille8090b512020-11-30 16:19:22 +01002580 struct quic_arng ar = { .first = pkt->pn, .last = pkt->pn };
2581
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002582 if (pkt->flags & QUIC_FL_RX_PACKET_ACK_ELICITING &&
2583 !(HA_ATOMIC_ADD_FETCH(&el->pktns->rx.nb_ack_eliciting, 1) & 1))
2584 HA_ATOMIC_OR(&el->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002585
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002586 if (pkt->pn > largest_pn)
2587 largest_pn = pkt->pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002588 /* Update the list of ranges to acknowledge. */
Frédéric Lécaille654c6912021-06-04 10:27:23 +02002589 if (!quic_update_ack_ranges_list(&el->pktns->rx.arngs, &ar))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002590 TRACE_DEVEL("Could not update ack range list",
2591 QUIC_EV_CONN_ELRXPKTS, ctx->conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002592 }
2593 }
2594 node = eb64_next(node);
2595 quic_rx_packet_eb64_delete(&pkt->pn_node);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002596 }
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002597 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &el->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002598
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002599 /* Update the largest packet number. */
2600 if (largest_pn != -1)
2601 HA_ATOMIC_UPDATE_MAX(&el->pktns->rx.largest_pn, largest_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002602 if (!qc_treat_rx_crypto_frms(el, ctx))
2603 goto err;
2604
2605 TRACE_LEAVE(QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2606 return 1;
2607
2608 err:
2609 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_ELRXPKTS, ctx->conn);
2610 return 0;
2611}
2612
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002613/* QUIC connection packet handler task. */
2614struct task *quic_conn_io_cb(struct task *t, void *context, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002615{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002616 int ret, ssl_err;
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002617 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002618 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002619 enum quic_tls_enc_level tel, next_tel;
2620 struct quic_enc_level *qel, *next_qel;
2621 struct quic_tls_ctx *tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002622 struct qring *qr; // Tx ring
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002623 int prev_st, st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002624
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002625 ctx = context;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002626 qc = ctx->conn->qc;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002627 qr = NULL;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002628 st = HA_ATOMIC_LOAD(&qc->state);
2629 TRACE_ENTER(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002630 ssl_err = SSL_ERROR_NONE;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002631 if (!quic_get_tls_enc_levels(&tel, &next_tel, st))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002632 goto err;
2633
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02002634 qel = &qc->els[tel];
2635 next_qel = &qc->els[next_tel];
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002636
2637 next_level:
2638 tls_ctx = &qel->tls_ctx;
2639
2640 /* If the header protection key for this level has been derived,
2641 * remove the packet header protections.
2642 */
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002643 if (!MT_LIST_ISEMPTY(&qel->rx.pqpkts) &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002644 (tls_ctx->rx.flags & QUIC_FL_TLS_SECRETS_SET))
2645 qc_rm_hp_pkts(qel, ctx);
2646
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002647 prev_st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaille120ea6f2021-07-26 16:42:56 +02002648 if (!qc_treat_rx_pkts(qel, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002649 goto err;
2650
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002651 st = HA_ATOMIC_LOAD(&qc->state);
2652 if (prev_st == QUIC_HS_ST_SERVER_HANDSHAKE && st >= QUIC_HS_ST_COMPLETE) {
2653 /* Discard the Handshake keys. */
2654 quic_tls_discard_keys(&qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE]);
2655 quic_pktns_discard(qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE].pktns, qc);
2656 qc_set_timer(ctx);
2657 if (!quic_build_post_handshake_frames(qc))
2658 goto err;
2659 }
2660
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002661 if (!qr)
2662 qr = MT_LIST_POP(qc->tx.qring_list, typeof(qr), mt_list);
2663 ret = qc_prep_hdshk_pkts(qr, ctx);
2664 if (ret == -1)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002665 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002666 else if (ret == 0)
2667 goto skip_send;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002668
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002669 if (!qc_send_ppkts(qr, ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002670 goto err;
2671
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002672 skip_send:
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002673 /* Check if there is something to do for the next level.
2674 */
2675 if ((next_qel->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002676 (!MT_LIST_ISEMPTY(&next_qel->rx.pqpkts) || !eb_is_empty(&next_qel->rx.pkts))) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002677 qel = next_qel;
2678 goto next_level;
2679 }
2680
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002681 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002682 TRACE_LEAVE(QUIC_EV_CONN_HDSHK, ctx->conn, &st);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002683 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002684
2685 err:
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02002686 if (qr)
2687 MT_LIST_APPEND(qc->tx.qring_list, &qr->mt_list);
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002688 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaille91ae7aa2021-08-03 16:45:39 +02002689 return t;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002690}
2691
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002692/* Uninitialize <qel> QUIC encryption level. Never fails. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002693static void quic_conn_enc_level_uninit(struct quic_enc_level *qel)
2694{
2695 int i;
2696
2697 for (i = 0; i < qel->tx.crypto.nb_buf; i++) {
2698 if (qel->tx.crypto.bufs[i]) {
2699 pool_free(pool_head_quic_crypto_buf, qel->tx.crypto.bufs[i]);
2700 qel->tx.crypto.bufs[i] = NULL;
2701 }
2702 }
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002703 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002704}
2705
2706/* Initialize QUIC TLS encryption level with <level<> as level for <qc> QUIC
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05002707 * connection allocating everything needed.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002708 * Returns 1 if succeeded, 0 if not.
2709 */
2710static int quic_conn_enc_level_init(struct quic_conn *qc,
2711 enum quic_tls_enc_level level)
2712{
2713 struct quic_enc_level *qel;
2714
2715 qel = &qc->els[level];
2716 qel->level = quic_to_ssl_enc_level(level);
2717 qel->tls_ctx.rx.aead = qel->tls_ctx.tx.aead = NULL;
2718 qel->tls_ctx.rx.md = qel->tls_ctx.tx.md = NULL;
2719 qel->tls_ctx.rx.hp = qel->tls_ctx.tx.hp = NULL;
2720 qel->tls_ctx.rx.flags = 0;
2721 qel->tls_ctx.tx.flags = 0;
2722
2723 qel->rx.pkts = EB_ROOT;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02002724 HA_RWLOCK_INIT(&qel->rx.pkts_rwlock);
Frédéric Lécaillea11d0e22021-06-07 14:38:18 +02002725 MT_LIST_INIT(&qel->rx.pqpkts);
Frédéric Lécaille9054d1b2021-07-26 16:23:53 +02002726 qel->rx.crypto.offset = 0;
2727 qel->rx.crypto.frms = EB_ROOT_UNIQUE;
2728 HA_RWLOCK_INIT(&qel->rx.crypto.frms_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002729
2730 /* Allocate only one buffer. */
2731 qel->tx.crypto.bufs = malloc(sizeof *qel->tx.crypto.bufs);
2732 if (!qel->tx.crypto.bufs)
2733 goto err;
2734
2735 qel->tx.crypto.bufs[0] = pool_alloc(pool_head_quic_crypto_buf);
2736 if (!qel->tx.crypto.bufs[0])
2737 goto err;
2738
2739 qel->tx.crypto.bufs[0]->sz = 0;
2740 qel->tx.crypto.nb_buf = 1;
2741
2742 qel->tx.crypto.sz = 0;
2743 qel->tx.crypto.offset = 0;
2744
2745 return 1;
2746
2747 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002748 ha_free(&qel->tx.crypto.bufs);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002749 return 0;
2750}
2751
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002752/* Release all the memory allocated for <conn> QUIC connection. */
2753static void quic_conn_free(struct quic_conn *conn)
2754{
2755 int i;
2756
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002757 if (!conn)
2758 return;
2759
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002760 free_quic_conn_cids(conn);
2761 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++)
2762 quic_conn_enc_level_uninit(&conn->els[i]);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002763 if (conn->timer_task)
2764 task_destroy(conn->timer_task);
2765 pool_free(pool_head_quic_conn, conn);
2766}
2767
2768/* Callback called upon loss detection and PTO timer expirations. */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002769static struct task *process_timer(struct task *task, void *ctx, unsigned int state)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002770{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002771 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002772 struct quic_conn *qc;
2773 struct quic_pktns *pktns;
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002774 int st;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002775
2776 conn_ctx = task->context;
2777 qc = conn_ctx->conn->qc;
Frédéric Lécaillef7e0b8d2020-12-16 17:33:11 +01002778 TRACE_ENTER(QUIC_EV_CONN_PTIMER, conn_ctx->conn,
2779 NULL, NULL, &qc->path->ifae_pkts);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002780 task->expire = TICK_ETERNITY;
2781 pktns = quic_loss_pktns(qc);
2782 if (tick_isset(pktns->tx.loss_time)) {
2783 struct list lost_pkts = LIST_HEAD_INIT(lost_pkts);
2784
2785 qc_packet_loss_lookup(pktns, qc, &lost_pkts);
2786 if (!LIST_ISEMPTY(&lost_pkts))
2787 qc_release_lost_pkts(pktns, ctx, &lost_pkts, now_ms);
2788 qc_set_timer(conn_ctx);
2789 goto out;
2790 }
2791
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002792 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002793 if (qc->path->in_flight) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002794 pktns = quic_pto_pktns(qc, st >= QUIC_HS_ST_COMPLETE, NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002795 pktns->tx.pto_probe = 1;
2796 }
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002797 else if (objt_server(qc->conn->target) && st <= QUIC_HS_ST_COMPLETE) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002798 struct quic_enc_level *iel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2799 struct quic_enc_level *hel = &qc->els[QUIC_TLS_ENC_LEVEL_HANDSHAKE];
2800
2801 if (hel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2802 hel->pktns->tx.pto_probe = 1;
2803 if (iel->tls_ctx.rx.flags == QUIC_FL_TLS_SECRETS_SET)
2804 iel->pktns->tx.pto_probe = 1;
2805 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02002806 HA_ATOMIC_STORE(&qc->tx.nb_pto_dgrams, QUIC_MAX_NB_PTO_DGRAMS);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002807 tasklet_wakeup(conn_ctx->wait_event.tasklet);
2808 qc->path->loss.pto_count++;
2809
2810 out:
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01002811 TRACE_LEAVE(QUIC_EV_CONN_PTIMER, conn_ctx->conn, pktns);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002812
2813 return task;
2814}
2815
2816/* Initialize <conn> QUIC connection with <quic_initial_clients> as root of QUIC
2817 * connections used to identify the first Initial packets of client connecting
2818 * to listeners. This parameter must be NULL for QUIC connections attached
2819 * to listeners. <dcid> is the destination connection ID with <dcid_len> as length.
2820 * <scid> is the source connection ID with <scid_len> as length.
2821 * Returns 1 if succeeded, 0 if not.
2822 */
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002823static struct quic_conn *qc_new_conn(unsigned int version, int ipv4,
2824 unsigned char *dcid, size_t dcid_len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002825 unsigned char *scid, size_t scid_len, int server, void *owner)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002826{
2827 int i;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002828 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002829 /* Initial CID. */
2830 struct quic_connection_id *icid;
2831
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002832 TRACE_ENTER(QUIC_EV_CONN_INIT);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002833 qc = pool_zalloc(pool_head_quic_conn);
2834 if (!qc) {
2835 TRACE_PROTO("Could not allocate a new connection", QUIC_EV_CONN_INIT);
2836 goto err;
2837 }
2838
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002839 qc->cids = EB_ROOT;
2840 /* QUIC Server (or listener). */
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002841 if (server) {
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002842 struct listener *l = owner;
2843
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002844 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_SERVER_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002845 /* Copy the initial DCID. */
2846 qc->odcid.len = dcid_len;
2847 if (qc->odcid.len)
2848 memcpy(qc->odcid.data, dcid, dcid_len);
2849
2850 /* Copy the SCID as our DCID for this connection. */
2851 if (scid_len)
2852 memcpy(qc->dcid.data, scid, scid_len);
2853 qc->dcid.len = scid_len;
Frédéric Lécaille6b197642021-07-06 16:25:08 +02002854 qc->tx.qring_list = &l->rx.tx_qrings;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002855 }
2856 /* QUIC Client (outgoing connection to servers) */
2857 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02002858 HA_ATOMIC_STORE(&qc->state, QUIC_HS_ST_CLIENT_INITIAL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002859 if (dcid_len)
2860 memcpy(qc->dcid.data, dcid, dcid_len);
2861 qc->dcid.len = dcid_len;
2862 }
2863
2864 /* Initialize the output buffer */
2865 qc->obuf.pos = qc->obuf.data;
2866
2867 icid = new_quic_cid(&qc->cids, 0);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002868 if (!icid) {
2869 TRACE_PROTO("Could not allocate a new connection ID", QUIC_EV_CONN_INIT);
2870 goto err;
2871 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002872
2873 /* Select our SCID which is the first CID with 0 as sequence number. */
2874 qc->scid = icid->cid;
2875
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002876 /* Packet number spaces initialization. */
2877 for (i = 0; i < QUIC_TLS_PKTNS_MAX; i++)
2878 quic_pktns_init(&qc->pktns[i]);
2879 /* QUIC encryption level context initialization. */
2880 for (i = 0; i < QUIC_TLS_ENC_LEVEL_MAX; i++) {
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002881 if (!quic_conn_enc_level_init(qc, i)) {
2882 TRACE_PROTO("Could not initialize an encryption level", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002883 goto err;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002884 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002885 /* Initialize the packet number space. */
2886 qc->els[i].pktns = &qc->pktns[quic_tls_pktns(i)];
2887 }
2888
Frédéric Lécaillec8d3f872021-07-06 17:19:44 +02002889 qc->version = version;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002890 /* TX part. */
2891 LIST_INIT(&qc->tx.frms_to_send);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002892 qc->tx.nb_buf = QUIC_CONN_TX_BUFS_NB;
2893 qc->tx.wbuf = qc->tx.rbuf = 0;
2894 qc->tx.bytes = 0;
2895 qc->tx.nb_pto_dgrams = 0;
2896 /* RX part. */
2897 qc->rx.bytes = 0;
2898
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002899 /* XXX TO DO: Only one path at this time. */
2900 qc->path = &qc->paths[0];
2901 quic_path_init(qc->path, ipv4, default_quic_cc_algo, qc);
2902
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002903 TRACE_LEAVE(QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002904
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002905 return qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002906
2907 err:
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02002908 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_INIT);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002909 quic_conn_free(qc);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02002910 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01002911}
2912
2913/* Initialize the timer task of <qc> QUIC connection.
2914 * Returns 1 if succeeded, 0 if not.
2915 */
2916static int quic_conn_init_timer(struct quic_conn *qc)
2917{
2918 qc->timer_task = task_new(MAX_THREADS_MASK);
2919 if (!qc->timer_task)
2920 return 0;
2921
2922 qc->timer = TICK_ETERNITY;
2923 qc->timer_task->process = process_timer;
2924 qc->timer_task->context = qc->conn->xprt_ctx;
2925
2926 return 1;
2927}
2928
2929/* Parse into <pkt> a long header located at <*buf> buffer, <end> begin a pointer to the end
2930 * past one byte of this buffer.
2931 */
2932static inline int quic_packet_read_long_header(unsigned char **buf, const unsigned char *end,
2933 struct quic_rx_packet *pkt)
2934{
2935 unsigned char dcid_len, scid_len;
2936
2937 /* Version */
2938 if (!quic_read_uint32(&pkt->version, (const unsigned char **)buf, end))
2939 return 0;
2940
2941 if (!pkt->version) { /* XXX TO DO XXX Version negotiation packet */ };
2942
2943 /* Destination Connection ID Length */
2944 dcid_len = *(*buf)++;
2945 /* We want to be sure we can read <dcid_len> bytes and one more for <scid_len> value */
2946 if (dcid_len > QUIC_CID_MAXLEN || end - *buf < dcid_len + 1)
2947 /* XXX MUST BE DROPPED */
2948 return 0;
2949
2950 if (dcid_len) {
2951 /* Check that the length of this received DCID matches the CID lengths
2952 * of our implementation for non Initials packets only.
2953 */
2954 if (pkt->type != QUIC_PACKET_TYPE_INITIAL && dcid_len != QUIC_CID_LEN)
2955 return 0;
2956
2957 memcpy(pkt->dcid.data, *buf, dcid_len);
2958 }
2959
2960 pkt->dcid.len = dcid_len;
2961 *buf += dcid_len;
2962
2963 /* Source Connection ID Length */
2964 scid_len = *(*buf)++;
2965 if (scid_len > QUIC_CID_MAXLEN || end - *buf < scid_len)
2966 /* XXX MUST BE DROPPED */
2967 return 0;
2968
2969 if (scid_len)
2970 memcpy(pkt->scid.data, *buf, scid_len);
2971 pkt->scid.len = scid_len;
2972 *buf += scid_len;
2973
2974 return 1;
2975}
2976
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002977/* If the header protection of <pkt> packet attached to <qc> connection with <ctx>
2978 * as context may be removed, return 1, 0 if not. Also set <*qel> to the associated
2979 * encryption level matching with the packet type. <*qel> may be null if not found.
2980 * Note that <ctx> may be null (for Initial packets).
2981 */
2982static int qc_pkt_may_rm_hp(struct quic_rx_packet *pkt,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02002983 struct quic_conn *qc, struct ssl_sock_ctx *ctx,
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02002984 struct quic_enc_level **qel)
2985{
2986 enum quic_tls_enc_level tel;
2987
2988 /* Special case without connection context (firt Initial packets) */
2989 if (!ctx) {
2990 *qel = &qc->els[QUIC_TLS_ENC_LEVEL_INITIAL];
2991 return 1;
2992 }
2993
2994 tel = quic_packet_type_enc_level(pkt->type);
2995 if (tel == QUIC_TLS_ENC_LEVEL_NONE) {
2996 *qel = NULL;
2997 return 0;
2998 }
2999
3000 *qel = &qc->els[tel];
3001 if ((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_DCD) {
3002 TRACE_DEVEL("Discarded keys", QUIC_EV_CONN_TRMHP, ctx->conn);
3003 return 0;
3004 }
3005
3006 if (((*qel)->tls_ctx.rx.flags & QUIC_FL_TLS_SECRETS_SET) &&
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02003007 (tel != QUIC_TLS_ENC_LEVEL_APP ||
3008 HA_ATOMIC_LOAD(&ctx->conn->qc->state) >= QUIC_HS_ST_COMPLETE))
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003009 return 1;
3010
3011 return 0;
3012}
3013
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003014/* Try to remove the header protecttion of <pkt> QUIC packet attached to <conn>
3015 * QUIC connection with <buf> as packet number field address, <end> a pointer to one
3016 * byte past the end of the buffer containing this packet and <beg> the address of
3017 * the packet first byte.
3018 * If succeeded, this function updates <*buf> to point to the next packet in the buffer.
3019 * Returns 1 if succeeded, 0 if not.
3020 */
3021static inline int qc_try_rm_hp(struct quic_rx_packet *pkt,
3022 unsigned char **buf, unsigned char *beg,
3023 const unsigned char *end,
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003024 struct quic_conn *qc, struct ssl_sock_ctx *ctx)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003025{
3026 unsigned char *pn = NULL; /* Packet number field */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003027 struct quic_enc_level *qel;
3028 /* Only for traces. */
3029 struct quic_rx_packet *qpkt_trace;
3030
3031 qpkt_trace = NULL;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003032 TRACE_ENTER(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003033 /* The packet number is here. This is also the start minus
3034 * QUIC_PACKET_PN_MAXLEN of the sample used to add/remove the header
3035 * protection.
3036 */
3037 pn = *buf;
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003038 if (qc_pkt_may_rm_hp(pkt, qc, ctx, &qel)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003039 /* Note that the following function enables us to unprotect the packet
3040 * number and its length subsequently used to decrypt the entire
3041 * packets.
3042 */
3043 if (!qc_do_rm_hp(pkt, &qel->tls_ctx,
3044 qel->pktns->rx.largest_pn, pn, beg, end, ctx)) {
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003045 TRACE_PROTO("hp error", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003046 goto err;
3047 }
3048
3049 /* The AAD includes the packet number field found at <pn>. */
3050 pkt->aad_len = pn - beg + pkt->pnl;
3051 qpkt_trace = pkt;
3052 /* Store the packet */
3053 pkt->pn_node.key = pkt->pn;
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003054 HA_RWLOCK_WRLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003055 quic_rx_packet_eb64_insert(&qel->rx.pkts, &pkt->pn_node);
Frédéric Lécaille98cdeb22021-07-26 16:38:14 +02003056 HA_RWLOCK_WRUNLOCK(QUIC_LOCK, &qel->rx.pkts_rwlock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003057 }
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003058 else if (qel) {
3059 TRACE_PROTO("hp not removed", QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003060 pkt->pn_offset = pn - beg;
3061 quic_rx_packet_list_addq(&qel->rx.pqpkts, pkt);
3062 }
3063
3064 memcpy(pkt->data, beg, pkt->len);
3065 /* Updtate the offset of <*buf> for the next QUIC packet. */
3066 *buf = beg + pkt->len;
3067
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003068 TRACE_LEAVE(QUIC_EV_CONN_TRMHP, ctx ? ctx->conn : NULL, qpkt_trace);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003069 return 1;
3070
3071 err:
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003072 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 +01003073 return 0;
3074}
3075
3076/* Parse the header form from <byte0> first byte of <pkt> pacekt to set type.
3077 * Also set <*long_header> to 1 if this form is long, 0 if not.
3078 */
3079static inline void qc_parse_hd_form(struct quic_rx_packet *pkt,
3080 unsigned char byte0, int *long_header)
3081{
3082 if (byte0 & QUIC_PACKET_LONG_HEADER_BIT) {
3083 pkt->type =
3084 (byte0 >> QUIC_PACKET_TYPE_SHIFT) & QUIC_PACKET_TYPE_BITMASK;
3085 *long_header = 1;
3086 }
3087 else {
3088 pkt->type = QUIC_PACKET_TYPE_SHORT;
3089 *long_header = 0;
3090 }
3091}
3092
3093static ssize_t qc_srv_pkt_rcv(unsigned char **buf, const unsigned char *end,
3094 struct quic_rx_packet *pkt,
3095 struct quic_dgram_ctx *dgram_ctx,
3096 struct sockaddr_storage *saddr)
3097{
3098 unsigned char *beg;
3099 uint64_t len;
3100 struct quic_conn *qc;
3101 struct eb_root *cids;
3102 struct ebmb_node *node;
3103 struct connection *srv_conn;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003104 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003105 int long_header;
3106
3107 qc = NULL;
3108 TRACE_ENTER(QUIC_EV_CONN_SPKT);
3109 if (end <= *buf)
3110 goto err;
3111
3112 /* Fixed bit */
3113 if (!(**buf & QUIC_PACKET_FIXED_BIT))
3114 /* XXX TO BE DISCARDED */
3115 goto err;
3116
3117 srv_conn = dgram_ctx->owner;
3118 beg = *buf;
3119 /* Header form */
3120 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3121 if (long_header) {
3122 size_t cid_lookup_len;
3123
3124 if (!quic_packet_read_long_header(buf, end, pkt))
3125 goto err;
3126
3127 /* For Initial packets, and for servers (QUIC clients connections),
3128 * there is no Initial connection IDs storage.
3129 */
3130 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3131 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3132 cid_lookup_len = pkt->dcid.len;
3133 }
3134 else {
3135 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3136 cid_lookup_len = QUIC_CID_LEN;
3137 }
3138
3139 node = ebmb_lookup(cids, pkt->dcid.data, cid_lookup_len);
3140 if (!node)
3141 goto err;
3142
3143 qc = ebmb_entry(node, struct quic_conn, scid_node);
3144
3145 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3146 qc->dcid.len = pkt->scid.len;
3147 if (pkt->scid.len)
3148 memcpy(qc->dcid.data, pkt->scid.data, pkt->scid.len);
3149 }
3150
3151 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
3152 uint64_t token_len;
3153
3154 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) || end - *buf < token_len)
3155 goto err;
3156
3157 /* XXX TO DO XXX 0 value means "the token is not present".
3158 * A server which sends an Initial packet must not set the token.
3159 * So, a client which receives an Initial packet with a token
3160 * MUST discard the packet or generate a connection error with
3161 * PROTOCOL_VIOLATION as type.
3162 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3163 */
3164 pkt->token_len = token_len;
3165 }
3166 }
3167 else {
3168 /* XXX TO DO: Short header XXX */
3169 if (end - *buf < QUIC_CID_LEN)
3170 goto err;
3171
3172 cids = &((struct server *)__objt_server(srv_conn->target))->cids;
3173 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3174 if (!node)
3175 goto err;
3176
3177 qc = ebmb_entry(node, struct quic_conn, scid_node);
3178 *buf += QUIC_CID_LEN;
3179 }
3180 /* Store the DCID used for this packet to check the packet which
3181 * come in this UDP datagram match with it.
3182 */
3183 if (!dgram_ctx->dcid_node)
3184 dgram_ctx->dcid_node = node;
3185 /* Only packets packets with long headers and not RETRY or VERSION as type
3186 * have a length field.
3187 */
3188 if (long_header && pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3189 if (!quic_dec_int(&len, (const unsigned char **)buf, end) || end - *buf < len)
3190 goto err;
3191
3192 pkt->len = len;
3193 }
3194 else if (!long_header) {
3195 /* A short packet is the last one of an UDP datagram. */
3196 pkt->len = end - *buf;
3197 }
3198
3199 conn_ctx = qc->conn->xprt_ctx;
3200
3201 /* Increase the total length of this packet by the header length. */
3202 pkt->len += *buf - beg;
3203 /* Do not check the DCID node before the length. */
3204 if (dgram_ctx->dcid_node != node) {
3205 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_SPKT, qc->conn);
3206 goto err;
3207 }
3208
3209 if (pkt->len > sizeof pkt->data) {
3210 TRACE_PROTO("Too big packet", QUIC_EV_CONN_SPKT, qc->conn, pkt, &pkt->len);
3211 goto err;
3212 }
3213
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003214 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003215 goto err;
3216
3217 /* Wake the tasklet of the QUIC connection packet handler. */
3218 if (conn_ctx)
3219 tasklet_wakeup(conn_ctx->wait_event.tasklet);
3220
3221 TRACE_LEAVE(QUIC_EV_CONN_SPKT, qc->conn);
3222
3223 return pkt->len;
3224
3225 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003226 TRACE_DEVEL("Leaing in error", QUIC_EV_CONN_SPKT, qc ? qc->conn : NULL);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003227 return -1;
3228}
3229
3230static ssize_t qc_lstnr_pkt_rcv(unsigned char **buf, const unsigned char *end,
3231 struct quic_rx_packet *pkt,
3232 struct quic_dgram_ctx *dgram_ctx,
3233 struct sockaddr_storage *saddr)
3234{
3235 unsigned char *beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003236 struct quic_conn *qc;
3237 struct eb_root *cids;
3238 struct ebmb_node *node;
3239 struct listener *l;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02003240 struct ssl_sock_ctx *conn_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003241 int long_header = 0;
3242
3243 qc = NULL;
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003244 conn_ctx = NULL;
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003245 TRACE_ENTER(QUIC_EV_CONN_LPKT, NULL, pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003246 if (end <= *buf)
3247 goto err;
3248
3249 /* Fixed bit */
3250 if (!(**buf & QUIC_PACKET_FIXED_BIT)) {
3251 /* XXX TO BE DISCARDED */
3252 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3253 goto err;
3254 }
3255
3256 l = dgram_ctx->owner;
3257 beg = *buf;
3258 /* Header form */
3259 qc_parse_hd_form(pkt, *(*buf)++, &long_header);
3260 if (long_header) {
3261 unsigned char dcid_len;
3262
3263 if (!quic_packet_read_long_header(buf, end, pkt)) {
3264 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3265 goto err;
3266 }
3267
3268 dcid_len = pkt->dcid.len;
3269 /* For Initial packets, and for servers (QUIC clients connections),
3270 * there is no Initial connection IDs storage.
3271 */
3272 if (pkt->type == QUIC_PACKET_TYPE_INITIAL) {
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003273 uint64_t token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003274 /* DCIDs of first packets coming from clients may have the same values.
3275 * Let's distinguish them concatenating the socket addresses to the DCIDs.
3276 */
3277 quic_cid_saddr_cat(&pkt->dcid, saddr);
3278 cids = &l->rx.odcids;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003279
3280 if (!quic_dec_int(&token_len, (const unsigned char **)buf, end) ||
3281 end - *buf < token_len) {
3282 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3283 goto err;
3284 }
3285
3286 /* XXX TO DO XXX 0 value means "the token is not present".
3287 * A server which sends an Initial packet must not set the token.
3288 * So, a client which receives an Initial packet with a token
3289 * MUST discard the packet or generate a connection error with
3290 * PROTOCOL_VIOLATION as type.
3291 * The token must be provided in a Retry packet or NEW_TOKEN frame.
3292 */
3293 pkt->token_len = token_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003294 }
3295 else {
3296 if (pkt->dcid.len != QUIC_CID_LEN) {
3297 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3298 goto err;
3299 }
3300
3301 cids = &l->rx.cids;
3302 }
3303
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003304 /* Only packets packets with long headers and not RETRY or VERSION as type
3305 * have a length field.
3306 */
3307 if (pkt->type != QUIC_PACKET_TYPE_RETRY && pkt->version) {
3308 uint64_t len;
3309
3310 if (!quic_dec_int(&len, (const unsigned char **)buf, end) ||
3311 end - *buf < len) {
3312 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3313 goto err;
3314 }
3315
3316 pkt->len = len;
3317 }
3318
3319
3320 HA_RWLOCK_RDLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003321 node = ebmb_lookup(cids, pkt->dcid.data, pkt->dcid.len);
3322 if (!node && pkt->type == QUIC_PACKET_TYPE_INITIAL && dcid_len == QUIC_CID_LEN &&
3323 cids == &l->rx.odcids) {
3324 /* Switch to the definitive tree ->cids containing the final CIDs. */
3325 node = ebmb_lookup(&l->rx.cids, pkt->dcid.data, dcid_len);
3326 if (node) {
3327 /* If found, signal this with NULL as special value for <cids>. */
3328 pkt->dcid.len = dcid_len;
3329 cids = NULL;
3330 }
3331 }
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003332 HA_RWLOCK_RDUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003333
3334 if (!node) {
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003335 int ipv4;
3336 struct quic_cid *odcid;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003337 struct ebmb_node *n = NULL;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003338
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003339 if (pkt->type != QUIC_PACKET_TYPE_INITIAL) {
3340 TRACE_PROTO("Non Initiial packet", QUIC_EV_CONN_LPKT);
3341 goto err;
3342 }
3343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003344 pkt->saddr = *saddr;
3345 /* Note that here, odcid_len equals to pkt->dcid.len minus the length
3346 * of <saddr>.
3347 */
3348 pkt->odcid_len = dcid_len;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003349 ipv4 = saddr->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003350 qc = qc_new_conn(pkt->version, ipv4, pkt->dcid.data, pkt->dcid.len,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02003351 pkt->scid.data, pkt->scid.len, 1, l);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02003352 if (qc == NULL)
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003353 goto err;
3354
3355 odcid = &qc->rx.params.original_destination_connection_id;
3356 /* Copy the transport parameters. */
3357 qc->rx.params = l->bind_conf->quic_params;
3358 /* Copy original_destination_connection_id transport parameter. */
3359 memcpy(odcid->data, &pkt->dcid, pkt->odcid_len);
3360 odcid->len = pkt->odcid_len;
3361 /* Copy the initial source connection ID. */
3362 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
3363 qc->enc_params_len =
3364 quic_transport_params_encode(qc->enc_params,
3365 qc->enc_params + sizeof qc->enc_params,
3366 &qc->rx.params, 1);
3367 if (!qc->enc_params_len)
3368 goto err;
3369
Frédéric Lécaille497fa782021-05-31 15:16:13 +02003370 /* NOTE: the socket address has been concatenated to the destination ID
3371 * chosen by the client for Initial packets.
3372 */
3373 if (!qc_new_isecs(qc, pkt->dcid.data, pkt->odcid_len, 1)) {
3374 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3375 goto err;
3376 }
3377
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02003378 pkt->qc = qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003379 /* This is the DCID node sent in this packet by the client. */
3380 node = &qc->odcid_node;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003381 /* Enqueue this packet. */
3382 MT_LIST_APPEND(&l->rx.pkts, &pkt->rx_list);
3383 /* Try to accept a new connection. */
3384 listener_accept(l);
3385
3386 HA_RWLOCK_WRLOCK(OTHER_LOCK, &l->rx.cids_lock);
3387 /* Insert the DCID the QUIC client has chosen (only for listeners) */
3388 ebmb_insert(&l->rx.odcids, &qc->odcid_node, qc->odcid.len);
3389 /* Insert our SCID, the connection ID for the QUIC client. */
3390 n = ebmb_insert(&l->rx.cids, &qc->scid_node, qc->scid.len);
3391 HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &l->rx.cids_lock);
3392 if (n != &qc->scid_node) {
3393 quic_conn_free(qc);
3394 qc = ebmb_entry(n, struct quic_conn, scid_node);
3395 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003396 }
3397 else {
3398 if (pkt->type == QUIC_PACKET_TYPE_INITIAL && cids == &l->rx.odcids)
3399 qc = ebmb_entry(node, struct quic_conn, odcid_node);
3400 else
3401 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003402 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003403 }
3404 }
3405 else {
3406 if (end - *buf < QUIC_CID_LEN) {
3407 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3408 goto err;
3409 }
3410
3411 cids = &l->rx.cids;
3412 node = ebmb_lookup(cids, *buf, QUIC_CID_LEN);
3413 if (!node) {
3414 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT);
3415 goto err;
3416 }
3417
3418 qc = ebmb_entry(node, struct quic_conn, scid_node);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003419 conn_ctx = qc->conn->xprt_ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003420 *buf += QUIC_CID_LEN;
Frédéric Lécaillef3d078d2021-06-14 14:18:10 +02003421 /* A short packet is the last one of an UDP datagram. */
3422 pkt->len = end - *buf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003423 }
3424
3425 /* Store the DCID used for this packet to check the packet which
3426 * come in this UDP datagram match with it.
3427 */
3428 if (!dgram_ctx->dcid_node) {
3429 dgram_ctx->dcid_node = node;
3430 dgram_ctx->qc = qc;
3431 }
3432
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003433 /* Increase the total length of this packet by the header length. */
3434 pkt->len += *buf - beg;
3435 /* Do not check the DCID node before the length. */
3436 if (dgram_ctx->dcid_node != node) {
3437 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3438 goto err;
3439 }
3440
3441 if (pkt->len > sizeof pkt->data) {
3442 TRACE_PROTO("Too big packet", QUIC_EV_CONN_LPKT, qc->conn, pkt, &pkt->len);
3443 goto err;
3444 }
3445
Frédéric Lécaille1a5e88c2021-05-31 18:04:07 +02003446 if (!qc_try_rm_hp(pkt, buf, beg, end, qc, conn_ctx)) {
3447 TRACE_PROTO("Packet dropped", QUIC_EV_CONN_LPKT, qc->conn);
3448 goto err;
3449 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003450
Frédéric Lécaille2e7ffc92021-06-10 08:18:45 +02003451
3452 TRACE_PROTO("New packet", QUIC_EV_CONN_LPKT, qc->conn, pkt);
Frédéric Lécaille01abc462021-07-21 09:34:27 +02003453 /* Wake up the connection packet handler task from here only if all
3454 * the contexts have been initialized, especially the mux context
3455 * conn_ctx->conn->ctx. Note that this is ->start xprt callback which
3456 * will start it if these contexts for the connection are not already
3457 * initialized.
3458 */
3459 if (conn_ctx && HA_ATOMIC_LOAD(&conn_ctx->conn->ctx))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003460 tasklet_wakeup(conn_ctx->wait_event.tasklet);
Frédéric Lécailled24c2ec2021-05-31 10:24:49 +02003461
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003462 TRACE_LEAVE(QUIC_EV_CONN_LPKT, qc->conn, pkt);
3463
3464 return pkt->len;
3465
3466 err:
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01003467 TRACE_DEVEL("Leaving in error", QUIC_EV_CONN_LPKT,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003468 qc ? qc->conn : NULL, pkt);
3469 return -1;
3470}
3471
3472/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3473 * in advance. This is the reponsability of the caller to check there is enough room in this
3474 * buffer to build a long header.
3475 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3476 */
3477static int quic_build_packet_long_header(unsigned char **buf, const unsigned char *end,
3478 int type, size_t pn_len, struct quic_conn *conn)
3479{
3480 if (type > QUIC_PACKET_TYPE_RETRY)
3481 return 0;
3482
3483 /* #0 byte flags */
3484 *(*buf)++ = QUIC_PACKET_FIXED_BIT | QUIC_PACKET_LONG_HEADER_BIT |
3485 (type << QUIC_PACKET_TYPE_SHIFT) | (pn_len - 1);
3486 /* Version */
3487 quic_write_uint32(buf, end, conn->version);
3488 *(*buf)++ = conn->dcid.len;
3489 /* Destination connection ID */
3490 if (conn->dcid.len) {
3491 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3492 *buf += conn->dcid.len;
3493 }
3494 /* Source connection ID */
3495 *(*buf)++ = conn->scid.len;
3496 if (conn->scid.len) {
3497 memcpy(*buf, conn->scid.data, conn->scid.len);
3498 *buf += conn->scid.len;
3499 }
3500
3501 return 1;
3502}
3503
3504/* This function builds into <buf> buffer a QUIC long packet header whose size may be computed
3505 * in advance. This is the reponsability of the caller to check there is enough room in this
3506 * buffer to build a long header.
3507 * Returns 0 if <type> QUIC packet type is not supported by long header, or 1 if succeeded.
3508 */
3509static int quic_build_packet_short_header(unsigned char **buf, const unsigned char *end,
3510 size_t pn_len, struct quic_conn *conn)
3511{
3512 /* #0 byte flags */
3513 *(*buf)++ = QUIC_PACKET_FIXED_BIT | (pn_len - 1);
3514 /* Destination connection ID */
3515 if (conn->dcid.len) {
3516 memcpy(*buf, conn->dcid.data, conn->dcid.len);
3517 *buf += conn->dcid.len;
3518 }
3519
3520 return 1;
3521}
3522
3523/* Apply QUIC header protection to the packet with <buf> as first byte address,
3524 * <pn> as address of the Packet number field, <pnlen> being this field length
3525 * with <aead> as AEAD cipher and <key> as secret key.
3526 * Returns 1 if succeeded or 0 if failed.
3527 */
3528static int quic_apply_header_protection(unsigned char *buf, unsigned char *pn, size_t pnlen,
3529 const EVP_CIPHER *aead, const unsigned char *key)
3530{
3531 int i, ret, outlen;
3532 EVP_CIPHER_CTX *ctx;
3533 /* We need an IV of at least 5 bytes: one byte for bytes #0
3534 * and at most 4 bytes for the packet number
3535 */
3536 unsigned char mask[5] = {0};
3537
3538 ret = 0;
3539 ctx = EVP_CIPHER_CTX_new();
3540 if (!ctx)
3541 return 0;
3542
3543 if (!EVP_EncryptInit_ex(ctx, aead, NULL, key, pn + QUIC_PACKET_PN_MAXLEN) ||
3544 !EVP_EncryptUpdate(ctx, mask, &outlen, mask, sizeof mask) ||
3545 !EVP_EncryptFinal_ex(ctx, mask, &outlen))
3546 goto out;
3547
3548 *buf ^= mask[0] & (*buf & QUIC_PACKET_LONG_HEADER_BIT ? 0xf : 0x1f);
3549 for (i = 0; i < pnlen; i++)
3550 pn[i] ^= mask[i + 1];
3551
3552 ret = 1;
3553
3554 out:
3555 EVP_CIPHER_CTX_free(ctx);
3556
3557 return ret;
3558}
3559
3560/* Reduce the encoded size of <ack_frm> ACK frame removing the last
3561 * ACK ranges if needed to a value below <limit> in bytes.
3562 * Return 1 if succeeded, 0 if not.
3563 */
3564static int quic_ack_frm_reduce_sz(struct quic_frame *ack_frm, size_t limit)
3565{
3566 size_t room, ack_delay_sz;
3567
3568 ack_delay_sz = quic_int_getsize(ack_frm->tx_ack.ack_delay);
3569 /* A frame is made of 1 byte for the frame type. */
3570 room = limit - ack_delay_sz - 1;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003571 if (!quic_rm_last_ack_ranges(ack_frm->tx_ack.arngs, room))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003572 return 0;
3573
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003574 return 1 + ack_delay_sz + ack_frm->tx_ack.arngs->enc_sz;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003575}
3576
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003577/* Prepare as most as possible CRYPTO or STREAM frames from their prebuilt frames
3578 * 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 +01003579 * and <*len> the packet Length field initialized with the number of bytes already present
3580 * 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 +02003581 * <headlen> is the number of bytes already present in this packet before building frames.
3582 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003583 * Update consequently <*len> to reflect the size of these frames built
3584 * by this function. Also attach these frames to <pkt> QUIC packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003585 * Return 1 if succeeded, 0 if not.
3586 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003587static inline int qc_build_frms(struct quic_tx_packet *pkt,
3588 size_t room, size_t *len, size_t headlen,
3589 struct quic_enc_level *qel,
3590 struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003591{
Frédéric Lécailleea604992020-12-24 13:01:37 +01003592 int ret;
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003593 struct quic_frame *cf;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003594 struct mt_list *tmp1, tmp2;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003595 size_t remain = quic_path_prep_data(conn->path);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003596
Frédéric Lécailleea604992020-12-24 13:01:37 +01003597 ret = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003598 if (*len > room || headlen > remain)
3599 return 0;
3600
Frédéric Lécailleea604992020-12-24 13:01:37 +01003601 /* If we are not probing we must take into an account the congestion
3602 * control window.
3603 */
3604 if (!conn->tx.nb_pto_dgrams)
3605 room = QUIC_MIN(room, quic_path_prep_data(conn->path) - headlen);
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003606 TRACE_PROTO("************** frames build (headlen)",
Frédéric Lécailleea604992020-12-24 13:01:37 +01003607 QUIC_EV_CONN_BCFRMS, conn->conn, &headlen);
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003608 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 +01003609 /* header length, data length, frame length. */
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003610 size_t hlen, dlen, flen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003611
Frédéric Lécailleea604992020-12-24 13:01:37 +01003612 if (!room)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003613 break;
3614
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003615 switch (cf->type) {
3616 case QUIC_FT_CRYPTO:
3617 TRACE_PROTO(" New CRYPTO frame build (room, len)",
3618 QUIC_EV_CONN_BCFRMS, conn->conn, &room, len);
3619 /* Compute the length of this CRYPTO frame header */
3620 hlen = 1 + quic_int_getsize(cf->crypto.offset);
3621 /* Compute the data length of this CRyPTO frame. */
3622 dlen = max_stream_data_size(room, *len + hlen, cf->crypto.len);
3623 TRACE_PROTO(" CRYPTO data length (hlen, crypto.len, dlen)",
3624 QUIC_EV_CONN_BCFRMS, conn->conn, &hlen, &cf->crypto.len, &dlen);
3625 if (!dlen)
3626 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003627
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003628 pkt->cdata_len += dlen;
3629 /* CRYPTO frame length. */
3630 flen = hlen + quic_int_getsize(dlen) + dlen;
3631 TRACE_PROTO(" CRYPTO frame length (flen)",
3632 QUIC_EV_CONN_BCFRMS, conn->conn, &flen);
3633 /* Add the CRYPTO data length and its encoded length to the packet
3634 * length and the length of this length.
3635 */
3636 *len += flen;
3637 room -= flen;
3638 if (dlen == cf->crypto.len) {
3639 /* <cf> CRYPTO data have been consumed. */
3640 MT_LIST_DELETE_SAFE(tmp1);
3641 LIST_APPEND(&pkt->frms, &cf->list);
3642 }
3643 else {
3644 struct quic_frame *new_cf;
3645
3646 new_cf = pool_alloc(pool_head_quic_frame);
3647 if (!new_cf) {
3648 TRACE_PROTO("No memory for new crypto frame", QUIC_EV_CONN_BCFRMS, conn->conn);
3649 return 0;
3650 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003651
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003652 new_cf->type = QUIC_FT_CRYPTO;
3653 new_cf->crypto.len = dlen;
3654 new_cf->crypto.offset = cf->crypto.offset;
3655 new_cf->crypto.qel = qel;
3656 LIST_APPEND(&pkt->frms, &new_cf->list);
3657 /* Consume <dlen> bytes of the current frame. */
3658 cf->crypto.len -= dlen;
3659 cf->crypto.offset += dlen;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003660 }
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003661 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003662
Frédéric Lécaille0ac38512021-08-03 16:38:49 +02003663 case QUIC_FT_STREAM_8 ... QUIC_FT_STREAM_F:
3664 break;
3665
3666 default:
3667 flen = qc_frm_len(cf);
3668 BUG_ON(!flen);
3669 if (flen > room)
3670 continue;
3671
3672 *len += flen;
3673 room -= flen;
3674 MT_LIST_DELETE_SAFE(tmp1);
3675 LIST_APPEND(&pkt->frms, &cf->list);
3676 break;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003677 }
Frédéric Lécailleea604992020-12-24 13:01:37 +01003678 ret = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003679 }
3680
Frédéric Lécailleea604992020-12-24 13:01:37 +01003681 return ret;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003682}
3683
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003684/* This function evaluates if <pkt> packet may be built into a buffer with
3685 * <room> as available room. A valid packet should at least contain a valid
3686 * header and at least a frame.
3687 * To estimate the minimal space to build a packet, we consider the worst case:
3688 - there is not enough space to build ack-eliciting frames from
3689 qel->pktns->tx.frms. This is safe to consider this because when we build
3690 a packet we first build the ACK frames, then the ack-eliciting frames
3691 from qel->pktns->tx.frms only if there is enough space for these
3692 ack-eliciting frames, finally PING and PADDING frames if needed,
3693 - we have to ensure there is enough space to build an ACK frame if required,
3694 and a PING frame, even if we do not have to probe,
3695 - we also have to verify there is enough space to build a PADDING frame
3696 if needed, especially if there is no need to send an ACK frame.
3697 * Returns 1 if the <pkt> may be built, 0 if not (not enough room to build
3698 * a valid packet).
3699 */
3700static int qc_eval_pkt(ssize_t room, struct quic_tx_packet *pkt,
3701 int ack, int nb_pto_dgrams,
3702 struct quic_enc_level *qel, struct quic_conn *conn)
3703{
3704 size_t minlen, token_fields_len;
3705 /* XXX FIXME XXX : ack delay not supported */
3706 uint64_t ack_delay = 0;
3707 size_t ack_frm_len = 0;
3708
3709 TRACE_PROTO("Available room", QUIC_EV_CONN_HPKT,
3710 conn->conn, NULL, NULL, &room);
3711 /* When we do not have to probe nor send acks either, we must take into
3712 * an account the data which have already been prepared and limit
3713 * the size of this packet. We will potentially build an ack-eliciting
3714 * packet.
3715 */
3716 if (!nb_pto_dgrams && !ack) {
3717 size_t path_room;
3718
3719 path_room = quic_path_prep_data(conn->path);
3720 if (room > path_room)
3721 room = path_room;
3722 }
3723
3724 if (ack)
3725 /* A frame is made of 1 byte for the frame type. */
3726 ack_frm_len = 1 + quic_int_getsize(ack_delay) + qel->pktns->rx.arngs.enc_sz;
3727
3728 /* XXX FIXME XXX : token not supported */
3729 token_fields_len = pkt->type == QUIC_PACKET_TYPE_INITIAL ? 1 : 0;
3730 /* Check there is enough room to build the header followed by a token,
3731 * if present. The trailing room needed for the QUIC_TLS_TAG_LEN-bytes
3732 * encryption tag is also taken into an account. Note that we have no
3733 * knowledge of the packet number for this packet. It must be atomically
3734 * incremented each time a packet is built. But before building a packet
3735 * we must estimate if it may be built if we do not want to consume a packet
3736 * number for nothing! Note that we add 1 byte more to
3737 * <minlen> to be able to build an ack-eliciting packet when probing without
3738 * ack-eliciting frames to send. In this case we need to add a 1-byte length
3739 * PING frame.
3740 */
3741 minlen = QUIC_TLS_TAG_LEN + QUIC_PACKET_PN_MAXLEN + ack_frm_len + 1;
3742 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
3743 minlen += QUIC_LONG_PACKET_MINLEN + conn->dcid.len + conn->scid.len
3744 + token_fields_len;
3745 else
3746 minlen += QUIC_SHORT_PACKET_MINLEN + conn->dcid.len;
3747
3748 /* Consider any PADDING frame to add */
3749 if (objt_server(conn->conn->target) &&
3750 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
3751 minlen < QUIC_INITIAL_PACKET_MINLEN) {
3752 /* Pad too short client Initial packet */
3753 minlen += QUIC_INITIAL_PACKET_MINLEN - minlen;
3754 }
3755 else if (!ack) {
3756 /* Consider we will have to add the longest short PADDING frame to
3757 * protect a 1-byte length packet number.
3758 */
3759 minlen += QUIC_PACKET_PN_MAXLEN - 1;
3760 }
3761
3762 if (room < minlen) {
3763 TRACE_PROTO("Not enoug room", QUIC_EV_CONN_HPKT,
3764 conn->conn, NULL, NULL, &room);
3765 return 0;
3766 }
3767
3768 return 1;
3769}
3770
3771/* This function builds a clear packet from <pkt> information (its type)
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003772 * into a buffer with <pos> as position pointer and <qel> as QUIC TLS encryption
3773 * level for <conn> QUIC connection and <qel> as QUIC TLS encryption level,
3774 * filling the buffer with as much frames as possible.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003775 * 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 +02003776 * 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 +02003777 * having returned from this function.
3778 * 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 +01003779 * number field in this packet. <pn_len> will also have the packet number
3780 * length as value.
3781 *
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003782 * Always succeeds: this is the responsability of the caller to ensure there is
3783 * enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003784 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003785static void qc_do_build_pkt(unsigned char *pos, const unsigned char *end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003786 struct quic_tx_packet *pkt, int ack, int nb_pto_dgrams,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003787 int64_t pn, size_t *pn_len, unsigned char **buf_pn,
3788 struct quic_enc_level *qel, struct quic_conn *conn)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003789{
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003790 unsigned char *beg;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003791 size_t len, len_frms, padding_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003792 struct quic_frame frm = { .type = QUIC_FT_CRYPTO, };
3793 struct quic_frame ack_frm = { .type = QUIC_FT_ACK, };
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003794 size_t ack_frm_len;
3795 int64_t largest_acked_pn;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003796 int add_ping_frm;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003797
Frédéric Lécailleea604992020-12-24 13:01:37 +01003798 /* Length field value with CRYPTO frames if present. */
3799 len_frms = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003800 beg = pos;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003801 /* When not probing and not acking, reduce the size of this buffer to respect
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003802 * the congestion controller window. So, we do not limit the size of this
3803 * packet if we have an ACK frame to send because an ACK frame is not
3804 * ack-eliciting. This size will be limited if we have ack-eliciting
3805 * frames to send from qel->pktns->tx.frms.
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003806 */
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003807 if (!nb_pto_dgrams && !ack) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003808 size_t path_room;
3809
3810 path_room = quic_path_prep_data(conn->path);
3811 if (end - beg > path_room)
3812 end = beg + path_room;
3813 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003814
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003815 largest_acked_pn = HA_ATOMIC_LOAD(&qel->pktns->tx.largest_acked_pn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003816 /* packet number length */
3817 *pn_len = quic_packet_number_length(pn, largest_acked_pn);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003818 /* Build the header */
3819 if (pkt->type == QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003820 quic_build_packet_short_header(&pos, end, *pn_len, conn);
3821 else
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003822 quic_build_packet_long_header(&pos, end, pkt->type, *pn_len, conn);
3823 /* XXX FIXME XXX Encode the token length (0) for an Initial packet. */
3824 if (pkt->type == QUIC_PACKET_TYPE_INITIAL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003825 *pos++ = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003826 /* Ensure there is enough room for the TLS encryption tag */
3827 end -= QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003828 /* Build an ACK frame if required. */
3829 ack_frm_len = 0;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003830 if (ack && !eb_is_empty(&qel->pktns->rx.arngs.root)) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003831 ack_frm.tx_ack.ack_delay = 0;
Frédéric Lécaille8090b512020-11-30 16:19:22 +01003832 ack_frm.tx_ack.arngs = &qel->pktns->rx.arngs;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003833 /* XXX BE CAREFUL XXX : here we reserved at least one byte for the
3834 * smallest frame (PING) and <*pn_len> more for the packet number. Note
3835 * that from here, we do not know if we will have to send a PING frame.
3836 * This will be decided after having computed the ack-eliciting frames
3837 * to be added to this packet.
3838 */
3839 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 +01003840 if (!ack_frm_len) {
3841 ssize_t room = end - pos;
3842 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3843 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003844 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003845 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003846 }
3847
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003848 /* Length field value without the ack-eliciting frames. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003849 len = ack_frm_len + *pn_len;
Frédéric Lécaillec88df072021-07-27 11:43:11 +02003850 if (!MT_LIST_ISEMPTY(&qel->pktns->tx.frms)) {
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003851 ssize_t room = end - pos;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003852
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003853 /* Initialize the length of the frames built below to <len>.
3854 * If any frame could be successfully built by qc_build_frms(),
3855 * we will have len_frms > len.
3856 */
3857 len_frms = len;
3858 if (!qc_build_frms(pkt, end - pos, &len_frms, pos - beg, qel, conn))
Frédéric Lécailleea604992020-12-24 13:01:37 +01003859 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3860 conn->conn, NULL, NULL, &room);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003861 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003862
3863 add_ping_frm = 0;
3864 padding_len = 0;
3865 if (objt_server(conn->conn->target) &&
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003866 pkt->type == QUIC_PACKET_TYPE_INITIAL &&
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003867 len < QUIC_INITIAL_PACKET_MINLEN) {
3868 len += padding_len = QUIC_INITIAL_PACKET_MINLEN - len;
3869 }
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003870 else if (LIST_ISEMPTY(&pkt->frms) || len_frms == len) {
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003871 if (qel->pktns->tx.pto_probe) {
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003872 /* If we cannot send a frame, we send a PING frame. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003873 add_ping_frm = 1;
3874 len += 1;
3875 }
3876 /* If there is no frame at all to follow, add at least a PADDING frame. */
3877 if (!ack_frm_len)
3878 len += padding_len = QUIC_PACKET_PN_MAXLEN - *pn_len;
3879 }
3880
3881 /* Length (of the remaining data). Must not fail because, the buffer size
3882 * has been checked above. Note that we have reserved QUIC_TLS_TAG_LEN bytes
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003883 * for the encryption tag. It must be taken into an account for the length
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003884 * of this packet.
3885 */
Frédéric Lécailleea604992020-12-24 13:01:37 +01003886 if (len_frms)
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003887 len = len_frms + QUIC_TLS_TAG_LEN;
Frédéric Lécailleea604992020-12-24 13:01:37 +01003888 else
3889 len += QUIC_TLS_TAG_LEN;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003890 if (pkt->type != QUIC_PACKET_TYPE_SHORT)
Frédéric Lécaillee1aa0d32021-08-03 16:03:09 +02003891 quic_enc_int(&pos, end, len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003892
3893 /* Packet number field address. */
3894 *buf_pn = pos;
3895
3896 /* Packet number encoding. */
3897 quic_packet_number_encode(&pos, end, pn, *pn_len);
3898
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003899 if (ack_frm_len && !qc_build_frm(&pos, end, &ack_frm, pkt, conn)) {
3900 ssize_t room = end - pos;
3901 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3902 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003903 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003904 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003905
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003906 /* Ack-eliciting frames */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003907 if (!LIST_ISEMPTY(&pkt->frms)) {
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003908 struct quic_frame *cf;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003909
3910 list_for_each_entry(cf, &pkt->frms, list) {
Frédéric Lécaillef252adb2021-08-03 17:01:25 +02003911 if (!qc_build_frm(&pos, end, cf, pkt, conn)) {
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003912 ssize_t room = end - pos;
3913 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3914 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003915 BUG_ON(1);
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003916 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003917 }
3918 }
3919
3920 /* Build a PING frame if needed. */
3921 if (add_ping_frm) {
3922 frm.type = QUIC_FT_PING;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003923 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3924 ssize_t room = end - pos;
3925 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3926 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003927 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003928 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003929 }
3930
3931 /* Build a PADDING frame if needed. */
3932 if (padding_len) {
3933 frm.type = QUIC_FT_PADDING;
3934 frm.padding.len = padding_len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003935 if (!qc_build_frm(&pos, end, &frm, pkt, conn)) {
3936 ssize_t room = end - pos;
3937 TRACE_PROTO("Not enough room", QUIC_EV_CONN_HPKT,
3938 conn->conn, NULL, NULL, &room);
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003939 BUG_ON(1);
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003940 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003941 }
3942
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01003943 /* Always reset this variable as this function has no idea
3944 * if it was set. It is handle by the loss detection timer.
3945 */
3946 qel->pktns->tx.pto_probe = 0;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003947 pkt->len = pos - beg;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003948}
3949
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003950static inline void quic_tx_packet_init(struct quic_tx_packet *pkt, int type)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003951{
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02003952 pkt->type = type;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003953 pkt->len = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003954 pkt->cdata_len = 0;
3955 pkt->in_flight_len = 0;
3956 LIST_INIT(&pkt->frms);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003957 pkt->next = NULL;
3958 pkt->refcnt = 1;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003959}
3960
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05003961/* Free <pkt> TX packet which has not already attached to any tree. */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003962static inline void free_quic_tx_packet(struct quic_tx_packet *pkt)
3963{
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003964 struct quic_frame *frm, *frmbak;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003965
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003966 if (!pkt)
3967 return;
3968
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003969 list_for_each_entry_safe(frm, frmbak, &pkt->frms, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02003970 LIST_DELETE(&frm->list);
Frédéric Lécaille0ad04582021-07-27 14:51:54 +02003971 pool_free(pool_head_quic_frame, frm);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003972 }
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003973 quic_tx_packet_refdec(pkt);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003974}
3975
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003976/* Build a packet into <buf> packet buffer with <pkt_type> as packet
3977 * type for <qc> QUIC connection from <qel> encryption level.
3978 * Return -2 if the packet could not be allocated or encrypted for any reason,
3979 * -1 if there was not enough room to build a packet.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003980 */
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003981static struct quic_tx_packet *qc_build_pkt(unsigned char **pos,
3982 const unsigned char *buf_end,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003983 struct quic_enc_level *qel,
Frédéric Lécaille9445abc2021-08-04 10:49:51 +02003984 struct quic_conn *qc, int pkt_type,
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003985 int nb_pto_dgrams, int *err)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003986{
3987 /* The pointer to the packet number field. */
3988 unsigned char *buf_pn;
3989 unsigned char *beg, *end, *payload;
3990 int64_t pn;
3991 size_t pn_len, payload_len, aad_len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003992 struct quic_tls_ctx *tls_ctx;
3993 struct quic_tx_packet *pkt;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02003994 int ack;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003995
Frédéric Lécaille133e8a72020-12-18 09:33:27 +01003996 TRACE_ENTER(QUIC_EV_CONN_HPKT, qc->conn, NULL, qel);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02003997 *err = 0;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01003998 pkt = pool_alloc(pool_head_quic_tx_packet);
3999 if (!pkt) {
4000 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 +02004001 *err = -2;
4002 goto err;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004003 }
4004
Frédéric Lécaille0e50e1b2021-08-03 15:03:35 +02004005 quic_tx_packet_init(pkt, pkt_type);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004006 beg = *pos;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004007 pn_len = 0;
4008 buf_pn = NULL;
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004009 ack = HA_ATOMIC_BTR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED);
4010 if (!qc_eval_pkt(buf_end - beg, pkt, ack, nb_pto_dgrams, qel, qc)) {
4011 if (ack)
4012 HA_ATOMIC_OR(&qel->pktns->flags, QUIC_FL_PKTNS_ACK_REQUIRED);
4013 *err = -1;
4014 goto err;
4015 }
4016
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004017 /* Consume a packet number. */
4018 pn = HA_ATOMIC_ADD_FETCH(&qel->pktns->tx.next_pn, 1);
Frédéric Lécaille4436cb62021-08-16 12:06:46 +02004019 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 +01004020
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004021 end = beg + pkt->len;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004022 payload = buf_pn + pn_len;
4023 payload_len = end - payload;
4024 aad_len = payload - beg;
4025
4026 tls_ctx = &qel->tls_ctx;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004027 if (!quic_packet_encrypt(payload, payload_len, beg, aad_len, pn, tls_ctx, qc->conn)) {
4028 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004029 goto err;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004030 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004031
4032 end += QUIC_TLS_TAG_LEN;
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004033 pkt->len += QUIC_TLS_TAG_LEN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004034 if (!quic_apply_header_protection(beg, buf_pn, pn_len,
4035 tls_ctx->tx.hp, tls_ctx->tx.hp_key)) {
4036 TRACE_DEVEL("Could not apply the header protection", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004037 *err = -2;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004038 goto err;
4039 }
4040
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004041 /* Now that a correct packet is built, let us consume <*pos> buffer. */
4042 *pos = end;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004043 /* Attach the built packet to its tree. */
Frédéric Lécaillea7348f62021-08-03 16:50:14 +02004044 pkt->pn_node.key = pn;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004045 /* Set the packet in fligth length for in flight packet only. */
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004046 if (pkt->flags & QUIC_FL_TX_PACKET_IN_FLIGHT) {
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004047 pkt->in_flight_len = pkt->len;
4048 qc->path->prep_in_flight += pkt->len;
Frédéric Lécaille04ffb662020-12-08 15:58:39 +01004049 }
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004050 pkt->pktns = qel->pktns;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004051 TRACE_LEAVE(QUIC_EV_CONN_HPKT, qc->conn, pkt);
4052
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004053 return pkt;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004054
4055 err:
4056 free_quic_tx_packet(pkt);
4057 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_HPKT, qc->conn);
Frédéric Lécaillec5b0c932021-07-06 16:35:52 +02004058 return NULL;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004059}
4060
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004061/* Copy up to <count> bytes from connection <conn> internal stream storage into buffer <buf>.
4062 * Return the number of bytes which have been copied.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004063 */
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004064static size_t quic_conn_to_buf(struct connection *conn, void *xprt_ctx,
4065 struct buffer *buf, size_t count, int flags)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004066{
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004067 size_t try, done = 0;
4068
4069 if (!conn_ctrl_ready(conn))
4070 return 0;
4071
4072 if (!fd_recv_ready(conn->handle.fd))
4073 return 0;
4074
4075 conn->flags &= ~CO_FL_WAIT_ROOM;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004076
4077 /* read the largest possible block. For this, we perform only one call
4078 * to recv() unless the buffer wraps and we exactly fill the first hunk,
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004079 * in which case we accept to do it once again.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004080 */
4081 while (count > 0) {
4082 try = b_contig_space(buf);
4083 if (!try)
4084 break;
4085
4086 if (try > count)
4087 try = count;
4088
Frédéric Lécaillefbe3b772021-03-03 16:23:44 +01004089 b_add(buf, try);
4090 done += try;
4091 count -= try;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004092 }
4093
4094 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4095 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4096
4097 leave:
4098 return done;
4099
4100 read0:
4101 conn_sock_read0(conn);
4102 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4103
4104 /* Now a final check for a possible asynchronous low-level error
4105 * report. This can happen when a connection receives a reset
4106 * after a shutdown, both POLL_HUP and POLL_ERR are queued, and
4107 * we might have come from there by just checking POLL_HUP instead
4108 * of recv()'s return value 0, so we have no way to tell there was
4109 * an error without checking.
4110 */
Willy Tarreauf5090652021-04-06 17:23:40 +02004111 if (unlikely(fdtab[conn->handle.fd].state & FD_POLL_ERR))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004112 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4113 goto leave;
4114}
4115
4116
4117/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
4118 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
4119 * other pending data for example, but this flag is ignored at the moment.
4120 * Only one call to send() is performed, unless the buffer wraps, in which case
4121 * a second call may be performed. The connection's flags are updated with
4122 * whatever special event is detected (error, empty). The caller is responsible
4123 * for taking care of those events and avoiding the call if inappropriate. The
4124 * function does not call the connection's polling update function, so the caller
4125 * is responsible for this. It's up to the caller to update the buffer's contents
4126 * based on the return value.
4127 */
4128static size_t quic_conn_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
4129{
4130 ssize_t ret;
4131 size_t try, done;
4132 int send_flag;
4133
4134 if (!conn_ctrl_ready(conn))
4135 return 0;
4136
4137 if (!fd_send_ready(conn->handle.fd))
4138 return 0;
4139
4140 done = 0;
4141 /* send the largest possible block. For this we perform only one call
4142 * to send() unless the buffer wraps and we exactly fill the first hunk,
4143 * in which case we accept to do it once again.
4144 */
4145 while (count) {
4146 try = b_contig_data(buf, done);
4147 if (try > count)
4148 try = count;
4149
4150 send_flag = MSG_DONTWAIT | MSG_NOSIGNAL;
4151 if (try < count || flags & CO_SFL_MSG_MORE)
4152 send_flag |= MSG_MORE;
4153
4154 ret = sendto(conn->handle.fd, b_peek(buf, done), try, send_flag,
4155 (struct sockaddr *)conn->dst, get_addr_len(conn->dst));
4156 if (ret > 0) {
4157 count -= ret;
4158 done += ret;
4159
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004160 /* A send succeeded, so we can consider ourself connected */
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004161 conn->flags |= CO_FL_WAIT_L4L6;
4162 /* if the system buffer is full, don't insist */
4163 if (ret < try)
4164 break;
4165 }
4166 else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) {
4167 /* nothing written, we need to poll for write first */
4168 fd_cant_send(conn->handle.fd);
4169 break;
4170 }
4171 else if (errno != EINTR) {
4172 conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
4173 break;
4174 }
4175 }
4176 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && done)
4177 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4178
4179 if (done > 0) {
4180 /* we count the total bytes sent, and the send rate for 32-byte
4181 * blocks. The reason for the latter is that freq_ctr are
4182 * limited to 4GB and that it's not enough per second.
4183 */
4184 _HA_ATOMIC_ADD(&global.out_bytes, done);
4185 update_freq_ctr(&global.out_32bps, (done + 16) / 32);
4186 }
4187 return done;
4188}
4189
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004190/* Called from the upper layer, to subscribe <es> to events <event_type>. The
4191 * event subscriber <es> is not allowed to change from a previous call as long
4192 * as at least one event is still subscribed. The <event_type> must only be a
4193 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
4194 */
4195static int quic_conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4196{
4197 return conn_subscribe(conn, xprt_ctx, event_type, es);
4198}
4199
4200/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
4201 * The <es> pointer is not allowed to differ from the one passed to the
4202 * subscribe() call. It always returns zero.
4203 */
4204static int quic_conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
4205{
4206 return conn_unsubscribe(conn, xprt_ctx, event_type, es);
4207}
4208
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004209/* Initialize a QUIC connection (quic_conn struct) to be attached to <conn>
4210 * connection with <xprt_ctx> as address of the xprt context.
4211 * Returns 1 if succeeded, 0 if not.
4212 */
4213static int qc_conn_init(struct connection *conn, void **xprt_ctx)
4214{
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004215 struct ssl_sock_ctx *ctx;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004216
4217 TRACE_ENTER(QUIC_EV_CONN_NEW, conn);
4218
4219 if (*xprt_ctx)
4220 goto out;
4221
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004222 ctx = pool_alloc(pool_head_quic_conn_ctx);
4223 if (!ctx) {
4224 conn->err_code = CO_ER_SYS_MEMLIM;
4225 goto err;
4226 }
4227
4228 ctx->wait_event.tasklet = tasklet_new();
4229 if (!ctx->wait_event.tasklet) {
4230 conn->err_code = CO_ER_SYS_MEMLIM;
4231 goto err;
4232 }
4233
4234 ctx->wait_event.tasklet->process = quic_conn_io_cb;
4235 ctx->wait_event.tasklet->context = ctx;
4236 ctx->wait_event.events = 0;
4237 ctx->conn = conn;
4238 ctx->subs = NULL;
4239 ctx->xprt_ctx = NULL;
4240
4241 ctx->xprt = xprt_get(XPRT_QUIC);
4242 if (objt_server(conn->target)) {
4243 /* Server */
4244 struct server *srv = __objt_server(conn->target);
4245 unsigned char dcid[QUIC_CID_LEN];
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004246 struct quic_conn *qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004247 int ssl_err, ipv4;
4248
4249 ssl_err = SSL_ERROR_NONE;
4250 if (RAND_bytes(dcid, sizeof dcid) != 1)
4251 goto err;
4252
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004253 ipv4 = conn->dst->ss_family == AF_INET;
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004254 qc = qc_new_conn(QUIC_PROTOCOL_VERSION_DRAFT_28, ipv4,
Frédéric Lécaille6b197642021-07-06 16:25:08 +02004255 dcid, sizeof dcid, NULL, 0, 0, srv);
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004256 if (qc == NULL)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004257 goto err;
4258
Frédéric Lécaille6de72872021-06-11 15:44:24 +02004259 /* Insert our SCID, the connection ID for the QUIC client. */
4260 ebmb_insert(&srv->cids, &qc->scid_node, qc->scid.len);
4261
4262 conn->qc = qc;
4263 qc->conn = conn;
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004264 if (!qc_new_isecs(qc, dcid, sizeof dcid, 0))
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004265 goto err;
4266
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004267 if (ssl_bio_and_sess_init(conn, srv->ssl_ctx.ctx,
4268 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4269 goto err;
4270
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004271 qc->rx.params = srv->quic_params;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004272 /* Copy the initial source connection ID. */
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004273 quic_cid_cpy(&qc->rx.params.initial_source_connection_id, &qc->scid);
4274 qc->enc_params_len =
4275 quic_transport_params_encode(qc->enc_params, qc->enc_params + sizeof qc->enc_params,
4276 &qc->rx.params, 0);
4277 if (!qc->enc_params_len)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004278 goto err;
4279
Frédéric Lécaillea5fe49f2021-06-04 11:52:35 +02004280 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004281 SSL_set_connect_state(ctx->ssl);
4282 ssl_err = SSL_do_handshake(ctx->ssl);
4283 if (ssl_err != 1) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004284 int st;
4285
4286 st = HA_ATOMIC_LOAD(&qc->state);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004287 ssl_err = SSL_get_error(ctx->ssl, ssl_err);
4288 if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE) {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004289 TRACE_PROTO("SSL handshake", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004290 }
4291 else {
Frédéric Lécailleeed7a7d2021-08-18 09:16:01 +02004292 TRACE_DEVEL("SSL handshake error", QUIC_EV_CONN_HDSHK, ctx->conn, &st, &ssl_err);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004293 goto err;
4294 }
4295 }
4296 }
4297 else if (objt_listener(conn->target)) {
4298 /* Listener */
4299 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004300 struct quic_conn *qc = ctx->conn->qc;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004301
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004302 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
4303 &ctx->ssl, &ctx->bio, ha_quic_meth, ctx) == -1)
4304 goto err;
4305
Frédéric Lécaille1e1aad42021-05-27 14:57:09 +02004306 SSL_set_quic_transport_params(ctx->ssl, qc->enc_params, qc->enc_params_len);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004307 SSL_set_accept_state(ctx->ssl);
4308 }
4309
4310 *xprt_ctx = ctx;
4311
4312 /* Leave init state and start handshake */
4313 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004314
4315 out:
4316 TRACE_LEAVE(QUIC_EV_CONN_NEW, conn);
4317
4318 return 0;
4319
4320 err:
Willy Tarreau7deb28c2021-05-10 07:40:27 +02004321 if (ctx && ctx->wait_event.tasklet)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004322 tasklet_free(ctx->wait_event.tasklet);
4323 pool_free(pool_head_quic_conn_ctx, ctx);
Frédéric Lécaille6c1e36c2020-12-23 17:17:37 +01004324 TRACE_DEVEL("leaving in error", QUIC_EV_CONN_NEW, conn);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004325 return -1;
4326}
4327
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004328/* Start the QUIC transport layer */
4329static int qc_xprt_start(struct connection *conn, void *ctx)
4330{
4331 struct quic_conn *qc;
Frédéric Lécaille1eaec332021-06-04 14:59:59 +02004332 struct ssl_sock_ctx *qctx = ctx;
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004333
4334 qc = conn->qc;
4335 if (!quic_conn_init_timer(qc)) {
4336 TRACE_PROTO("Non initialized timer", QUIC_EV_CONN_LPKT, conn);
4337 return 0;
4338 }
4339
4340 tasklet_wakeup(qctx->wait_event.tasklet);
4341 return 1;
4342}
4343
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004344/* transport-layer operations for QUIC connections. */
4345static struct xprt_ops ssl_quic = {
4346 .snd_buf = quic_conn_from_buf,
4347 .rcv_buf = quic_conn_to_buf,
Frédéric Lécaille422a39c2021-03-03 17:28:34 +01004348 .subscribe = quic_conn_subscribe,
4349 .unsubscribe = quic_conn_unsubscribe,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004350 .init = qc_conn_init,
Frédéric Lécaille3d77fa72021-05-31 09:30:14 +02004351 .start = qc_xprt_start,
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004352 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
4353 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
4354 .name = "QUIC",
4355};
4356
4357__attribute__((constructor))
4358static void __quic_conn_init(void)
4359{
4360 ha_quic_meth = BIO_meth_new(0x666, "ha QUIC methods");
4361 xprt_register(XPRT_QUIC, &ssl_quic);
4362}
4363
4364__attribute__((destructor))
4365static void __quic_conn_deinit(void)
4366{
4367 BIO_meth_free(ha_quic_meth);
4368}
4369
4370/* Read all the QUIC packets found in <buf> with <len> as length (typically a UDP
4371 * datagram), <ctx> being the QUIC I/O handler context, from QUIC connections,
4372 * calling <func> function;
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05004373 * Return the number of bytes read if succeeded, -1 if not.
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004374 */
4375static ssize_t quic_dgram_read(char *buf, size_t len, void *owner,
4376 struct sockaddr_storage *saddr, qpkt_read_func *func)
4377{
4378 unsigned char *pos;
4379 const unsigned char *end;
4380 struct quic_dgram_ctx dgram_ctx = {
4381 .dcid_node = NULL,
4382 .owner = owner,
4383 };
4384
4385 pos = (unsigned char *)buf;
4386 end = pos + len;
4387
4388 do {
4389 int ret;
4390 struct quic_rx_packet *pkt;
4391
Willy Tarreaue4498932021-03-22 21:13:05 +01004392 pkt = pool_zalloc(pool_head_quic_rx_packet);
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004393 if (!pkt)
4394 goto err;
4395
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004396 quic_rx_packet_refinc(pkt);
4397 ret = func(&pos, end, pkt, &dgram_ctx, saddr);
4398 if (ret == -1) {
4399 size_t pkt_len;
4400
4401 pkt_len = pkt->len;
4402 free_quic_rx_packet(pkt);
4403 /* If the packet length could not be found, we cannot continue. */
4404 if (!pkt_len)
4405 break;
4406 }
4407 } while (pos < end);
4408
4409 /* Increasing the received bytes counter by the UDP datagram length
4410 * if this datagram could be associated to a connection.
4411 */
4412 if (dgram_ctx.qc)
4413 dgram_ctx.qc->rx.bytes += len;
4414
4415 return pos - (unsigned char *)buf;
4416
4417 err:
4418 return -1;
4419}
4420
4421ssize_t quic_lstnr_dgram_read(char *buf, size_t len, void *owner,
4422 struct sockaddr_storage *saddr)
4423{
4424 return quic_dgram_read(buf, len, owner, saddr, qc_lstnr_pkt_rcv);
4425}
4426
4427ssize_t quic_srv_dgram_read(char *buf, size_t len, void *owner,
4428 struct sockaddr_storage *saddr)
4429{
4430 return quic_dgram_read(buf, len, owner, saddr, qc_srv_pkt_rcv);
4431}
4432
4433/* QUIC I/O handler for connection to local listeners or remove servers
4434 * depending on <listener> boolean value, with <fd> as socket file
4435 * descriptor and <ctx> as context.
4436 */
4437static size_t quic_conn_handler(int fd, void *ctx, qpkt_read_func *func)
4438{
4439 ssize_t ret;
4440 size_t done = 0;
4441 struct buffer *buf = get_trash_chunk();
4442 /* Source address */
4443 struct sockaddr_storage saddr = {0};
4444 socklen_t saddrlen = sizeof saddr;
4445
4446 if (!fd_recv_ready(fd))
4447 return 0;
4448
4449 do {
4450 ret = recvfrom(fd, buf->area, buf->size, 0,
4451 (struct sockaddr *)&saddr, &saddrlen);
4452 if (ret < 0) {
4453 if (errno == EINTR)
4454 continue;
4455 if (errno == EAGAIN)
4456 fd_cant_recv(fd);
4457 goto out;
4458 }
4459 } while (0);
4460
4461 done = buf->data = ret;
4462 quic_dgram_read(buf->area, buf->data, ctx, &saddr, func);
4463
4464 out:
4465 return done;
4466}
4467
4468/* QUIC I/O handler for connections to local listeners with <fd> as socket
4469 * file descriptor.
4470 */
4471void quic_fd_handler(int fd)
4472{
Willy Tarreauf5090652021-04-06 17:23:40 +02004473 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004474 quic_conn_handler(fd, fdtab[fd].owner, &qc_lstnr_pkt_rcv);
4475}
4476
4477/* QUIC I/O handler for connections to remote servers with <fd> as socket
4478 * file descriptor.
4479 */
4480void quic_conn_fd_handler(int fd)
4481{
Willy Tarreauf5090652021-04-06 17:23:40 +02004482 if (fdtab[fd].state & FD_POLL_IN)
Frédéric Lécaillea7e7ce92020-11-23 14:14:04 +01004483 quic_conn_handler(fd, fdtab[fd].owner, &qc_srv_pkt_rcv);
4484}
4485
4486/*
4487 * Local variables:
4488 * c-indent-level: 8
4489 * c-basic-offset: 8
4490 * End:
4491 */